Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Lexical doesn't respect default values

default discord avatar
Mng-dev-ailast year
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
    last year

    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 dedicated engineering support directly from the Payload team..