-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathindex.tsx
More file actions
79 lines (73 loc) · 2.05 KB
/
index.tsx
File metadata and controls
79 lines (73 loc) · 2.05 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
import React, { CSSProperties, PropsWithChildren, ReactNode } from 'react';
import { Dropdown, DropdownProps, Menu, Popconfirm, PopconfirmProps } from 'antd';
import './style.scss';
interface IMenuProps {
/**
* @required
*/
key: React.Key;
/**
* 菜单栏的标题文案
*/
text: ReactNode;
/**
* 菜单栏的样式
*/
style?: CSSProperties;
/**
* 是否支持 popconfirm 的弹出
*/
confirm?: boolean;
/**
* 只有设置了 `confirm` 项的情况下,该属性才会生效
*/
confirmProps?: PopconfirmProps;
/**
* 菜单栏的点击事件
*/
cb?: () => void;
}
interface IContextMenu
extends Pick<
DropdownProps,
'destroyPopupOnHide' | 'getPopupContainer' | 'placement' | 'overlayClassName'
> {
data: IMenuProps[];
wrapperClassName?: string;
}
export default function ContextMenu({
data = [],
children,
wrapperClassName,
...restProps
}: PropsWithChildren<IContextMenu>) {
const menu = () => (
<Menu
className="dtc-contextMenu-menu"
onClick={(item) => {
item.domEvent.stopPropagation();
data.find((i) => i.key === item.key)?.cb?.();
}}
>
{data.map((item) =>
item.confirm ? (
<Menu.Item style={item.style} key={item.key}>
<Popconfirm key={item.key} {...item.confirmProps!}>
<div>{item.text}</div>
</Popconfirm>
</Menu.Item>
) : (
<Menu.Item style={item.style} key={item.key}>
{item.text}
</Menu.Item>
)
)}
</Menu>
);
if (!data.length) return <span className={wrapperClassName}>{children}</span>;
return (
<Dropdown dropdownRender={menu} trigger={['contextMenu']} {...restProps}>
<span className={wrapperClassName}>{children}</span>
</Dropdown>
);
}