-
-
Save Hypnosphi/f233a501d08707a759fdc0ec8debe95a to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/dowura/edit?js,output
This file contains 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 {div, h, p, button, ul, li, makeDOMDriver} = CycleDOM; | |
const {Observable} = Rx; | |
const isolate = CycleIsolate; | |
function OriginalInput(sources) { | |
const input$ = sources.DOM | |
.events('input') | |
.map(ev => ev.target.value); | |
const vtree$ = sources.Assign | |
.merge(input$) | |
.startWith(null) | |
.map(value => | |
h('input', { | |
type: 'text', | |
value | |
}) | |
); | |
return { | |
DOM: vtree$, | |
Values: input$ | |
}; | |
} | |
const Input = s => isolate(OriginalInput)(s); | |
function main(sources) { | |
const add$ = sources.DOM.select('.add') | |
.events('click').map(null); | |
const input = Input({ | |
Assign: add$.map(() => ''), | |
DOM: sources.DOM | |
}); | |
const list$ = input.Values.flatMapLatest(text => add$.first().map(text)) | |
.startWith([]) | |
.scan((acc, curr) => acc.concat(curr)); | |
const vtree$ = list$.combineLatest(input.DOM, | |
(list, inputDOM) => | |
div([ | |
inputDOM, | |
button('.add', 'Add'), | |
ul(list.map(item => li(item))) | |
]) | |
); | |
return { | |
DOM: vtree$, | |
}; | |
} | |
Cycle.run(main, { | |
DOM: makeDOMDriver('#app') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is modification of A.Stalz's controlled input example (blog post , JSBin).
Instead of using virtual-dom hooks, I merge assign and DOM input sources into a value attribute stream.