import { LogLevels } from "@editorjs/editorjs"; import { FormControl, FormHelperText, InputLabel } from "@material-ui/core"; import { useId } from "@reach/auto-id"; import { EditorCore, Props as ReactEditorJSProps } from "@react-editor-js/core"; import clsx from "clsx"; import React from "react"; import { createReactEditorJS } from "react-editor-js"; import { tools } from "./consts"; import { useHasRendered } from "./hooks"; import useStyles from "./styles"; export type EditorJsProps = Omit; export interface RichTextEditorProps extends Omit { id?: string; disabled: boolean; error: boolean; helperText: string; label: string; name: string; editorRef: | React.RefCallback | React.MutableRefObject | null; // onChange with value shouldn't be used due to issues with React and EditorJS integration onChange?: () => void; } const ReactEditorJS = createReactEditorJS(); const RichTextEditor: React.FC = ({ id: defaultId, disabled, error, label, name, helperText, editorRef, onInitialize, ...props }) => { const classes = useStyles({}); const id = useId(defaultId); const [isFocused, setIsFocused] = React.useState(false); const handleInitialize = React.useCallback((editor: EditorCore) => { if (onInitialize) { onInitialize(editor); } if (typeof editorRef === "function") { return editorRef(editor); } if (editorRef) { return (editorRef.current = editor); } }, []); // We need to render FormControl first to get id from @reach/auto-id const hasRendered = useHasRendered(); return ( {label} {hasRendered && (
setIsFocused(true)} onBlur={() => setIsFocused(false)} /> )} {helperText} ); }; RichTextEditor.displayName = "RichTextEditor"; export default RichTextEditor;