-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
285 lines (272 loc) · 7.56 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
import React, { memo, useRef, useState } from "react"
import { Cascader, DatePicker, Form, Input, Select } from "antd"
import { useDeepCompareEffect } from "common-hook"
import { getFlatData } from "common-mid"
import { isArray, isNil, isObject, toEnumArray } from "common-screw"
// 生成 Select 的 options 属性
const toOptions = (
optionList: any,
placeholder: any,
allTip: any,
isAll: boolean = true
) => {
const arr: any = []
isAll && arr.push({ value: "", label: `${placeholder} - ${allTip}` })
if (isNil(optionList)) {
return []
} else if (
isArray(optionList) &&
optionList[0].label &&
optionList[0].value
) {
arr.push(...optionList)
} else if (isObject(optionList)) {
arr.push(...toEnumArray(optionList, "value", "label"))
}
return arr
}
interface InputProps {
label?: string
name?: string | string[]
type?: any
hide?: boolean
itemProps?: any
optionList?: any
}
interface SearchProps {
language?: string // 语言
langList?: any // 语言包
onChange?: (values: object) => void
search?: Array<InputProps>
style?: any
className?: any
initialValues?: any
children?: any
searchIcon?: any
itemStyle?: any
extra?: any
}
const DEFAULT_LANG_LIST = {
"zh-CN": {
ALL_TIP: "全部",
TIME_START: "开始时间",
TIME_END: "结束时间"
},
"en-US": {
ALL_TIP: "All",
TIME_START: "Start Time",
TIME_END: "End Time"
}
}
/**
* @name 搜索栏
* @param {Object} 配置项
* @example
* <MidSearch className={styles.section} searchIcon={searchIcon} {...props} />
*/
export const MidSearch = memo((props: SearchProps) => {
const {
language,
langList = DEFAULT_LANG_LIST,
search,
onChange,
className,
style = {},
initialValues = {},
searchIcon,
itemStyle,
extra
} = props
const [form] = Form.useForm()
const FormRef: any = useRef(null)
const [renderItem, setRenderItem] = useState(<></>)
// 默认第一个语言包
const [LANG, setLANG] = useState(langList[Object.keys(langList)[0]])
useDeepCompareEffect(() => {
// 语言国际化 ,如果没有对应语言包,默认第一个语言包
const list = Object.keys(langList)
const e = language && list.includes(language) ? language : list[0]
setLANG(langList[e])
}, [langList, language])
useDeepCompareEffect(() => {
FormRef && FormRef.current && FormRef.current.resetFields()
}, [initialValues])
useDeepCompareEffect(() => {
// 渲染search
const content: any = search?.map((item: any, index: any) => {
const { hide = false } = item
return !hide && formInputRender(item, itemStyle, index)
})
setRenderItem(content)
}, [search, LANG, itemStyle])
const formInputRender = (item: InputProps, itemStyle, index) => {
let elem: any = <></>
const { name, type, optionList, label, itemProps = {} } = item
const { defaultValue, ...restProps } = itemProps
switch (type) {
case "select":
elem = (
<Select
placeholder={label}
options={toOptions(optionList, label, LANG.ALL_TIP)}
allowClear
onChange={onValuesChange}
{...restProps}
/>
)
break
case "selectMul":
elem = (
<Select
placeholder={label}
options={toOptions(optionList, label, LANG.ALL_TIP, false)}
mode="multiple"
allowClear
onChange={onValuesChange}
showSearch={true}
filterOption={(input, option) =>
(option?.label ?? "").toLowerCase().includes(input.toLowerCase())
}
{...restProps}
/>
)
break
case "search":
elem = (
<Input
placeholder={label}
allowClear
onChange={({ target }) => {
!target.value && onValuesChange()
}}
prefix={searchIcon}
onPressEnter={onValuesChange}
{...restProps}
/>
)
break
case "dateRange":
elem = (
<DatePicker.RangePicker
onChange={onValuesChange}
placeholder={[LANG.TIME_START, LANG.TIME_END]}
style={{ width: "100%" }}
{...restProps}
/>
)
break
case "dateRangeNoTime":
elem = (
<DatePicker.RangePicker
onChange={onValuesChange}
placeholder={[LANG.TIME_START, LANG.TIME_END]}
style={{ width: "100%" }}
{...restProps}
/>
)
break
case "monthRange":
elem = (
<DatePicker.MonthPicker
onChange={onValuesChange}
placeholder={label}
showTime={false}
style={{ width: "100%" }}
{...restProps}
/>
)
break
case "cascader":
elem = (
<Cascader
options={optionList}
changeOnSelect
onChange={onValuesChange}
placeholder={label}
{...restProps}
/>
)
break
default:
}
return (
<Form.Item name={String(name)} key={index} style={itemStyle}>
{elem}
</Form.Item>
)
}
// 表单数据处理
const handleFields = (values: { [x: string]: any }) => {
let result: any = {}
const searchData = getFlatData(search)
Object.keys(values).forEach((name) => {
const item = searchData[name]
const type = item.type
const itemValue = values[name]
switch (type) {
case "dateRange":
// 日期
if (Array.isArray(itemValue) && itemValue.length === 2) {
result[item.name[0]] =
itemValue[0].format("YYYY-MM-DD") + " 00:00:00"
result[item.name[1]] =
itemValue[1].format("YYYY-MM-DD") + " 23:59:59"
} else {
// 没有值时,也要保留表单key
result[item.name[0]] = undefined
result[item.name[1]] = undefined
}
break
case "dateRangeNoTime":
// 日期 无时间
if (Array.isArray(itemValue) && itemValue.length === 2) {
result[item.name[0]] = itemValue[0].format("YYYY-MM-DD")
result[item.name[1]] = itemValue[1].format("YYYY-MM-DD")
} else {
// 没有值时,也要保留表单key
result[item.name[0]] = undefined
result[item.name[1]] = undefined
}
break
case "monthRange":
if (Array.isArray(itemValue) && itemValue.length === 2) {
result[item.name[0]] = itemValue[0].format("YYYY-MM")
result[item.name[1]] = itemValue[1].format("YYYY-MM")
} else {
// 没有值时,也要保留表单key
result[item.name[0]] = undefined
result[item.name[1]] = undefined
}
break
case "cascader":
// 级联选择
item.name?.forEach((childItem: any, childIndex: any) => {
result[childItem] = itemValue?.[childIndex] || undefined
})
break
default:
result[name] = itemValue
}
})
return result
}
const onValuesChange = () => {
const values = form.getFieldsValue()
onChange && onChange(handleFields(values))
}
return (
<div className={className} style={style}>
<Form
autoComplete="off"
layout="inline"
initialValues={initialValues}
ref={FormRef}
form={form}
>
{renderItem}
</Form>
{/* 右侧操作区域 */}
{extra}
</div>
)
})