How do I focus an input with Cycle? Do I need to reach inside the DOM and call .focus()
either with or without jQuery, or is there some other way with Cycle/RxJS?
How do I focus an input with Cycle.js and RxJS?
1.1k Views Asked by Ilya At
3
There are 3 best solutions below
1

Here's one example, written by Mr. Staltz himself: https://github.com/cyclejs/cycle-examples/blob/master/autocomplete-search/src/main.js#L298
0

You can tailor for your own situation, but this should illustrate how to solve your problem. Let's assume you have a text input and a button. When the button is clicked, you want the focus to remain on the text input.
First write the intent() function:
function intent(DOMSource) {
const textStream$ = DOMSource.select('#input-msg').events('keyup').map(e => e.target);
const buttonClick$ = DOMSource.select('#send-btn').events('click').map(e => e.target);
return buttonClick$.withLatestFrom(textStream$, (buttonClick, textStream) => {
return textStream;
});
}
Then the main which has a sink to handle the lost focus side effect
function main(sources) {
const textStream$ = intent(sources.DOM);
const sink = {
DOM: view(sources.DOM),
EffectLostFocus: textStream$,
}
return sink;
}
Then the driver to handle this side effect would look something like
Cycle.run(main, {
DOM: makeDOMDriver('#app'),
EffectLostFocus: function(textStream$) {
textStream$.subscribe((textStream) => {
console.log(textStream.value);
textStream.focus();
textStream.value = '';
})
}
});
The entire example is in this codepen.
Yes, you do need to reach inside the DOM and call
.focus()
either with or without jQuery. However this is a side-effect and it is Cycle.js convention to move these kinds of side effects to a so-called driver.The two questions the driver needs to know are:
The answer to both questions can be provided by a single stream of DOM elements.
Create the driver
First make your driver. Let's call it
SetFocus
. We'll make it a so-called read-only driver. It will read from the app's sinks but it will not provide a source to the app. Because it is reading, the driver's function will need to accept a formal parameter that will be a stream, call itelem$
:This driver takes whatever DOM element arrives in the stream and calls
.focus()
on it.Use the Driver
Add it to the list of drivers provided to the
Cycle.run
function:Then in your main function:
You can then configure
focusNeeded
to say when you want.field
to be focused.