I have written a tiny component for Drafj.js to have defaultValue
prop. Everything works well, But after load defaultValue
, Inline css remove from my HTML!
export function Editor({ ...props }) {
const [editorState, setEditorState] = useState<any>(null)
useEffect(() => {
if (props?.defaultValue) {
const blocksFromHTML: any = convertFromHTML(props?.defaultValue)
setEditorState(EditorState.createWithContent(ContentState.createFromBlockArray(blocksFromHTML)))
}
}, [props?.defaultValue])
const stateChange = (estate: any) => {
setEditorState(estate)
}
if (props?.noeditor) {
return <><Label htmlFor={props?.id}>{props.title}</Label>
<Textarea name={props?.name}></Textarea>
</>
}
return <><Label htmlFor={props?.id}>{props.title}</Label><DraftEditor
editorState={editorState}
onEditorStateChange={stateChange}
/><textarea className="hidden" name={props?.name} value={editorState ? draftToHtml(convertToRaw(editorState?.getCurrentContent() || "")) : ""}></textarea></>;
}
Usage :
<Editor name="short_content" title={t("Short Content")} defaultValue={edit?.short_content || ""} />
When I save below HTML code in my database:
<p><span style="color: rgb(97,189,109);"><strong>1000 test</strong></span><strong> test </strong></p>
After setting it to defaultValue
inline CSS will be removed from my editor