contentState.hasText() && contentState.getPlainText().length > 0
export const isEmptyDraftJs = (rawState) => {
if (!rawState || _.isEmpty(rawState)) { // filter undefined and {}
return true;
}
const contentState = convertFromRaw(rawState);
return !(contentState.hasText() && (contentState.getPlainText() !== ''));
};
我不确定它是否完美,但我使用上面的代码。
当您仅添加图像时,有一个空格字符,因此
getPlainText()
只能过滤图像draftJS。
const text=editorState.getCurrentState().getPlainText()
return text===''
const [editorState, setEditorState] = useState(EditorState.createEmpty());
const [errorMessage, setErrorMessage] = useState('');
const handleSubmit = () => {
const contentState = editorState.getCurrentContent();
const rawContentState = convertToRaw(contentState);
const text = rawContentState.blocks[0].text.trim();
if (text === '') {
setErrorMessage('Please enter some content before submitting.');
} else {
// Proceed with submission logic
setErrorMessage('');
// Your submission logic goes here
}
};