1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-02 22:52:21 +01:00

72 lines
3.0 KiB
TypeScript
Raw Normal View History

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<TFieldValues, TContext extends object> extends FormControlledComponent<TFieldValues, TContext>, AbstractFormItemProps<TFieldValues> {
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 = <TFieldValues extends FieldValues, TContext extends object>({ id, label, tooltip, className, control, valueDefault, error, warning, rules, disabled = false, formState, limit, heading, bulletList, blockquote, video, image, link }: FormRichTextProps<TFieldValues, TContext>) => {
const textEditorRef = React.useRef<FabTextEditorRef>();
const [isDisabled, setIsDisabled] = React.useState<boolean>(false);
useEffect(() => {
if (typeof disabled === 'function') {
2022-05-10 15:22:01 +02:00
setIsDisabled(disabled(id));
} else {
2022-05-10 15:22:01 +02:00
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.
*/
2022-08-22 15:25:00 +02:00
function focusTextEditor (event: React.MouseEvent<HTMLParagraphElement, MouseEvent>) {
event.preventDefault();
textEditorRef.current.focus();
}
return (
<AbstractFormItem id={id} label={label} tooltip={tooltip}
2022-08-22 15:25:00 +02:00
containerType={'div'}
className={`form-rich-text ${className || ''}`}
error={error} warning={warning} rules={rules}
disabled={disabled} formState={formState} onLabelClick={focusTextEditor}>
<Controller name={id as FieldPath<TFieldValues>}
control={control}
defaultValue={valueDefault as UnpackNestedValue<FieldPathValue<TFieldValues, Path<TFieldValues>>>}
rules={rules}
render={({ field: { onChange, value } }) =>
<FabTextEditor onChange={onChange}
content={value}
limit={limit}
heading={heading}
bulletList={bulletList}
blockquote={blockquote}
video={video}
image={image}
link={link}
2022-05-10 15:22:01 +02:00
disabled={isDisabled}
ref={textEditorRef} />
} />
</AbstractFormItem>
);
};