fix(richtext-lexical): inaccurate detection of whether the editor is empty or not

This commit is contained in:
Alessio Gravili
2024-08-21 17:26:02 -04:00
parent d894ac75f0
commit 5174c7092f

View File

@@ -20,15 +20,24 @@ export const richTextValidateHOC = ({
} = options
if (required) {
const hasChildren = value?.root?.children?.length
const hasChildren = !!value?.root?.children?.length
const hasOnlyEmptyParagraph =
(value?.root?.children?.length === 1 &&
value?.root?.children[0]?.type === 'paragraph' &&
(value?.root?.children[0] as SerializedParagraphNode)?.children?.length === 0) ||
((value?.root?.children[0] as SerializedParagraphNode)?.children?.length === 1 &&
(value?.root?.children[0] as SerializedParagraphNode)?.children[0]?.type === 'text' &&
(value?.root?.children[0] as SerializedParagraphNode)?.children[0]?.['text'] === '')
let hasOnlyEmptyParagraph = false
if (value?.root?.children?.length === 1) {
if (value?.root?.children[0]?.type === 'paragraph') {
const paragraphNode = value?.root?.children[0] as SerializedParagraphNode
if (paragraphNode?.children?.length === 0) {
hasOnlyEmptyParagraph = true
} else if (paragraphNode?.children?.length === 1) {
const paragraphNodeChild = paragraphNode?.children[0]
if (paragraphNodeChild.type === 'text') {
if (!paragraphNodeChild?.['text']?.length) {
hasOnlyEmptyParagraph = true
}
}
}
}
}
if (!hasChildren || hasOnlyEmptyParagraph) {
return t('validation:required')