Skip to content

Instantly share code, notes, and snippets.

@xfoxfu
Created March 24, 2018 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xfoxfu/e76db4122257e5eb5a7877f1565d9606 to your computer and use it in GitHub Desktop.
Save xfoxfu/e76db4122257e5eb5a7877f1565d9606 to your computer and use it in GitHub Desktop.
import * as React from 'react';
import { Form, Input } from 'antd';
const FormItem = Form.Item;
import { FormComponentProps } from 'antd/lib/form/Form';
class LoginForm extends React.Component<FormComponentProps, any> {
render() {
const { getFieldDecorator } = this.props.form;
return (
<Form layout="inline">
<FormItem label="Username">
{getFieldDecorator('username', {
rules: [{ required: true, message: 'Username is required!' }],
})(<Input />)}
</FormItem>
</Form>
);
}
}
const CustomizedForm = Form.create<{ username: any }>({
onFieldsChange(props, changedFields) {
props.onChange(changedFields);
},
mapPropsToFields(props) {
return {
username: Form.createFormField({
...props.username,
value: props.username.value,
}),
};
},
onValuesChange(_, values) {
console.log(values);
},
})(LoginForm);
class Demo extends React.Component {
state = {
fields: {
username: {
value: 'benjycui',
},
},
};
handleFormChange = (changedFields: any) => {
this.setState({
fields: { ...this.state.fields, ...changedFields },
});
}
render() {
const fields = this.state.fields;
return (
<div>
<CustomizedForm {...fields} onChange={this.handleFormChange} />
<pre className="language-bash">
{JSON.stringify(fields, null, 2)}
</pre>
</div>
);
}
}
export default Demo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment