Ant Design 組件提供了Input,InputNumber,Radio,Select,uplod等表單組件,但實(shí)際開發(fā)中這是不能滿足需求,同時(shí)我們希望可以繼續(xù)使用Form提供的驗(yàn)證和提示等方法(使用起來確實(shí)很爽),這時(shí)需要自己動(dòng)手封裝一些表單,同時(shí)我們還要保持方法可以繼續(xù)是使用。

組件的源碼 https://github.com/haozhaohang/ant-design-expand-component
下面看一下如何自己封裝表單組件,這是一個(gè)最基礎(chǔ)的表單使用例子:
import React, { PureComponent } from 'react'
import { Button, Form, Input, Radio } from 'antd'
import FormItem from 'components/FormItem'
const RadioGroup = Radio.Group
const options = [
{ label: '男', value: 1 },
{ label: '女', value: 2 },
]
class Test extends PureComponent {
handleSubmit = (e) => {
e.preventDefault();
const { form: { validateFields } } = this.props;
validateFields((errors, values) => {
if (errors) {
return;
}
console.log(values)
})
}
render() {
const { form: { getFieldDecorator } } = this.props
const nameDecorator = getFieldDecorator('name')
const sexDecorator = getFieldDecorator('sex')
return (
<section>
<Form layout="horizontal" onSubmit={this.handleSubmit}>
<FormItem label="姓名">
{nameDecorator(<Input />)}
</FormItem>
<FormItem label="年齡">
{sexDecorator(<RadioGroup options={options} />)}
</FormItem>
<FormItem buttonsContainer>
<Button type="primary" size="default" htmlType="submit">提交</Button>
</FormItem>
</Form>
</section>
);
}
}
export default Form.create()(Test)
分享標(biāo)題:Android實(shí)現(xiàn)AntDesign自定義表單組件-創(chuàng)新互聯(lián)
分享URL:http://www.chinadenli.net/article16/deesgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、自適應(yīng)網(wǎng)站、定制開發(fā)、服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容