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 KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
|
||||||
|
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
import { renderTruncatedValue } from "../../../Utils/truncateUtils.jsx";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @component
|
* @component
|
||||||
@@ -50,7 +52,12 @@ const Select = ({
|
|||||||
sx,
|
sx,
|
||||||
name = "",
|
name = "",
|
||||||
labelControlSpacing = 2,
|
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 theme = useTheme();
|
||||||
const itemStyles = {
|
const itemStyles = {
|
||||||
fontSize: "var(--env-var-font-size-medium)",
|
fontSize: "var(--env-var-font-size-medium)",
|
||||||
@@ -59,6 +66,31 @@ const Select = ({
|
|||||||
margin: theme.spacing(2),
|
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 (
|
return (
|
||||||
<Stack
|
<Stack
|
||||||
gap={theme.spacing(labelControlSpacing)}
|
gap={theme.spacing(labelControlSpacing)}
|
||||||
@@ -75,6 +107,7 @@ const Select = ({
|
|||||||
</Typography>
|
</Typography>
|
||||||
)}
|
)}
|
||||||
<MuiSelect
|
<MuiSelect
|
||||||
|
ref={selectRef}
|
||||||
className="select-component"
|
className="select-component"
|
||||||
value={value}
|
value={value}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
@@ -99,6 +132,9 @@ const Select = ({
|
|||||||
},
|
},
|
||||||
...sx,
|
...sx,
|
||||||
}}
|
}}
|
||||||
|
renderValue={() =>
|
||||||
|
renderTruncatedValue(selectedItemName, truncate, maxLength, placeholder)
|
||||||
|
}
|
||||||
>
|
>
|
||||||
{placeholder && (
|
{placeholder && (
|
||||||
<MenuItem
|
<MenuItem
|
||||||
@@ -146,6 +182,8 @@ Select.propTypes = {
|
|||||||
onBlur: PropTypes.func,
|
onBlur: PropTypes.func,
|
||||||
sx: PropTypes.object,
|
sx: PropTypes.object,
|
||||||
labelControlSpacing: PropTypes.number,
|
labelControlSpacing: PropTypes.number,
|
||||||
|
truncate: PropTypes.bool,
|
||||||
|
maxStringLength: PropTypes.number,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Select;
|
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