Skip to content

Instantly share code, notes, and snippets.

@iliyan-trifonov
Created October 15, 2017 09:46
Show Gist options
  • Save iliyan-trifonov/fe91fc83529d68719388703c28cd6656 to your computer and use it in GitHub Desktop.
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
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