2024-08-29 22:51:46 +00:00
|
|
|
import React, { ReactNode } from "react";
|
2021-04-12 13:32:25 +00:00
|
|
|
import classnames from "classnames";
|
2023-02-21 14:16:38 +00:00
|
|
|
import { DEFAULT_EMPTY_CELL_VALUE } from "utilities/constants";
|
2024-06-17 17:47:24 +00:00
|
|
|
import TooltipWrapper from "components/TooltipWrapper";
|
2024-06-26 13:17:43 +00:00
|
|
|
import { capitalize } from "lodash";
|
2021-02-11 16:22:22 +00:00
|
|
|
|
2024-08-29 22:51:46 +00:00
|
|
|
const baseClass = "status-indicator";
|
|
|
|
|
|
|
|
|
|
export type IIndicatorValue = "success" | "warning" | "error" | "indeterminate";
|
|
|
|
|
|
2022-12-13 18:04:07 +00:00
|
|
|
interface IStatusIndicatorProps {
|
2024-08-29 22:51:46 +00:00
|
|
|
/** Only the first letter of value will be capitalized by the component.
|
|
|
|
|
* NOTE: Do not rely on the value prop to determine the status indicator. Use the
|
|
|
|
|
* `indicator` prop instead.
|
|
|
|
|
*/
|
2021-03-03 16:51:39 +00:00
|
|
|
value: string;
|
2024-08-29 22:51:46 +00:00
|
|
|
/** The indicator type allows for showing the desired indicator.
|
|
|
|
|
* NOTE: use this instead relying on the `value` prop to determine the indicator.
|
|
|
|
|
*/
|
|
|
|
|
indicator?: IIndicatorValue;
|
2022-11-22 22:15:17 +00:00
|
|
|
tooltip?: {
|
2024-08-29 22:51:46 +00:00
|
|
|
tooltipText: ReactNode;
|
2023-02-22 16:13:12 +00:00
|
|
|
position?: "top" | "bottom";
|
2022-11-22 22:15:17 +00:00
|
|
|
};
|
2024-08-29 22:51:46 +00:00
|
|
|
/**
|
|
|
|
|
* @deprecated Use `indicator` instead to show the desired indicator.
|
|
|
|
|
*/
|
2023-07-17 21:09:12 +00:00
|
|
|
customIndicatorType?: string;
|
2024-08-29 22:51:46 +00:00
|
|
|
className?: string;
|
2021-03-03 16:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-17 21:09:12 +00:00
|
|
|
const generateIndicatorStateClassTag = (
|
|
|
|
|
rawValue: string,
|
|
|
|
|
customIndicatorType?: string
|
|
|
|
|
): string => {
|
2023-02-21 14:16:38 +00:00
|
|
|
if (rawValue === DEFAULT_EMPTY_CELL_VALUE) {
|
2022-04-07 19:12:38 +00:00
|
|
|
return "indeterminate";
|
|
|
|
|
}
|
2023-07-17 21:09:12 +00:00
|
|
|
const prefix = customIndicatorType ? `${customIndicatorType}-` : "";
|
|
|
|
|
return `${prefix}${rawValue.replace(" ", "-").toLowerCase()}`;
|
2021-04-14 09:20:56 +00:00
|
|
|
};
|
|
|
|
|
|
2022-12-13 18:04:07 +00:00
|
|
|
const StatusIndicator = ({
|
|
|
|
|
value,
|
2024-08-29 22:51:46 +00:00
|
|
|
indicator,
|
2022-12-13 18:04:07 +00:00
|
|
|
tooltip,
|
2023-07-17 21:09:12 +00:00
|
|
|
customIndicatorType,
|
2024-08-29 22:51:46 +00:00
|
|
|
className,
|
2022-12-13 18:04:07 +00:00
|
|
|
}: IStatusIndicatorProps): JSX.Element => {
|
2023-07-17 21:09:12 +00:00
|
|
|
const indicatorStateClassTag = generateIndicatorStateClassTag(
|
|
|
|
|
value,
|
|
|
|
|
customIndicatorType
|
2021-02-11 16:22:22 +00:00
|
|
|
);
|
2024-08-29 22:51:46 +00:00
|
|
|
|
|
|
|
|
const classes = classnames(
|
|
|
|
|
baseClass,
|
|
|
|
|
className,
|
|
|
|
|
`${baseClass}--${indicatorStateClassTag}`,
|
|
|
|
|
`status--${indicatorStateClassTag}`,
|
|
|
|
|
indicator ? `${baseClass}--${indicator}` : null
|
2022-11-22 22:15:17 +00:00
|
|
|
);
|
2024-06-26 13:17:43 +00:00
|
|
|
|
|
|
|
|
const capitalizedValue = capitalize(value);
|
|
|
|
|
|
2024-06-17 17:47:24 +00:00
|
|
|
const indicatorContent = tooltip ? (
|
|
|
|
|
<TooltipWrapper
|
|
|
|
|
position={tooltip?.position ? tooltip.position : "top"}
|
|
|
|
|
showArrow
|
|
|
|
|
underline={false}
|
|
|
|
|
tipContent={tooltip.tooltipText}
|
|
|
|
|
tipOffset={14}
|
|
|
|
|
>
|
2024-06-26 13:17:43 +00:00
|
|
|
{capitalizedValue}
|
2024-06-17 17:47:24 +00:00
|
|
|
</TooltipWrapper>
|
|
|
|
|
) : (
|
2024-06-26 13:17:43 +00:00
|
|
|
capitalizedValue
|
2024-06-17 17:47:24 +00:00
|
|
|
);
|
2024-06-26 13:17:43 +00:00
|
|
|
|
2024-08-29 22:51:46 +00:00
|
|
|
return <span className={classes}>{indicatorContent}</span>;
|
2021-02-11 16:22:22 +00:00
|
|
|
};
|
|
|
|
|
|
2022-12-13 18:04:07 +00:00
|
|
|
export default StatusIndicator;
|