lexical: pass the actual EditorState as the ref, count with onChange callback

This commit is contained in:
Alex Gleason
2023-09-23 14:54:47 -05:00
parent 89b1574c77
commit 448d320f4e
2 changed files with 13 additions and 8 deletions

View File

@ -13,7 +13,7 @@ import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';
import { PlainTextPlugin } from '@lexical/react/LexicalPlainTextPlugin';
import clsx from 'clsx';
import { $createParagraphNode, $createTextNode, $getRoot } from 'lexical';
import { $createParagraphNode, $createTextNode, $getRoot, type EditorState } from 'lexical';
import React, { useMemo, useState } from 'react';
import { FormattedMessage } from 'react-intl';
@ -40,9 +40,10 @@ interface IComposeEditor {
eventDiscussion?: boolean
hasPoll?: boolean
autoFocus?: boolean
handleSubmit?: () => void
handleSubmit?(): void
onPaste?(files: FileList): void
onChange?(text: string): void
onFocus?: React.FocusEventHandler<HTMLDivElement>
onPaste?: (files: FileList) => void
placeholder?: JSX.Element | string
}
@ -65,7 +66,7 @@ const theme: InitialConfigType['theme'] = {
},
};
const ComposeEditor = React.forwardRef<string, IComposeEditor>(({
const ComposeEditor = React.forwardRef<EditorState, IComposeEditor>(({
className,
placeholderClassName,
composeId,
@ -74,6 +75,7 @@ const ComposeEditor = React.forwardRef<string, IComposeEditor>(({
hasPoll,
autoFocus,
handleSubmit,
onChange,
onFocus,
onPaste,
placeholder,
@ -153,8 +155,9 @@ const ComposeEditor = React.forwardRef<string, IComposeEditor>(({
ErrorBoundary={LexicalErrorBoundary}
/>
<OnChangePlugin onChange={(_, editor) => {
onChange?.(editor.getEditorState().read(() => $getRoot().getTextContent()));
if (editorStateRef && typeof editorStateRef !== 'function') {
editorStateRef.current = editor.getEditorState().read(() => $getRoot().getTextContent());
editorStateRef.current = editor.getEditorState();
}
}}
/>