2022-03-13 22:09:39 +00:00
|
|
|
import React, { FC } from "react";
|
2021-09-29 21:33:18 +00:00
|
|
|
|
2022-05-18 21:05:49 +00:00
|
|
|
import Dropdown, { DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@calcom/ui/Dropdown";
|
2022-07-27 02:24:00 +00:00
|
|
|
import { Icon } from "@calcom/ui/Icon";
|
2022-09-02 19:30:06 +00:00
|
|
|
import Button from "@calcom/ui/v2/core/Button";
|
2022-03-16 19:55:18 +00:00
|
|
|
|
2021-09-29 21:33:18 +00:00
|
|
|
import { SVGComponent } from "@lib/types/SVGComponent";
|
|
|
|
|
2021-10-12 13:11:33 +00:00
|
|
|
export type ActionType = {
|
2021-09-29 21:33:18 +00:00
|
|
|
id: string;
|
2022-04-14 21:25:24 +00:00
|
|
|
icon?: SVGComponent;
|
2022-05-27 23:27:41 +00:00
|
|
|
iconClassName?: string;
|
2021-09-29 21:33:18 +00:00
|
|
|
label: string;
|
|
|
|
disabled?: boolean;
|
2021-09-30 10:46:39 +00:00
|
|
|
color?: "primary" | "secondary";
|
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();
|
|
|
|
};
|
|
|
|
|
|
|
|
const DropdownActions = ({
|
|
|
|
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>
|
2022-09-09 15:02:31 +00:00
|
|
|
<Button type="button" color="secondary" size="icon" StartIcon={Icon.FiMoreHorizontal} />
|
2022-04-14 21:25:24 +00:00
|
|
|
</DropdownMenuTrigger>
|
|
|
|
) : (
|
|
|
|
<DropdownMenuTrigger asChild>{actionTrigger}</DropdownMenuTrigger>
|
|
|
|
)}
|
|
|
|
<DropdownMenuContent portalled>
|
|
|
|
{actions.map((action) => (
|
|
|
|
<DropdownMenuItem key={action.id} className="focus-visible:outline-none">
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
color="minimal"
|
|
|
|
className="w-full rounded-none font-normal"
|
|
|
|
href={action.href}
|
|
|
|
StartIcon={action.icon}
|
2022-05-18 21:05:49 +00:00
|
|
|
onClick={action.onClick || defaultAction}
|
2022-04-14 21:25:24 +00:00
|
|
|
data-testid={action.id}>
|
|
|
|
{action.label}
|
|
|
|
</Button>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
))}
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</Dropdown>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-09-29 21:33:18 +00:00
|
|
|
const TableActions: FC<Props> = ({ actions }) => {
|
2022-04-17 15:37:16 +00:00
|
|
|
const mobileActions = actions.flatMap((action) => {
|
|
|
|
if (action.actions) {
|
|
|
|
return action.actions;
|
|
|
|
}
|
|
|
|
return action;
|
|
|
|
});
|
2021-09-29 21:33:18 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-09-07 12:49:30 +00:00
|
|
|
<div className="hidden space-x-2 rtl:space-x-reverse lg:flex">
|
2022-04-14 21:25:24 +00:00
|
|
|
{actions.map((action) => {
|
|
|
|
const button = (
|
|
|
|
<Button
|
|
|
|
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}
|
2022-08-03 16:01:29 +00:00
|
|
|
{...(action?.actions ? { EndIcon: Icon.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>
|
2022-03-13 22:09:39 +00:00
|
|
|
<div className="inline-block text-left lg:hidden">
|
2022-04-17 15:37:16 +00:00
|
|
|
<DropdownActions actions={mobileActions} />
|
2022-03-13 22:09:39 +00:00
|
|
|
</div>
|
2021-09-29 21:33:18 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TableActions;
|