2020-03-18 16:59:15 +08:00
|
|
|
---
|
|
|
|
order: 6.1
|
|
|
|
title:
|
|
|
|
zh-CN: 选择不超过七天的范围
|
|
|
|
en-US: Select range dates in 7 days
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
这里举例如何用 `onCalendarChange` 和 `disabledDate` 来限制动态的日期区间选择。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
A example shows how to select a dynamic range by using `onCalendarChange` and `disabledDate`.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2020-03-18 17:06:01 +08:00
|
|
|
import React, { useState } from 'react';
|
2020-03-18 16:59:15 +08:00
|
|
|
import { DatePicker } from 'antd';
|
2022-05-19 09:46:26 +08:00
|
|
|
import type { Moment } from 'moment';
|
2020-03-18 16:59:15 +08:00
|
|
|
|
|
|
|
const { RangePicker } = DatePicker;
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
type RangeValue = [Moment | null, Moment | null] | null;
|
|
|
|
|
|
|
|
const App: React.FC = () => {
|
|
|
|
const [dates, setDates] = useState<RangeValue>(null);
|
|
|
|
const [hackValue, setHackValue] = useState<RangeValue>(null);
|
|
|
|
const [value, setValue] = useState<RangeValue>(null);
|
|
|
|
|
|
|
|
const disabledDate = (current: Moment) => {
|
|
|
|
if (!dates) {
|
2020-03-18 16:59:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const tooLate = dates[0] && current.diff(dates[0], 'days') > 7;
|
|
|
|
const tooEarly = dates[1] && dates[1].diff(current, 'days') > 7;
|
2022-05-19 09:46:26 +08:00
|
|
|
return !!tooEarly || !!tooLate;
|
2020-03-18 16:59:15 +08:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const onOpenChange = (open: boolean) => {
|
2020-11-13 17:05:16 +08:00
|
|
|
if (open) {
|
2022-05-19 09:46:26 +08:00
|
|
|
setHackValue([null, null]);
|
|
|
|
setDates([null, null]);
|
2020-11-13 17:05:16 +08:00
|
|
|
} else {
|
2022-05-19 09:46:26 +08:00
|
|
|
setHackValue(null);
|
2020-11-13 17:05:16 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-18 16:59:15 +08:00
|
|
|
return (
|
|
|
|
<RangePicker
|
2020-11-13 17:05:16 +08:00
|
|
|
value={hackValue || value}
|
2020-03-18 16:59:15 +08:00
|
|
|
disabledDate={disabledDate}
|
2020-11-13 17:05:16 +08:00
|
|
|
onCalendarChange={val => setDates(val)}
|
|
|
|
onChange={val => setValue(val)}
|
|
|
|
onOpenChange={onOpenChange}
|
2020-03-18 16:59:15 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-04-15 16:20:56 +08:00
|
|
|
export default App;
|
2020-03-18 16:59:15 +08:00
|
|
|
```
|