Skip to content

Commit

Permalink
Fix max length on return (niuware#194)
Browse files Browse the repository at this point in the history
* Extract maximum length logic condition

* Handle maximum length on return
  • Loading branch information
niuware authored Oct 8, 2020
1 parent 12ad09d commit 92f7cff
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/MUIRichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ const MUIRichTextEditor: RefForwardingComponent<TMUIRichTextEditorRef, IMUIRichT
setEditorState(state)
}

const handleBeforeInput = (chars: string): DraftHandleValue => {
const handleBeforeInput = (chars: string, editorState: EditorState): DraftHandleValue => {
if (chars === " " && searchTerm.length) {
clearSearch()
} else if (autocompleteSelectionStateRef.current) {
Expand All @@ -501,8 +501,7 @@ const MUIRichTextEditor: RefForwardingComponent<TMUIRichTextEditorRef, IMUIRichT
updateAutocompletePosition()
}
}
const currentLength = editorState.getCurrentContent().getPlainText('').length
return isGreaterThan(currentLength + 1, props.maxLength) ? "handled" : "not-handled"
return isMaxLengthHandled(editorState, 1)
}

const isSyntheticEventTriggeredByTab = (event: SyntheticEvent): boolean => {
Expand Down Expand Up @@ -745,8 +744,16 @@ const MUIRichTextEditor: RefForwardingComponent<TMUIRichTextEditorRef, IMUIRichT
}

const handlePastedText = (text: string, _html: string|undefined, editorState: EditorState): DraftHandleValue => {
return isMaxLengthHandled(editorState, text.length)
}

const handleReturn = (_e: any, editorState: EditorState): DraftHandleValue => {
return isMaxLengthHandled(editorState, 1)
}

const isMaxLengthHandled = (editorState: EditorState, nextLength: number): DraftHandleValue => {
const currentLength = editorState.getCurrentContent().getPlainText('').length
return isGreaterThan(currentLength + text.length, props.maxLength) ? "handled" : "not-handled"
return isGreaterThan(currentLength + nextLength, props.maxLength) ? "handled" : "not-handled"
}

const toggleMouseUpListener = (addAfter = false) => {
Expand Down Expand Up @@ -1074,6 +1081,7 @@ const MUIRichTextEditor: RefForwardingComponent<TMUIRichTextEditorRef, IMUIRichT
handleKeyCommand={handleKeyCommand}
handleBeforeInput={handleBeforeInput}
handlePastedText={handlePastedText}
handleReturn={handleReturn}
keyBindingFn={keyBindingFn}
ref={editorRef}
{...props.draftEditorProps}
Expand Down

0 comments on commit 92f7cff

Please sign in to comment.