2021-07-26 17:07:27 +00:00
|
|
|
import React, { useCallback } from "react";
|
2023-05-11 19:25:56 +00:00
|
|
|
import { kebabCase, noop } from "lodash";
|
2025-04-28 23:32:41 +00:00
|
|
|
import classnames from "classnames";
|
2021-07-26 17:07:27 +00:00
|
|
|
|
2021-09-10 19:06:37 +00:00
|
|
|
import { ButtonVariant } from "components/buttons/Button/Button";
|
2023-05-30 21:16:47 +00:00
|
|
|
import Icon from "components/Icon/Icon";
|
|
|
|
|
import { IconNames } from "components/icons";
|
2025-04-28 23:32:41 +00:00
|
|
|
import TooltipWrapper from "components/TooltipWrapper";
|
|
|
|
|
|
2026-01-29 14:24:28 +00:00
|
|
|
import { Colors } from "styles/var/colors";
|
|
|
|
|
|
2021-07-26 17:07:27 +00:00
|
|
|
import Button from "../../../buttons/Button";
|
|
|
|
|
|
2021-08-03 20:09:01 +00:00
|
|
|
const baseClass = "action-button";
|
2021-07-26 17:07:27 +00:00
|
|
|
export interface IActionButtonProps {
|
|
|
|
|
name: string;
|
2023-05-11 19:25:56 +00:00
|
|
|
buttonText: string | ((targetIds: number[]) => string);
|
2025-04-28 23:32:41 +00:00
|
|
|
onClick?: (ids: number[]) => void;
|
2021-07-26 17:07:27 +00:00
|
|
|
targetIds?: number[]; // TODO figure out undefined case
|
2021-09-10 19:06:37 +00:00
|
|
|
variant?: ButtonVariant;
|
2021-07-26 17:07:27 +00:00
|
|
|
hideButton?: boolean | ((targetIds: number[]) => boolean);
|
2023-05-30 21:16:47 +00:00
|
|
|
iconSvg?: IconNames;
|
2025-04-28 23:32:41 +00:00
|
|
|
iconStroke?: boolean;
|
2026-01-29 14:24:28 +00:00
|
|
|
iconColor?: Colors;
|
2021-08-03 20:09:01 +00:00
|
|
|
iconPosition?: string;
|
2025-04-28 23:32:41 +00:00
|
|
|
isDisabled?: boolean;
|
|
|
|
|
tooltipContent?: React.ReactNode;
|
2021-07-26 17:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function useActionCallback(
|
|
|
|
|
callbackFn: (targetIds: number[]) => void | undefined
|
|
|
|
|
) {
|
|
|
|
|
return useCallback(
|
2024-03-13 19:09:16 +00:00
|
|
|
(targetIds: any) => {
|
2021-07-26 17:07:27 +00:00
|
|
|
callbackFn(targetIds);
|
|
|
|
|
},
|
|
|
|
|
[callbackFn]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 20:09:01 +00:00
|
|
|
const ActionButton = (buttonProps: IActionButtonProps): JSX.Element | null => {
|
2021-07-26 17:07:27 +00:00
|
|
|
const {
|
|
|
|
|
name,
|
|
|
|
|
buttonText,
|
2025-04-28 23:32:41 +00:00
|
|
|
onClick,
|
2021-07-26 17:07:27 +00:00
|
|
|
targetIds = [],
|
2025-04-16 13:56:09 +00:00
|
|
|
variant = "default",
|
2021-07-26 17:07:27 +00:00
|
|
|
hideButton,
|
2023-05-30 21:16:47 +00:00
|
|
|
iconSvg,
|
2025-04-28 23:32:41 +00:00
|
|
|
iconStroke = false,
|
2026-01-29 14:24:28 +00:00
|
|
|
iconColor,
|
2021-08-03 20:09:01 +00:00
|
|
|
iconPosition,
|
2025-04-28 23:32:41 +00:00
|
|
|
isDisabled,
|
|
|
|
|
tooltipContent,
|
2021-08-03 20:09:01 +00:00
|
|
|
} = buttonProps;
|
2025-04-28 23:32:41 +00:00
|
|
|
const onButtonClick = useActionCallback(onClick || noop);
|
2021-07-26 17:07:27 +00:00
|
|
|
|
|
|
|
|
// hideButton is intended to provide a flexible way to specify show/hide conditions via a boolean or a function that evaluates to a boolean
|
|
|
|
|
// currently it is typed to accept an array of targetIds but this typing could easily be expanded to include other use cases
|
2021-08-03 20:09:01 +00:00
|
|
|
const isHidden = (
|
|
|
|
|
hideButtonProp: boolean | ((ids: number[]) => boolean) | undefined
|
2021-07-26 17:07:27 +00:00
|
|
|
) => {
|
|
|
|
|
if (typeof hideButtonProp === "function") {
|
2021-08-03 20:09:01 +00:00
|
|
|
return hideButtonProp(targetIds);
|
2021-07-26 17:07:27 +00:00
|
|
|
}
|
2021-08-03 20:09:01 +00:00
|
|
|
return Boolean(hideButtonProp);
|
2021-07-26 17:07:27 +00:00
|
|
|
};
|
|
|
|
|
|
2023-04-27 15:53:30 +00:00
|
|
|
if (isHidden(hideButton)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-05-30 21:16:47 +00:00
|
|
|
|
2025-04-28 23:32:41 +00:00
|
|
|
const buttonClasses = classnames(
|
|
|
|
|
baseClass,
|
|
|
|
|
`${baseClass}__${kebabCase(name)}`,
|
|
|
|
|
{ [`${baseClass}__disabled`]: isDisabled }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const renderButton = () => (
|
|
|
|
|
<div className={buttonClasses}>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => onButtonClick(targetIds)}
|
|
|
|
|
variant={variant}
|
|
|
|
|
iconStroke={iconStroke}
|
2025-09-29 17:10:41 +00:00
|
|
|
size="small"
|
2025-04-28 23:32:41 +00:00
|
|
|
>
|
2021-08-03 20:09:01 +00:00
|
|
|
<>
|
2026-01-29 14:24:28 +00:00
|
|
|
{iconPosition === "left" && iconSvg && (
|
|
|
|
|
<Icon name={iconSvg} color={iconColor} />
|
|
|
|
|
)}
|
2021-08-03 20:09:01 +00:00
|
|
|
{buttonText}
|
2026-01-29 14:24:28 +00:00
|
|
|
{iconPosition !== "left" && iconSvg && (
|
|
|
|
|
<Icon name={iconSvg} color={iconColor} />
|
|
|
|
|
)}
|
2021-08-03 20:09:01 +00:00
|
|
|
</>
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2021-12-29 16:46:47 +00:00
|
|
|
);
|
2025-04-28 23:32:41 +00:00
|
|
|
|
|
|
|
|
if (tooltipContent) {
|
|
|
|
|
return (
|
|
|
|
|
<div className={baseClass}>
|
|
|
|
|
<TooltipWrapper
|
|
|
|
|
tipContent={tooltipContent}
|
|
|
|
|
position="top"
|
|
|
|
|
fixedPositionStrategy
|
|
|
|
|
underline={false}
|
|
|
|
|
clickable={false}
|
|
|
|
|
showArrow
|
|
|
|
|
>
|
|
|
|
|
{renderButton()}
|
|
|
|
|
</TooltipWrapper>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return renderButton();
|
2021-07-26 17:07:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ActionButton;
|