2022-03-13 22:09:39 +00:00
|
|
|
import React, { FC } from "react";
|
2021-09-29 21:33:18 +00:00
|
|
|
|
2022-09-30 10:29:22 +00:00
|
|
|
import Dropdown, {
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuItem,
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
DropdownMenuPortal,
|
|
|
|
} from "@calcom/ui/Dropdown";
|
2022-07-27 02:24:00 +00:00
|
|
|
import { Icon } from "@calcom/ui/Icon";
|
2022-11-04 15:40:46 +00:00
|
|
|
import { Button } from "@calcom/ui/components/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>
|
|
|
|
)}
|
2022-09-30 10:29:22 +00:00
|
|
|
<DropdownMenuPortal>
|
|
|
|
<DropdownMenuContent>
|
|
|
|
{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}
|
|
|
|
onClick={action.onClick || defaultAction}
|
|
|
|
data-testid={action.id}>
|
|
|
|
{action.label}
|
|
|
|
</Button>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
))}
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</DropdownMenuPortal>
|
2022-04-14 21:25:24 +00:00
|
|
|
</Dropdown>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-09-29 21:33:18 +00:00
|
|
|
const TableActions: FC<Props> = ({ actions }) => {
|
|
|
|
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}
|
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>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default TableActions;
|