@bit-sun/business-component 2.2.3 → 2.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ declare const _default: {
6
6
  USER_INFO: string;
7
7
  DICT_CODES: string;
8
8
  CHILD_APP_BACK: string;
9
+ SEARCH_FIELDS_CONDITION: string;
9
10
  };
10
11
  };
11
12
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bit-sun/business-component",
3
- "version": "2.2.3",
3
+ "version": "2.2.5",
4
4
  "scripts": {
5
5
  "start": "dumi dev",
6
6
  "docs:build": "dumi build",
@@ -0,0 +1,538 @@
1
+ // @ts-nocheck
2
+ import React from 'react';
3
+ import {
4
+ Table,
5
+ message,
6
+ Checkbox,
7
+ Modal,
8
+ Input,
9
+ Button,
10
+ Tooltip,
11
+ } from 'antd';
12
+ import {
13
+ SortableContainer,
14
+ SortableElement,
15
+ SortableHandle,
16
+ } from 'react-sortable-hoc';
17
+ import axios from 'axios';
18
+ import { SearchOutlined } from '@ant-design/icons';
19
+ import { arrayMoveImmutable } from 'array-move';
20
+ import close from '../../../assets/close.svg';
21
+ import drag from '../../../assets/drag.svg';
22
+ import './index.less';
23
+ import ENUM from '@/utils/enumConfig';
24
+
25
+ const DragHandle = SortableHandle(() => <img src={drag} />);
26
+ const SortableItem = SortableElement((props) => <tr {...props} />);
27
+ const SortableBody = SortableContainer((props) => <tbody {...props} />);
28
+
29
+
30
+ class SearchItemTable extends React.Component {
31
+ state = {
32
+ dataSource: [],
33
+ columns: [],
34
+ sortDataSource: [],
35
+ setVisible: false,
36
+ searchDataSource: false,
37
+ onSearchSort: false,
38
+ isDefaultValue: false,
39
+ defaultValue: [],
40
+ bsTableCode: '', //设置table 列的标识
41
+ };
42
+
43
+ patchUserSearchFieldsConfig = (config) => {
44
+ let that = this;
45
+ let configvalue = config
46
+ ? config.map((item) => ({
47
+ name: item.name,
48
+ hidden: item.hidden,
49
+ }))
50
+ : '';
51
+
52
+ axios({
53
+ url: '/user/appConfig/saveQueryCriteria',
54
+ method: 'POST',
55
+ data: {
56
+ code: that.state.bsTableCode,
57
+ detail: configvalue ? JSON.stringify(configvalue) : '',
58
+ },
59
+ }).then((res:any) => {
60
+ if (res?.data?.status === '0') {
61
+ this.patchConfigToLocalstorage(configvalue)
62
+ } else {
63
+ message.error('保存搜索项自定义失败,请稍后尝试');
64
+ }
65
+ })
66
+ };
67
+
68
+ getConfigFromlocalstorage = () => {
69
+ let config = localStorage.getItem(ENUM.BROWSER_CACHE.SEARCH_FIELDS_CONDITION) || '[]';
70
+ let configArray = JSON.parse(config);
71
+ let configSetting = configArray.filter(
72
+ (item) => item.code === this.state.bsTableCode,
73
+ );
74
+
75
+ if (configSetting.length && configSetting[0].detail) {
76
+ return JSON.parse(configSetting[0].detail);
77
+ }
78
+ return [];
79
+ };
80
+
81
+ patchConfigToLocalstorage = (configvalue) => {
82
+ const { setShowSearchFields } = this.props;
83
+ const { sortDataSource, bsTableCode } = this.state;
84
+ let config = localStorage.getItem(ENUM.BROWSER_CACHE.SEARCH_FIELDS_CONDITION) || '[]'
85
+ let configArray = JSON.parse(config)
86
+ let currentSetting = configArray.filter(item => item.code === bsTableCode)
87
+ if (currentSetting.length) {
88
+ currentSetting[0].detail = JSON.stringify(configvalue)
89
+ } else {
90
+ configArray.push({
91
+ "code": bsTableCode,
92
+ "detail": JSON.stringify(configvalue)
93
+ })
94
+ }
95
+
96
+ localStorage.setItem(ENUM.BROWSER_CACHE.SEARCH_FIELDS_CONDITION, JSON.stringify(configArray));
97
+ this.setState({
98
+ visible: false,
99
+ });
100
+ setShowSearchFields([...sortDataSource]);
101
+ };
102
+
103
+ setInitValue = (isClick = false) => {
104
+ const { datasource, showSearchFields }: any =
105
+ this.props;
106
+ let config = this.getConfigFromlocalstorage();
107
+ this.setState({
108
+ isDefaultValue: true,
109
+ dataSource: datasource.map((item) => {
110
+ let innerItem = config.filter(
111
+ (inneritem) => {
112
+ let innerKey = Array.isArray(inneritem.name) ? JSON.stringify(inneritem.name) : (inneritem.name);
113
+ let itemKey = Array.isArray(item.name) ? JSON.stringify(item.name) : (item.name);
114
+ return innerKey && innerKey === itemKey;
115
+ }
116
+ );
117
+ return {
118
+ ...item,
119
+ hidden: config.length && !innerItem.length,
120
+ };
121
+ }),
122
+ defaultValue: datasource.map((item) => {
123
+ let innerItem = config.filter(
124
+ (inneritem) => {
125
+ let innerKey = Array.isArray(inneritem.name) ? JSON.stringify(inneritem.name) : (inneritem.name);
126
+ let itemKey = Array.isArray(item.name) ? JSON.stringify(item.name) : (item.name);
127
+ return innerKey && innerKey === itemKey;
128
+ }
129
+ );
130
+ return {
131
+ ...item,
132
+ hidden: config.length && !innerItem.length,
133
+ };
134
+ }),
135
+ sortDataSource: [...showSearchFields]
136
+ });
137
+ };
138
+
139
+ componentDidMount() {
140
+ const {
141
+ datasource,
142
+ showSearchFields,
143
+ bsTableCode
144
+ }: any = this.props;
145
+
146
+ let config = this.getConfigFromlocalstorage();
147
+
148
+ this.setState({
149
+ dataSource: datasource.map((item) => {
150
+ let innerItem = config.filter(
151
+ (inneritem) => {
152
+ let innerKey = Array.isArray(inneritem.name) ? JSON.stringify(inneritem.name) : (inneritem.name);
153
+ let itemKey = Array.isArray(item.name) ? JSON.stringify(item.name) : (item.name);
154
+ return innerKey && innerKey === itemKey;
155
+ }
156
+ );
157
+ return {
158
+ ...item,
159
+ hidden: config.length && !innerItem.length,
160
+ };
161
+ }),
162
+ defaultValue: datasource.map((item) => {
163
+ let innerItem = config.filter(
164
+ (inneritem) => {
165
+ let innerKey = Array.isArray(inneritem.name) ? JSON.stringify(inneritem.name) : (inneritem.name);
166
+ let itemKey = Array.isArray(item.name) ? JSON.stringify(item.name) : (item.name);
167
+ return innerKey && innerKey === itemKey;
168
+ }
169
+ );
170
+ return {
171
+ ...item,
172
+ hidden: config.length && !innerItem.length,
173
+ };
174
+ }),
175
+ sortDataSource: datasource
176
+ .filter((item) => !item.hidden)
177
+ .map((item) => ({
178
+ ...item,
179
+ })),
180
+ bsTableCode
181
+ });
182
+ }
183
+
184
+ columns = [
185
+ {
186
+ title: '搜索字段名称',
187
+ dataIndex: 'label',
188
+ className: 'drag-visible',
189
+ width: 100,
190
+ },
191
+ {
192
+ title: '删除',
193
+ dataIndex: 'title1',
194
+ render: (text, record) => {
195
+ return (
196
+ <span
197
+ onClick={() => {
198
+ this.setState({
199
+ sortDataSource: this.state.sortDataSource.filter((item) => {
200
+ return item.label !== record.label;
201
+ }),
202
+ isDefaultValue: false,
203
+ dataSource: [
204
+ ...this.state.dataSource.map((item) => {
205
+ if (item.label === record.label) {
206
+ return {
207
+ ...item,
208
+ hidden: true,
209
+ };
210
+ }
211
+ return item;
212
+ }),
213
+ ],
214
+ });
215
+ }}
216
+ >
217
+ <img src={close} />
218
+ </span>
219
+ );
220
+ },
221
+ },
222
+ {
223
+ title: 'Sort',
224
+ dataIndex: 'sort',
225
+ className: 'drag-visible',
226
+ render: () => <DragHandle />,
227
+ },
228
+ ];
229
+
230
+ showModal = () => {
231
+ this.setState({
232
+ visible: true,
233
+ });
234
+ this.setInitValue();
235
+ };
236
+
237
+ handleOk = (e?: React.MouseEvent<HTMLElement>) => {
238
+ const { sortDataSource, isDefaultValue, defaultValue } = this.state;
239
+ if (!sortDataSource.length) {
240
+ message.warning('至少选择一个搜索项!');
241
+ return;
242
+ }
243
+ this.patchUserSearchFieldsConfig(sortDataSource);
244
+ };
245
+
246
+ handleCancel = (e: React.MouseEvent<HTMLElement>) => {
247
+ console.log(e);
248
+ this.setState({
249
+ visible: false,
250
+ });
251
+ };
252
+
253
+ handleTableHeadHidden = (title: string) => {
254
+ const { sortDataSource, dataSource } = this.state;
255
+ this.setState(
256
+ {
257
+ sortDataSource: sortDataSource.filter((item) => {
258
+ return item.title !== title;
259
+ }),
260
+ dataSource: [
261
+ ...dataSource.map((item) => {
262
+ if (item.title === title) {
263
+ return {
264
+ ...item,
265
+ hidden: true,
266
+ };
267
+ }
268
+ return item;
269
+ }),
270
+ ],
271
+ },
272
+ () => {
273
+ this.handleOk();
274
+ },
275
+ );
276
+ };
277
+
278
+ onSortEnd = ({ oldIndex, newIndex }) => {
279
+ const { sortDataSource } = this.state;
280
+ if (oldIndex !== newIndex) {
281
+ const newData = arrayMoveImmutable(
282
+ [].concat(sortDataSource),
283
+ oldIndex,
284
+ newIndex,
285
+ ).filter((el) => !!el);
286
+ this.setState({
287
+ sortDataSource: [ ...newData],
288
+ isDefaultValue: false,
289
+ });
290
+ }
291
+ };
292
+
293
+ DraggableContainer = (props) => (
294
+ <SortableBody
295
+ useDragHandle
296
+ disableAutoscroll
297
+ helperClass="row-dragging"
298
+ onSortEnd={this.onSortEnd}
299
+ {...props}
300
+ />
301
+ );
302
+
303
+ DraggableBodyRow = ({ className, style, ...restProps }) => {
304
+ const { sortDataSource } = this.state;
305
+ const index = sortDataSource.findIndex(
306
+ (x) => x.name === restProps['data-row-key'],
307
+ );
308
+ return <SortableItem index={index} {...restProps} />;
309
+ };
310
+
311
+ onChange = (e: CheckboxChangeEvent, label) => {
312
+ const { sortDataSource, dataSource } = this.state;
313
+ if (!e.target.checked) {
314
+ this.setState({
315
+ sortDataSource: sortDataSource.filter((item) => {
316
+ return item.label !== label;
317
+ }),
318
+ isDefaultValue: false,
319
+ dataSource: [
320
+ ...dataSource.map((item) => {
321
+ if (item.label === label) {
322
+ return {
323
+ ...item,
324
+ hidden: true,
325
+ };
326
+ }
327
+ return item;
328
+ }),
329
+ ],
330
+ });
331
+ } else {
332
+ let newSortData = [
333
+ ...sortDataSource,
334
+ ...dataSource
335
+ .filter((item) => item.label === label)
336
+ .map((source) => {
337
+ return {
338
+ ...source,
339
+ hidden: false,
340
+ };
341
+ }),
342
+ ];
343
+ this.setState({
344
+ sortDataSource: [...newSortData],
345
+ isDefaultValue: false,
346
+ dataSource: [
347
+ ...dataSource.map((item) => {
348
+ if (item.label === label) {
349
+ return {
350
+ ...item,
351
+ hidden: false,
352
+ };
353
+ }
354
+ return item;
355
+ }),
356
+ ],
357
+ });
358
+ }
359
+ };
360
+
361
+ handleReset = () => {
362
+ this.setInitValue(true);
363
+ };
364
+
365
+ onSearch = (e: any) => {
366
+ this.setState({
367
+ searchDataSource: e.target.value,
368
+ });
369
+ };
370
+
371
+ onSearchSort = (e: any) => {
372
+ this.setState({
373
+ onSearchSort: e.target.value,
374
+ });
375
+ };
376
+
377
+ render() {
378
+ const {
379
+ dataSource = [],
380
+ searchDataSource,
381
+ sortDataSource,
382
+ visible,
383
+ onSearchSort,
384
+ } = this.state;
385
+ let newSearchSource = dataSource.filter(
386
+ (item) => item?.label?.indexOf(searchDataSource||'') > -1,
387
+ );
388
+ return (
389
+ <div className={'sort_table_wrapper'}>
390
+ {visible && (
391
+ <Modal
392
+ title="设置搜索项内容"
393
+ wrapClassName={'sort_table_wrapper'}
394
+ width={820}
395
+ visible={visible}
396
+ onOk={this.handleOk}
397
+ onCancel={this.handleCancel}
398
+ footer={[
399
+ <Button key="back" onClick={this.handleReset}>
400
+ 恢复默认
401
+ </Button>,
402
+ <Button key="submit" onClick={this.handleCancel}>
403
+ 取消
404
+ </Button>,
405
+ <Button key="submit" type="primary" onClick={this.handleOk}>
406
+ 确认
407
+ </Button>,
408
+ ]}
409
+ >
410
+ <div className={'sort_table'}>
411
+ <div className={'sort_table_column_wrapper'}>
412
+ <span className={'sort_table_column_count'}>
413
+ 可选字段 <span>(共{dataSource.length}个)</span>
414
+ </span>
415
+ <div className={'sort_table_column'}>
416
+ <Input
417
+ prefix={<SearchOutlined className="site-form-item-icon" />}
418
+ placeholder="搜索"
419
+ allowClear
420
+ onChange={this.onSearch}
421
+ style={{ width: 540 }}
422
+ />
423
+ <div>
424
+ {!searchDataSource && (
425
+ <Checkbox
426
+ checked={
427
+ !dataSource.some((item) => {
428
+ if (item.hidden) return true;
429
+
430
+ return false;
431
+ })
432
+ }
433
+ onClick={(e) => {
434
+ this.setState({
435
+ isDefaultValue: false,
436
+ dataSource: [
437
+ ...dataSource.map((item) => {
438
+ return {
439
+ ...item,
440
+ hidden: !e.target.checked,
441
+ };
442
+ }),
443
+ ],
444
+ sortDataSource: e.target.checked
445
+ ? [...dataSource.map((item) => ({...item,hidden: false}))]
446
+ : [],
447
+ });
448
+ }}
449
+ >
450
+ 全选
451
+ </Checkbox>
452
+ )}
453
+ </div>
454
+ <div className={'sort_table_column_all'}>
455
+ {searchDataSource
456
+ ? newSearchSource.map((item) => {
457
+ return (
458
+ <Checkbox
459
+ checked={!item.hidden}
460
+ onChange={(e) => {
461
+ this.onChange(e, item.label);
462
+ }}
463
+ >
464
+ {item.label}
465
+ </Checkbox>
466
+ );
467
+ })
468
+ : dataSource.map((item) => {
469
+ return (
470
+ <Checkbox
471
+ checked={!item.hidden}
472
+ onChange={(e) => {
473
+ this.onChange(e, item.label);
474
+ }}
475
+ >
476
+ {item.label}
477
+ </Checkbox>
478
+ );
479
+ })}
480
+ {!!newSearchSource.length && (
481
+ <span style={{ width: '144px' }}></span>
482
+ )}
483
+
484
+ {!newSearchSource.length && (
485
+ <div className={'sort_table_column_all_empty'}>
486
+ 未查询到结果
487
+ </div>
488
+ )}
489
+ </div>
490
+ </div>
491
+ </div>
492
+ <div className={'sort_table_content_wrapper'}>
493
+ <span className={'sort_table_content_count'}>
494
+ 已选字段 <span>(共{sortDataSource.length}个)</span>
495
+ </span>
496
+ <div className={'sort_table_content'}>
497
+ <span style={{ paddingLeft: '10px' }}>
498
+ <Input
499
+ prefix={
500
+ <SearchOutlined className="site-form-item-icon" />
501
+ }
502
+ placeholder="搜索"
503
+ allowClear
504
+ onChange={this.onSearchSort}
505
+ style={{ width: 190 }}
506
+ />
507
+ </span>
508
+ <Table
509
+ pagination={false}
510
+ showHeader={false}
511
+ dataSource={
512
+ onSearchSort
513
+ ? sortDataSource.filter(
514
+ (item) => item?.label?.indexOf(onSearchSort) > -1,
515
+ )
516
+ : sortDataSource
517
+ }
518
+ columns={this.columns}
519
+ rowKey="name"
520
+ components={{
521
+ body: {
522
+ wrapper: this.DraggableContainer,
523
+ row: this.DraggableBodyRow,
524
+ },
525
+ }}
526
+ />
527
+ </div>
528
+ </div>
529
+ </div>
530
+ </Modal>
531
+ )}
532
+ <div onClick={this.showModal} style={{fontSize:14}}>搜索项设置</div>
533
+ </div>
534
+ );
535
+ }
536
+ }
537
+
538
+ export default SearchItemTable;