From ec3704552fce8fe449cfa9b8971149a82c11ddd6 Mon Sep 17 00:00:00 2001 From: Owaise Imdad Date: Wed, 5 Mar 2025 21:03:03 +0530 Subject: [PATCH] Changed the logic to only depend on css styles and typography component. --- src/Components/Inputs/Select/index.jsx | 60 ++++++++----------- .../Components/OptionsHeader/index.jsx | 1 - src/Utils/truncateUtils.jsx | 31 ---------- 3 files changed, 24 insertions(+), 68 deletions(-) delete mode 100644 src/Utils/truncateUtils.jsx diff --git a/src/Components/Inputs/Select/index.jsx b/src/Components/Inputs/Select/index.jsx index 91d08cf18..0d183d4e3 100644 --- a/src/Components/Inputs/Select/index.jsx +++ b/src/Components/Inputs/Select/index.jsx @@ -4,8 +4,6 @@ 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 @@ -52,12 +50,8 @@ const Select = ({ sx, name = "", labelControlSpacing = 2, - truncate = false, - maxStringLength = 10, + maxWidthValue = 250, }) => { - const [maxLength, setMaxLength] = useState(0); - const selectRef = useRef(null); - const theme = useTheme(); const itemStyles = { fontSize: "var(--env-var-font-size-medium)", @@ -66,29 +60,6 @@ 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 = () => { - 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]); - - const selectedItemName = truncate - ? items.find((item) => item._id === value)?.name // Only calculate if truncate is true - : ""; - return ( )} - renderTruncatedValue(selectedItemName, truncate, maxLength, placeholder) - } + renderValue={(selected) => { + const selectedItem = items.find((item) => item._id === selected); + const displayName = selectedItem ? selectedItem.name : placeholder; + return ( + + {displayName} + + ); + }} > {placeholder && ( diff --git a/src/Utils/truncateUtils.jsx b/src/Utils/truncateUtils.jsx deleted file mode 100644 index 579b71622..000000000 --- a/src/Utils/truncateUtils.jsx +++ /dev/null @@ -1,31 +0,0 @@ -// src/utils/truncateUtils.js -import { isEmpty } from "./stringUtils"; - -/** - * Utility function to render a truncated value with an optional title. - * @param {string} displayName - The name or text to display. - * @param {number} maxLength - The maximum length of the text. - * @param {string} placeholder - Placeholder text if no value is provided. - * @returns {JSX.Element} - The JSX element for the rendered value. - * - */ - -const truncateTextWithDottedEnds = function (text, maxLength) { - if (!text || text.length <= maxLength) return text; - return `${text.substring(0, maxLength)}...`; -}; - -export const renderTruncatedValue = function ( - displayName, - truncate, - maxLength, - placeholder -) { - if (isEmpty(displayName)) return placeholder; - - return truncate ? ( - {truncateTextWithDottedEnds(displayName, maxLength)} - ) : ( - {displayName} - ); -};