Skip to content

Commit

Permalink
Update editor focus call (niuware#195)
Browse files Browse the repository at this point in the history
* Move focus logic to Editor control

* Update placeholder focus logic
  • Loading branch information
niuware authored Oct 8, 2020
1 parent 92f7cff commit 5593cd4
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/MUIRichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ const MUIRichTextEditor: RefForwardingComponent<TMUIRichTextEditorRef, IMUIRichT
const autocompleteSelectionStateRef = useRef<SelectionState | undefined>(undefined)
const autocompletePositionRef = useRef<TPosition | undefined>(undefined)
const autocompleteLimit = props.autocomplete ? props.autocomplete.suggestLimit || 5 : 5
const isFirstFocus = useRef(true)
const selectionRef = useRef<TStateOffset>({
start: 0,
end: 0
Expand Down Expand Up @@ -520,6 +521,15 @@ const MUIRichTextEditor: RefForwardingComponent<TMUIRichTextEditorRef, IMUIRichT
setTimeout(() => (setEditorState(EditorState.moveFocusToEnd(nextEditorState))), 0)
}

const handlePlaceholderFocus = () => {
if (isFirstFocus.current === false) {
focusEditor()
return
}
handleFocus()
isFirstFocus.current = false
}

const handleFocus = () => {
focusEditor()
if (props.onFocus) {
Expand Down Expand Up @@ -1015,7 +1025,7 @@ const MUIRichTextEditor: RefForwardingComponent<TMUIRichTextEditorRef, IMUIRichT
[classes.error]: props.error
})}
tabIndex={0}
onFocus={focusEditor}
onFocus={handlePlaceholderFocus}
>
{props.label || ""}
</div>
Expand Down Expand Up @@ -1070,13 +1080,14 @@ const MUIRichTextEditor: RefForwardingComponent<TMUIRichTextEditorRef, IMUIRichT
<div id={`${editorId}-editor-container`} className={classNames(className, classes.editorContainer, {
[classes.editorReadOnly]: !editable,
[classes.error]: props.error
})} onFocus={handleEditorFocus} onBlur={handleBlur}>
})} onBlur={handleBlur}>
<Editor
customStyleMap={customRenderers.style}
blockRenderMap={customRenderers.block}
blockRendererFn={blockRenderer}
editorState={editorState}
onChange={handleChange}
onFocus={handleEditorFocus}
readOnly={props.readOnly}
handleKeyCommand={handleKeyCommand}
handleBeforeInput={handleBeforeInput}
Expand Down

0 comments on commit 5593cd4

Please sign in to comment.