Skip to content
This repository was archived by the owner on Apr 17, 2022. It is now read-only.

Commit f15bb7f

Browse files
committed
feat: Allow month change on calendar wheel scroll (#43)
1 parent dfe53f8 commit f15bb7f

File tree

6 files changed

+23
-1
lines changed

6 files changed

+23
-1
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ interface Vue3DatePicker {
139139
showNowButton?: boolean;
140140
nowButtonLabel?: string;
141141
partialRange?: boolean;
142+
monthChangeOnScroll?: boolean;
142143
}
143144

144145
interface PublicMethods extends MethodOptions {

src/Vue3DatePicker/Vue3DatePicker.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
showNowButton: { type: Boolean as PropType<boolean>, default: false },
200200
nowButtonLabel: { type: String as PropType<string>, default: 'Now' },
201201
partialRange: { type: Boolean as PropType<boolean>, default: true },
202+
monthChangeOnScroll: { type: Boolean as PropType<boolean>, default: true },
202203
});
203204
const slots = useSlots();
204205
const isOpen = ref(false);

src/Vue3DatePicker/components/Calendar.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
<slot :name="slot" v-bind="args" />
2525
</template>
2626
</component>
27-
<div v-if="!specificMode" :class="calendarWrapClass" role="grid" aria-label="Calendar wrapper">
27+
<div
28+
v-if="!specificMode"
29+
:class="calendarWrapClass"
30+
role="grid"
31+
aria-label="Calendar wrapper"
32+
@wheel="$emit('handleScroll', $event)"
33+
>
2834
<div class="db__calendar_header" role="row">
2935
<div class="dp__calendar_header_item" role="gridcell" v-if="weekNumbers">{{ weekNumName }}</div>
3036
<div class="dp__calendar_header_item" role="gridcell" v-for="(dayVal, i) in weekDays" :key="i">
@@ -117,6 +123,7 @@
117123
'update:month',
118124
'update:year',
119125
'monthYearSelect',
126+
'handleScroll',
120127
]);
121128
122129
const props = defineProps({

src/Vue3DatePicker/components/DatepickerMenu.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
@month-year-select="monthYearSelect"
2424
@select-date="selectDate($event)"
2525
@set-hover-date="setHoverDate($event)"
26+
@handle-scroll="handleScroll"
2627
>
2728
<template v-for="(slot, i) in calendarSlots" #[slot]="args" :key="i">
2829
<slot :name="slot" v-bind="{ ...args }" />
@@ -43,6 +44,7 @@
4344
@update:year="updateMonthYear($event, false, true)"
4445
@select-date="selectDate($event, true)"
4546
@set-hover-date="setHoverDate($event)"
47+
@handle-scroll="handleScroll($event, true)"
4648
>
4749
<template v-for="(slot, i) in calendarSlots" #[slot]="args" :key="i">
4850
<slot :name="slot" v-bind="{ ...args }" />
@@ -170,6 +172,7 @@
170172
allowedDates: { type: Array as PropType<string[] | Date[]>, default: () => [] },
171173
showNowButton: { type: Boolean as PropType<boolean>, default: false },
172174
nowButtonLabel: { type: String as PropType<string>, default: 'Now' },
175+
monthChangeOnScroll: { type: Boolean as PropType<boolean>, default: true },
173176
});
174177
const slots = useSlots();
175178
const calendarWrapperRef = ref(null);
@@ -209,6 +212,7 @@
209212
clearHoverDate,
210213
rangeActiveStartEnd,
211214
monthYearSelect,
215+
handleScroll,
212216
} = useCalendar(props, emit);
213217
214218
const calendarSlots = mapSlots(slots, 'calendar');

src/Vue3DatePicker/components/composition/calendar.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ interface IUseCalendar {
3434
rangeActiveStartEnd: (day: UnwrapRef<ICalendarDay>, isStart?: boolean) => boolean;
3535
monthYearSelect: (isYear?: boolean) => void;
3636
clearHoverDate: () => void;
37+
handleScroll: (event: WheelEvent, isNext?: boolean) => void;
3738
today: Ref<Date>;
3839
month: Ref<number>;
3940
year: Ref<number>;
@@ -455,6 +456,12 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
455456
}
456457
};
457458

459+
const handleScroll = (event: WheelEvent, isNext = false): void => {
460+
const yearMonth: [number, number] = isNext ? [monthNext.value, yearNext.value] : [month.value, year.value];
461+
const dates = event.deltaY < 0 ? getNextYearMonth(...yearMonth) : getPreviousMonthYear(...yearMonth);
462+
updateMonthYear(dates.month, true, isNext);
463+
};
464+
458465
return {
459466
today,
460467
hours,
@@ -477,5 +484,6 @@ export const useCalendar = (props: UseCalendar, emit: VueEmit): IUseCalendar =>
477484
isAutoRangeStart,
478485
clearHoverDate,
479486
rangeActiveStartEnd,
487+
handleScroll,
480488
};
481489
};

src/Vue3DatePicker/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export type UseCalendar = {
100100
autoRange: number | string;
101101
disabledWeekDays: number[] | string[];
102102
allowedDates: Date[] | string[];
103+
monthChangeOnScroll: boolean;
103104
} & { [key: string]: any };
104105

105106
export interface UseMonthYearPick {

0 commit comments

Comments
 (0)