import * as React from "react"; import { useEffect, useRef, useState } from "react"; import Tribute from "tributejs"; import { useTags } from "../Map/hooks/useTags"; type TextAreaProps = { labelTitle?: string; labelStyle?: string; containerStyle?: string; dataField?: string; inputStyle?: string; defaultValue: string; placeholder?: string; updateFormValue?: (value: string) => void; } interface KeyValue { [key: string]: string; } export function TextAreaInput({ labelTitle, dataField, labelStyle, containerStyle, inputStyle, defaultValue, placeholder, updateFormValue }: TextAreaProps) { const ref = useRef(null); const [inputValue, setInputValue] = useState(defaultValue); // prevent react18 from calling useEffect twice const init = useRef(false); const tags = useTags(); const values: KeyValue[] = []; tags.forEach(tag => { values.push({ key: tag.name, value: tag.name, color: tag.color }); }); const tribute = new Tribute({ containerClass: 'tw-z-3000 tw-bg-base-100 tw-p-2 tw-rounded-lg tw-shadow', selectClass: 'tw-font-bold', trigger: "#", values: values, menuShowMinLength: 3, noMatchTemplate: () => { return "" }, menuItemTemplate: function (item) { return `#${item.string}`; } }); useEffect(() => { if (!init.current) { if (ref.current) { tribute.attach(ref.current); } init.current = true; } }, [ref]); useEffect(() => { setInputValue(defaultValue); }, [defaultValue]); const handleChange = (e: React.ChangeEvent) => { const newValue = e.target.value; setInputValue(newValue); if (updateFormValue) { updateFormValue(newValue); } }; return (
{labelTitle ? ( ) : null}
); }