fix-1847 truncate fix for selection component.
This commit is contained in:
1 parent
314968d1a1
commit
3226035589
2 files changed
+70
No files matched your search
@@ -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 (
|
||||
<Stack
|
||||
gap={theme.spacing(labelControlSpacing)}
|
||||
@@ -75,6 +107,7 @@ const Select = ({
|
||||
</Typography>
|
||||
)}
|
||||
<MuiSelect
|
||||
ref={selectRef}
|
||||
className="select-component"
|
||||
value={value}
|
||||
onChange={onChange}
|
||||
@@ -99,6 +132,9 @@ const Select = ({
|
||||
},
|
||||
...sx,
|
||||
}}
|
||||
renderValue={() =>
|
||||
renderTruncatedValue(selectedItemName, truncate, maxLength, placeholder)
|
||||
}
|
||||
>
|
||||
{placeholder && (
|
||||
<MenuItem
|
||||
@@ -146,6 +182,8 @@ Select.propTypes = {
|
||||
onBlur: PropTypes.func,
|
||||
sx: PropTypes.object,
|
||||
labelControlSpacing: PropTypes.number,
|
||||
truncate: PropTypes.bool,
|
||||
maxStringLength: PropTypes.number,
|
||||
};
|
||||
|
||||
export default Select;
|
||||
@@ -0,0 +1,32 @@
|
||||
// src/utils/truncateUtils.js
|
||||
import { isEmpty } from "./stringUtils"; // assuming you have the stringUtils file
|
||||
|
||||
/**
|
||||
* 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
|
||||
) {
|
||||
console.log(maxLength);
|
||||
if (isEmpty(displayName)) return placeholder;
|
||||
|
||||
return truncate ? (
|
||||
<span title={displayName}>{truncateTextWithDottedEnds(displayName, maxLength)}</span>
|
||||
) : (
|
||||
<span title={displayName}>{displayName}</span>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user