import { Box, Chip, MenuItem, Select, styled, Typography } from "@mui/material"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { PropsContentProps } from "./CustomPropsItem.tsx"; export const NoLabelFilledSelect = styled(Select, { shouldForwardProp: (prop) => prop !== "fullSize", })<{ fullSize?: boolean }>(({ theme, fullSize }) => ({ "& .MuiSelect-select": { paddingTop: theme.spacing(1), paddingBottom: theme.spacing(1), ...(!fullSize ? { paddingLeft: 0, paddingRight: 0, paddingTop: theme.spacing(0.5), paddingBottom: theme.spacing(0.5), } : {}), fontSize: theme.typography.body2.fontSize, "&.Mui-disabled": { borderBottomStyle: "none", "&::before": { borderBottomStyle: "none !important", }, }, }, "&.MuiInputBase-root.MuiFilledInput-root.MuiSelect-root": { ...(!fullSize ? { borderRadius: 0, backgroundColor: "initial", } : {}), "&.Mui-disabled": { borderBottomStyle: "none", "&::before": { borderBottomStyle: "none !important", }, }, }, })); const MultiSelectPropsContent = ({ prop, onChange, loading, readOnly, fullSize }: PropsContentProps) => { const { t } = useTranslation(); const [values, setValues] = useState(() => { if (prop.value) { try { return JSON.parse(prop.value); } catch { return []; } } return []; }); useEffect(() => { if (prop.value) { try { setValues(JSON.parse(prop.value)); } catch { setValues([]); } } else { setValues([]); } }, [prop.value]); const handleChange = (selectedValues: string[]) => { setValues(selectedValues); const newValue = JSON.stringify(selectedValues); if (newValue !== prop.value) { onChange(newValue); } }; const handleDelete = (valueToDelete: string) => { const newValues = values.filter((value) => value !== valueToDelete); handleChange(newValues); }; if (readOnly) { return ( {values.length > 0 ? values.map((value, index) => ) : ""} ); } const options = prop.props.options || []; return ( handleChange(e.target.value as string[])} onClick={(e) => e.stopPropagation()} multiple displayEmpty fullSize={fullSize} renderValue={(selected) => { const selectedArray = Array.isArray(selected) ? selected : []; if (selectedArray.length === 0) { return ( {t("application:fileManager.clickToEditSelect")} ); } return ( {selectedArray.map((value, index) => ( handleDelete(value)} onMouseDown={(e) => e.stopPropagation()} /> ))} ); }} > {options.map((option) => ( {option} ))} ); }; export default MultiSelectPropsContent;