-
I'm experiencing an issue with RichText fields in PayloadCMS. When a RichText field has never been edited, its value is null. However, after adding content and then completely removing it, instead of returning to null, it returns an empty structure: {
"root": {
"type": "root",
"format": "",
"indent": 0,
"version": 1,
"children": [
{
"type": "paragraph",
"format": "",
"indent": 0,
"version": 1,
"children": [],
"direction": null,
"textStyle": "",
"textFormat": 0
}
],
"direction": null
}
} This inconsistency causes problems when conditionally rendering content based on the field's value, as checking for null won't work for cleared fields. Is there a way to configure the RichText field to return null when all content is removed, maintaining consistency with its initial state? Or is there a recommended approach to handle this situation? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Same issue here. Bug raised #12633 |
Beta Was this translation helpful? Give feedback.
-
I've created this function for my own workaround in the meantime: export function isLexicalRichTextEmpty(value:any) {
if (!value) return true;
// Defensive: root must exist
const root = value.root;
if (!root || !Array.isArray(root.children)) return true;
// If root has no children, it's empty
if (root.children.length === 0) return true;
// If root has only EMPTY paragraph nodes as children, check them
const isAllEmptyParagraphs:boolean = root.children.every((child:any) =>
child.type === 'paragraph' &&
Array.isArray(child.children) &&
child.children.length === 0
);
return isAllEmptyParagraphs;
} |
Beta Was this translation helpful? Give feedback.
@hades200082
u can use: