"use client"
import { Autocomplete, MenuItem, TextField, Checkbox } from "@mui/material";
import { Controller, FieldValues, Path, Control, RegisterOptions } from "react-hook-form";
import { useTranslation } from "react-i18next";
import CheckBoxOutlineBlankIcon from '@mui/icons-material/CheckBoxOutlineBlank';
import CheckBoxIcon from '@mui/icons-material/CheckBox';
const icon = ;
const checkedIcon = ;
// label -> e.g. code - name -> 001 - WL
// name -> WL
interface Props {
control: Control,
options: T[],
name: Path, // register name
label?: string, // display label
noOptionsText?: string,
isMultiple?: boolean,
rules?: RegisterOptions
error?: boolean,
}
function ControlledAutoComplete<
T extends { id?: number | string; label?: string; name?: string },
TField extends FieldValues
>(
props: Props
) {
const { t } = useTranslation()
const { control, options, name, label, noOptionsText, isMultiple, rules, error } = props;
return (
(
isMultiple ?
{
// console.log(field.value)
return field.value?.includes(option.id)
})}
options={options}
getOptionLabel={(option) => option.label ?? option.name!!}
isOptionEqualToValue={(option, value) => option.id === value.id}
renderOption={(params, option, { selected }) => {
return (
{option.label ?? option.name}
);
}}
onChange={(event, value) => {
field.onChange(value?.map(v => v.id))
}}
renderInput={(params) => }
/>
:
option.id === field.value) ?? options[0]}
options={options}
getOptionLabel={(option) => option.label ?? option.name!!}
isOptionEqualToValue={(option, value) => option.id === value.id}
renderOption={(params, option) => {
return (
);
}}
onChange={(event, value) => {
field.onChange(value?.id)
}}
renderInput={(params) => }
/>
)}
/>
)
}
export default ControlledAutoComplete;