React實現(xiàn)復(fù)雜搜索表單的展開收起功能
給時間時間,讓過去過去。
上節(jié)我們寫過了【搜索】表單,以及查詢、重置功能。本節(jié)對于需要展開收起效果的查詢表單 進(jìn)行概述,主要涉及前端樣式知識。
樣式效果如下:
思路:在Search組件中定義兩個組件renderAdvancedForm,renderSimpleForm,其中renderSimpleForm中只有五個查詢選項,而在renderAdvancedForm包含所有的搜索選項。點擊'展開‘'收起‘按鈕調(diào)用onClick={toggleForm}切換form顯示樣式即可。
1. 寫renderSimpleForm和renderAdvancedForm
使用Col和Row進(jìn)行分行分塊,并注意為展開按鈕添加點擊事件。
const 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設(shè)置兩行,并在對應(yīng)位置空出收起按鈕,為收起按鈕添加點擊函數(shù)
const 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函數(shù)實現(xiàn)‘展開'‘收起'切換
const toggleForm = useCallback(() => { setExpandForm(!expandForm) }, [expandForm])
3.在search組件中按情況渲染表單效果
return ( <Card bordered={false}> <div className={styles.search}> {expandForm ? renderAdvancedForm : renderSimpleForm} </div> </Card> )
4.附全部search組件代碼
const 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="手機(jī)號" /> )} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('status', { initialValue: query.type === undefined ? '' : query.type.toString() })( <Select> <Option value={''} disabled> {' '} 實驗狀態(tài){' '} </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="手機(jī)號" /> )} </FormItem> </Col> <Col md={4} sm={24}> <FormItem label=""> {getFieldDecorator('status', { initialValue: query.type === undefined ? '' : query.type.toString() })( <Select> <Option value={''}> 實驗狀態(tài) </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> ) })
到此這篇關(guān)于React實現(xiàn)復(fù)雜搜索表單的展開-收起功能的文章就介紹到這了,更多相關(guān)React表單展開收起內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react router 4.0以上的路由應(yīng)用詳解
本篇文章主要介紹了react router 4.0以上的路由應(yīng)用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09react如何修改循環(huán)數(shù)組對象的數(shù)據(jù)
這篇文章主要介紹了react如何修改循環(huán)數(shù)組對象的數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12React 組件中的state和setState()你知道多少
這篇文章主要為大家詳細(xì)介紹了React組件中的state和setState(),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03