Files: b7351a2c0d7fa0d7d5a065cfd2cf419681d5eee8 / index.js
1623 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | |
4 | module.exports = function combobox(opt) { |
5 | var otherSelectOption = h('option', {value: ''}, 'other…') |
6 | var input = h('input', { |
7 | onkeyup: function () { |
8 | otherSelectOption.value = this.value |
9 | if (opt.onchange) opt.onchange.call(select) |
10 | }, |
11 | onblur: function () { |
12 | if (!this.value) { |
13 | select.selectedIndex = 0 |
14 | this.parentNode.replaceChild(select, this) |
15 | } |
16 | } |
17 | }) |
18 | var select = h('select', { |
19 | style: opt.style, |
20 | onchange: function () { |
21 | if (!this.value) { |
22 | input.style.width = this.offsetWidth + 'px' |
23 | this.parentNode.replaceChild(input, this) |
24 | input.focus() |
25 | } |
26 | if (opt.onchange) opt.onchange.call(this) |
27 | } |
28 | }, |
29 | opt.placeholder ? h('option', |
30 | {disabled: true, selected: true, hidden: true, value: ''}, |
31 | opt.placeholder) : null |
32 | ) |
33 | if (opt.read) pull( |
34 | opt.read, |
35 | pull.filter(), |
36 | pull.drain(function (option) { |
37 | select.appendChild(option) |
38 | if (option.value === opt.default) { |
39 | select.value = option.value |
40 | } |
41 | if (select.value === option.value) { |
42 | if (opt.onchange) opt.onchange.call(select) |
43 | } |
44 | }, function (err) { |
45 | if (err) return cb ? cb(err) : console.error(err) |
46 | select.appendChild(otherSelectOption) |
47 | }) |
48 | ) |
49 | else select.appendChild(otherSelectOption) |
50 | select.swap = function (newEl) { |
51 | if (select.parentNode) select.parentNode.replaceChild(newEl, select) |
52 | else if (input.parentNode) input.parentNode.replaceChild(newEl, input) |
53 | return newEl |
54 | } |
55 | return select |
56 | } |
57 | |
58 | |
59 |
Built with git-ssb-web