-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (57 loc) · 2.05 KB
/
index.js
File metadata and controls
65 lines (57 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var extend = require('xtend')
var createClassName = require('data-field-classname')
var Emitter = require('component-emitter')
/**
* Create a virtual-dom string data-field for use with [data-ui](https://github.com/editdata/data-ui).
* @param {Object} options an options object, including any properties you can pass to virtual-dom/h
* @param {Boolean} options.display true for display mode, default is false for input mode
* @returns field
* @name createStringField
* @example
* var createStringField = require('data-field-string')
* var field = createStringField()
* var vtree = field.render(h, {}, 'example string')
*/
module.exports = function createStringField (options) {
options = extend({
tagName: 'textarea',
display: false,
size: 'normal',
fieldType: 'input',
attributes: {}
}, options)
var field = {}
Emitter(field)
options.dataType = 'string'
options.oninput = function (e) {
field.emit('update', e, e.target.value)
}
/**
* Create a virtual-dom string data-field for use with [data-ui](https://github.com/editdata/data-ui).
* @param {function} h virtual-dom `h` function
* @param {Object} properties an options object, including any properties you can pass to virtual-dom/h
* @param {Boolean} properties.display true for display mode, default is false for input mode
* @param {String} properties.value any string
* @param {String} value any string
* @returns virtual-dom tree
* @name createStringField
* @example
* var createStringField = require('data-field-string')
* var field = createStringField()
* var vtree = field.render(h, {}, 'example string')
*/
field.render = function (h, properties, value) {
properties = extend(options, properties)
value = value || properties.value
if (properties.display) {
properties.tagName = 'div'
properties.fieldType = 'display'
}
if (properties.size === 'small') {
properties.attributes.rows = 1
}
properties.className = createClassName(properties)
return h(properties.tagName, properties, String(value))
}
return field
}