This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
/
index.tsx
302 lines (286 loc) · 9.06 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import React from 'react';
import { ConfigConsumer, ConfigConsumerProps } from 'antd/lib/config-provider/context';
import { PushpinOutlined, SettingOutlined, VerticalAlignMiddleOutlined } from '@ant-design/icons';
import { Checkbox, Popover, Tooltip } from 'antd';
import { DndProvider } from 'react-dnd';
import Backend from 'react-dnd-html5-backend';
import Container from '../../container';
import { ProColumns, ColumnsState } from '../../Table';
import DnDItem from './DndItem';
import { useIntl } from '../intlContext';
import './index.less';
import { genColumnKey } from '../util';
interface ColumnSettingProps<T = any> {
columns?: ProColumns<T>[];
}
const ToolTipIcon: React.FC<{
title: string;
columnKey: string | number;
show: boolean;
fixed: 'left' | 'right' | undefined;
}> = ({ title, show, children, columnKey, fixed }) => {
const { columnsMap, setColumnsMap } = Container.useContainer();
if (show) {
return (
<Tooltip title={title}>
<span
onClick={() => {
const config = columnsMap[columnKey || ''] || {};
const columnKeyMap = {
...columnsMap,
[columnKey]: { ...config, fixed } as ColumnsState,
};
setColumnsMap(columnKeyMap);
}}
>
{children}
</span>
</Tooltip>
);
}
return null;
};
const CheckboxListItem: React.FC<{
columnKey: string | number;
className?: string;
title?: React.ReactNode;
columnsMap: {
[key: string]: ColumnsState;
};
fixed?: boolean | 'left' | 'right';
setColumnsMap: (map: { [key: string]: ColumnsState }) => void;
}> = ({ columnKey, className, columnsMap, title, setColumnsMap, fixed }) => {
const intl = useIntl();
const config = columnsMap[columnKey || 'null'] || { show: true };
return (
<span className={`${className}-list-item`} key={columnKey}>
<Checkbox
onChange={(e) => {
if (columnKey) {
const tempConfig = columnsMap[columnKey || ''] || {};
const newSetting = { ...tempConfig };
if (e.target.checked) {
delete newSetting.show;
} else {
newSetting.show = false;
}
const columnKeyMap = {
...columnsMap,
[columnKey]: newSetting as ColumnsState,
};
setColumnsMap(columnKeyMap);
}
}}
checked={config.show !== false}
>
{title}
</Checkbox>
<span className={`${className}-list-item-option`}>
<ToolTipIcon
columnKey={columnKey}
fixed="left"
title={intl.getMessage('tableToolBar.leftPin', '固定到左边')}
show={fixed !== 'left'}
>
<PushpinOutlined
style={{
transform: 'rotate(-90deg)',
}}
/>
</ToolTipIcon>
<ToolTipIcon
columnKey={columnKey}
fixed={undefined}
title={intl.getMessage('tableToolBar.noPin', '取消固定')}
show={!!fixed}
>
<VerticalAlignMiddleOutlined />
</ToolTipIcon>
<ToolTipIcon
columnKey={columnKey}
fixed="right"
title={intl.getMessage('tableToolBar.rightPin', '固定到右边')}
show={fixed !== 'right'}
>
<PushpinOutlined />
</ToolTipIcon>
</span>
</span>
);
};
const CheckboxList: React.FC<{
list: (ProColumns<any> & { index?: number })[];
className?: string;
title: string;
showTitle?: boolean;
}> = ({ list, className, showTitle = true, title: listTitle }) => {
const { columnsMap, setColumnsMap, sortKeyColumns, setSortKeyColumns } = Container.useContainer();
const show = list && list.length > 0;
if (!show) {
return null;
}
const move = (id: string, targetIndex: number) => {
const newColumns = [...sortKeyColumns];
const findIndex = newColumns.findIndex((columnKey) => columnKey === id);
const key = newColumns[findIndex];
newColumns.splice(findIndex, 1);
if (targetIndex === 0) {
newColumns.unshift(key);
} else {
newColumns.splice(targetIndex, 0, key);
}
setSortKeyColumns(newColumns);
};
const listDom = list.map(({ key, dataIndex, title, fixed, ...rest }, index) => {
const columnKey = genColumnKey(key, dataIndex, rest.index);
return (
<DnDItem
index={index}
id={`${columnKey}_${rest.index}`}
key={columnKey}
end={(id, targetIndex) => {
move(id, targetIndex);
}}
>
<CheckboxListItem
setColumnsMap={setColumnsMap}
columnKey={columnKey || `${index}`}
columnsMap={columnsMap}
title={title}
fixed={fixed}
className={className}
/>
</DnDItem>
);
});
return (
<DndProvider backend={Backend}>
{showTitle && <span className={`${className}-list-title`}>{listTitle}</span>}
{listDom}
</DndProvider>
);
};
const GroupCheckboxList: React.FC<{
localColumns: (ProColumns<any> & { index?: number })[];
className?: string;
}> = ({ localColumns, className }) => {
const rightList: (ProColumns<any> & { index?: number })[] = [];
const leftList: (ProColumns<any> & { index?: number })[] = [];
const list: (ProColumns<any> & { index?: number })[] = [];
const intl = useIntl();
localColumns.forEach((item) => {
const { fixed } = item;
if (fixed === 'left') {
leftList.push(item);
return;
}
if (fixed === 'right') {
rightList.push(item);
return;
}
list.push(item);
});
const showRight = rightList && rightList.length > 0;
const showLeft = leftList && leftList.length > 0;
return (
<div className={`${className}-list`}>
<CheckboxList
title={intl.getMessage('tableToolBar.leftFixedTitle', '固定在左侧')}
list={leftList}
className={className}
/>
{/* 如果没有任何固定,不需要显示title */}
<CheckboxList
list={list}
title={intl.getMessage('tableToolBar.noFixedTitle', '不固定')}
showTitle={showLeft || showRight}
className={className}
/>
<CheckboxList
title={intl.getMessage('tableToolBar.rightFixedTitle', '固定在右侧')}
list={rightList}
className={className}
/>
</div>
);
};
const ColumnSetting = <T, U = {}>(props: ColumnSettingProps<T>) => {
const counter = Container.useContainer();
const localColumns: Omit<ProColumns<any> & { index?: number }, 'ellipsis'>[] =
props.columns || counter.columns || [];
const { columnsMap, setColumnsMap, setSortKeyColumns } = counter;
/**
* 设置全部选中,或全部未选中
* @param show
*/
const setAllSelectAction = (show: boolean = true) => {
const columnKeyMap = {};
localColumns.forEach(({ key, fixed, dataIndex, index }) => {
const columnKey = genColumnKey(key, dataIndex, index);
if (columnKey) {
columnKeyMap[columnKey] = {
show,
fixed,
};
}
});
setColumnsMap(columnKeyMap);
};
// 选中的 key 列表
const selectedKeys = Object.values(columnsMap).filter((value) => !value || value.show === false);
// 是否已经选中
const indeterminate = selectedKeys.length > 0 && selectedKeys.length !== localColumns.length;
const intl = useIntl();
return (
<ConfigConsumer>
{({ getPrefixCls }: ConfigConsumerProps) => {
const className = getPrefixCls('pro-table-column-setting');
const toolBarClassName = getPrefixCls('pro-table-toolbar');
return (
<Popover
arrowPointAtCenter
title={
<div className={`${className}-title`}>
<Checkbox
indeterminate={indeterminate}
checked={selectedKeys.length === 0 && selectedKeys.length !== localColumns.length}
onChange={(e) => {
if (e.target.checked) {
setAllSelectAction();
} else {
setAllSelectAction(false);
}
}}
>
{intl.getMessage('tableToolBar.columnDisplay', '列展示')}
</Checkbox>
<a
onClick={() => {
setColumnsMap({});
setSortKeyColumns([]);
}}
>
{intl.getMessage('tableToolBar.reset', '重置')}
</a>
</div>
}
overlayClassName={`${className}-overlay`}
trigger="click"
placement="bottomRight"
content={<GroupCheckboxList className={className} localColumns={localColumns} />}
>
<Tooltip title={intl.getMessage('tableToolBar.columnSetting', '列设置')}>
<SettingOutlined
className={`${toolBarClassName}-item-icon`}
style={{
fontSize: 16,
}}
/>
</Tooltip>
</Popover>
);
}}
</ConfigConsumer>
);
};
export default ColumnSetting;