question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug - clearing editorState when blurred doesn't work

See original GitHub issue

Hello, when I tried to use onBlur handler bellow, it doesn’t work and I have no idea why. Otherwise editor works OK, I use the latest version.

Note that:

  • yes, the method runs after user blur from editor (and handleBlur is binded to this in contructor)
  • it also passes through my condition, so the actual code inside runs
  • it triggers another re-render of component after that, but the editorState is not changed
handleBlur() {
    const { resetTextOnBlur, text } = this.props;
    // reset editor to previous value in props
    if (resetTextOnBlur) {
      let editorState = EditorState.push(this.state.editorState, ContentState.createFromText(text));
      this.setState({ editorState });
    }
  }

render() {
    const { placeholder } = this.props;

    return (
      <Editor
        name="message"
        editorState={this.state.editorState}
        keyBindingFn={this.myKeyBindingFn}
        onChange={this.onChange}
        onBlur={this.handleBlur}
        ref="editor"
        placeholder={placeholder}
      />
    );
  }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

4reactions
calic39commented, Jan 11, 2017

Thank you very much guys! The solution is listen to getHasFocus() in onChange handler as @JohnONeill suggested. So this way, it works!

onChange(editorState) {
    const { resetTextOnBlur, text } = this.props;
    const focused = editorState.getSelection().getHasFocus();

    focused && this.setState({ editorState });

    if (resetTextOnBlur && !focused) {
      let newEditorState = EditorState.push(this.state.editorState, ContentState.createFromText(text));
      this.setState({ editorState: newEditorState });
    }
  }
1reaction
JohnONeillcommented, Jan 11, 2017

@calic39 Maybe you could just listen to the selection state’s getHasFocus() – when it’s false, you know the editor’s blurred, and you can listen to that / restore the original text within your handleChange() handler:

https://jsfiddle.net/9xvLqgka/4/ (just added a line of logging around the selection state handleChange())

Read more comments on GitHub >

github_iconTop Results From Across the Web

Detect blur aka losing focus of a Draft.js editor - Stack Overflow
Is there a "correct way" to detect if I lose focus of a Draft.js editor? The use case is, that I want "quit...
Read more >
FIXED: Youtube Blur Effect Won't Save
You spent hours on adding blur effects to your video then nothing happens when you click save and all the time you spend...
Read more >
NodeView selection is lost if div.ProseMirror is altered on tab ...
I'm encountering some other bugs where nodeselections are lost when blurring the view as well. Do you have any ideas for why, on...
Read more >
BUG : Writing Text with the Pen Code makes it blur away
It does this in the full screen mode, on the public version of the code, and in full screen in the Editor. Yet...
Read more >
23440 - Wrong text-shadow blur rendering - chromium
An acknowledged and year-and-a-half old bug, would be nice to see fixed. ... Also, very importantly, the if you don't blur the text,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found