2019-07-03 20:14:39 +08:00
|
|
|
---
|
|
|
|
order: 16
|
|
|
|
title:
|
|
|
|
zh-CN: 时间类控件
|
|
|
|
en-US: Time-related Controls
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
时间类组件的 `value` 类型为 `moment` 对象,所以在提交服务器前需要预处理。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
The `value` of time-related components is a `moment` object, which we need to pre-process it before we submit to server.
|
|
|
|
|
|
|
|
```tsx
|
2022-05-21 22:14:15 +08:00
|
|
|
import { Button, DatePicker, Form, TimePicker } from 'antd';
|
2022-05-19 09:46:26 +08:00
|
|
|
import React from 'react';
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-07-23 11:25:17 +08:00
|
|
|
const { RangePicker } = DatePicker;
|
2019-07-03 20:14:39 +08:00
|
|
|
|
|
|
|
const formItemLayout = {
|
|
|
|
labelCol: {
|
|
|
|
xs: { span: 24 },
|
|
|
|
sm: { span: 8 },
|
|
|
|
},
|
|
|
|
wrapperCol: {
|
|
|
|
xs: { span: 24 },
|
|
|
|
sm: { span: 16 },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const config = {
|
2020-12-17 15:09:18 +08:00
|
|
|
rules: [{ type: 'object' as const, required: true, message: 'Please select time!' }],
|
2019-07-03 20:14:39 +08:00
|
|
|
};
|
|
|
|
const rangeConfig = {
|
2020-12-17 15:09:18 +08:00
|
|
|
rules: [{ type: 'array' as const, required: true, message: 'Please select time!' }],
|
2019-07-03 20:14:39 +08:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
2020-12-17 15:09:18 +08:00
|
|
|
const onFinish = (fieldsValue: any) => {
|
2019-07-03 20:14:39 +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);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form name="time_related_controls" {...formItemLayout} onFinish={onFinish}>
|
|
|
|
<Form.Item name="date-picker" label="DatePicker" {...config}>
|
|
|
|
<DatePicker />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item name="date-time-picker" label="DatePicker[showTime]" {...config}>
|
|
|
|
<DatePicker showTime format="YYYY-MM-DD HH:mm:ss" />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item name="month-picker" label="MonthPicker" {...config}>
|
2020-07-23 11:25:17 +08:00
|
|
|
<DatePicker picker="month" />
|
2019-07-03 20:14:39 +08:00
|
|
|
</Form.Item>
|
|
|
|
<Form.Item name="range-picker" label="RangePicker" {...rangeConfig}>
|
|
|
|
<RangePicker />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item name="range-time-picker" label="RangePicker[showTime]" {...rangeConfig}>
|
|
|
|
<RangePicker showTime format="YYYY-MM-DD HH:mm:ss" />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item name="time-picker" label="TimePicker" {...config}>
|
|
|
|
<TimePicker />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item
|
|
|
|
wrapperCol={{
|
|
|
|
xs: { span: 24, offset: 0 },
|
|
|
|
sm: { span: 16, offset: 8 },
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Button type="primary" htmlType="submit">
|
|
|
|
Submit
|
|
|
|
</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2019-07-03 20:14:39 +08:00
|
|
|
```
|