import { useEffect, useMemo, useRef, useState } from 'react' import { SimpleMdeReact } from 'react-simplemde-editor' import type SimpleMDE from 'easymde' import type { InputHTMLAttributes } from 'react' interface TextAreaProps { labelTitle?: string labelStyle?: string containerStyle?: string dataField?: string inputStyle?: string defaultValue: string placeholder?: string required?: boolean size?: string updateFormValue?: (value: string) => void } /** * @category Input */ export function TextAreaInput({ labelTitle, dataField, labelStyle, containerStyle, inputStyle, defaultValue, placeholder, required = true, size, updateFormValue, }: TextAreaProps) { const ref = useRef(null) const [inputValue, setInputValue] = useState(defaultValue) const containerRef = useRef(null) const [containerHeight, setContainerHeight] = useState('100%') useEffect(() => { if (containerRef.current) { const height = containerRef.current.offsetHeight setContainerHeight(`${height - 200}px`) } }, []) useEffect(() => { setInputValue(defaultValue) }, [defaultValue]) const handleChange = (value: string) => { const newValue = value setInputValue(newValue) if (updateFormValue) { updateFormValue(newValue) } } const options = useMemo(() => { return { status: false, lineNumbers: false, minHeight: size === 'small' ? '100%' : '100%', maxHeight: size === 'small' ? '30px' : containerHeight, forceSync: true, /* autoDownloadFontAwesome?: boolean; autofocus?: boolean; autosave?: AutoSaveOptions; autoRefresh?: boolean | { delay: number; }; blockStyles?: BlockStyleOptions; element?: HTMLElement; forceSync?: boolean; hideIcons?: ReadonlyArray; indentWithTabs?: boolean; initialValue?: string; insertTexts?: InsertTextOptions; lineWrapping?: boolean; minHeight?: string; maxHeight?: string; parsingConfig?: ParsingOptions; placeholder?: string; previewClass?: string | ReadonlyArray; previewImagesInEditor?: boolean; imagesPreviewHandler?: (src: string) => string, previewRender?: (markdownPlaintext: string, previewElement: HTMLElement) => string | null; promptURLs?: boolean; renderingConfig?: RenderingOptions; shortcuts?: Shortcuts; showIcons?: ReadonlyArray; spellChecker?: boolean | ((options: SpellCheckerOptions) => void); inputStyle?: 'textarea' | 'contenteditable'; nativeSpellcheck?: boolean; sideBySideFullscreen?: boolean; status?: boolean | ReadonlyArray; styleSelectedText?: boolean; tabSize?: number; toolbar?: boolean | ReadonlyArray<'|' | ToolbarButton | ToolbarIcon | ToolbarDropdownIcon>; toolbarTips?: boolean; toolbarButtonClassPrefix?: string; onToggleFullScreen?: (goingIntoFullScreen: boolean) => void; theme?: string; scrollbarStyle?: string; unorderedListStyle?: '*' | '-' | '+'; uploadImage?: boolean; imageMaxSize?: number; imageAccept?: string; imageUploadFunction?: (file: File, onSuccess: (url: string) => void, onError: (error: string) => void) => void; imageUploadEndpoint?: string; imagePathAbsolute?: boolean; imageCSRFToken?: string; imageCSRFName?: string; imageCSRFHeader?: boolean; imageTexts?: ImageTextsOptions; imageInputName?: string errorMessages?: ImageErrorTextsOptions; errorCallback?: (errorMessage: string) => void; promptTexts?: PromptTexts; syncSideBySidePreviewScroll?: boolean; overlayMode?: OverlayModeOptions; direction?: 'ltr' | 'rtl'; */ } as SimpleMDE.Options }, [containerHeight, size]) return (
{labelTitle ? ( ) : null} } ref={ref} id={dataField} value={inputValue} placeholder={placeholder ?? ''} onChange={handleChange} options={options} className={`${inputStyle ?? ''}`} />
) }