2016-11-07 16:31:15 +08:00
|
|
|
---
|
|
|
|
order: 8
|
|
|
|
title:
|
2016-11-15 17:16:20 +08:00
|
|
|
zh-CN: 预设范围
|
2019-08-29 15:07:35 +08:00
|
|
|
en-US: Preset Ranges
|
2016-11-07 16:31:15 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
2018-08-04 12:04:07 +08:00
|
|
|
可以预设常用的日期范围以提高用户体验。
|
2016-11-07 16:31:15 +08:00
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2019-08-29 15:07:35 +08:00
|
|
|
We can set preset ranges to RangePicker to improve user experience.
|
2016-11-07 16:31:15 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2022-10-28 16:44:19 +08:00
|
|
|
import React from 'react';
|
2020-07-30 19:17:34 +08:00
|
|
|
import { DatePicker, Space } from 'antd';
|
2022-02-14 14:40:40 +08:00
|
|
|
import dayjs from 'dayjs';
|
2022-10-28 16:44:19 +08:00
|
|
|
import type { Dayjs } from 'dayjs';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-05-27 21:32:45 +08:00
|
|
|
const { RangePicker } = DatePicker;
|
2016-11-07 16:31:15 +08:00
|
|
|
|
2022-10-28 16:44:19 +08:00
|
|
|
const onChange = (date: Dayjs) => {
|
|
|
|
if (date) {
|
|
|
|
console.log('Date: ', date);
|
|
|
|
} else {
|
|
|
|
console.log('Clear');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const onRangeChange = (dates: null | (Dayjs | null)[], dateStrings: string[]) => {
|
2022-05-18 17:25:40 +08:00
|
|
|
if (dates) {
|
|
|
|
console.log('From: ', dates[0], ', to: ', dates[1]);
|
|
|
|
console.log('From: ', dateStrings[0], ', to: ', dateStrings[1]);
|
|
|
|
} else {
|
|
|
|
console.log('Clear');
|
|
|
|
}
|
2022-05-19 09:46:26 +08:00
|
|
|
};
|
2017-05-15 16:36:07 +08:00
|
|
|
|
2022-10-28 16:44:19 +08:00
|
|
|
const rangePresets: {
|
|
|
|
label: string;
|
|
|
|
value: [Dayjs, Dayjs];
|
|
|
|
}[] = [
|
|
|
|
{ label: 'Last 7 Days', value: [dayjs().add(-7, 'd'), dayjs()] },
|
|
|
|
{ label: 'Last 14 Days', value: [dayjs().add(-14, 'd'), dayjs()] },
|
|
|
|
{ label: 'Last 30 Days', value: [dayjs().add(-30, 'd'), dayjs()] },
|
|
|
|
{ label: 'Last 90 Days', value: [dayjs().add(-90, 'd'), dayjs()] },
|
|
|
|
];
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => (
|
2020-07-30 19:17:34 +08:00
|
|
|
<Space direction="vertical" size={12}>
|
2022-10-28 16:44:19 +08:00
|
|
|
<DatePicker
|
|
|
|
presets={[
|
|
|
|
{ label: 'Yesterday', value: dayjs().add(-1, 'd') },
|
|
|
|
{ label: 'Last Week', value: dayjs().add(-7, 'd') },
|
|
|
|
{ label: 'Last Month', value: dayjs().add(-1, 'month') },
|
|
|
|
]}
|
2016-11-15 17:16:20 +08:00
|
|
|
onChange={onChange}
|
2016-11-07 16:31:15 +08:00
|
|
|
/>
|
2022-10-28 16:44:19 +08:00
|
|
|
<RangePicker presets={rangePresets} onChange={onRangeChange} />
|
2016-11-07 16:31:15 +08:00
|
|
|
<RangePicker
|
2022-10-28 16:44:19 +08:00
|
|
|
presets={rangePresets}
|
2017-05-15 14:37:22 +08:00
|
|
|
showTime
|
|
|
|
format="YYYY/MM/DD HH:mm:ss"
|
2022-10-28 16:44:19 +08:00
|
|
|
onChange={onRangeChange}
|
2016-11-07 16:31:15 +08:00
|
|
|
/>
|
2022-04-03 23:27:45 +08:00
|
|
|
</Space>
|
2016-11-07 16:31:15 +08:00
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
|
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|