FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 5 месеци
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { ItemCombo } from "@/app/api/settings/item/actions";
  2. import { Autocomplete, TextField } from "@mui/material";
  3. import { useCallback, useMemo } from "react";
  4. import { useTranslation } from "react-i18next";
  5. interface CommonProps {
  6. allUom: ItemCombo[];
  7. error?: boolean;
  8. }
  9. interface SingleAutocompleteProps extends CommonProps {
  10. value: number | string | undefined;
  11. onUomSelect: (itemId: number) => void | Promise<void>;
  12. // multiple: false;
  13. }
  14. type Props = SingleAutocompleteProps;
  15. const UomSelect: React.FC<Props> = ({
  16. allUom,
  17. value,
  18. error,
  19. onUomSelect
  20. }) => {
  21. const { t } = useTranslation("item");
  22. const filteredUom = useMemo(() => {
  23. return allUom
  24. }, [allUom])
  25. const options = useMemo(() => {
  26. return [
  27. {
  28. value: -1, // think think sin
  29. label: t("None"),
  30. group: "default",
  31. },
  32. ...filteredUom.map((i) => ({
  33. value: i.id as number,
  34. label: i.label,
  35. group: "existing",
  36. })),
  37. ];
  38. }, [t, filteredUom]);
  39. const currentValue = options.find((o) => o.value === value) || options[0];
  40. const onChange = useCallback(
  41. (
  42. event: React.SyntheticEvent,
  43. newValue: { value: number; group: string } | { value: number }[],
  44. ) => {
  45. const singleNewVal = newValue as {
  46. value: number;
  47. group: string;
  48. };
  49. onUomSelect(singleNewVal.value)
  50. }
  51. , [onUomSelect])
  52. return (
  53. <Autocomplete
  54. noOptionsText={t("No Uom")}
  55. disableClearable
  56. fullWidth
  57. value={currentValue}
  58. onChange={onChange}
  59. getOptionLabel={(option) => option.label}
  60. options={options}
  61. renderInput={(params) => <TextField {...params} error={error} />}
  62. />
  63. );
  64. }
  65. export default UomSelect