FPSMS-frontend
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.
 
 
 

79 righe
1.8 KiB

  1. import React, { useCallback, useMemo } from "react";
  2. import {
  3. Autocomplete,
  4. Box,
  5. Checkbox,
  6. Chip,
  7. ListSubheader,
  8. MenuItem,
  9. TextField,
  10. Tooltip,
  11. } from "@mui/material";
  12. import { QcItemWithChecks } from "@/app/api/qc";
  13. import { useTranslation } from "react-i18next";
  14. interface CommonProps {
  15. allQcs: QcItemWithChecks[];
  16. error?: boolean;
  17. }
  18. interface SingleAutocompleteProps extends CommonProps {
  19. value: number | string | undefined;
  20. onQcSelect: (qcItemId: number) => void | Promise<void>;
  21. // multiple: false;
  22. }
  23. type Props = SingleAutocompleteProps;
  24. const QcSelect: React.FC<Props> = ({ allQcs, value, error, onQcSelect }) => {
  25. const { t } = useTranslation("home");
  26. const filteredQc = useMemo(() => {
  27. // do filtering here if any
  28. return allQcs;
  29. }, [allQcs]);
  30. const options = useMemo(() => {
  31. return [
  32. {
  33. value: -1, // think think sin
  34. label: t("None"),
  35. group: "default",
  36. },
  37. ...filteredQc.map((q) => ({
  38. value: q.id,
  39. label: `${q.code} - ${q.name}`,
  40. group: "existing",
  41. })),
  42. ];
  43. }, [t, filteredQc]);
  44. const currentValue = options.find((o) => o.value === value) || options[0];
  45. const onChange = useCallback(
  46. (
  47. event: React.SyntheticEvent,
  48. newValue: { value: number; group: string } | { value: number }[],
  49. ) => {
  50. const singleNewVal = newValue as {
  51. value: number;
  52. group: string;
  53. };
  54. onQcSelect(singleNewVal.value);
  55. },
  56. [onQcSelect],
  57. );
  58. return (
  59. <Autocomplete
  60. noOptionsText={t("No Qc")}
  61. disableClearable
  62. fullWidth
  63. value={currentValue}
  64. onChange={onChange}
  65. getOptionLabel={(option) => option.label}
  66. options={options}
  67. renderInput={(params) => <TextField {...params} error={error} />}
  68. />
  69. );
  70. };
  71. export default QcSelect;