Yes , you need to get inside the DOM and invoke .focus()with or without jQuery. However, this is a side effect , and this Cycle.js convention transfers these kinds of side effects to the so-called driver .
The two questions a driver should know are:
- which element do you want to focus?
- ?
DOM.
. SetFocus. . , . , , , elem$:
function makeSetFocusDriver() {
function SetFocusDriver(elem$) {
elem$.subscribe(elem => {
elem.focus();
});
}
return SetFocusDriver;
}
DOM .focus() .
, Cycle.run:
Cycle.run(main, {
DOM: makeDOMDriver('#app'),
SetFocus: makeSetFocusDriver() // add a driver
});
:
function main({DOM}) {
const textbox$ = DOM.select('.field').observable.flatMap(x => x);
const focusNeeded = [
clickingSomewhere$,
someKindofStateChange$
];
const focus$ = Observable.merge(...focusNeeded)
.withLatestFrom(textbox$, (_, textbox) => textbox);
return {
DOM: vtree$,
SetFocus: focus$,
};
}
focusNeeded, , .field .