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): disable the previous month button correctly when the minDate is the current time #13006

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions packages/vant/src/calendar/CalendarHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createNamespace, HAPTICS_FEEDBACK, makeStringProp } from '../utils';
import {
t,
bem,
compareMonth,
getPrevMonth,
getPrevYear,
getNextMonth,
Expand Down Expand Up @@ -39,22 +40,22 @@ export default defineComponent({
setup(props, { slots, emit }) {
const prevMonthDisabled = computed(() => {
const prevMonth = getPrevMonth(props.date!);
return props.minDate && prevMonth < props.minDate;
return props.minDate && compareMonth(prevMonth, props.minDate) < 0;
});

const prevYearDisabled = computed(() => {
const prevYear = getPrevYear(props.date!);
return props.minDate && prevYear < props.minDate;
return props.minDate && compareMonth(prevYear, props.minDate) < 0;
});

const nextMonthDisabled = computed(() => {
const nextMonth = getNextMonth(props.date!);
return props.maxDate && nextMonth > props.maxDate;
return props.maxDate && compareMonth(nextMonth, props.maxDate) > 0;
});

const nextYearDisabled = computed(() => {
const nextYear = getNextYear(props.date!);
return props.maxDate && nextYear > props.maxDate;
return props.maxDate && compareMonth(nextYear, props.maxDate) > 0;
});

const renderTitle = () => {
Expand Down
23 changes: 23 additions & 0 deletions packages/vant/src/calendar/test/switch-mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ test('disable previous and next month buttons', async () => {
expect(nextMonth.classes()).not.toContain(disabledActionClass);
});

test('disable the previous month button correctly when the minDate is the current time', async () => {
const wrapper = mount(Calendar, {
props: {
minDate: new Date(),
poppable: false,
switchMode: 'month',
},
});

await later();
const [prevMonth, nextMonth] = wrapper.findAll(
'.van-calendar__header-action',
);

expect(prevMonth.classes()).toContain(disabledActionClass);

await nextMonth.trigger('click');
expect(prevMonth.classes()).not.toContain(disabledActionClass);

await prevMonth.trigger('click');
expect(prevMonth.classes()).toContain(disabledActionClass);
});

test('disable previous and next year buttons', async () => {
const maxDate = getNextYear(minDate);
const wrapper = mount(Calendar, {
Expand Down