-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlist.js
More file actions
74 lines (61 loc) · 1.98 KB
/
list.js
File metadata and controls
74 lines (61 loc) · 1.98 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
66
67
68
69
70
71
72
73
74
var assert = require('assert')
var html = require('bel')
var css = require('dom-css')
module.exports = function createList (options) {
return function renderList (state) {
state.removeButtonText = state.removeButtonText || 'x'
assert.ok(state.items || typeof state.items === 'object', 'state.items must be an object')
var keys = Object.keys(state.items)
function eachKey (key) {
var value = state.items[key]
var previousKey = key
var keyInput = html`<input
type="text"
name="list-editor-input-key"
class="list-editor-input-key ${options.showKeys ? '' : 'list-editor-hide-key'}"
oninput=${onKeyChange}
placeholder="key"
data-key=${key}
value="${key}"
/>`
css(keyInput, { display: options.showKeys ? 'initial' : 'none' })
var valueInput = html`<input
type="text"
name="list-editor-input-value"
class="list-editor-input-value"
oninput=${onValueChange}
placeholder="value"
data-key="${key}"
value="${value}"
/>`
function onKeyChange (e) {
previousKey = key
key = e.target.value
options.onChange({ key: key, previousKey: previousKey, value: value })
}
function onValueChange (e) {
options.onChange({ key: key, value: e.target.value })
}
function onclick (e) {
e.preventDefault()
options.onRemove({ key: key })
key = e.target.parentNode.children[0].value
}
var button = html`<button class="list-editor-item-remove" onclick=${onclick}>
${options.removeButtonText}
</button>`
var el = html`<li class="list-editor-item" id="list-editor-item-key-${key}">
${keyInput}
${valueInput}
${button}
</li>`
css(el, { listStyleType: 'none' })
return el
}
var el = html`<ul class="list-editor-items">
${keys.map(eachKey)}
</ul>`
css(el, { padding: 0, margin: 0 })
return el
}
}