ant-design/components/form/demo/time-related-controls.md

134 lines
3.7 KiB
Markdown
Raw Normal View History

---
2016-10-31 15:42:57 +08:00
order: 6
title:
zh-CN: 时间类控件
en-US: Time-related Controls
---
## zh-CN
`antd@2.0` 之后,时间类组件的 `value` 改为 `moment` 类型,所以在提交前需要预处理。
## en-US
After `antd@2.0`, the `value` of time-related components had been changed to `moment`. So, we need to pre-process those values.
2017-02-13 10:55:53 +08:00
````jsx
2016-11-02 10:47:50 +08:00
import { Form, DatePicker, TimePicker, Button } from 'antd';
const FormItem = Form.Item;
const MonthPicker = DatePicker.MonthPicker;
const RangePicker = DatePicker.RangePicker;
class TimeRelatedForm extends React.Component {
handleSubmit = (e) => {
e.preventDefault();
this.props.form.validateFields((err, fieldsValue) => {
if (err) {
return;
}
// Should format date value before submit.
const rangeValue = fieldsValue['range-picker'];
const rangeTimeValue = fieldsValue['range-time-picker'];
const values = {
...fieldsValue,
'date-picker': fieldsValue['date-picker'].format('YYYY-MM-DD'),
'date-time-picker': fieldsValue['date-time-picker'].format('YYYY-MM-DD HH:mm:ss'),
'month-picker': fieldsValue['month-picker'].format('YYYY-MM'),
'range-picker': [rangeValue[0].format('YYYY-MM-DD'), rangeValue[1].format('YYYY-MM-DD')],
'range-time-picker': [
rangeTimeValue[0].format('YYYY-MM-DD HH:mm:ss'),
rangeTimeValue[1].format('YYYY-MM-DD HH:mm:ss'),
],
'time-picker': fieldsValue['time-picker'].format('HH:mm:ss'),
};
console.log('Received values of form: ', values);
});
}
render() {
const { getFieldDecorator } = this.props.form;
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 8 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 16 },
},
};
const config = {
rules: [{ type: 'object', required: true, message: 'Please select time!' }],
};
const rangeConfig = {
rules: [{ type: 'array', required: true, message: 'Please select time!' }],
};
return (
2017-01-05 16:57:41 +08:00
<Form onSubmit={this.handleSubmit}>
<FormItem
{...formItemLayout}
label="DatePicker"
>
{getFieldDecorator('date-picker', config)(
<DatePicker />
)}
</FormItem>
<FormItem
{...formItemLayout}
label="DatePicker[showTime]"
>
2017-02-23 11:45:48 +08:00
{getFieldDecorator('date-time-picker', config)(
<DatePicker showTime format="YYYY-MM-DD HH:mm:ss" />
)}
</FormItem>
<FormItem
{...formItemLayout}
label="MonthPicker"
>
2017-02-23 11:45:48 +08:00
{getFieldDecorator('month-picker', config)(
<MonthPicker />
)}
</FormItem>
<FormItem
{...formItemLayout}
label="RangePicker"
>
2017-02-23 11:45:48 +08:00
{getFieldDecorator('range-picker', rangeConfig)(
<RangePicker />
)}
</FormItem>
<FormItem
{...formItemLayout}
label="RangePicker[showTime]"
>
2017-02-23 11:45:48 +08:00
{getFieldDecorator('range-time-picker', rangeConfig)(
<RangePicker showTime format="YYYY-MM-DD HH:mm:ss" />
)}
</FormItem>
<FormItem
{...formItemLayout}
label="TimePicker"
>
2017-02-23 11:45:48 +08:00
{getFieldDecorator('time-picker', config)(
<TimePicker />
)}
</FormItem>
<FormItem
wrapperCol={{
xs: { span: 24, offset: 0 },
sm: { span: 16, offset: 8 },
}}
>
2017-07-16 15:41:51 +08:00
<Button type="primary" htmlType="submit">Submit</Button>
</FormItem>
</Form>
);
}
}
const WrappedTimeRelatedForm = Form.create()(TimeRelatedForm);
ReactDOM.render(<WrappedTimeRelatedForm />, mountNode);
````