From d720b4ddc7824893fb7d85f3eec0f88fd7913dee Mon Sep 17 00:00:00 2001 From: Erik Lopez Date: Thu, 8 Oct 2020 10:26:52 +0900 Subject: [PATCH 1/2] Extract maximum length logic condition --- src/MUIRichTextEditor.tsx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/MUIRichTextEditor.tsx b/src/MUIRichTextEditor.tsx index 6f87b7b..b269d78 100644 --- a/src/MUIRichTextEditor.tsx +++ b/src/MUIRichTextEditor.tsx @@ -489,7 +489,7 @@ const MUIRichTextEditor: RefForwardingComponent { + const handleBeforeInput = (chars: string, editorState: EditorState): DraftHandleValue => { if (chars === " " && searchTerm.length) { clearSearch() } else if (autocompleteSelectionStateRef.current) { @@ -501,8 +501,7 @@ const MUIRichTextEditor: RefForwardingComponent { @@ -745,8 +744,12 @@ const MUIRichTextEditor: RefForwardingComponent { + return isMaxLengthHandled(editorState, text.length) + } + + 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) => { From c03e00e39c0f88268dd6218755d4ff581d9b2d48 Mon Sep 17 00:00:00 2001 From: Erik Lopez Date: Thu, 8 Oct 2020 10:27:11 +0900 Subject: [PATCH 2/2] Handle maximum length on return --- src/MUIRichTextEditor.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/MUIRichTextEditor.tsx b/src/MUIRichTextEditor.tsx index b269d78..a4a0aba 100644 --- a/src/MUIRichTextEditor.tsx +++ b/src/MUIRichTextEditor.tsx @@ -747,6 +747,10 @@ const MUIRichTextEditor: RefForwardingComponent { + return isMaxLengthHandled(editorState, 1) + } + const isMaxLengthHandled = (editorState: EditorState, nextLength: number): DraftHandleValue => { const currentLength = editorState.getCurrentContent().getPlainText('').length return isGreaterThan(currentLength + nextLength, props.maxLength) ? "handled" : "not-handled" @@ -1077,6 +1081,7 @@ const MUIRichTextEditor: RefForwardingComponent