ant-design/components/form/demo/horizontal-form.md

97 lines
2.9 KiB
Markdown
Raw Normal View History

2015-07-07 11:25:16 +08:00
# 水平排列的表单
2015-06-15 20:24:01 +08:00
2015-10-29 08:41:51 +08:00
- order: 2
2015-06-15 20:24:01 +08:00
2015-10-25 11:35:03 +08:00
示例展示了如何通过使用 `Form.ValueMixin` 来获取和更新表单提交的数值。
**注意:** 1需要为每个输入控件声明 `name` 属性2ES6 语法 [不支持 `mixins`](https://facebook.github.io/react/docs/reusable-components.html#no-mixins)
2015-07-17 15:12:48 +08:00
2015-06-15 20:24:01 +08:00
---
````jsx
2015-10-29 08:53:54 +08:00
import {Form, Input, Button, Checkbox, Radio, Row, Col, message} from 'antd';
2015-10-29 08:41:51 +08:00
const FormItem = Form.Item;
const RadioGroup = Radio.Group;
2015-10-29 08:41:51 +08:00
const Demo = React.createClass({
2015-10-25 11:35:03 +08:00
mixins: [Form.ValueMixin],
getInitialState() {
return {
formData: {
userName: '大眼萌 minion',
password: undefined,
gender: 'male',
remark: undefined,
agreement: undefined,
}
};
},
handleSubmit(e) {
e.preventDefault();
message.success('收到表单值~~~ ' + JSON.stringify(this.state.formData, function(k, v) {
2015-10-27 19:23:45 +08:00
if (typeof v === 'undefined') {
return '';
}
return v;
}));
2015-10-25 11:35:03 +08:00
},
2015-10-09 15:44:10 +08:00
2015-10-25 11:35:03 +08:00
render() {
2015-10-29 08:41:51 +08:00
const formData = this.state.formData;
2015-10-25 11:35:03 +08:00
return (
<Form horizontal onSubmit={this.handleSubmit}>
2015-10-29 08:41:51 +08:00
<FormItem
2015-10-25 11:35:03 +08:00
label="用户名:"
2015-10-29 08:41:51 +08:00
labelCol={{span: 6}}
wrapperCol={{span: 6}}
2015-11-25 17:47:55 +08:00
required>
2015-10-29 08:41:51 +08:00
<p className="ant-form-text" id="userName" name="userName">大眼萌 minion</p>
</FormItem>
<FormItem
2015-10-27 19:23:45 +08:00
id="password"
2015-10-25 11:35:03 +08:00
label="密码:"
2015-10-29 08:41:51 +08:00
labelCol={{span: 6}}
wrapperCol={{span: 14}}
2015-11-25 17:47:55 +08:00
required>
2015-10-29 08:41:51 +08:00
<Input type="password" id="password" name="password" placeholder="请输入密码" value={formData.password} onChange={this.setValue.bind(this, 'password')} />
</FormItem>
<FormItem
2015-10-25 11:35:03 +08:00
label="您的性别:"
2015-10-29 08:41:51 +08:00
labelCol={{span: 6}}
wrapperCol={{span: 14}}
2015-11-25 17:47:55 +08:00
required>
<RadioGroup name="gender" value={formData.gender} onChange={this.setValue.bind(this, 'gender')} >
2015-10-25 11:35:03 +08:00
<Radio value="male">男的</Radio>
<Radio value="female">女的</Radio>
</RadioGroup>
2015-10-29 08:41:51 +08:00
</FormItem>
<FormItem
2015-10-27 19:23:45 +08:00
id="remark"
2015-10-25 11:35:03 +08:00
label="备注:"
2015-10-29 08:41:51 +08:00
labelCol={{span: 6}}
wrapperCol={{span: 14}}
2015-11-25 17:47:55 +08:00
required
help="随便写点什么">
2015-10-29 08:41:51 +08:00
<Input type="textarea" placeholder="随便写" id="remark" name="remark" value={formData.remark} onChange={this.setValue.bind(this, 'remark')} />
</FormItem>
<FormItem
wrapperCol={{span: 14, offset: 6}} >
2015-10-25 11:35:03 +08:00
<label>
<Checkbox name="agreement" value={formData.agreement} onChange={this.setValue.bind(this, 'agreement')} />同意
2015-10-25 11:35:03 +08:00
</label>
2015-10-29 08:41:51 +08:00
</FormItem>
<Row>
<Col span="16" offset="6">
2015-10-25 11:35:03 +08:00
<Button type="primary" htmlType="submit">确定</Button>
2015-10-29 08:41:51 +08:00
</Col>
</Row>
2015-10-25 11:35:03 +08:00
</Form>
);
}
});
2015-10-29 08:41:51 +08:00
ReactDOM.render(<Demo />, mountNode);
2015-06-15 20:24:01 +08:00
````