Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

121 wiersze
2.7 KiB

  1. import React, { useCallback } from "react";
  2. import { MenuItem, MenuProps, 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) => Promise<void> | void;
  15. error?: boolean;
  16. }
  17. const menuProps: Partial<MenuProps> = {
  18. slotProps: {
  19. paper: {
  20. sx: { maxHeight: 400 },
  21. },
  22. },
  23. anchorOrigin: {
  24. vertical: "bottom",
  25. horizontal: "left",
  26. },
  27. transformOrigin: {
  28. vertical: "top",
  29. horizontal: "left",
  30. },
  31. };
  32. const TaskGroupSelect: React.FC<Props> = ({
  33. value,
  34. projectId,
  35. onTaskGroupSelect,
  36. taskGroupsByProject,
  37. error,
  38. }) => {
  39. const { t } = useTranslation("home");
  40. const taskGroups = projectId ? taskGroupsByProject[projectId] : [];
  41. const onChange = useCallback(
  42. (event: SelectChangeEvent<number>) => {
  43. const newValue = event.target.value;
  44. onTaskGroupSelect(newValue);
  45. },
  46. [onTaskGroupSelect],
  47. );
  48. return (
  49. <Select
  50. error={error}
  51. displayEmpty
  52. disabled={taskGroups.length === 0}
  53. value={value || ""}
  54. onChange={onChange}
  55. sx={{ width: "100%" }}
  56. MenuProps={menuProps}
  57. >
  58. {taskGroups.length === 0 && <MenuItem value={""}>{t("None")}</MenuItem>}
  59. {taskGroups.map((taskGroup) => (
  60. <MenuItem
  61. key={taskGroup.value}
  62. value={taskGroup.value}
  63. sx={{ whiteSpace: "wrap" }}
  64. >
  65. {taskGroup.label}
  66. </MenuItem>
  67. ))}
  68. </Select>
  69. );
  70. };
  71. type TaskGroupSelectWithoutProjectProps = Pick<
  72. Props,
  73. "value" | "onTaskGroupSelect" | "error"
  74. > & { taskGroups: TaskGroup[] };
  75. export const TaskGroupSelectWithoutProject: React.FC<
  76. TaskGroupSelectWithoutProjectProps
  77. > = ({ value, onTaskGroupSelect, error, taskGroups }) => {
  78. const { t } = useTranslation("home");
  79. const onChange = useCallback(
  80. (event: SelectChangeEvent<number>) => {
  81. const newValue = event.target.value;
  82. onTaskGroupSelect(newValue);
  83. },
  84. [onTaskGroupSelect],
  85. );
  86. return (
  87. <Select
  88. error={error}
  89. displayEmpty
  90. disabled={taskGroups.length === 0}
  91. value={value || ""}
  92. onChange={onChange}
  93. sx={{ width: "100%" }}
  94. MenuProps={menuProps}
  95. >
  96. {<MenuItem value={""}>{t("None")}</MenuItem>}
  97. {taskGroups.map((taskGroup) => (
  98. <MenuItem
  99. key={taskGroup.id}
  100. value={taskGroup.id}
  101. sx={{ whiteSpace: "wrap" }}
  102. >
  103. {taskGroup.name}
  104. </MenuItem>
  105. ))}
  106. </Select>
  107. );
  108. };
  109. export default TaskGroupSelect;