import React, { useState } from "react"; import Autocomplete from "@mui/material/Autocomplete"; import FormHelperText from "@mui/material/FormHelperText"; import Grid from "@mui/material/Grid2"; import Stack from "@mui/material/Stack"; import TextField from "@mui/material/TextField"; import { useCardContext } from "../../contexts/CardContext"; import LabeledValue from "../LabeledValue"; import { CardFieldBaseProps, FormOption } from "./shared"; export interface CardFieldAutoCompleteProps extends CardFieldBaseProps, React.PropsWithChildren { fallback: string; isEditable?: boolean; type: "autocomplete"; value: FormOption[]; autoSelect?: boolean; clearOnBlur?: boolean; freeSolo?: boolean; } export const CardFieldAutoComplete: React.FC> = ({ fallback = "—", fallbackPredicate = (value) => value == null || (Array.isArray(value) && value.length === 0), formName, helperText, isEditable = true, isReadable = true, label, options, size = "grow", value: defaultValue, autoSelect = false, clearOnBlur = false, freeSolo = false, ...props }) => { const { isCompact, isEditing } = useCardContext(); // Manage the selected options state const [selectedOptions, setSelectedOptions] = useState(defaultValue ?? []); return ( {isEditing && isEditable ? ( option.id === value.id} multiple={true} options={options ?? []} renderInput={(props) => } sx={{ width: "100%" }} value={selectedOptions} onChange={(_, value) => setSelectedOptions( value.map((v) => (typeof v == "string" ? { id: v, label: v } : v)) as FormOption[], ) } {...props} /> {/* Render hidden inputs for form submission */} {selectedOptions.map((option) => ( ))} {helperText != null && {helperText}} ) : ( isReadable && ( label).join(", ") } /> ) )} ); }; export default CardFieldAutoComplete;