@nish1896/rhf-mui-components
Version:
A suite of 25+ production-ready react-hook-form components built with material-ui. Fully typed, tree-shakable, and optimized for enterprise-grade forms.
154 lines (153 loc) • 5.36 kB
TypeScript
import { CustomComponentIds } from "../../types/common.js";
import { FormHelperTextProps, FormLabelProps } from "../../types/mui.js";
import { DefaultEditorConfig } from "./config.js";
import { JSX, ReactNode, Ref } from "react";
import { Control, FieldValues, Path, RegisterOptions } from "react-hook-form";
import { CKEditor } from "@ckeditor/ckeditor5-react";
import { ClassicEditor } from "ckeditor5";
import { EventInfo } from "@ckeditor/ckeditor5-utils";
import { EditorConfig } from "@ckeditor/ckeditor5-core";
//#region src/misc/rich-text-editor/index.d.ts
/**
* CK Editor Props ref -
* https://ckeditor.com/docs/ckeditor5/latest/getting-started/legacy/legacy-integrations/react.html#context-feature-properties
*/
type ErrorDetails = {
phase: 'initialization' | 'runtime';
willContextRestart?: boolean;
};
type RHFRichTextEditorOnValueChangeProps = {
newValue: string;
event: EventInfo;
editor: ClassicEditor;
};
type RHFRichTextEditorCustomOnChangeProps = RHFRichTextEditorOnValueChangeProps & {
rhfOnChange: (newValue: string) => void;
};
type RHFRichTextEditorProps<T extends FieldValues> = {
/**
* Name/path of the React Hook Form field this component controls.
*/
fieldName: Path<T>;
/**
* React Hook Form control object returned by `useForm`.
*/
control: Control<T>;
/**
* Validation rules passed to React Hook Form for this field.
*/
registerOptions?: RegisterOptions<T, Path<T>>;
/**
* When true, marks the field as required in the UI and accessibility attributes.
*/
required?: boolean;
/**
* HTML id applied to the CKEditor instance.
*
* Defaults to the generated field id.
*/
id?: string;
/**
* CKEditor configuration passed to `ClassicEditor`.
*
* Defaults to this package's `DefaultEditorConfig`.
*/
editorConfig?: EditorConfig;
/**
* Callback fired when the CKEditor instance is ready.
*/
onReady?: (editor: ClassicEditor) => void;
/**
* Callback fired when the CKEditor instance receives focus.
*/
onFocus?: (event: EventInfo<string, unknown>, editor: ClassicEditor) => void;
/**
* Callback fired when the CKEditor instance loses focus.
*
* The wrapper also marks the React Hook Form field as touched.
*/
onBlur?: (event: EventInfo<string, unknown>, editor: ClassicEditor) => void;
/**
* Overrides the default rich text editor change handling.
* Receives the next editor HTML string, CKEditor event info, and editor instance.
* Call `rhfOnChange` with the HTML string that should be stored; otherwise the previous form value is kept.
* After the handler runs, the editor content is synced back to the committed form value.
*
* @param rhfOnChange - React Hook Form field change handler for the editor HTML string.
* @param newValue - Current editor HTML string.
* @param event - CKEditor change event info.
* @param editor - CKEditor instance that emitted the change.
*/
customOnChange?: ({
rhfOnChange,
newValue,
event,
editor
}: RHFRichTextEditorCustomOnChangeProps) => void;
/**
* Called after the default rich text editor handler stores the current HTML string in React Hook Form.
*
* ⚠️ Important:
* This callback is not called when `customOnChange` is used.
*
* @param newValue - Current editor HTML string.
* @param event - CKEditor change event info.
* @param editor - CKEditor instance that emitted the change.
*/
onValueChange?: ({
newValue,
event,
editor
}: RHFRichTextEditorOnValueChangeProps) => void;
/**
* When true, disables the field and associated controls.
*/
disabled?: boolean;
/**
* Label content shown for the field. Defaults to a label generated from `fieldName`.
*/
label?: ReactNode;
/**
* When true, renders the field label above the form field instead of inside or beside it.
*/
showLabelAboveFormField?: boolean;
/**
* Props forwarded to the internal `FormLabel`. The `id` is managed by the component.
*/
formLabelProps?: Omit<FormLabelProps, 'id'>;
/**
* When true, hides the rendered field label while preserving accessible labeling where possible.
*/
hideLabel?: boolean;
/**
* Helper text shown below the field when there is no visible validation error.
*/
helperText?: ReactNode;
/**
* Callback fired when CKEditor reports an initialization or runtime error.
*/
onError?: (error: Error, details: ErrorDetails) => void;
/**
* @deprecated
* Field error message is now automatically derived from form state.
* Passing this prop is no longer necessary and it will be removed in the next major version.
*/
errorMessage?: ReactNode;
/**
* If true, hides the error message text while keeping the field in an error state.
*/
hideErrorMessage?: boolean;
/**
* Props forwarded to the internal `FormHelperText`. The `id` is managed by the component.
*/
formHelperTextProps?: Omit<FormHelperTextProps, 'id'>;
/**
* Custom ids for generated field, label, helper text, and error elements.
*/
customIds?: CustomComponentIds;
};
declare const RHFRichTextEditor: <T extends FieldValues>(props: RHFRichTextEditorProps<T> & {
ref?: Ref<CKEditor<ClassicEditor>>;
}) => JSX.Element;
//#endregion
export { DefaultEditorConfig, RHFRichTextEditorProps, RHFRichTextEditor as default };