给时间时间,让过去过去。
上节我们写过了【搜索】表单,以及查询、重置功能。本节对于需要展开收起效果的查询表单 进行概述,主要涉及前端样式知识。
样式效果如下:
思路:在Search组件中定义两个组件renderAdvancedForm,renderSimpleForm,其中renderSimpleForm中只有五个查询选项,而在renderAdvancedForm包含所有的搜索选项。点击'展开‘'收起‘按钮调用onClick={toggleForm}切换form显示样式即可。
1. 写renderSimpleForm和renderAdvancedForm
使用Col和Row进行分行分块,并注意为展开按钮添加点击事件。
复制代码
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
42const renderSimpleForm = useMemo(() => { const { getFieldDecorator } = form const { query } = getLocation() return ( <Form layout="inline"> <Row> <Col md={4} sm={24}> <FormItem label="">...</FormItem> </Col> <Col md={4} sm={24}> <FormItem label="">...</FormItem> </Col> <Col md={4} sm={24}> <FormItem label="">...</FormItem> </Col> <Col md={4} sm={24}> <FormItem label="">...</FormItem> </Col> <Col md={4} sm={24}> <FormItem label="">...</FormItem> </Col> <Col md={4} sm={24} style={{ textAlign: 'right' }}> <a onClick={toggleForm} style={{ marginRight: '15px' }} className={styles.a} > 展开 <Icon type="down" /> </a> <Button onClick={handleSearch} className={'searchBtn'}> <img src={search} alt="" /> 查询 </Button> <Button onClick={handleFormReset} className={'resetBtn'}> <img src={reset} alt="" /> 重置 </Button> </Col> </Row> </Form> ) }, [form, handleFormReset, handleSearch, toggleForm])
同理,需要使用Rol和Row设置两行,并在对应位置空出收起按钮,为收起按钮添加点击函数
复制代码
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
38const renderAdvancedForm = useMemo(() => { const { getFieldDecorator, getFieldValue } = form const { query } = getLocation() return ( <Form layout="inline"> <Row style={{ marginBottom: '20px' }}> <Col md={4} sm={24}><FormItem label="">...</FormItem></Col> <Col md={4} sm={24}><FormItem label="">...</FormItem></Col> <Col md={4} sm={24}><FormItem label="">...</FormItem></Col> <Col md={4} sm={24}><FormItem label="">...</FormItem></Col> <Col md={4} sm={24}><FormItem label="">...</FormItem></Col> <Col md={4} sm={24} style={{ textAlign: 'right' }}> <a onClick={toggleForm} style={{ marginRight: '15px' }} className={styles.a} > 收起 <Icon type="up" /> </a> <Button onClick={handleSearch} className={'searchBtn'}> <img src={search} alt="" /> 查询 </Button> <Button onClick={handleFormReset} className={'resetBtn'}> <img src={reset} alt="" /> 重置 </Button> </Col> </Row> <Row> <Col md={4} sm={24}><FormItem label="">...</FormItem></Col> <Col md={4} sm={24}><FormItem label="">...</FormItem></Col> <Col md={4} sm={24}><FormItem label="">...</FormItem></Col> <Col md={4} sm={24}><FormItem label="">...</FormItem></Col> </Row> </Form> ) }, [form, handleFormReset, handleSearch, time1, time2, toggleForm])
2.添加toggleForm函数实现‘展开'‘收起'切换
复制代码
1
2
3const toggleForm = useCallback(() => { setExpandForm(!expandForm) }, [expandForm])
3.在search组件中按情况渲染表单效果
复制代码
1
2
3
4
5
6
7return ( <Card bordered={false}> <div className={styles.search}> {expandForm ? renderAdvancedForm : renderSimpleForm} </div> </Card> )
4.附全部search组件代码
复制代码
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
266const Search: any = Form.create()(function({ form, init }: any) { const { validateFields } = form const [expandForm, setExpandForm] = useState(false) const [time11, settime11] = useState('') const [time21, settime21] = useState('') const [time1, settime1] = useState(moment().format('YYYY-MM-DD')) const [time2, settime2] = useState(moment().format('YYYY-MM-DD')) const handleSearch = useCallback(() => { validateFields((err: any, data: any) => { pushPath({ query: { ...data, pageNum: 1, orderTimeStart: time11, orderTimeEnd: time21, orderNumber: data.orderNumber.replace(/s+/g, ''), experimentName: data.experimentName.replace(/s+/g, ''), userName: data.userName.replace(/s+/g, ''), mobile: data.mobile.replace(/s+/g, ''), priceLow: data.priceLow.replace(/s+/g, ''), priceHigh: data.priceHigh.replace(/s+/g, '') } }) init() }) }, [init, time11, time21, validateFields]) const handleFormReset = useCallback(() => { clearPath() pushPath({ query: { pageSize: 10, pageNum: 1 } }) init() form.resetFields() }, [form, init]) const toggleForm = useCallback(() => { setExpandForm(!expandForm) }, [expandForm]) const renderSimpleForm = useMemo(() => { const { getFieldDecorator } = form const { query } = getLocation() return ( <Form layout="inline"> <Row> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('orderNumber', { initialValue: query.name || '' })(<Input placeholder="需求编号" />)} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('experimentName', { initialValue: query.name || '' })(<Input placeholder="需求名称" />)} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('userName', { initialValue: query.name || '' })(<Input placeholder="用户名" />)} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('mobile', { initialValue: query.name || '' })( <Input placeholder="手机号" /> )} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('status', { initialValue: query.type === undefined ? '' : query.type.toString() })( <Select> <Option value={''} disabled> {' '} 实验状态{' '} </Option> {testStatus.map((v: any) => ( <Option key={v.key} value={v.key}> {v.value} </Option> ))} </Select> )} </FormItem> </Col> <Col md={4} sm={24} style={{ textAlign: 'right' }}> <a onClick={toggleForm} style={{ marginRight: '15px' }} className={styles.a} > 展开 <Icon type="down" /> </a> <Button onClick={handleSearch} className={'searchBtn'}> <img src={search} alt="" /> 查询 </Button> <Button onClick={handleFormReset} className={'resetBtn'}> <img src={reset} alt="" /> 重置 </Button> </Col> </Row> </Form> ) }, [form, handleFormReset, handleSearch, toggleForm]) const renderAdvancedForm = useMemo(() => { const { getFieldDecorator, getFieldValue } = form const { query } = getLocation() function disabledDate1(current: any) { return current && current > time2 } function disabledDate2(current: any) { return current && current < time1 } function change1(date: any, dateString: any) { settime1(date) settime11(dateString) } function change2(date: any, dateString: any) { settime2(date) settime21(dateString) } const dataValidate = (rule: any, value: any, callback: any) => { if (value && parseInt(value) > parseInt(getFieldValue('priceHigh'))) { callback('不能高于最高值') } else if ( value && parseInt(value) < parseInt(getFieldValue('priceLow')) ) { callback('不能低于最低值') } else { callback() } } return ( <Form layout="inline"> <Row style={{ marginBottom: '20px' }}> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('orderNumber', { initialValue: query.name || '' })(<Input placeholder="需求编号" />)} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('experimentName', { initialValue: query.name || '' })(<Input placeholder="需求名称" />)} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('userName', { initialValue: query.name || '' })(<Input placeholder="用户名" />)} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('mobile', { initialValue: query.name || '' })( <Input placeholder="手机号" /> )} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('status', { initialValue: query.type === undefined ? '' : query.type.toString() })( <Select> <Option value={''}> 实验状态 </Option> {testStatus.map((v: any) => ( <Option key={v.key} value={v.key}> {v.value} </Option> ))} </Select> )} </FormItem> </Col> <Col md={4} sm={24} style={{ textAlign: 'right' }}> <a onClick={toggleForm} style={{ marginRight: '15px' }} className={styles.a} > 收起 <Icon type="up" /> </a> <Button onClick={handleSearch} className={'searchBtn'}> <img src={search} alt="" /> 查询 </Button> <Button onClick={handleFormReset} className={'resetBtn'}> <img src={reset} alt="" /> 重置 </Button> </Col> </Row> <Row> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('priceLow', { initialValue: query.name || '', rules: [{ validator: dataValidate }] })(<Input placeholder="总价范围" />)} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('priceHigh', { initialValue: query.name || '', rules: [{ validator: dataValidate }] })(<Input placeholder="总价范围" />)} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('orderTimeStart', { initialValue: query.name || '' })( <DatePicker onChange={change1} disabledDate={disabledDate1} placeholder="下单时间" /> )} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('orderTimeEnd', { initialValue: query.name || '' })( <DatePicker onChange={change2} disabledDate={disabledDate2} placeholder="下单时间" /> )} </FormItem> </Col> </Row> </Form> ) }, [form, handleFormReset, handleSearch, time1, time2, toggleForm]) return ( <Card bordered={false}> <div className={styles.search}> {expandForm ? renderAdvancedForm : renderSimpleForm} </div> </Card> ) })
到此这篇关于React实现复杂搜索表单的展开-收起功能的文章就介绍到这了,更多相关React表单展开收起内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!
最后
以上就是长情小霸王最近收集整理的关于React实现复杂搜索表单的展开收起功能的全部内容,更多相关React实现复杂搜索表单内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复