Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Calendar validRange prevents disabledDate #25626

Merged
merged 8 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/calendar/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function MonthSelect<DateType>(props: SharedProps<DateType>) {
const month = generateConfig.getMonth(value);

let start = 0;
let end = 12;
let end = 11;

if (validRange) {
const [rangeStart, rangeEnd] = validRange;
Expand All @@ -110,7 +110,7 @@ function MonthSelect<DateType>(props: SharedProps<DateType>) {

const months = locale.shortMonths || generateConfig.locale.getShortMonths!(locale.locale);
const options: { label: string; value: number }[] = [];
for (let index = start; index < end; index += 1) {
for (let index = start; index <= end; index += 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加个测试用例

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

options.push({
label: months[index],
value: index,
Expand Down
14 changes: 14 additions & 0 deletions components/calendar/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ describe('Calendar', () => {
expect(disabledDate(Moment('2018-04-02'))).toBe(false);
});

it('validRange should work with disabledDate function', () => {
const validRange = [Moment('2018-02-02'), Moment('2018-05-18')];
const wrapper = mount(
<Calendar validRange={validRange} disabledDate={data => data.isSame(Moment('2018-02-03'))} />,
);

const { disabledDate } = wrapper.find('PickerPanel').props();
expect(disabledDate(Moment('2018-02-01'))).toBe(true);
expect(disabledDate(Moment('2018-02-02'))).toBe(false);
expect(disabledDate(Moment('2018-02-03'))).toBe(true);
expect(disabledDate(Moment('2018-02-04'))).toBe(false);
expect(disabledDate(Moment('2018-06-01'))).toBe(true);
});

it('Calendar should change mode by prop', () => {
const monthMode = 'month';
const yearMode = 'year';
Expand Down
19 changes: 8 additions & 11 deletions components/calendar/generateCalendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,15 @@ function generateCalendar<DateType>(generateConfig: GenerateConfig<DateType>) {
);

// Disabled Date
const mergedDisabledDate = React.useMemo(() => {
if (validRange) {
return (date: DateType) => {
return (
generateConfig.isAfter(validRange[0], date) ||
const mergedDisabledDate = React.useCallback((date: DateType) => {
const notInRange = validRange
? generateConfig.isAfter(validRange[0], date) ||
generateConfig.isAfter(date, validRange[1])
);
};
}

return disabledDate;
}, [disabledDate, validRange]);
: false;
return notInRange || !!disabledDate?.(date);
},
[disabledDate, validRange],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你这个为啥会被格式化成这样,差点没看懂 😂😂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

提到外面来的话就直接用 useCallback 吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

);

// ====================== Events ======================
const triggerPanelChange = (date: DateType, newMode: CalendarMode) => {
Expand Down