网站网页设计收费,php 修改 wordpress,合肥 做网站的,做网络推广可以通过哪些渠道推广Ant Design 组件提供了Input#xff0c;InputNumber#xff0c;Radio#xff0c;Select#xff0c;uplod等表单组件#xff0c;但实际开发中这是不能满足需求#xff0c;同时我们希望可以继续使用Form提供的验证和提示等方法(使用起来确实很爽)#xff0c;这时需要自己动…Ant Design 组件提供了InputInputNumberRadioSelectuplod等表单组件但实际开发中这是不能满足需求同时我们希望可以继续使用Form提供的验证和提示等方法(使用起来确实很爽)这时需要自己动手封装一些表单同时我们还要保持方法可以继续是使用。下面看一下如何自己封装表单组件这是一个最基础的表单使用例子:import React, { PureComponent } from reactimport { Button, Form, Input, Radio } from antdimport FormItem from components/FormItemconst RadioGroup Radio.Groupconst 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.propsconst nameDecorator getFieldDecorator(name)const sexDecorator getFieldDecorator(sex)return ({nameDecorator()}{sexDecorator()}提交);}}export default Form.create()(Test)现在需求需要我们实现多个姓名的提交这时使用UI组件提供的表单便无法实现。下面我们可以封装一个InputArrary组件:import React, { PureComponent } from reactimport PropTypes from prop-typesimport { Button, Icon, Input } from antdimport ./index.scssclass InputArray extends PureComponent {constructor(props) {super(props)}handleChange index {const { value, onChange } this.propsconst newValue [...value]newValue[index] target.valueonChange(newValue)}handleDelete e {const target e.currentTargetconst index target.parentNode.parentNode.firstChild.dataset.indexconst { value, onChange } this.propsconst newValue [...value]newValue.splice(Number(index), 1)onChange(newValue)}handleAdd () {const { value, onChange } this.propsconst newValue [...value, ]onChange(newValue)}render() {const { value, ...others } this.propsconst closeBtn return ({value.map((v, i) {return ({...others}value{v}suffix{closeBtn}data-index{i}onChange{() this.handleChange(i)}/);})}添加);}}InputArray.defaultProps {value: []}export default InputArray这是我们就可以像引入Input组件一样引入InputArray组件实现了一组姓名的提交。{nameDecorator()}组件主要使用的form提供getFieldDecorator方法这个方法会向组件注入value参数onChange方法每次调用onChange方法都会去改变value从而刷新整个组件。为什么会这样那其实Ant Design 会在表单组件外层包裹一层组件维护一个State值每次onChange都是在改变外部state值调用setState来刷新表单组件。Upload组件使用中也遇到一个坑Upload组件action上传地址参数也是必填参数每次上传都会直接上传到服务器不能和其它表单的数据一起提交这时候我们也必须从新封装一个上传组件同时因为存在文件或图片数据就不能使用json格式和后台进行交互必须使用new FormData()的数据格式上传也就是原生的表单的submit提交。以上所述是小编给大家介绍的Android实现Ant Design 自定义表单组件希望对大家有所帮助如果大家有任何疑问请给我留言小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持