Skip to main content
Version: 2.0

RHFRichTextEditor

RHFRichTextEditor is a powerful and flexible rich text editor designed for content creation within forms. It allows users to format text with features like bold, italics, lists, links, images, and more, making it suitable for blog posts, comments, or any content requiring rich formatting. This component is built on top of CkEditor, a highly customizable editor that supports advanced features like codeBlocks, findAndReplace, tables, and more.

npm i ckeditor5 @ckeditor/ckeditor5-react
yarn add ckeditor5 @ckeditor/ckeditor5-react

Usage

import RHFRichTextEditor, { RHFRichTextEditorProps, DefaultEditorConfig } from '@nish1896/rhf-mui-components/misc/rich-text-editor';
<RHFRichTextEditor
fieldName='bio'
control={control}
errorMessage={errors?.bio?.message}
/>

To reorder formatting options or add advanced features, you can provide a custom configuration via the editorConfig prop, or override the existing configuration by modifying the DefaultEditorConfig object. You can check the default configuration here.

To customize editor theme, check the theme customization guide.

For advanced features like findAndReplace, inserting images, markdown support, and word count, refer to this example. For a full list of available CKEditor features, visit the CKEditor documentation.

note

If your website supports both light and dark themes, styling CKEditor5 for theme switching can be challenging, as the editor doesn't dynamically adapt to theme changes once rendered. To address this, you can override the editor's CSS based on the active theme by referring this gist.

You may also refer CkEditor5 documentation for a better understanding.

Examples

Alternatives

I chose CKEditor for its advanced features, customization options and support for multiple languages. Although the documentation can be overwhelming, I've provided both basic and advanced configuration options for this component. If you're looking for a simpler solution, you can consider implementing mui-rte.

API

RHFRichTextEditorProps accepts the following props:

NameTypeRequiredDescription
fieldNamestringReact Hook Form requires name as a key for the registration process. This is a required prop for all components.
controlUseFormControlThe control option yielded on calling the useForm hook.
registerOptionsRegisterOptionsRegister options for validation if using react-hook-form without any validation libraries like yup or Joi.
requiredbooleanIndicates that the field is mandatory by adding an asterisk symbol (*) to the formLabel. This visual cue helps users quickly identify required fields in the form.
idstringThe context ID. When this property changes, the component restarts the context with its editor and reinitializes it based on the current configuration.
editorConfigEditorConfigA Configuration object for CkEditor to customize formatting controls and toolbar positioning, as per requirement. Refer to the toolbar positioning guide for more details.
onReady(editor: ClassicEditor) => voidA function called when the context is ready and all editors inside were (re)initialized with the context instance.
onFocus(event: EventInfo<string, unknown>, editor: ClassicEditor) => voidCallback function triggered when the editor gains focus.
onValueChange(newValue: string, event: EventInfo, editor: ClassicEditor) => voidCallback function returning the editor value, event object and editor details.
onBlur(event: EventInfo<string, unknown>, editor: ClassicEditor) => voidCallback function triggered when the editor gains loses focus.
disabledbooleanA boolean value to enable or disable editing of the form field.
labelReactNodeThe text to render in FormLabel component. By default, the value of fieldName such as firstName will be transformed to display "First Name".
showLabelAboveFormFieldbooleanRenders the form label above the field by default. Set this prop to false to hide the label.
formLabelPropsFormLabelPropsFormLabelProps to customise FormLabel component for a field. Multiple fields can be configured using the ConfigProvider component.
helperTextReactNodeThe content to display within the FormHelperText component below the field. If the field validation fails, this content will be overridden by the corresponding error message.
onError(error: Error, details: ErrorDetails) => voidA function called when the editor has crashed during the initialization or runtime. It receives two arguments: the error instance and the error details.
errorMessageReactNodeError message to be shown for a field in FormHelperText component.
hideErrorMessagebooleanA flag to prevent replacement of helper text of a field by the error message when the validation is triggered.
formHelperTextPropsFormHelperTextPropsFormHelperTextProps to customise FormHelperText component for a field. Multiple fields can be configured using the ConfigProvider component.