fix(richtext-lexical): make editor reactive to initialValue changes (#5010)

This commit is contained in:
Alessio Gravili
2024-02-08 15:30:21 +01:00
committed by Alessio Gravili
parent 9e67efbd1b
commit bce821dee9
2 changed files with 3 additions and 2 deletions

View File

@@ -49,7 +49,7 @@ const RichText: React.FC<FieldProps> = (props) => {
validate: memoizedValidate,
})
const { errorMessage, setValue, showError, value } = fieldType
const { errorMessage, initialValue, setValue, showError, value } = fieldType
const classes = [
baseClass,
@@ -77,6 +77,7 @@ const RichText: React.FC<FieldProps> = (props) => {
<LexicalProvider
editorConfig={editorConfig}
fieldProps={props}
key={JSON.stringify({ initialValue, path })} // makes sure lexical is completely re-rendered when initialValue changes, bypassing the lexical-internal value memoization. That way, external changes to the form will update the editor. More infos in PR description (https://github.com/payloadcms/payload/pull/5010)
onChange={(editorState) => {
let serializedEditorState = editorState.toJSON()

View File

@@ -299,7 +299,7 @@ const RichText: React.FC<
{Label}
<Slate
editor={editor}
key={JSON.stringify({ initialValue, path })}
key={JSON.stringify({ initialValue, path })} // makes sure slate is completely re-rendered when initialValue changes, bypassing the slate-internal value memoization. That way, external changes to the form will update the editor
onChange={handleChange}
value={valueToRender as any[]}
>