2019-05-17 12:05:03 +08:00
|
|
|
---
|
|
|
|
order: 2
|
|
|
|
title:
|
|
|
|
zh-CN: 配合 Form 使用
|
|
|
|
en-US: With Form
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
受控模式,例如配合 Form 使用。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Controlled mode, for example, to work with `Form`.
|
|
|
|
|
|
|
|
```jsx
|
|
|
|
import { Mentions, Form, Button } from 'antd';
|
|
|
|
|
|
|
|
const { Option, getMentions } = Mentions;
|
|
|
|
|
|
|
|
class App extends React.Component {
|
|
|
|
handleReset = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.props.form.resetFields();
|
|
|
|
};
|
|
|
|
|
|
|
|
handleSubmit = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
this.props.form.validateFields((errors, values) => {
|
|
|
|
if (errors) {
|
|
|
|
console.log('Errors in the form!!!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log('Submit!!!');
|
|
|
|
console.log(values);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
checkMention = (rule, value, callback) => {
|
|
|
|
const mentions = getMentions(value);
|
|
|
|
if (mentions.length < 2) {
|
|
|
|
callback(new Error('More than one must be selected!'));
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
form: { getFieldDecorator },
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form layout="horizontal">
|
|
|
|
<Form.Item label="Top coders" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
|
2019-08-26 16:44:36 +08:00
|
|
|
{getFieldDecorator('coders', {
|
2019-05-17 12:05:03 +08:00
|
|
|
rules: [{ validator: this.checkMention }],
|
|
|
|
})(
|
2019-08-26 16:44:36 +08:00
|
|
|
<Mentions rows="1">
|
|
|
|
<Option value="afc163">afc163</Option>
|
|
|
|
<Option value="zombieJ">zombieJ</Option>
|
|
|
|
<Option value="yesmeck">yesmeck</Option>
|
|
|
|
</Mentions>,
|
|
|
|
)}
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Bio" labelCol={{ span: 6 }} wrapperCol={{ span: 16 }}>
|
|
|
|
{getFieldDecorator('bio', {
|
|
|
|
rules: [{ required: true }],
|
|
|
|
})(
|
|
|
|
<Mentions rows="3" placeholder="You can use @ to ref user here">
|
2019-05-17 12:05:03 +08:00
|
|
|
<Option value="afc163">afc163</Option>
|
|
|
|
<Option value="zombieJ">zombieJ</Option>
|
|
|
|
<Option value="yesmeck">yesmeck</Option>
|
|
|
|
</Mentions>,
|
|
|
|
)}
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item wrapperCol={{ span: 14, offset: 6 }}>
|
|
|
|
<Button type="primary" onClick={this.handleSubmit}>
|
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button onClick={this.handleReset}>Reset</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const FormDemo = Form.create()(App);
|
|
|
|
|
|
|
|
ReactDOM.render(<FormDemo />, mountNode);
|
|
|
|
```
|