From 19c6dddd2706acc0fbf07e76454ffadccb8d2937 Mon Sep 17 00:00:00 2001 From: Jonas Carlsen Date: Thu, 17 Oct 2024 12:16:09 +0200 Subject: [PATCH] fix: forward ref to PlainTextEditor --- .../SlateEditor/PlainTextEditor.tsx | 104 +++++++++--------- 1 file changed, 54 insertions(+), 50 deletions(-) diff --git a/src/components/SlateEditor/PlainTextEditor.tsx b/src/components/SlateEditor/PlainTextEditor.tsx index 39d8a81623..867ac8d3aa 100644 --- a/src/components/SlateEditor/PlainTextEditor.tsx +++ b/src/components/SlateEditor/PlainTextEditor.tsx @@ -7,7 +7,7 @@ */ import { FormikHandlers, useFormikContext } from "formik"; -import { useEffect, useCallback, useState } from "react"; +import { useEffect, useCallback, useState, forwardRef, Ref } from "react"; import { createEditor, Descendant } from "slate"; import { withHistory } from "slate-history"; import { Slate, Editable, ReactEditor, withReact } from "slate-react"; @@ -50,59 +50,63 @@ interface Props extends Omit { plugins?: SlatePlugin[]; } -const PlainTextEditor = ({ onChange, value, id, submitted, className, placeholder, plugins, ...rest }: Props) => { - const [editor] = useState(() => withPlugins(withHistory(withReact(createEditor())), plugins)); - const props = useFormControl({ id, readOnly: submitted, ...rest }); +const PlainTextEditor = forwardRef( + ({ onChange, value, id, submitted, className, placeholder, plugins, ...rest }, ref) => { + const [editor] = useState(() => withPlugins(withHistory(withReact(createEditor())), plugins)); + const props = useFormControl({ id, readOnly: submitted, ...rest }); - const onBlur = useCallback(() => { - ReactEditor.deselect(editor); - }, [editor]); + const onBlur = useCallback(() => { + ReactEditor.deselect(editor); + }, [editor]); - const onSlateChange = useCallback( - (val: Descendant[]) => - onChange({ - target: { - name: id, - value: val, - type: "SlateEditorValue", - }, - }), - [id, onChange], - ); + const onSlateChange = useCallback( + (val: Descendant[]) => + onChange({ + target: { + name: id, + value: val, + type: "SlateEditorValue", + }, + }), + [id, onChange], + ); - const { status, setStatus } = useFormikContext(); + const { status, setStatus } = useFormikContext(); - useEffect(() => { - if (status?.status === "revertVersion") { - ReactEditor.deselect(editor); - editor.children = value; - setStatus((prevStatus: FormikStatus) => ({ - ...prevStatus, - status: undefined, - })); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [status]); + useEffect(() => { + if (status?.status === "revertVersion") { + ReactEditor.deselect(editor); + editor.children = value; + setStatus((prevStatus: FormikStatus) => ({ + ...prevStatus, + status: undefined, + })); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [status]); - return ( - - { - // Remove inline styling to be able to apply styling from StyledPlaceholder - const { style, ...remainingAttributes } = attributes; - return {children}; - }} - {...props} - /> - - ); -}; + return ( + + { + // Remove inline styling to be able to apply styling from StyledPlaceholder + const { style, ...remainingAttributes } = attributes; + return {children}; + }} + {...props} + // Weird typescript error. Let's just ignore it + ref={ref as Ref} + /> + + ); + }, +); export default PlainTextEditor;