index.tsx
6.58 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 { Button, Select } from 'antd';
import { cloneDeep, findIndex, isEqual } from 'lodash-es';
import React, { useEffect, useState } from 'react';
import { QxBaseIcon, getWidgetsIcon } from '@qx/common';
import './styles.less';
const { Option } = Select;
type SortProps = {
key: string;
asc: boolean;
};
interface OptionItemProps {
name: string;
code: string | number;
extract?: {
widget?: string;
};
}
interface SortItemProps {
label: string,
value: string | number,
}
interface QxSortConditionProps {
/**
* @description 已选排序字段
* @default "-"
*/
value: SortProps[];
/**
* @description 字段变化方法
* @default "-"
*/
onChange: (val: SortProps[]) => void;
/**
* @description 可选字段
* @default "-"
*/
optionList: OptionItemProps[];
sortList?: SortItemProps[];
extraClassName?: string; // 定义额外 标签类名 覆盖排序原有样式
disabled?: boolean; // 是否整体禁用
}
const defaultSortList = [
{
label: 'A → Z',
value: 'true',
},
{
label: 'Z → A',
value: 'false',
},
];
/**
* 排序条件设置
*
* @param props
* @constructor
*/
export const QxSortCondition: React.FC<QxSortConditionProps> = (props) => {
const { value, onChange, optionList, sortList, extraClassName } = props;
const [sorts, setSorts] = useState<any[]>([]);
const [columns, setColumns] = useState<OptionItemProps[]>();
useEffect(() => {
setColumns(optionList);
}, [optionList]);
// 根据`code`获取对应列对象
const getSortItemByDataIndex = (code: string) => {
if (!columns || columns.length === 0) {
return {};
}
const index = findIndex(columns, (o: OptionItemProps) => {
return o.code === code;
});
return columns[index];
};
useEffect(() => {
if (!columns) {
return;
}
if (!value || value.length === 0) {
const sortCol: any = getSortItemByDataIndex('created_at');
const item: SortProps = {
key: sortCol?.code,
asc: false,
};
setSorts([item]);
} else {
setSorts(cloneDeep(value || []));
}
}, [columns, value]);
/**
* 通知到父组件
*/
useEffect(() => {
if (!sorts || (Array.isArray(sorts) && sorts.length === 0)) {
return;
}
const isSame = isEqual(value, sorts);
if (isSame) {
return;
}
if (!value && sorts?.length === 0) {
return;
}
// 更新父组件`columnsUser`
onChange(sorts);
}, [sorts]);
/**
* 获取未使用的`列`数组
*/
const getUnSelCols = (cols: OptionItemProps[], exceptKey: string) => {
const selectedKeyArr: string[] = [];
(sorts || []).forEach((v: SortProps) => {
// 跳过排除项
if (!(exceptKey && v.key === exceptKey)) {
selectedKeyArr.push(v.key);
}
});
const unSelCols: OptionItemProps[] = [];
cols.forEach((v2: OptionItemProps) => {
let isIn = false;
selectedKeyArr.forEach((v3) => {
if (v2.code === v3) {
isIn = true;
}
});
if (!isIn) {
unSelCols.push(v2);
}
});
return unSelCols;
};
// 新增排序条件
const addSort = () => {
const colsTem = getUnSelCols(cloneDeep(columns || []), '') || [];
const sortsNew = cloneDeep(sorts || []);
sortsNew.push({
key: colsTem[0]?.code,
asc: true,
});
setSorts(sortsNew);
};
// 删除排序条件
const removeSort = (index: number) => {
const orderNew = cloneDeep(sorts || []);
orderNew.splice(index, 1);
setSorts(orderNew);
};
return (
<div
className={`${extraClassName || ' '} qx-sort-condition-container`}
style={{ width: '100%' }}
>
{(sorts || []).map((v: any, k: any) => (
<div
style={{ marginBottom: sorts?.length - 1 === k ? 0 : '1em' }}
key={v.key}
>
{columns && (
<Select
disabled={props?.disabled}
defaultValue={v.key}
getPopupContainer={(triggerNode) => triggerNode}
value={v.key}
style={{
width: 'calc(100% - 86px - 64px - 8px)',
marginRight: 8,
}}
showSearch={true}
filterOption={(input, option) =>
option?.children
?.toString()
.toLowerCase()
.indexOf(input.toLowerCase()) !== -1
}
onChange={(val: string) => {
const item = sorts[k];
item.key = val;
const sortsNew = cloneDeep(sorts || []);
sortsNew.splice(k, 1, item);
setSorts(sortsNew);
}}
>
{getUnSelCols(cloneDeep(columns || []), v.key).map(
(item: OptionItemProps) => (
<Option key={item?.code} value={item?.code}>
{getWidgetsIcon(item?.extract?.widget || '')}{item?.name}
</Option>
),
)}
</Select>
)}
<Select
disabled={props?.disabled}
defaultValue={String(v.asc)}
getPopupContainer={(triggerNode) => triggerNode}
value={String(v.asc)}
style={{ width: '86px' }}
onChange={(val: string) => {
const item = sorts[k];
item.asc = val === 'true';
const sortsNew = cloneDeep(sorts || []);
sortsNew.splice(k, 1, item);
setSorts(sortsNew);
}}
>
{(sortList || defaultSortList).map((item: SortItemProps) => {
return (
<Option key={item?.value} value={item.value}>
{item.label}
</Option>
);
})}
</Select>
<span
className={'qx-sort-condition-container__operate'}
style={{ margin: '-2px 0 0 8px' }}
>
<Button
className={'operate-button'}
size="small"
icon={<QxBaseIcon type={'qx-icon-minus'} />}
disabled={props?.disabled || sorts.length <= 1}
onClick={() => removeSort(k)}
/>
<Button
className={'operate-button'}
size="small"
icon={<QxBaseIcon type={'qx-icon-plus'} />}
disabled={
props?.disabled || (sorts.length >=
((columns || []).length > 3 ? 3 : (columns || []).length))
}
onClick={addSort}
/>
</span>
</div>
))}
</div>
);
};