一、構造組件
1、表單一定會包含表單域,表單域可以是輸入控件,標準表單域,標簽,下拉菜單,文本域等。
這里先引用了封裝的表單域 <Form.Item />
2、使用Form.create處理后的表單具有自動收集數據并校驗的功能,但如果不需要這個功能,或者默認的行為無法滿足業務需求,可以選擇不使用Form.create并自行處理數據
經過Form.create()包裝過的組件會自帶this.props.form屬性,this.props.form提供了很多API來處理數據,如getFieldDecorator——用于和表單進行雙向綁定等,詳細參加Antd官方文檔:點擊此處查看
先展示表單樣式:
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
|
import {Form, Table, Button, Select, Input, DatePicker} from 'antd' ; const FormItem = Form.Item; const Option = Select.Option; const {RangePicker} = DatePicker; //獲取日期選擇控件中的日期范圍控件 class UserManage extends React.Component { render() { const columns = [ { title: '聯系人' , dataIndex: 'userName' , key: 'userName' , }, { title: '手機號' , dataIndex: 'mobile' , key: 'mobile' , }, { title: '公司名稱' , dataIndex: 'companyName' , key: 'companyName' , }, { title: '最近活躍時間' , dataIndex: 'lastOnlineTime' , key: 'lastOnlineTime' , }, { title: '禁言狀態' , dataIndex: 'status' , key: 'status' , }, ]; return ( <div> <Form layout= "inline" style={{marginBottom: '10px' }}> <FormItem label= "最近活躍時間" > <RangePicker style={{width: '255px' }}/> </FormItem> <FormItem label= "用戶" > <Input type= "text" placeholder= "公司名稱、手機號" style={{width: '155px' }}/> </FormItem> <FormItem label= "禁言狀態" > <Select defaultValue= "全部" style={{width: '155px' }}> <Option value= "全部" >全部</Option> <Option value= "是" >是</Option> <Option value= "否" >否</Option> </Select> </FormItem> <Button type= "primary" style={{marginTop: '3px' , marginRight: '3px' }}>查詢</Button> <Button style={{marginTop: '3px' }}>重置</Button> </Form> <Table columns={columns} /> </div> ) } } export default Form.create()(UserManage) |
colums是Table組件的API,columns和Column組件使用相同的API:
dataIndex:列數據在數據項中對應的 key,支持a.b.c的嵌套寫法
key:React 需要的 key,如果已經設置了唯一的dataIndex,可以忽略這個屬性
二、使用getFieldDecorator(id, options) 進行表單交互
1、現在的問題就是如何獲取各種查詢條件的數據,所以先改寫render()里面的代碼,getFieldDecorator用于和表單進行雙向綁定:
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
|
... render(){ const {form} = this.props; const {getFieldDecorator} = form; ... return ( < div > < Form onSubmit={this.handleQuery} layout = "inline" style={{marginBottom: '10px'}}> < FormItem label = "最近活躍時間" > {getFieldDecorator('lastOnlineTime')(< RangePicker style={{width: '255px'}}/>)} </ FormItem > < FormItem label = "用戶" > {getFieldDecorator('userQueryLike')(< Input type = "text" placeholder = "公司名稱或手機號" style={{width: '155px'}}/>)} </ FormItem > < FormItem label = "禁言狀態" > {getFieldDecorator('status', {initialValue: "全部"})( < Select style={{width: '155px'}}> < Option value = "0" >全部</ Option > < Option value = "1" >是</ Option > < Option value = "2" >否</ Option > </ Select >)} </ FormItem > < Button type = "primary" htmlType = "submit" style={{marginTop: '3px', marginRight: '3px'}}>查詢</ Button > < Button style={{marginTop: '3px'}}>重置</ Button > </ Form > < Table columns={columns} /*dataSource={(從model取得的數據)}*/ /> </ div > ) } ... |
參數 | 說明 | 類型 | 默認值 |
---|---|---|---|
id | 必填輸入控件唯一標志。支持嵌套式的寫法。 | string | |
options.getValueFromEvent | 可以把 onChange 的參數(如 event)轉化為控件的值 | function(..args) | reference |
options.initialValue |
子節點的初始值,類型、可選值均由子節點決定(注意:由于內部校驗時使用 === 判斷是否變化,建議使用變量緩存所需設置的值而非直接使用字面量)) |
||
options.normalize | 轉換默認的 value 給控件 | function(value, prevValue, allValues): any | - |
options.rules | 校驗規則,詳細參考Antd官方文檔 | object[] | |
options.trigger | 收集子節點的值的時機 | string | 'onChange' |
options.validateFirst | 當某一規則校驗不通過時,是否停止剩下的規則的校驗 | boolean | false |
options.validateTrigger | 校驗子節點值的時機 | string|string[] | 'onChange' |
options.valuePropName | 子節點的值的屬性,如 Switch 的是 'checked' | string | 'value' |
2、上面給了表單一個onSubmit事件,當表單提交時執行handleQuery方法:
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
|
... class UserManage extends React.Component { //表單查詢 handleQuery = (e) => { if (e) e.preventDefault(); const {dispatch, form} = this .props; form.validateFields((err, fieldsValue) => { if (err) return ; //獲取時間范圍的值 const rangeValue = fieldsValue[ 'lastOnlineTime' ]; const userQueryLike = fieldsValue[ 'userQueryLike' ]; //獲取查詢條件 const values = { ...fieldsValue, "lastOnlineTime" : (rangeValue && rangeValue.length > 1) ? ([rangeValue[0].format( 'YYYY-MM-DD' ), rangeValue[1].format( 'YYYY-MM-DD' )]) : null , "userQueryLike" : userQueryLike ? userQueryLike.trim() : userQueryLike, }; dispatch({ type: "userManageModel/getUserList" , payload: { values: values, } }); }); }; ... } ... |
在此方法里又調用了form.validateFields校驗并獲取一組輸入域的值與Error,入參fieldsValue就是從表單的FormItem里取到的值,然后使用fieldsValue['lastOnlineTime']這種形式,通過與之前寫的getFieldDecorator('lastOnlineTime')產生映射,就獲取了單個輸入域的值。
總結一下,使用React的Form實現表單功能,必須要使用Form.create(組件),使包裝的組件帶有this.props.form屬性,才能調用form的getFieldDecorator和validateFields方法,getFieldDecorator中的id對應validateFields中的fieldsValue[''];而columns中的dateIndex對應的是從model取到數據json串的鍵名,這個要分清
除了這種方法,還有兩種實現獲取input輸入框的值然后提交的方法,可以看這篇文章:React獲取input的值并提交的兩種方法
總結
到此這篇關于React如何利用Antd的Form組件實現表單功能詳解的文章就介紹到這了,更多相關React用Form組件實現表單內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/GuanJdoJ/article/details/83306931