Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

97 righe
4.1 KiB

  1. "use client"
  2. import { Autocomplete, MenuItem, TextField, Checkbox } from "@mui/material";
  3. import { Controller, FieldValues, Path, Control, RegisterOptions } from "react-hook-form";
  4. import { useTranslation } from "react-i18next";
  5. import CheckBoxOutlineBlankIcon from '@mui/icons-material/CheckBoxOutlineBlank';
  6. import CheckBoxIcon from '@mui/icons-material/CheckBox';
  7. const icon = <CheckBoxOutlineBlankIcon fontSize="medium" />;
  8. const checkedIcon = <CheckBoxIcon fontSize="medium" />;
  9. // label -> e.g. code - name -> 001 - WL
  10. // name -> WL
  11. interface Props<T extends { id?: number | string; label?: string; name?: string }, TField extends FieldValues> {
  12. control: Control<TField>,
  13. options: T[],
  14. name: Path<TField>, // register name
  15. label?: string, // display label
  16. noOptionsText?: string,
  17. isMultiple?: boolean,
  18. rules?: RegisterOptions<FieldValues>
  19. error?: boolean,
  20. }
  21. function ControlledAutoComplete<
  22. T extends { id?: number | string; label?: string; name?: string },
  23. TField extends FieldValues
  24. >(
  25. props: Props<T, TField>
  26. ) {
  27. const { t } = useTranslation()
  28. const { control, options, name, label, noOptionsText, isMultiple, rules, error } = props;
  29. return (
  30. <Controller
  31. name={name}
  32. control={control}
  33. rules={rules}
  34. render={({ field }) => (
  35. isMultiple ?
  36. <Autocomplete
  37. multiple
  38. disableClearable
  39. disableCloseOnSelect
  40. disablePortal
  41. noOptionsText={noOptionsText ?? t("No Options")}
  42. value={options.filter(option => {
  43. // console.log(field.value)
  44. return field.value?.includes(option.id)
  45. })}
  46. options={options}
  47. getOptionLabel={(option) => option.label ?? option.name!!}
  48. isOptionEqualToValue={(option, value) => option.id === value.id}
  49. renderOption={(params, option, { selected }) => {
  50. return (
  51. <li {...params}>
  52. <Checkbox
  53. icon={icon}
  54. checkedIcon={checkedIcon}
  55. checked={selected}
  56. style={{ marginRight: 8 }}
  57. />
  58. {option.label ?? option.name}
  59. </li>
  60. );
  61. }}
  62. onChange={(event, value) => {
  63. field.onChange(value?.map(v => v.id))
  64. }}
  65. renderInput={(params) => <TextField {...params} error={error} variant="outlined" label={label} />}
  66. />
  67. :
  68. <Autocomplete
  69. disableClearable
  70. disablePortal
  71. noOptionsText={noOptionsText ?? t("No Options")}
  72. value={options.find(option => option.id === field.value) ?? options[0]}
  73. options={options}
  74. getOptionLabel={(option) => option.label ?? option.name!!}
  75. isOptionEqualToValue={(option, value) => option.id === value.id}
  76. renderOption={(params, option) => {
  77. return (
  78. <MenuItem {...params} key={option.id} value={option.id}>
  79. {option.label ?? option.name}
  80. </MenuItem>
  81. );
  82. }}
  83. onChange={(event, value) => {
  84. field.onChange(value?.id)
  85. }}
  86. renderInput={(params) => <TextField {...params} error={error} variant="outlined" label={label} />}
  87. />
  88. )}
  89. />
  90. )
  91. }
  92. export default ControlledAutoComplete;