2022-03-13 22:09:39 +00:00
|
|
|
import React, { FC } from "react";
|
2022-12-22 11:41:43 +00:00
|
|
|
import { IconType } from "react-icons/lib";
|
2021-09-29 21:33:18 +00:00
|
|
|
|
2023-01-26 22:51:03 +00:00
|
|
|
import { Button, ButtonBaseProps } from "../button";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
|
|
|
Dropdown,
|
2022-09-30 10:29:22 +00:00
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuItem,
|
2023-01-15 11:40:40 +00:00
|
|
|
DropdownItem,
|
2022-09-30 10:29:22 +00:00
|
|
|
DropdownMenuPortal,
|
2022-11-23 02:55:25 +00:00
|
|
|
DropdownMenuTrigger,
|
2023-01-26 22:51:03 +00:00
|
|
|
} from "../form/dropdown";
|
|
|
|
import { FiChevronDown, FiMoreHorizontal } from "../icon";
|
2022-03-16 19:55:18 +00:00
|
|
|
|
2021-10-12 13:11:33 +00:00
|
|
|
export type ActionType = {
|
2021-09-29 21:33:18 +00:00
|
|
|
id: string;
|
2022-12-22 11:41:43 +00:00
|
|
|
icon?: IconType;
|
2022-05-27 23:27:41 +00:00
|
|
|
iconClassName?: string;
|
2021-09-29 21:33:18 +00:00
|
|
|
label: string;
|
|
|
|
disabled?: boolean;
|
2022-12-22 11:41:43 +00:00
|
|
|
color?: ButtonBaseProps["color"];
|
2022-05-18 21:05:49 +00:00
|
|
|
} & (
|
|
|
|
| { href: string; onClick?: never; actions?: never }
|
|
|
|
| { href?: never; onClick: (e: React.MouseEvent<HTMLElement, MouseEvent>) => void; actions?: never }
|
|
|
|
| { actions?: ActionType[]; href?: never; onClick?: never }
|
|
|
|
);
|
2021-09-29 21:33:18 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
actions: ActionType[];
|
|
|
|
}
|
|
|
|
|
2022-05-18 21:05:49 +00:00
|
|
|
const defaultAction = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
};
|
|
|
|
|
2022-12-22 11:41:43 +00:00
|
|
|
export const DropdownActions = ({
|
2022-05-18 21:05:49 +00:00
|
|
|
actions,
|
|
|
|
actionTrigger,
|
|
|
|
}: {
|
|
|
|
actions: ActionType[];
|
|
|
|
actionTrigger?: React.ReactNode;
|
|
|
|
}) => {
|
2022-04-14 21:25:24 +00:00
|
|
|
return (
|
|
|
|
<Dropdown>
|
|
|
|
{!actionTrigger ? (
|
2022-07-27 02:24:00 +00:00
|
|
|
<DropdownMenuTrigger asChild>
|
2023-01-23 23:08:01 +00:00
|
|
|
<Button type="button" color="secondary" variant="icon" StartIcon={FiMoreHorizontal} />
|
2022-04-14 21:25:24 +00:00
|
|
|
</DropdownMenuTrigger>
|
|
|
|
) : (
|
|
|
|
<DropdownMenuTrigger asChild>{actionTrigger}</DropdownMenuTrigger>
|
|
|
|
)}
|
2022-09-30 10:29:22 +00:00
|
|
|
<DropdownMenuPortal>
|
|
|
|
<DropdownMenuContent>
|
|
|
|
{actions.map((action) => (
|
2022-12-22 11:41:43 +00:00
|
|
|
<DropdownMenuItem key={action.id}>
|
2023-01-15 11:40:40 +00:00
|
|
|
<DropdownItem
|
2022-09-30 10:29:22 +00:00
|
|
|
type="button"
|
2023-01-15 11:40:40 +00:00
|
|
|
color={action.color}
|
|
|
|
data-testid={action.id}
|
2022-09-30 10:29:22 +00:00
|
|
|
StartIcon={action.icon}
|
2023-01-15 11:40:40 +00:00
|
|
|
href={action.href}
|
|
|
|
onClick={action.onClick || defaultAction}>
|
2022-09-30 10:29:22 +00:00
|
|
|
{action.label}
|
2023-01-15 11:40:40 +00:00
|
|
|
</DropdownItem>
|
2022-09-30 10:29:22 +00:00
|
|
|
</DropdownMenuItem>
|
|
|
|
))}
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</DropdownMenuPortal>
|
2022-04-14 21:25:24 +00:00
|
|
|
</Dropdown>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-12-22 11:41:43 +00:00
|
|
|
export const TableActions: FC<Props> = ({ actions }) => {
|
2021-09-29 21:33:18 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-09-27 23:51:41 +00:00
|
|
|
<div className="flex space-x-2 rtl:space-x-reverse">
|
2022-04-14 21:25:24 +00:00
|
|
|
{actions.map((action) => {
|
|
|
|
const button = (
|
|
|
|
<Button
|
2022-10-06 09:57:57 +00:00
|
|
|
className="whitespace-nowrap"
|
2022-04-14 21:25:24 +00:00
|
|
|
key={action.id}
|
|
|
|
data-testid={action.id}
|
|
|
|
href={action.href}
|
2022-05-18 21:05:49 +00:00
|
|
|
onClick={action.onClick || defaultAction}
|
2022-04-14 21:25:24 +00:00
|
|
|
StartIcon={action.icon}
|
2023-01-23 23:08:01 +00:00
|
|
|
{...(action?.actions ? { EndIcon: FiChevronDown } : null)}
|
2022-04-14 21:25:24 +00:00
|
|
|
disabled={action.disabled}
|
|
|
|
color={action.color || "secondary"}>
|
|
|
|
{action.label}
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
if (!action.actions) {
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
return <DropdownActions key={action.id} actions={action.actions} actionTrigger={button} />;
|
|
|
|
})}
|
2021-09-29 21:33:18 +00:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|