Skip to content

Instantly share code, notes, and snippets.

@OneOfOne
Created January 16, 2019 23:46
Show Gist options
  • Save OneOfOne/7e13e1438503078fba61ea55e8bc48af to your computer and use it in GitHub Desktop.
Save OneOfOne/7e13e1438503078fba61ea55e8bc48af to your computer and use it in GitHub Desktop.
ant.design getFieldDecorator wrapper
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