-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathform.js
More file actions
51 lines (44 loc) · 1.38 KB
/
form.js
File metadata and controls
51 lines (44 loc) · 1.38 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
var assert = require('assert')
var html = require('bel')
var css = require('dom-css')
module.exports = function createForm (options) {
return function renderForm (state, send) {
state.addButtonText = state.addButtonText || 'Add'
var key = state.showKeys ? '' : Object.keys(state.items).length.toString()
var keyInput = html`<input
type="text"
name="list-editor-input-key"
class="list-editor-input-key ${options.showKeys ? '' : 'list-editor-hide-key'}"
placeholder="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"
placeholder="value"
value=""
/>`
function onsubmit (e) {
e.preventDefault()
key = keyInput.value
options.onAdd({ key: key, value: valueInput.value })
valueInput.value = null
keyInput.value = state.showKeys ? null : parseFloat(key) + 1
if (options.showKeys) {
keyInput.focus()
} else {
valueInput.focus()
}
}
console.log('render form', Object.keys(state.items).length)
return html`<form class="list-editor-form" onsubmit=${onsubmit}>
${keyInput}
${valueInput}
<button class="list-editor-submit">
${state.addButtonText}
</button>
</form>`
}
}