@nish1896/rhf-mui-components
Version:
A suite of 20+ production-ready react-hook-form components built with material-ui. Fully typed, tree-shakable, and optimized for enterprise-grade forms.
77 lines (76 loc) • 2.73 kB
JavaScript
"use client";
import { fieldNameToLabel } from "../../utils/text-transform.js";
import { useFieldIds } from "../../utils/useFieldIds.js";
import FormControl from "../../common/FormControl.js";
import FormHelperText from "../../common/FormHelperText.js";
import FormLabel from "../../common/FormLabel.js";
import { DefaultEditorConfig } from "./config.js";
import { jsx, jsxs } from "react/jsx-runtime";
import { Controller } from "react-hook-form";
import { CKEditor } from "@ckeditor/ckeditor5-react";
import { ClassicEditor } from "ckeditor5";
import "ckeditor5/ckeditor5.css";
//#region src/misc/rich-text-editor/index.tsx
const RHFRichTextEditor = ({ fieldName, control, registerOptions, required, id, editorConfig, onReady, onFocus, onBlur, onValueChange, disabled: muiDisabled, label, showLabelAboveFormField, formLabelProps, helperText, onError, errorMessage, hideErrorMessage, formHelperTextProps }) => {
const { fieldId, labelId, helperTextId, errorId } = useFieldIds(fieldName);
const fieldLabel = label ?? fieldNameToLabel(fieldName);
const isFormLabelVisible = showLabelAboveFormField ?? true;
const isError = Boolean(errorMessage);
const showHelperTextElement = !!helperText || isError && !hideErrorMessage;
return /* @__PURE__ */ jsxs(FormControl, {
error: isError,
children: [
/* @__PURE__ */ jsx(FormLabel, {
label: fieldLabel,
isVisible: isFormLabelVisible,
required,
error: isError,
formLabelProps: {
id: labelId,
htmlFor: fieldId,
...formLabelProps
}
}),
/* @__PURE__ */ jsx(Controller, {
name: fieldName,
control,
rules: registerOptions,
render: ({ field: { value: rhfValue, onChange: rhfOnChange, onBlur: rhfOnBlur, ref: rhfRef } }) => /* @__PURE__ */ jsx(CKEditor, {
id: id ?? fieldId,
editor: ClassicEditor,
config: editorConfig ?? DefaultEditorConfig,
data: rhfValue,
onChange: (event, editor) => {
const content = editor.getData();
rhfOnChange(content);
onValueChange?.(content, event, editor);
},
ref: rhfRef,
onReady,
onBlur: (event, editor) => {
rhfOnBlur();
onBlur?.(event, editor);
},
"aria-labelledby": isFormLabelVisible ? labelId : void 0,
"aria-describedby": showHelperTextElement ? isError ? errorId : helperTextId : void 0,
onFocus,
onError,
disabled: muiDisabled
})
}),
/* @__PURE__ */ jsx(FormHelperText, {
error: isError,
errorMessage,
hideErrorMessage,
helperText,
showHelperTextElement,
formHelperTextProps: {
id: isError ? errorId : helperTextId,
...formHelperTextProps
}
})
]
});
};
//#endregion
export { DefaultEditorConfig, RHFRichTextEditor as default };