|
|
|
---
|
|
|
|
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`.
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
import { DatePicker } from 'antd';
|
|
|
|
import type { Dayjs } from 'dayjs';
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
|
|
|
|
const { RangePicker } = DatePicker;
|
|
|
|
|
|
|
|
type RangeValue = [Dayjs | null, Dayjs | 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: Dayjs) => {
|
|
|
|
if (!dates) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const tooLate = dates[0] && current.diff(dates[0], 'days') > 7;
|
|
|
|
const tooEarly = dates[1] && dates[1].diff(current, 'days') > 7;
|
|
|
|
return !!tooEarly || !!tooLate;
|
|
|
|
};
|
|
|
|
|
|
|
|
const onOpenChange = (open: boolean) => {
|
|
|
|
if (open) {
|
|
|
|
setHackValue([null, null]);
|
|
|
|
setDates([null, null]);
|
|
|
|
} else {
|
|
|
|
setHackValue(null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RangePicker
|
|
|
|
value={hackValue || value}
|
|
|
|
disabledDate={disabledDate}
|
|
|
|
onCalendarChange={val => setDates(val)}
|
|
|
|
onChange={val => setValue(val)}
|
|
|
|
onOpenChange={onOpenChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|
|
|
|
```
|