Created
October 15, 2017 09:46
-
-
Save iliyan-trifonov/fe91fc83529d68719388703c28cd6656 to your computer and use it in GitHub Desktop.
Cycle.js: text input and a button, latest text is assigned to result on button click
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
import {run} from '@cycle/run'; | |
import {makeDOMDriver, div, button, br, input} from '@cycle/dom'; | |
import xs from 'xstream'; | |
import sampleCombine from 'xstream/extra/sampleCombine'; | |
function main (sources) { | |
const click$ = sources.DOM.select('.button').events('click').startWith(false); | |
const input$ = sources.DOM.select('.input').events('input'); | |
const text$ = input$.map(ev => ev.target.value).startWith(''); | |
const combined$ = click$.compose(sampleCombine(text$)); | |
const result$ = combined$.map(([click, text]) => text); | |
return { | |
DOM: result$.map(result => | |
div([ | |
input('.input', {type: 'text'}), | |
button('.button', 'Click Me'), | |
br(), br(), | |
result ? 'Result: ' + result : '' | |
]) | |
) | |
}; | |
} | |
const drivers = { | |
DOM: makeDOMDriver('.app') | |
} | |
run(main, drivers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment