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`.
|
|
|
|
|
|
|
|
```jsx
|
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';
|
|
|
|
|
|
|
|
const { RangePicker } = DatePicker;
|
|
|
|
|
|
|
|
const App = () => {
|
|
|
|
const [dates, setDates] = useState([]);
|
2020-11-13 17:05:16 +08:00
|
|
|
const [hackValue, setHackValue] = useState();
|
|
|
|
const [value, setValue] = useState();
|
2020-03-18 16:59:15 +08:00
|
|
|
const disabledDate = current => {
|
|
|
|
if (!dates || dates.length === 0) {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2020-11-13 17:05:16 +08:00
|
|
|
const onOpenChange = open => {
|
|
|
|
if (open) {
|
|
|
|
setHackValue([]);
|
|
|
|
setDates([]);
|
|
|
|
} else {
|
|
|
|
setHackValue(undefined);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
```
|