fix(Calendar): 日历组件已经禁用部分日期依然可以选择日期区间问题 (#9361)

* fix: Calendar 日历组件已经禁用部分日期依然可以选择日期区间问题

* feat: expose disabledDays from calendarMonth

Co-authored-by: lumdzeehol <lumdzeehol@163.com>
This commit is contained in:
Lumdzeehol 2021-09-09 10:05:08 +08:00 committed by GitHub
parent 1b6d77073b
commit 9144b5bd4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 2 deletions

View File

@ -368,6 +368,26 @@ export default defineComponent({
} }
}; };
// get first disabled calendarDay between date range
const getDisabledDate = (
disabledDays: CalendarDayItem[],
startDay: Date,
date: Date
): Date | undefined =>
disabledDays.find(
(day) =>
compareDay(startDay, day.date!) === -1 &&
compareDay(day.date!, date) === -1
)?.date;
// disabled calendarDay
const disabledDays = computed(() =>
monthRefs.value.reduce((arr, ref) => {
arr.push(...(ref.disabledDays?.value ?? []));
return arr;
}, [] as CalendarDayItem[])
);
const onClickDay = (item: CalendarDayItem) => { const onClickDay = (item: CalendarDayItem) => {
if (props.readonly || !item.date) { if (props.readonly || !item.date) {
return; return;
@ -388,7 +408,18 @@ export default defineComponent({
const compareToStart = compareDay(date, startDay); const compareToStart = compareDay(date, startDay);
if (compareToStart === 1) { if (compareToStart === 1) {
select([startDay, date], true); const disabledDay = getDisabledDate(
disabledDays.value,
startDay,
date
);
if (disabledDay) {
const lastAbledEndDay = getPrevDay(disabledDay);
select([startDay, lastAbledEndDay]);
} else {
select([startDay, date], true);
}
} else if (compareToStart === -1) { } else if (compareToStart === -1) {
select([date]); select([date]);
} else if (props.allowSameDay) { } else if (props.allowSameDay) {

View File

@ -241,6 +241,10 @@ export default defineComponent({
return days; return days;
}); });
const disabledDays = computed(() =>
days.value.filter((day) => day.type === 'disabled')
);
const renderDay = (item: CalendarDayItem, index: number) => ( const renderDay = (item: CalendarDayItem, index: number) => (
<CalendarDay <CalendarDay
v-slots={pick(slots, ['top-info', 'bottom-info'])} v-slots={pick(slots, ['top-info', 'bottom-info'])}
@ -265,6 +269,7 @@ export default defineComponent({
getHeight: () => height.value, getHeight: () => height.value,
setVisible, setVisible,
scrollIntoView, scrollIntoView,
disabledDays,
}); });
return () => ( return () => (

View File

@ -1,4 +1,4 @@
import type { ComponentPublicInstance } from 'vue'; import type { ComponentPublicInstance, ComputedRef, Ref } from 'vue';
import type { CalendarProps } from './Calendar'; import type { CalendarProps } from './Calendar';
import type { CalendarMonthProps } from './CalendarMonth'; import type { CalendarMonthProps } from './CalendarMonth';
@ -43,5 +43,6 @@ export type CalendarMonthInstance = ComponentPublicInstance<
getHeight: () => number; getHeight: () => number;
setVisible: (value?: boolean | undefined) => void; setVisible: (value?: boolean | undefined) => void;
scrollIntoView: (body: Element) => void; scrollIntoView: (body: Element) => void;
disabledDays: Ref<ComputedRef<CalendarDayItem[]>>;
} }
>; >;