index.tsx
6.29 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import React from 'react';
import { Button, Tooltip, Dropdown, Menu } from 'antd';
import './index.less';
import {
CloseCircleOutlined,
CloseOutlined,
DeleteOutlined,
EditOutlined,
EllipsisOutlined,
MinusCircleOutlined,
MinusOutlined,
PlusCircleOutlined,
PlusOutlined,
ToolOutlined,
SettingOutlined,
CheckOutlined,
CopyOutlined,
ImportOutlined,
ExportOutlined,
FileTextOutlined,
PlayCircleOutlined,
PauseCircleOutlined,
AppstoreOutlined,
BarsOutlined,
AlertOutlined,
EyeOutlined,
// DownOutlined,
PrinterOutlined,
} from '@ant-design/icons';
import { QxBaseIcon } from '@qx/common';
import type { DropDownProps as DropdownProps, ButtonProps, TooltipProps } from 'antd/lib';
export const BUTTON_ICONS_MAP: { [index: string]: React.ReactElement } = {
PlusOutlined: <PlusOutlined />,
MinusOutlined: <MinusOutlined />,
PlusCircleOutlined: <PlusCircleOutlined />,
MinusCircleOutlined: <MinusCircleOutlined />,
CloseCircleOutlined: <CloseCircleOutlined />,
CloseOutlined: <CloseOutlined />,
CheckOutlined: <CheckOutlined />,
DeleteOutlined: <DeleteOutlined />,
EyeOutlined: <EyeOutlined />,
EditOutlined: <EditOutlined />,
FileTextOutlined: <FileTextOutlined />,
EllipsisOutlined: <EllipsisOutlined />,
SettingOutlined: <SettingOutlined />,
ToolOutlined: <ToolOutlined />,
CopyOutlined: <CopyOutlined />,
ImportOutlined: <ImportOutlined />,
ExportOutlined: <ExportOutlined />,
PlayCircleOutlined: <PlayCircleOutlined />,
PauseCircleOutlined: <PauseCircleOutlined />,
AppstoreOutlined: <AppstoreOutlined />,
BarsOutlined: <BarsOutlined />,
AlertOutlined: <AlertOutlined />,
PrinterOutlined: <PrinterOutlined />,
};
export const BUTTON_COLORS = {
blue: '#1764FF',
green: '#52c41a',
orange: '#faad14',
red: '#f5222d',
purple: '#722ed1',
cyan: '#13c2c2',
};
//'#1890ff' primary
//'#52c41a' success
//'#faad14' Warning
// #f5222d error
/*@purple-base: #722ed1;
@cyan-base: #13c2c2;*/
export interface DropdownMenuItem {
code?: string;
name?: string;
icon?: string;
}
export interface QxButtonProps extends ButtonProps {
name?: string;
color?: string;
batch?: boolean; //冗余属性
flag?: string; //冗余属性
action?: string; //冗余属性
line?: boolean; //冗余属性
code?: string; //冗余属性
needConfirm?: boolean; //冗余属性
tooltip?: string;
/**
* 下拉菜单选项
*/
dropdownMenuItems?: DropdownMenuItem[];
/**
* 下拉菜单点击事件
*/
handleMenuClick?: (...args: any) => void;
/**
* 下拉菜单参数
*/
dropdownProps?: DropdownProps;
/**
* 是否显示下拉菜单,默认 true
*/
showDropdown?: boolean;
// 提示框相关参数
tooltipProps?: TooltipProps | undefined;
extraDom?: React.ReactNode | undefined; // 额外标签
}
const Icon = ({ name }: { name: any }) => {
if (name?.$$typeof) return name;
return name ? (
String(name).indexOf('http') > -1 ? (
<img
src={String(name)}
style={{
width: '16px',
height: '16px',
marginRight: '4px',
verticalAlign: 'text-bottom',
borderRadius: '50%',
}}
alt=""
/>
) : String(name).indexOf('-') > -1 ? (
<QxBaseIcon type={String(name)} />
) : (
BUTTON_ICONS_MAP[name + ''] || <PlusOutlined />
)
) : null;
};
export const QxButton: React.FC<QxButtonProps> = (props) => {
const {
name,
style,
icon,
batch,
line,
needConfirm,
className = '',
flag,
color,
tooltip = '',
dropdownMenuItems = [],
handleMenuClick,
dropdownProps = {},
showDropdown = true,
tooltipProps,
extraDom,
...rest
} = props;
const showMenu = Array.isArray(dropdownMenuItems) && dropdownMenuItems?.length && showDropdown;
const getMenuItem = (_menuItems: any) =>
_menuItems?.map((menu: any) => {
return {
...menu,
label: menu?.label || menu?.name,
key: menu?.key || menu?.code,
children: getMenuItem(menu?.dropdownMenuItems),
};
});
const generateMenu = (items: any) => {
return getMenuItem(items)?.map((item: any) => {
if (item.children?.length) {
return (
<Menu.SubMenu
icon={
<span>
<Icon name={item.icon} />
</span>
}
title={item.label}
key={item.key}
disabled={!!item?.disabled}
>
{generateMenu(item.children)}
</Menu.SubMenu>
);
} else {
return (
<Menu.Item
icon={
<span>
<Icon name={item.icon} />
</span>
}
key={item.key}
onClick={() => handleMenuClick?.(item)}
disabled={!!item?.disabled}
>
{item.label}
</Menu.Item>
);
}
});
};
const menu = () => <Menu>{generateMenu(dropdownMenuItems)}</Menu>;
const btnChildren = name ? name : props.children;
const button = !!extraDom ? (
<Button
className={'qx-btn ' + className + ' ' + (color ? `qx-btn-${color}` : '')}
{...rest}
icon={<Icon name={icon} />}
>
{showMenu && btnChildren && props?.type !== 'link' ? (
<>
<QxBaseIcon type={'icon-common-arrow'} className={'qx-core-dropdown-arrow'} />
{btnChildren}
</>
) : (
btnChildren
)}
{extraDom}
</Button>
) : (
<Button
className={'qx-btn ' + className + ' ' + (color ? `qx-btn-${color}` : '')}
{...rest}
icon={<Icon name={icon} />}
>
{showMenu && btnChildren && props?.type !== 'link' ? (
<>
<QxBaseIcon type={'icon-common-arrow'} className={'qx-core-dropdown-arrow'} />
{btnChildren}
</>
) : (
btnChildren
)}
</Button>
);
const _dropdownProps: Omit<DropdownProps, 'overlay'> = {
overlayClassName: 'qx-core-drop_down',
...dropdownProps,
};
const content = showMenu ? (
<Dropdown overlay={menu} {..._dropdownProps}>
{button}
</Dropdown>
) : (
button
);
// extraDom 有额外标签时 tooltip不生效
if (tooltip && !extraDom) {
return (
<Tooltip {...(tooltipProps || {})} title={tooltip}>
{content}
</Tooltip>
);
}
return content;
};