import { Autocomplete, Button, TextField } from "@mui/material"; import { getLocalizedString } from "@olenbetong/appframe-core"; import { useEffect, useRef, useState } from "react"; export type DescriptionInputType = { Description?: string | null; Cause?: string | null; Action?: string | null; Date?: Date; }; type DescriptionInputProps = { value?: DescriptionInputType | null; defaultValue?: DescriptionInputType | null; descriptionOptions?: string[]; storageNamespace?: string; onCopyPrevDamage?: (value: DescriptionInputType) => void; onChange?: (value: DescriptionInputType) => void; }; export function DescriptionInput({ value, defaultValue, descriptionOptions = [ getLocalizedString("Tear in element"), getLocalizedString("Damage around lifting anchor"), getLocalizedString("Damage on underside"), ], storageNamespace = "ElementDamage", onCopyPrevDamage, onChange, }: DescriptionInputProps) { let initializedRef = useRef(false); let [input, setInput] = useState(defaultValue ?? null); let inputValue = value === undefined ? input : value; // biome-ignore lint: intentionally skipping "value" dependency as this shouldn't run when "value" changes useEffect(() => { if (initializedRef.current) { return; } if (value === undefined && defaultValue) { setInput(defaultValue); initializedRef.current = true; } }, [defaultValue]); function handleCopyPrevDamage() { let description = localStorage.getItem(`${storageNamespace}_Description`) ?? ""; let cause = localStorage.getItem(`${storageNamespace}_Cause`) ?? ""; let action = localStorage.getItem(`${storageNamespace}_Action`) ?? ""; onCopyPrevDamage?.({ Description: description, Cause: cause, Action: action, }); setInput({ ...inputValue, Description: description, Cause: cause, Action: action, }); } function handleOnChange(value: DescriptionInputType) { onChange?.(value); setInput(value); } return ( <> handleOnChange({ ...inputValue, Date: new Date(event.target.value) })} id="NewElementDamage_Date" label={getLocalizedString("Date")} fullWidth required slotProps={{ inputLabel: { shrink: true } }} /> handleOnChange({ ...inputValue, Description: value ?? "" })} options={descriptionOptions} freeSolo renderInput={(params) => ( )} /> handleOnChange({ ...inputValue, Cause: event.currentTarget.value })} label={getLocalizedString("Cause")} fullWidth /> handleOnChange({ ...inputValue, Action: event.currentTarget.value })} label={getLocalizedString("Action")} fullWidth /> ); }