Created
June 10, 2016 09:37
-
-
Save emilbayes/d5c3aa9238af034d4a6e45963d0791d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const html = require('yo-yo') | |
const struct = require('observ-struct') | |
const value = require('observ') | |
const list = require('observ-array') | |
const EventEmitter = require('event').Emitter | |
function autocomplete () { | |
var state = struct({ | |
name: value(Math.random().toString(16)), | |
options: list([]), | |
iconClass: value('dropdown') | |
}) | |
var element = render(state()) | |
var emitter = new EventEmitter() | |
state(function (state) { | |
html.update(element, render(state)) | |
}) | |
return { | |
element, | |
name: getset(state.name), | |
options: getset(state.options), | |
on: emitter.on | |
} | |
function render (state) { | |
return html`<div class="ui dropdown"> | |
<input type="hidden" name="${state.name}"> | |
<i class="${state.iconClass} icon"></i> | |
<div class="menu"> | |
${state.options.map(option)} | |
</div> | |
</div>` | |
} | |
function option (option) { | |
return html`<div class="item" data-value="${option.value}">${option.content}</div>` | |
} | |
} | |
function getset (o) { | |
return function (val) { | |
if (val === undefined) return o() | |
return o.set(val) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const autocomplete = require('./autocomplete') | |
var ac = autocomplete() | |
ac.name('countries') | |
ac.options([ | |
{value: 'au', content: 'Australia'}, | |
{value: 'dk', content: 'Denmark'} | |
]) | |
document.body.appendChild(ac) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment