-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathActionDropdown.tsx
More file actions
117 lines (110 loc) · 2.97 KB
/
ActionDropdown.tsx
File metadata and controls
117 lines (110 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { Dropdown, Popconfirm, Button, Space } from "antd";
import { EllipsisOutlined } from "@ant-design/icons";
import { useState } from "react";
interface ActionItem {
key: string;
label: string;
icon?: React.ReactNode;
danger?: boolean;
confirm?: {
title: string;
description?: string;
okText?: string;
cancelText?: string;
};
}
interface ActionDropdownProps {
actions?: ActionItem[];
onAction?: (key: string, action: ActionItem) => void;
placement?:
| "bottomRight"
| "topLeft"
| "topCenter"
| "topRight"
| "bottomLeft"
| "bottomCenter"
| "top"
| "bottom";
}
const ActionDropdown = ({
actions = [],
onAction,
placement = "bottomRight",
}: ActionDropdownProps) => {
const [open, setOpen] = useState(false);
const handleActionClick = (action: ActionItem) => {
if (action.confirm) {
// 如果有确认框,不立即执行,等待确认
return;
}
// 执行操作
onAction?.(action.key, action);
// 如果没有确认框,则立即关闭 Dropdown
setOpen(false);
};
const dropdownContent = (
<div className="bg-white p-2 rounded shadow-md">
<Space direction="vertical" className="w-full">
{actions.map((action) => {
if (action.confirm) {
return (
<Popconfirm
key={action.key}
title={action.confirm.title}
description={action.confirm.description}
onConfirm={() => {
onAction?.(action.key, action);
setOpen(false);
}}
okText={action.confirm.okText || "确定"}
cancelText={action.confirm.cancelText || "取消"}
okType={action.danger ? "danger" : "primary"}
styles={{ root: { zIndex: 9999 } }}
>
<Button
type="text"
size="small"
disabled={action.disabled || false}
className="w-full text-left"
danger={action.danger}
icon={action.icon}
>
{action.label}
</Button>
</Popconfirm>
);
}
return (
<Button
key={action.key}
className="w-full"
size="small"
type="text"
disabled={action.disabled || false}
danger={action.danger}
icon={action.icon}
onClick={() => handleActionClick(action)}
>
{action.label}
</Button>
);
})}
</Space>
</div>
);
return (
<Dropdown
overlay={dropdownContent}
trigger={["click"]}
placement={placement}
open={open}
onOpenChange={setOpen}
>
<Button
type="text"
icon={<EllipsisOutlined style={{ fontSize: 24 }} />}
/>
</Dropdown>
);
};
export default ActionDropdown;