Created
January 16, 2019 23:46
-
-
Save OneOfOne/7e13e1438503078fba61ea55e8bc48af to your computer and use it in GitHub Desktop.
ant.design getFieldDecorator wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { Input, Checkbox, Select } from 'antd'; | |
const FormField = (props) => { | |
let { hidden, form, name, required = false, message = '', valuePropName = 'value', initialValue, type, ...rest } = props, | |
{ getFieldDecorator } = form; | |
if (hidden) return null; | |
let Child = null; | |
switch (type) { | |
case 'checkbox': | |
Child = Checkbox; | |
valuePropName = 'checked'; | |
break; | |
case 'select': | |
Child = Select; | |
break; | |
case 'input': | |
Child = Input; | |
break; | |
default: | |
if (!['object', 'function'].includes(typeof type)) throw new Error('bad type: ' + type); | |
Child = type; | |
} | |
return getFieldDecorator(name, { | |
rules: [{ required, message }], | |
valuePropName, | |
initialValue, | |
})(<Child {...rest} />); | |
}; | |
export default FormField; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment