FIX-1847 implemented the logic.

This commit is contained in:
Owaise Imdad committed 2025-03-02 12:51:36 +05:30
1 parent f7a172791e
commit 039d471af7
3 files changed
+72 -19

No files matched your search

+14 -1
View File
@@ -2,7 +2,7 @@ import PropTypes from "prop-types";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { MenuItem, Select as MuiSelect, Stack, Typography } from "@mui/material"; 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 { renderTruncatedValue } from "../../../Utils/Input/utils";
import "./index.css"; import "./index.css";
/** /**
@@ -50,8 +50,14 @@ const Select = ({
sx, sx,
name = "", name = "",
labelControlSpacing = 2, labelControlSpacing = 2,
truncate = false,
maxTruncateLength = 15,
}) => { }) => {
const theme = useTheme(); const theme = useTheme();
const selectedItem = items.find((item) => item._id === value);
const displayName = selectedItem ? selectedItem.name : "";
const itemStyles = { const itemStyles = {
fontSize: "var(--env-var-font-size-medium)", fontSize: "var(--env-var-font-size-medium)",
color: theme.palette.primary.contrastTextTertiary, color: theme.palette.primary.contrastTextTertiary,
@@ -59,6 +65,10 @@ const Select = ({
margin: theme.spacing(2), margin: theme.spacing(2),
}; };
const renderShortValue = () => {
return renderTruncatedValue(displayName, truncate, maxTruncateLength, placeholder);
};
return ( return (
<Stack <Stack
gap={theme.spacing(labelControlSpacing)} gap={theme.spacing(labelControlSpacing)}
@@ -99,6 +109,7 @@ const Select = ({
}, },
...sx, ...sx,
}} }}
renderValue={renderShortValue}
> >
{placeholder && ( {placeholder && (
<MenuItem <MenuItem
@@ -146,6 +157,8 @@ Select.propTypes = {
onBlur: PropTypes.func, onBlur: PropTypes.func,
sx: PropTypes.object, sx: PropTypes.object,
labelControlSpacing: PropTypes.number, labelControlSpacing: PropTypes.number,
truncate: PropTypes.bool, // New prop: enable/disable truncation
maxTruncateLength: PropTypes.number, // New prop: configurable length
}; };
export default Select; export default Select;
@@ -20,6 +20,14 @@ const OptionsHeader = ({
const theme = useTheme(); const theme = useTheme();
const monitorNames = typeof monitors !== "undefined" ? Object.values(monitors) : []; const monitorNames = typeof monitors !== "undefined" ? Object.values(monitors) : [];
// The stacks below which are three in number have the same style so
const stackStyles = {
direction: "row",
alignItems: "center",
gap: theme.spacing(6),
marginLeft: "5px",
};
if (!shouldRender) return <SkeletonLayout />; if (!shouldRender) return <SkeletonLayout />;
return ( return (
@@ -27,17 +35,13 @@ const OptionsHeader = ({
direction="row" direction="row"
justifyContent="space-between" justifyContent="space-between"
> >
<Stack <Stack {...stackStyles}>
direction="row"
alignItems="center"
gap={theme.spacing(6)}
>
<Typography <Typography
display="inline-block" display="inline-block"
component="h1" component="h1"
color={theme.palette.primary.contrastTextSecondary} color={theme.palette.primary.contrastTextSecondary}
> >
Incidents for Incidents for:
</Typography> </Typography>
<Select <Select
id="incidents-select-monitor" id="incidents-select-monitor"
@@ -49,13 +53,10 @@ const OptionsHeader = ({
backgroundColor: theme.palette.primary.main, backgroundColor: theme.palette.primary.main,
color: theme.palette.primary.contrastTextSecondary, color: theme.palette.primary.contrastTextSecondary,
}} }}
truncate={true}
/> />
</Stack> </Stack>
<Stack <Stack {...stackStyles}>
direction="row"
alignItems="center"
gap={theme.spacing(6)}
>
<Typography <Typography
display="inline-block" display="inline-block"
component="h1" component="h1"
@@ -66,7 +67,15 @@ const OptionsHeader = ({
<ButtonGroup <ButtonGroup
sx={{ sx={{
ml: "auto", ml: "auto",
"& .MuiButtonBase-root, & .MuiButtonBase-root:hover": { alignItems: "stretch",
"& .MuiButtonBase-root": {
whiteSpace: "nowrap",
height: "auto",
"&.MuiButton-group": {
borderColor: theme.palette.primary.lowContrast,
},
},
"& .MuiButtonBase-root:hover": {
borderColor: theme.palette.primary.lowContrast, borderColor: theme.palette.primary.lowContrast,
}, },
}} }}
@@ -94,11 +103,7 @@ const OptionsHeader = ({
</Button> </Button>
</ButtonGroup> </ButtonGroup>
</Stack> </Stack>
<Stack <Stack {...stackStyles}>
direction="row"
alignItems="center"
gap={theme.spacing(6)}
>
<Typography <Typography
display="inline-block" display="inline-block"
component="h1" component="h1"
@@ -109,7 +114,15 @@ const OptionsHeader = ({
<ButtonGroup <ButtonGroup
sx={{ sx={{
ml: "auto", ml: "auto",
"& .MuiButtonBase-root, & .MuiButtonBase-root:hover": { alignItems: "stretch",
"& .MuiButtonBase-root": {
whiteSpace: "nowrap",
height: "auto",
"&.MuiButton-group": {
borderColor: theme.palette.primary.lowContrast,
},
},
"& .MuiButtonBase-root:hover": {
borderColor: theme.palette.primary.lowContrast, borderColor: theme.palette.primary.lowContrast,
}, },
}} }}
+27
View File
@@ -0,0 +1,27 @@
/**
* Utility function to render a truncated value with an optional title.
* @param {string} value - The selected value (But this will always come as id).
* @param {array} items - The list of items to find the selected value from.
* @param {boolean} truncate - Whether to truncate the text or not.
* @param {number} maxLength - The maximum length of the text.
* @param {string} placeholder - Placeholder text if no value is selected.
* @returns {JSX.Element} - The JSX element for the rendered value.
*/
export const renderTruncatedValue = (displayName, truncate, maxLength, placeholder) => {
// If no value or the value is '0', return the placeholder
if (!displayName || displayName === "0") return placeholder;
// Function to truncate the text with a dotted end
const truncateTextWithDottedEnds = (text, maxLength) => {
if (!text || text.length <= maxLength) return text;
return `${text.substring(0, maxLength)}...`;
};
// If truncation is enabled, return the truncated text with a title for full value
return truncate ? (
<span title={displayName}>{truncateTextWithDottedEnds(displayName, maxLength)}</span>
) : (
displayName
);
};