如何使用Cycle.js和RxJS聚焦输入?

Ily*_*yaK 4 javascript functional-programming focus rxjs cyclejs

如何使用Cycle聚焦输入?我是否需要进入DOM内部并.focus()使用或不使用jQuery 调用,或者是否有其他方式使用Cycle/RxJS?

Jam*_*son 6

是的,你需要进入DOM内部并.focus()使用或不使用jQuery 调用.然而,这是一个副作用,并且将这些副作用移动到所谓的驱动程序是Cycle.js惯例.

司机需要知道的两个问题是:

  • 你想要关注哪个元素
  • 你什么时候想要关注这个元素?

这两个问题的答案可以由单个DOM元素流提供.

创建驱动程序

首先让你的司机.我们称之为SetFocus.我们将它变成一个所谓的只读驱动程序.它将从应用程序的接收器中读取,但它不会为应用程序提供源.因为它正在读取,所以驱动程序的函数需要接受一个将成为流的形式参数,称之为elem$:

function makeSetFocusDriver() {
  function SetFocusDriver(elem$) {
    elem$.subscribe(elem => {
      elem.focus();
    });
  }
  return SetFocusDriver;
}
Run Code Online (Sandbox Code Playgroud)

此驱动程序接受流中的任何DOM元素并对其进行调用.focus().

使用驱动程序

将其添加到提供给该Cycle.run函数的驱动程序列表中:

Cycle.run(main, {
  DOM: makeDOMDriver('#app'),
  SetFocus: makeSetFocusDriver() // add a driver
});
Run Code Online (Sandbox Code Playgroud)

然后在你的主要功能:

function main({DOM}) {

  // setup some code to produce the elem$ stream
  // that will be read by the driver ...
  // [1]: say _when_ we want to focus, perhaps we need to focus when
  //      the user clicked somewhere, or maybe when some model value
  //      has changed
  // [2]: say _what_ we want to focus
  //      provide the textbox dom element as actual value to the stream
  //      the result is:
  //      |----o-----o-----o--->
  //      where each o indicates we want to focus the textfield
  //      with the class 'field'
  const textbox$ = DOM.select('.field').observable.flatMap(x => x); // [2]
  const focusNeeded = [
    clickingSomewhere$,    // [1]
    someKindofStateChange$ // [1]
  ];
  const focus$ = Observable.merge(...focusNeeded)
    .withLatestFrom(textbox$, (_, textbox) => textbox); // [2]

  // ...

  // [*]: Add driver to sinks, the driver reads from sinks.
  //      Cycle.js will call your driver function with the parameter
  //      `elem$` being supplied with the argument of `focus$`
  return {
    DOM: vtree$,
    SetFocus: focus$, // [*]
  };
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以配置focusNeeded为何时想要.field集中注意力.


Kev*_*nle 5

您可以根据自己的情况量身定制,但这应该说明如何解决您的问题.假设您有一个文本输入和一个按钮.单击该按钮时,您希望焦点保留在文本输入上.

首先编写intent()函数:

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;
    });
}
Run Code Online (Sandbox Code Playgroud)

然后主要有一个水槽来处理失去焦点的副作用

function main(sources) {
    const textStream$ = intent(sources.DOM);

    const sink = {
       DOM: view(sources.DOM),
       EffectLostFocus: textStream$,
    }

    return sink;
}
Run Code Online (Sandbox Code Playgroud)

然后处理这种副作用的驱动程序看起来像

Cycle.run(main, {
    DOM: makeDOMDriver('#app'),
    EffectLostFocus: function(textStream$) {    
         textStream$.subscribe((textStream) => {
         console.log(textStream.value);
         textStream.focus();
         textStream.value = '';
      })
    }
});
Run Code Online (Sandbox Code Playgroud)

整个示例都在此codepen中.