import { FormHelperTextProps, FormLabelProps } from "../../common/types/mui.js"; import "../../common/index.js"; import { JSX, ReactNode, Ref } from "react"; import { Control, FieldError, FieldValues, Path, RegisterOptions } from "react-hook-form"; import { CKEditor } from "@ckeditor/ckeditor5-react"; import { ClassicEditor } from "ckeditor5"; import { DefaultEditorConfig } from "@nish1896/mui-components/misc/rich-text-editor"; import { CustomComponentIds } from "@nish1896/mui-components/types"; 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 = { /** * Name/path of the React Hook Form field this component controls. */ fieldName: Path; /** * React Hook Form control object returned by `useForm`. */ control: Control; /** * Validation rules passed to React Hook Form for this field. */ registerOptions?: RegisterOptions>; /** * HTML id applied to the CKEditor instance. * * Defaults to the generated field id. */ id?: string; /** * When true, marks the field as required in the UI and accessibility attributes. */ required?: boolean; /** * 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; /** * 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; /** * Callback fired when the CKEditor instance receives focus. */ onFocus?: (event: EventInfo, 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, editor: ClassicEditor) => 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; /** * Renders the label above the component. * @default true */ showLabelAboveFormField?: boolean; /** * Props forwarded to the internal `FormLabel`. The `id` is managed by the component. */ formLabelProps?: Omit; /** * When true, hides the rendered field label while preserving accessible labeling where possible. */ hideLabel?: boolean; /** * Callback fired when CKEditor reports an initialization or runtime error. */ onError?: (error: Error, details: ErrorDetails) => void; /** * Custom renderer for the React Hook Form field error. * Receives the current field error and must return renderable content, such as `error.message` or a custom element. * * @param error - React Hook Form field error for this field. */ renderError?: (error: FieldError) => ReactNode; /** * If true, hides the error message text while keeping the field in an error state. */ hideErrorMessage?: boolean; /** * Helper text shown below the field when there is no visible validation error. */ helperText?: ReactNode; /** * Props forwarded to the internal `FormHelperText`. The `id` is managed by the component. */ formHelperTextProps?: Omit; /** * Custom ids for generated field, label, helper text, and error elements. */ customIds?: CustomComponentIds; }; /** * Controlled CKEditor 5 rich text editor, wired to a React Hook Form field * via `control`. * * Docs: [RHFRichTextEditor](https://rhf-mui-components.vercel.app/components/misc/RHFRichTextEditor) * * API: [RHFRichTextEditorProps](https://rhf-mui-components.vercel.app/components/misc/RHFRichTextEditor#api) */ declare const RHFRichTextEditor: (props: RHFRichTextEditorProps & { ref?: Ref>; }) => JSX.Element; //#endregion export { DefaultEditorConfig, RHFRichTextEditorProps, RHFRichTextEditor as default };