最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react-quill onBlur not working - Stack Overflow

programmeradmin5浏览0评论

I am using react-quill as a rich text editor in a react app.

I have to store the text-editor's content in local React state, and only send the value up to Redux 'onBlur'. My issue is that onBlur won't work when, after having focus, you click buttons outside of the text-editor. (I.e., onBlur works when clicking non-interactive elements on the screen, but it won't work when clicking a "save" button).

Current code:

    class TextEditor extends React.Component {
        constructor(props) {
            super(props)
            this.state={
                content: this.props.content;
            }
            this.handleQuillEditor_change = this.handleQuillEditor_change.bind(this)
            this.handleQuillEditor_update = this.handleQuillEditor_update.bind(this)
        }

        handleQuillEditor_change (value) {
            // handleChange...
        }

        handleQuillEditor_update () {
           console.log('blur activated!')
        }


        render() {
            const cmsProps = this.props.cmsProps
            return (
                <div style={containerStyle}>

                      <ReactQuill value={this.state.content}
                                  onChange={this.handleQuillEditor_change} 
                                  onBlur={this.handleQuillEditor_update}/>

               </div>
            )           
        }
    }

I am using react-quill as a rich text editor in a react app.

I have to store the text-editor's content in local React state, and only send the value up to Redux 'onBlur'. My issue is that onBlur won't work when, after having focus, you click buttons outside of the text-editor. (I.e., onBlur works when clicking non-interactive elements on the screen, but it won't work when clicking a "save" button).

Current code:

    class TextEditor extends React.Component {
        constructor(props) {
            super(props)
            this.state={
                content: this.props.content;
            }
            this.handleQuillEditor_change = this.handleQuillEditor_change.bind(this)
            this.handleQuillEditor_update = this.handleQuillEditor_update.bind(this)
        }

        handleQuillEditor_change (value) {
            // handleChange...
        }

        handleQuillEditor_update () {
           console.log('blur activated!')
        }


        render() {
            const cmsProps = this.props.cmsProps
            return (
                <div style={containerStyle}>

                      <ReactQuill value={this.state.content}
                                  onChange={this.handleQuillEditor_change} 
                                  onBlur={this.handleQuillEditor_update}/>

               </div>
            )           
        }
    }
Share Improve this question edited Oct 30, 2017 at 15:23 jaimefps asked Oct 30, 2017 at 15:17 jaimefpsjaimefps 2,4148 gold badges27 silver badges45 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

My quick-fix: move the blur handler to the container div of the QuillComponent, like so:

            <div style={containerStyle} onBlur={this.handleQuillEditor_update}>

                  <ReactQuill value={this.state.content}
                              onChange={this.handleQuillEditor_change} />

           </div>

I have been running through same error and i fixed it with this bunch of code.

<ReactQuill
    onChange={(newValue, delta, source) => {
                if (source === 'user') {
                  input.onChange(newValue);
                 }}}
    onBlur={(range, source, quill) => {
            input.onBlur(quill.getHTML());
           }}
 />

Try this piece of code:

const handleBlur = (value: string) => {}

return (
  <Quill
    onBlur={(range, source, quill) => {
       handleBlur(quill.getHTML())
    }}
/>
)
发布评论

评论列表(0)

  1. 暂无评论