/
index.tsx
221 lines (209 loc) · 6.62 KB
/
index.tsx
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
/* eslint-disable jsx-a11y/anchor-is-valid */
import React from 'react';
import _isArray from 'lodash/isArray';
import _isFunction from 'lodash/isFunction';
import { Icon, Dropdown, Menu, Popconfirm, Modal } from 'antd';
import { DETAIL_NAME, UPDATE_NAME, DELETE_NAME } from '../../../constants';
import { InternalCurdBox, ActionsConfig } from '../index';
import styles from './index.less';
export type ConfirmKey<T> = (number | [number, (record: T) => string]);
function isConfirmKeyAndItem<T>(key: number, confirmKeys: ConfirmKey<T>[] = []): ConfirmKey<T> | null {
for (let i = 0; i < confirmKeys.length; i += 1) {
if (_isArray(confirmKeys[i]) && confirmKeys[i][0] === key) {
return confirmKeys[i];
}
if (confirmKeys[i] === key) {
return confirmKeys[i];
}
}
return null;
}
function setConfirmTitle<T>(confirmKey: ConfirmKey<T>, action: ActionType<T>, record: T) {
if (_isArray(confirmKey)) {
const [, setTitle] = confirmKey;
return setTitle(record);
}
return `确定${action.title}吗?`;
}
export interface ActionType<T> {
key: number;
title: string;
handleClick: (record: T) => void;
};
export function sortAndFilterActionsAsc<T>(record: T, actions: ActionType<T>[], hideActions: number[] | ((record?: T) => void | number[])) {
return [...actions]
.filter((item) => {
if (_isFunction(hideActions)) {
const hideActionKeys = hideActions(record) || [];
return !hideActionKeys.includes(item.key);
} else {
return !hideActions.includes(item.key);
}
})
.sort((x, y) => {
if (x.key > y.key) return 1;
if (x.key < y.key) return -1;
return 0;
});
}
export function initialActions<T extends { id: number | string }>(record: T, handleDefaultActionClick: InternalCurdBox<T>['handleDefaultActionClick'], actionsConfig: ActionsConfig<T>) {
const {
detailActionTitle = '详情',
updateActionTitle = '编辑',
deleteActionTitle = '删除',
showActionsCount = 3,
extraActions = [],
hideActions = []
} = actionsConfig;
const actions = [
{
key: 4,
title: detailActionTitle,
handleClick: () => { handleDefaultActionClick(DETAIL_NAME, record) }
},
{
key: 8,
title: updateActionTitle,
handleClick: () => { handleDefaultActionClick(UPDATE_NAME, record) }
},
{
key: 12,
title: deleteActionTitle,
handleClick: () => { handleDefaultActionClick(DELETE_NAME, record) }
},
...extraActions
];
const sortedActions = sortAndFilterActionsAsc(record, actions, hideActions);
return [sortedActions.slice(0, showActionsCount), sortedActions.slice(showActionsCount)];
}
function renderShowActions<T>(record: T) {
return (
actions: ActionType<T>[],
actionsConfig: ActionsConfig<T>,
disabledActionKeys: number[] = []
) => {
const { confirmKeys, confirmProps } = actionsConfig;
return actions.map((item) => {
if (disabledActionKeys.includes(item.key)) {
return (
<span key={item.key} className={styles.disabledAction}>
{item.title}
</span>
);
}
const confirmKey = isConfirmKeyAndItem(item.key, confirmKeys);
if (confirmKey) {
return (
<span
key={item.key}
onClick={(event) => {
if (event) {
event.stopPropagation();
}
}}
>
<Popconfirm
{...confirmProps}
title={setConfirmTitle(confirmKey, item, record)}
onConfirm={() => {
item.handleClick(record);
}}
onCancel={(event) => {
if (event) {
event.stopPropagation();
}
}}
>
<a>{item.title}</a>
</Popconfirm>
</span>
);
}
return (
<a
key={item.key}
onClick={(event) => {
event.stopPropagation();
item.handleClick(record);
}}
>
{item.title}
</a>
);
});
};
}
export function renderActions<T>(record: T) {
return (
actions: ActionType<T>[],
moreActions: ActionType<T>[],
actionsConfig: ActionsConfig<T>
) => {
const { confirmKeys, disabledActions } = actionsConfig;
let disabledActionKeys: number[] = [];
if (disabledActions) {
disabledActionKeys = disabledActions(record) || [];
}
if (!moreActions.length) {
return renderShowActions(record)(actions, actionsConfig, disabledActionKeys);
}
return [
...renderShowActions(record)(actions, actionsConfig, disabledActionKeys),
<span
key="dropdown"
onClick={(event) => {
if (event) {
event.stopPropagation();
}
}}
>
<Dropdown
key="more"
overlay={
// 阻止 Menu 点击事件冒泡,否则会触发 actions 容器点击事件
<Menu
onClick={({ domEvent }) => {
domEvent.stopPropagation();
}}
>
{moreActions.map((item) => {
const confirmKey = isConfirmKeyAndItem(item.key, confirmKeys);
const isDisabled = disabledActionKeys.includes(item.key);
return (
<Menu.Item
key={item.key}
disabled={isDisabled}
onClick={() => {
if (confirmKey) {
Modal.confirm({
title: setConfirmTitle(confirmKey, item, record),
onOk() {
item.handleClick(record);
},
okText: '确定',
cancelText: '取消'
});
return;
}
item.handleClick(record);
}}
>
{isDisabled ? item.title : <a>{item.title}</a>}
</Menu.Item>
);
})}
</Menu>
}
>
<a style={{ whiteSpace: 'nowrap' }}>
更多 <Icon type="down" style={{ width: 24 }} />
</a>
</Dropdown>
</span>
];
};
}
export function setActions<T extends { id: number | string }>(record: T, handleDefaultActionClick: InternalCurdBox<T>['handleDefaultActionClick'], actionsConfig: ActionsConfig<T>) {
const [actions, moreActions] = initialActions(record, handleDefaultActionClick, actionsConfig);
return renderActions(record)(actions, moreActions, actionsConfig);
}