Lexical doesn't respect default values

default discord avatar
Mng-dev-ai8 months ago
1 1

Link to reproduction

No response

Describe the Bug

i'm creating a custom feature where i need to render lexical inside a modal, it works fine but when i try to set defaultValue it doesn't respect it

To Reproduce

 import RichText from 'payload/dist/admin/components/forms/field-types/RichText';
 import { lexicalEditor } from '@payloadcms/richtext-lexical';
 // other imports
 
 export const Modal = ({}) => {
    // some code
    const defaultValue = {
        root: {
            children: [
                {
                    children: [
                        {
                            text: "test",
                            type: 'text',
                        },
                    ],
                    direction: null,
                    format: '',
                    indent: 0,
                    type: 'paragraph',
                    version: 1,
                },
            ],
            direction: null,
            format: '',
            indent: 0,
            type: 'root',
            version: 1,
        },
    };
    return (
         <Form onSubmit={onSubmit}>
             <RichText
                        name="test"
                        defaultValue={defaultValue}
                        label="test"
                        editor={lexicalEditor({ features: [] })}
                        type="richText"
                        admin={{
                            className: `${BASE_CLASS}__rich-text`,
                        }}
                    />
                    <Submit>Submit</Submit>
      </Form>
  )}

Payload Version

2.1.1

Adapters and Plugins

No response

  • Selected Answer
    discord user avatar
    AlessioGr
    8 months ago

    The defaultValue prop in the RichText component actually doesn't do anything. Since that's intended, I'm closing this issue & converted it into a discussion. I've also removed this prop in 4bc5fa7 since it wasn't used.

    The defaultValue of the richtext field is populated solely through the Form. The RichText component itself receives it as value from the useField hook - which is provided by that Form.

    In order to give it a default value, you will have to set the initialState property of the Form component. Check out the Blocks Feature if you need an example of how initialState can be created & passed to the Form - I did something similar there

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.