Draftjs测试编辑器的自动对焦

时间:2017-03-24 17:43:50

标签: javascript reactjs draftjs

如何确保我的界面上的唯一draft-js文本框在页面加载时自动对焦?

有一个名为focus的文档化方法,但我不确定实现所需的语法。

2 个答案:

答案 0 :(得分:2)

请参阅here

基本上,你做的事情就像

<Editor ref="someRef" />

componentDidMount = () => {
    this.refs.someRef.focus();
}

答案 1 :(得分:1)

如果您的编辑器是使用某个默认值启动的,那么仅执行this.refs.editor.focus()就会在文本开头出现光标。因此,要将光标移到末尾,我们需要在componentDidMount

中执行类似的操作
  componentDidMount() {
    this.setState({
      editorState:  EditorState.moveFocusToEnd(this.state.editorState), // EditorState imported from draft-js
    });
  }

https://github.com/draft-js-plugins/draft-js-plugins/blob/master/draft-js-plugins-editor/src/Editor/moveSelectionToEnd.js-链接到方法