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.

77 lines
1.7 KiB

  1. import React, { useCallback } from "react";
  2. import { MenuItem, Select, SelectChangeEvent } from "@mui/material";
  3. import { useTranslation } from "react-i18next";
  4. import { TaskGroup } from "@/app/api/tasks";
  5. interface Props {
  6. taskGroupsByProject: {
  7. [projectId: number]: {
  8. value: TaskGroup["id"];
  9. label: string;
  10. }[];
  11. };
  12. projectId: number | undefined;
  13. value: number | undefined;
  14. onTaskGroupSelect: (taskGroupId: number | string) => void;
  15. error?: boolean;
  16. }
  17. const TaskGroupSelect: React.FC<Props> = ({
  18. value,
  19. projectId,
  20. onTaskGroupSelect,
  21. taskGroupsByProject,
  22. error,
  23. }) => {
  24. const { t } = useTranslation("home");
  25. const taskGroups = projectId ? taskGroupsByProject[projectId] : [];
  26. const onChange = useCallback(
  27. (event: SelectChangeEvent<number>) => {
  28. const newValue = event.target.value;
  29. onTaskGroupSelect(newValue);
  30. },
  31. [onTaskGroupSelect],
  32. );
  33. return (
  34. <Select
  35. error={error}
  36. displayEmpty
  37. disabled={taskGroups.length === 0}
  38. value={value || ""}
  39. onChange={onChange}
  40. sx={{ width: "100%" }}
  41. MenuProps={{
  42. slotProps: {
  43. paper: {
  44. sx: { maxHeight: 400 },
  45. },
  46. },
  47. anchorOrigin: {
  48. vertical: "bottom",
  49. horizontal: "left",
  50. },
  51. transformOrigin: {
  52. vertical: "top",
  53. horizontal: "left",
  54. },
  55. }}
  56. >
  57. {taskGroups.length === 0 && <MenuItem value={""}>{t("None")}</MenuItem>}
  58. {taskGroups.map((taskGroup) => (
  59. <MenuItem
  60. key={taskGroup.value}
  61. value={taskGroup.value}
  62. sx={{ whiteSpace: "wrap" }}
  63. >
  64. {taskGroup.label}
  65. </MenuItem>
  66. ))}
  67. </Select>
  68. );
  69. };
  70. export default TaskGroupSelect;