FIX-1847 implemented the logic.
This commit is contained in:
1 parent
f7a172791e
commit
039d471af7
3 files changed
+72
-19
No files matched your search
@@ -2,7 +2,7 @@ import PropTypes from "prop-types";
|
||||
import { useTheme } from "@emotion/react";
|
||||
import { MenuItem, Select as MuiSelect, Stack, Typography } from "@mui/material";
|
||||
import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
|
||||
|
||||
import { renderTruncatedValue } from "../../../Utils/Input/utils";
|
||||
import "./index.css";
|
||||
|
||||
/**
|
||||
@@ -50,8 +50,14 @@ const Select = ({
|
||||
sx,
|
||||
name = "",
|
||||
labelControlSpacing = 2,
|
||||
truncate = false,
|
||||
maxTruncateLength = 15,
|
||||
}) => {
|
||||
const theme = useTheme();
|
||||
|
||||
const selectedItem = items.find((item) => item._id === value);
|
||||
const displayName = selectedItem ? selectedItem.name : "";
|
||||
|
||||
const itemStyles = {
|
||||
fontSize: "var(--env-var-font-size-medium)",
|
||||
color: theme.palette.primary.contrastTextTertiary,
|
||||
@@ -59,6 +65,10 @@ const Select = ({
|
||||
margin: theme.spacing(2),
|
||||
};
|
||||
|
||||
const renderShortValue = () => {
|
||||
return renderTruncatedValue(displayName, truncate, maxTruncateLength, placeholder);
|
||||
};
|
||||
|
||||
return (
|
||||
<Stack
|
||||
gap={theme.spacing(labelControlSpacing)}
|
||||
@@ -99,6 +109,7 @@ const Select = ({
|
||||
},
|
||||
...sx,
|
||||
}}
|
||||
renderValue={renderShortValue}
|
||||
>
|
||||
{placeholder && (
|
||||
<MenuItem
|
||||
@@ -146,6 +157,8 @@ Select.propTypes = {
|
||||
onBlur: PropTypes.func,
|
||||
sx: PropTypes.object,
|
||||
labelControlSpacing: PropTypes.number,
|
||||
truncate: PropTypes.bool, // New prop: enable/disable truncation
|
||||
maxTruncateLength: PropTypes.number, // New prop: configurable length
|
||||
};
|
||||
|
||||
export default Select;
|
||||
@@ -20,6 +20,14 @@ const OptionsHeader = ({
|
||||
const theme = useTheme();
|
||||
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 />;
|
||||
|
||||
return (
|
||||
@@ -27,17 +35,13 @@ const OptionsHeader = ({
|
||||
direction="row"
|
||||
justifyContent="space-between"
|
||||
>
|
||||
<Stack
|
||||
direction="row"
|
||||
alignItems="center"
|
||||
gap={theme.spacing(6)}
|
||||
>
|
||||
<Stack {...stackStyles}>
|
||||
<Typography
|
||||
display="inline-block"
|
||||
component="h1"
|
||||
color={theme.palette.primary.contrastTextSecondary}
|
||||
>
|
||||
Incidents for
|
||||
Incidents for:
|
||||
</Typography>
|
||||
<Select
|
||||
id="incidents-select-monitor"
|
||||
@@ -49,13 +53,10 @@ const OptionsHeader = ({
|
||||
backgroundColor: theme.palette.primary.main,
|
||||
color: theme.palette.primary.contrastTextSecondary,
|
||||
}}
|
||||
truncate={true}
|
||||
/>
|
||||
</Stack>
|
||||
<Stack
|
||||
direction="row"
|
||||
alignItems="center"
|
||||
gap={theme.spacing(6)}
|
||||
>
|
||||
<Stack {...stackStyles}>
|
||||
<Typography
|
||||
display="inline-block"
|
||||
component="h1"
|
||||
@@ -66,7 +67,15 @@ const OptionsHeader = ({
|
||||
<ButtonGroup
|
||||
sx={{
|
||||
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,
|
||||
},
|
||||
}}
|
||||
@@ -94,11 +103,7 @@ const OptionsHeader = ({
|
||||
</Button>
|
||||
</ButtonGroup>
|
||||
</Stack>
|
||||
<Stack
|
||||
direction="row"
|
||||
alignItems="center"
|
||||
gap={theme.spacing(6)}
|
||||
>
|
||||
<Stack {...stackStyles}>
|
||||
<Typography
|
||||
display="inline-block"
|
||||
component="h1"
|
||||
@@ -109,7 +114,15 @@ const OptionsHeader = ({
|
||||
<ButtonGroup
|
||||
sx={{
|
||||
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,
|
||||
},
|
||||
}}
|
||||
|
||||
@@ -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
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user