index.tsx
8.4 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
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
303
304
305
306
307
import { ReactElement, useCallback, useEffect, useMemo, useState } from 'react';
import { Space, Button, Tooltip, Tag } from 'antd';
import './index.less';
import { FormProvider, VoidField } from '@formily/react';
import { createForm, onFieldValueChange, Field, FormPath } from '@formily/core';
import { ossAddress } from '@/utils/ossAddress';
import { useDebounce } from '@/utils/format';
import moment from 'moment';
// Avoid magic numbers
const MAX_COMPONENTS = 6;
const DEBOUNCE_DELAY = 800;
export interface SearchWrapProps {
leftSectionComponents?: ReactElement[];
searchInput?: ReactElement;
popupContent?: ReactElement[];
searchChange?: Function;
getFormInstance?: Function;
moreCategories?: Boolean;
ProForm?: any;
}
type FormPathPattern =
| string
| number
| Array<string | number>
| FormPath
| RegExp
| (((address: Array<string | number>) => boolean) & {
path: FormPath;
});
interface SearchChooseProps {
name: string;
title: string;
value: string;
path: FormPathPattern;
address: any;
}
const getValueLable = ({
title,
value,
dataSource,
props,
path,
address,
componentProps,
}: Field) => {
const { name } = props;
const { componenttypename, format } = componentProps;
const labelMap: any = {
name,
path,
address,
title: title ? title : '',
};
const getValue = (key: any) => {
const dataSourceMap: Record<string, string> = {};
dataSource.map((item) => {
if (item && item.value) {
dataSourceMap[item.value] = item.label;
}
});
return dataSourceMap[key];
};
switch (componenttypename) {
case 'Radio.Group':
labelMap.value = getValue(value);
break;
case 'Select':
console.log(value, 'selectValue');
case 'Checkbox.Group':
const valArray: any[] = [];
if (value && value.length > 0 && typeof value != 'string') {
value.map((val: any) => {
valArray.push(getValue(val));
});
labelMap.value = valArray.join(',');
} else {
labelMap.value = getValue(value);
}
break;
case 'Cascader':
break;
case 'DatePicker':
case 'MapSelect':
case 'InputNumber':
case 'Input':
case 'Input.TextArea':
case 'UploadFiles':
labelMap.value = value;
break;
case 'DatePicker.RangePicker':
if (value && value.length > 0) {
const start = value[0] ? moment(value[0] * 1000).format(format[0]) : '';
const end = value[1] ? moment(value[1] * 1000).format(format[1]) : '';
labelMap.value = `${start}~${end}`;
} else {
labelMap.value = undefined;
}
break;
default:
labelMap.value = getValue(value);
}
return labelMap;
};
const getPopupValuesLabel = (field: any, searchLabels: SearchChooseProps[]) => {
const { props } = field;
const { name } = props;
const searchLableMap: any = {};
const changeLabel = getValueLable(field);
if (searchLabels.length > 0) {
searchLabels.map((label) => {
if (name == label.name) {
if (changeLabel && changeLabel.value) {
searchLableMap[label.name] = changeLabel;
} else {
delete searchLableMap[label.name];
}
} else {
searchLableMap[label.name] = label;
}
});
}
if (changeLabel && changeLabel.value) {
searchLableMap[name] = changeLabel;
}
return searchLableMap;
}
const SearchWrap: React.FC<SearchWrapProps> = (props) => {
const {
leftSectionComponents = [],
searchInput,
popupContent,
searchChange,
moreCategories = false,
ProForm,
getFormInstance,
} = props;
const [isPopupVisible, setPopupVisible] = useState(false);
const [open, setOpen] = useState(false);
const [searchLabels, setSearchLabels] = useState<SearchChooseProps[]>([]);
const [showPopupValue, setShowPopupValue] = useState<boolean>(false);
const [centerWidth, setCenterWidth] = useState<number>(0);
const valueChange = useDebounce((value: any) => {
searchChange && searchChange(value);
}, DEBOUNCE_DELAY);
const form = ProForm ? ProForm : useMemo(() => createForm(), []);
useEffect(() => {
if (searchLabels && searchLabels.length === 0) {
setShowPopupValue(false);
}
}, [searchLabels]);
useEffect(() => {
const removeEffects = form.addEffects('popup_search', () => {
onFieldValueChange('*', (field) => {
const {
address: { entire = '' },
} = field;
if ((entire as string).indexOf('popup') === -1) {
searchChange && valueChange(form.values);
}
if ((entire as string).indexOf('popup') > -1) {
setSearchLabels((searchLabels) => {
const labelValues = getPopupValuesLabel(field, searchLabels);
return Object.values(labelValues);
});
}
});
});
return () => {
form.removeEffects(removeEffects);
};
}, [ProForm]);
useEffect(() => {
getFormInstance && getFormInstance(form);
}, [getFormInstance]);
useEffect(() => {
const leftWidth = document.querySelector('.left-section')?.clientWidth || 0;
const rightWidth = document.querySelector('.right-section')?.clientWidth || 0;
const clientWidth = document.querySelector('.ant-layout-content')?.clientWidth || 0;
const width = clientWidth - leftWidth - rightWidth - 85;
setCenterWidth(width);
}, []);
const togglePopup = () => {
setPopupVisible(!isPopupVisible);
setOpen(!open);
};
const submit = useCallback(() => {
const { values: allValue } = form;
searchChange && searchChange(allValue);
togglePopup();
setShowPopupValue(true);
}, []);
const closeLabel = useCallback((label: SearchChooseProps) => {
const { path } = label;
const field = form.query(path).take();
if (field && field.setValue) {
field.setValue(undefined);
}
const { values: allValue } = form;
searchChange && searchChange(allValue);
}, []);
const handleCancle = useCallback(() => {
form.reset('popup.*');
const { values: allValue } = form;
searchChange && searchChange(allValue);
togglePopup();
}, []);
const text = useCallback(() => {
return (
<div>
<VoidField name="popup">
{popupContent &&
popupContent.map((item, index) => {
return <div key={index}>{item}</div>;
})}
</VoidField>
<div className="btn">
<Button onClick={handleCancle}>重置</Button>
<Button style={{ marginLeft: 10 }} type="primary" onClick={submit}>
确定
</Button>
</div>
</div>
);
}, [popupContent]);
const labels = useCallback(() => {
// 左边显示操作6个搜索条件
const len = (leftSectionComponents && leftSectionComponents.length - 1) || 0;
return (
<div style={{ width: centerWidth, overflow: 'hidden' }}>
{showPopupValue &&
searchLabels &&
searchLabels.slice(0, MAX_COMPONENTS - len).map((item) => {
return (
<Tag key={item.name} closable onClose={() => closeLabel(item)}>
{item.value}
</Tag>
);
})}
</div>
);
}, [showPopupValue, centerWidth]);
return (
<FormProvider form={form}>
<div className="search-component">
<Space className="left-section">
{leftSectionComponents.slice(0, MAX_COMPONENTS).map((component) => (
<div key={component.key || component.props.id} className="left-section-component">
{component}
</div>
))}
{labels()}
</Space>
<div className="right-section">
<div className="section-search">{searchInput}</div>
{moreCategories && (
<Tooltip
placement="bottomRight"
overlayStyle={{ padding: 0 }}
color="#fff"
title={text}
overlayClassName="popups"
trigger="click"
zIndex={10}
open={open}
getPopupContainer={(triggerNode) => triggerNode.parentNode as any}
>
<div
style={{ cursor: 'pointer' }}
onClick={() => {
setOpen(!open);
}}
>
<span className="more">更多筛选</span>
<img className="img" src={ossAddress['searchMore']}></img>
</div>
</Tooltip>
)}
</div>
</div>
</FormProvider>
);
};
export default SearchWrap;