saleor-dashboard/src/components/RichTextEditor/RichTextEditorContent.tsx

55 lines
1.3 KiB
TypeScript
Raw Normal View History

import { LogLevels } from "@editorjs/editorjs";
import { useId } from "@reach/auto-id";
2020-11-05 16:30:38 +00:00
import classNames from "classnames";
import React from "react";
import { createReactEditorJS } from "react-editor-js";
2020-11-05 16:30:38 +00:00
import { tools } from "./consts";
import { useHasRendered } from "./hooks";
import { EditorJsProps } from "./RichTextEditor";
2020-11-05 16:30:38 +00:00
import useStyles from "./styles";
export interface RichTextEditorContentProps
extends Omit<EditorJsProps, "defaultValue"> {
id?: string;
2020-11-05 16:30:38 +00:00
className?: string;
}
const ReactEditorJS = createReactEditorJS();
2020-11-05 16:30:38 +00:00
const RichTextEditorContent: React.FC<RichTextEditorContentProps> = ({
id: defaultId,
2020-11-05 16:30:38 +00:00
className,
value,
...props
2020-11-05 16:30:38 +00:00
}) => {
const classes = useStyles({});
const id = useId(defaultId);
2020-11-05 16:30:38 +00:00
// We need to render FormControl first to get id from @reach/auto-id
const hasRendered = useHasRendered();
if (!hasRendered) {
return <div />;
}
2020-11-05 16:30:38 +00:00
return (
<ReactEditorJS
holder={id}
logLevel={"ERROR" as LogLevels.ERROR}
tools={tools}
{...props}
defaultValue={value}
readOnly={true}
>
<div
id={id}
className={classNames(classes.editor, classes.rootStatic, className)}
/>
</ReactEditorJS>
2020-11-05 16:30:38 +00:00
);
};
RichTextEditorContent.displayName = "RichTextEditorContent";
export default RichTextEditorContent;