diff --git a/src/Components/Inputs/Select/index.jsx b/src/Components/Inputs/Select/index.jsx index f0a1d11c0..f6accc415 100644 --- a/src/Components/Inputs/Select/index.jsx +++ b/src/Components/Inputs/Select/index.jsx @@ -4,6 +4,8 @@ import { MenuItem, Select as MuiSelect, Stack, Typography } from "@mui/material" import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown"; import "./index.css"; +import { useEffect, useRef, useState } from "react"; +import { renderTruncatedValue } from "../../../Utils/truncateUtils.jsx"; /** * @component @@ -50,7 +52,12 @@ const Select = ({ sx, name = "", labelControlSpacing = 2, + truncate = false, + maxStringLength = 10, }) => { + const [maxLength, setMaxLength] = useState(0); // Initially set maxLength to 0 + const selectRef = useRef(null); // Reference for the Select container + const theme = useTheme(); const itemStyles = { fontSize: "var(--env-var-font-size-medium)", @@ -59,6 +66,31 @@ const Select = ({ margin: theme.spacing(2), }; + // Dynamically adjust maxLength based on window width + useEffect(() => { + if (!truncate) return; // If truncate is false, do not run this effect + + const calculateMaxLength = () => { + console.log(selectRef); + if (selectRef.current) { + const width = selectRef.current.offsetWidth; + const calculatedMaxLength = Math.floor(width / maxStringLength); + setMaxLength(calculatedMaxLength); + } + }; + calculateMaxLength(); + window.addEventListener("resize", calculateMaxLength); + + return () => { + window.removeEventListener("resize", calculateMaxLength); // Cleanup on unmount + }; + }, [maxStringLength, truncate]); + + // Get the selected item's name + const selectedItemName = truncate + ? items.find((item) => item._id === value)?.name // Only calculate if truncate is true + : null; + return ( )} + renderTruncatedValue(selectedItemName, truncate, maxLength, placeholder) + } > {placeholder && ( {truncateTextWithDottedEnds(displayName, maxLength)} + ) : ( + {displayName} + ); +};