2016-10-28 11:29:26 +08:00
|
|
|
---
|
2016-10-31 15:42:57 +08:00
|
|
|
order: 6
|
2016-10-28 11:29:26 +08:00
|
|
|
title:
|
|
|
|
zh-CN: 时间类控件
|
|
|
|
en-US: Time-related Controls
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
2018-09-12 21:43:01 +08:00
|
|
|
时间类组件的 `value` 类型为 `moment` 对象,所以在提交服务器前需要预处理。
|
2016-10-28 11:29:26 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2018-09-12 21:43:01 +08:00
|
|
|
The `value` of time-related components is a `moment` object, which we need to pre-process it before we submit to server.
|
2016-10-28 11:29:26 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
|
|
|
import { Form, DatePicker, TimePicker, Button } from 'antd';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2018-09-12 21:43:01 +08:00
|
|
|
const { MonthPicker, RangePicker } = DatePicker;
|
2016-10-28 11:29:26 +08:00
|
|
|
|
2017-02-20 11:50:57 +08:00
|
|
|
class TimeRelatedForm extends React.Component {
|
2019-05-07 14:57:32 +08:00
|
|
|
handleSubmit = e => {
|
2016-10-28 11:29:26 +08:00
|
|
|
e.preventDefault();
|
|
|
|
|
2016-11-01 10:11:02 +08:00
|
|
|
this.props.form.validateFields((err, fieldsValue) => {
|
|
|
|
if (err) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-28 11:29:26 +08:00
|
|
|
|
2016-11-01 10:11:02 +08:00
|
|
|
// 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);
|
|
|
|
});
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2016-10-28 11:29:26 +08:00
|
|
|
render() {
|
|
|
|
const { getFieldDecorator } = this.props.form;
|
|
|
|
const formItemLayout = {
|
2017-03-15 11:25:58 +08:00
|
|
|
labelCol: {
|
|
|
|
xs: { span: 24 },
|
|
|
|
sm: { span: 8 },
|
|
|
|
},
|
|
|
|
wrapperCol: {
|
|
|
|
xs: { span: 24 },
|
|
|
|
sm: { span: 16 },
|
|
|
|
},
|
2016-11-01 10:11:02 +08:00
|
|
|
};
|
|
|
|
const config = {
|
|
|
|
rules: [{ type: 'object', required: true, message: 'Please select time!' }],
|
|
|
|
};
|
|
|
|
const rangeConfig = {
|
|
|
|
rules: [{ type: 'array', required: true, message: 'Please select time!' }],
|
2016-10-28 11:29:26 +08:00
|
|
|
};
|
|
|
|
return (
|
2019-02-25 19:04:09 +08:00
|
|
|
<Form {...formItemLayout} onSubmit={this.handleSubmit}>
|
2019-05-07 14:57:32 +08:00
|
|
|
<Form.Item label="DatePicker">
|
|
|
|
{getFieldDecorator('date-picker', config)(<DatePicker />)}
|
2018-12-22 16:48:30 +08:00
|
|
|
</Form.Item>
|
2019-05-07 14:57:32 +08:00
|
|
|
<Form.Item label="DatePicker[showTime]">
|
2017-02-23 11:45:48 +08:00
|
|
|
{getFieldDecorator('date-time-picker', config)(
|
2019-05-07 14:57:32 +08:00
|
|
|
<DatePicker showTime format="YYYY-MM-DD HH:mm:ss" />,
|
2016-11-01 10:11:02 +08:00
|
|
|
)}
|
2018-12-22 16:48:30 +08:00
|
|
|
</Form.Item>
|
2019-05-07 14:57:32 +08:00
|
|
|
<Form.Item label="MonthPicker">
|
|
|
|
{getFieldDecorator('month-picker', config)(<MonthPicker />)}
|
2018-12-22 16:48:30 +08:00
|
|
|
</Form.Item>
|
2019-05-07 14:57:32 +08:00
|
|
|
<Form.Item label="RangePicker">
|
|
|
|
{getFieldDecorator('range-picker', rangeConfig)(<RangePicker />)}
|
2018-12-22 16:48:30 +08:00
|
|
|
</Form.Item>
|
2019-05-07 14:57:32 +08:00
|
|
|
<Form.Item label="RangePicker[showTime]">
|
2017-02-23 11:45:48 +08:00
|
|
|
{getFieldDecorator('range-time-picker', rangeConfig)(
|
2019-05-07 14:57:32 +08:00
|
|
|
<RangePicker showTime format="YYYY-MM-DD HH:mm:ss" />,
|
2016-11-01 10:11:02 +08:00
|
|
|
)}
|
2018-12-22 16:48:30 +08:00
|
|
|
</Form.Item>
|
2019-05-07 14:57:32 +08:00
|
|
|
<Form.Item label="TimePicker">
|
|
|
|
{getFieldDecorator('time-picker', config)(<TimePicker />)}
|
2018-12-22 16:48:30 +08:00
|
|
|
</Form.Item>
|
|
|
|
<Form.Item
|
2017-03-15 11:25:58 +08:00
|
|
|
wrapperCol={{
|
|
|
|
xs: { span: 24, offset: 0 },
|
|
|
|
sm: { span: 16, offset: 8 },
|
|
|
|
}}
|
2017-03-02 10:49:06 +08:00
|
|
|
>
|
2019-05-07 14:57:32 +08:00
|
|
|
<Button type="primary" htmlType="submit">
|
|
|
|
Submit
|
|
|
|
</Button>
|
2018-12-22 16:48:30 +08:00
|
|
|
</Form.Item>
|
2016-10-28 11:29:26 +08:00
|
|
|
</Form>
|
|
|
|
);
|
2017-02-20 11:50:57 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 11:29:26 +08:00
|
|
|
|
2018-12-17 10:02:08 +08:00
|
|
|
const WrappedTimeRelatedForm = Form.create({ name: 'time_related_controls' })(TimeRelatedForm);
|
2017-02-20 11:50:57 +08:00
|
|
|
|
|
|
|
ReactDOM.render(<WrappedTimeRelatedForm />, mountNode);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|