draft-js icon indicating copy to clipboard operation
draft-js copied to clipboard

Focus on EditorBlock

Open alihaddadkar opened this issue 8 years ago • 9 comments

Hi. How to focus textarea when add new block?

I want autofocus EditorBlock

<EditorBlock {...Object.assign({}, this.props, {
              "editable": true })
} />

alihaddadkar avatar Sep 20 '17 23:09 alihaddadkar

if you have some setup like this, where you wrap a draft component in some other larger component but you want to make the outer element focus draft

const WrappedDraft = () => (
  <div>
    <Editor ... />
  </div>
)

you can focus the editor when clicking the outer div by doing something like

class WrappedDraft extends React.Component {
  render() {
    return <div onClick={() => this.editor.focus()}>
      <Editor ref={elt => this.editor = elt;} ... />
    </div>
  }
}

having that ref to the draft component will allow you to call .focus() on it from all over.

good luck.

nsfmc avatar Sep 21 '17 04:09 nsfmc

@nsfmc

I want autofocus EditorBlock

<EditorBlock {...Object.assign({}, this.props, {
              "editable": true })
} />

alihaddadkar avatar Sep 21 '17 08:09 alihaddadkar

you can autofocus in much the same way using componentDidMount or however

class WrappedDraft extends React.Component {
  componentDidMount() {
    this.editor && this.editor.focus(); // or however you want
  }
  render() {
    return <div>
      <Editor ref={elt => this.editor = elt;} ... />
    </div>
  }
}

nsfmc avatar Sep 21 '17 16:09 nsfmc

@nsfmc I used this method in the main component, but I can not use it for custom component.

alihaddadkar avatar Sep 27 '17 08:09 alihaddadkar

@nsfmc I think that @alihaddadkar is asking on how to do the focus on EditorBlock not Editor

michelson avatar Oct 26 '17 15:10 michelson

Sorry i misread the issue, do you mean that when adding a new custom editorblock to your editor you want the editor to be focused and the selection to be moved to a location in the new editorblock?

nsfmc avatar Oct 26 '17 15:10 nsfmc

@nsfmc Yes, I want to do the same.

alihaddadkar avatar Oct 27 '17 10:10 alihaddadkar

I can also use this, ref approach won't work on EditorBlock yet as it does not forward the ref using innerRef or something like the new React 16.3 ref API.

I will be happy to contribute if we can agree on an API.

Thanks! :)

divyenduz avatar Apr 21 '18 19:04 divyenduz

Any news or workarounds?

weeebdev avatar Sep 14 '22 14:09 weeebdev