import React, { useEffect } from 'react'; import { FormControlledComponent } from '../../models/form-component'; import { AbstractFormItem, AbstractFormItemProps } from './abstract-form-item'; import { FieldValues } from 'react-hook-form/dist/types/fields'; import FabTextEditor, { FabTextEditorRef } from '../base/text-editor/fab-text-editor'; import { Controller, Path } from 'react-hook-form'; import { FieldPath } from 'react-hook-form/dist/types/path'; import { FieldPathValue, UnpackNestedValue } from 'react-hook-form/dist/types'; interface FormRichTextProps extends FormControlledComponent, AbstractFormItemProps { valueDefault?: string, limit?: number, heading?: boolean, bulletList?: boolean, blockquote?: boolean, link?: boolean, video?: boolean, image?: boolean } /** * This component is a rich-text editor to use with react-hook-form. */ export const FormRichText = ({ id, label, tooltip, className, control, valueDefault, error, warning, rules, disabled = false, formState, limit, heading, bulletList, blockquote, video, image, link }: FormRichTextProps) => { const textEditorRef = React.useRef(); const [isDisabled, setIsDisabled] = React.useState(false); useEffect(() => { if (typeof disabled === 'function') { setIsDisabled(disabled(id)); } else { setIsDisabled(disabled); } }, [disabled]); /** * Callback triggered when the user clicks to get the focus on the editor. * We do not want the default behavior (focus the first child, which is the Bold button) * but we want to focus the text edition area. */ function focusTextEditor (event: React.MouseEvent) { event.preventDefault(); textEditorRef.current.focus(); } return ( } control={control} defaultValue={valueDefault as UnpackNestedValue>>} rules={rules} render={({ field: { onChange, value } }) => } /> ); };