Michael methods are correct, but the return value of the handler is not. "not_handled" is a pass-through case that allows the Editor component to process input data normally. In this case, we want to stop the editor from processing input.
In older versions of DraftJS, this looks like the presence of a string evaluating to “true” in the processing code, and therefore the above code behaves correctly. In later versions of DraftJS, the above fiddle doesn't work - I don't have a reputation to post more than one Fiddle here, but try Michael's code with v0.10 DraftJS for replication.
To fix this, return "processed" or "true" if you do not want the editor to continue to process the input.
Fiddle with Corrected Return Values
For instance,
_handleBeforeInput = () => { const currentContent = this.state.editorState.getCurrentContent(); const currentContentLength = currentContent.getPlainText('').length if (currentContentLength > MAX_LENGTH - 1) { console.log('you can type max ten characters'); return 'handled'; } }
For more information, see the DraftJS docs about canceling handlers.
source share