Skip to content

Commit 83d2cc8

Browse files
committed
feat: Accessibility improvements, add keyboard shortcuts (resolves #822)
1 parent 995d1ec commit 83d2cc8

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

src/VueDatePicker/components/DatePicker/DatePicker.vue

+29
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
import { CMP } from '@/constants';
9494
9595
import type { ICalendarDay } from '@/interfaces';
96+
import { endOfWeek, getMonth, startOfWeek } from 'date-fns';
9697
9798
const emit = defineEmits([
9899
'tooltip-open',
@@ -128,6 +129,7 @@
128129
modelValue,
129130
time,
130131
disabledTimesConfig,
132+
today,
131133
validateTime,
132134
getCalendarDays,
133135
getMarker,
@@ -218,6 +220,30 @@
218220
timePickerRef.value?.toggleTimePicker(flow, show, childOpen);
219221
};
220222
223+
const selectWeekDate = (selectStart: boolean) => {
224+
if (!props.range) {
225+
const date = modelValue.value ? modelValue.value : today;
226+
const toSelect = selectStart
227+
? startOfWeek(date as Date, { weekStartsOn: 1 })
228+
: endOfWeek(date as Date, { weekStartsOn: 1 });
229+
230+
selectDate({
231+
value: toSelect,
232+
current: getMonth(date as Date) === month.value(0),
233+
text: '',
234+
classData: {},
235+
});
236+
}
237+
};
238+
239+
const changeMonth = (isNext: boolean) => {
240+
headerRefs.value[0]?.handleMonthYearChange(isNext, true);
241+
};
242+
243+
const changeYear = (isNext: boolean) => {
244+
updateMonthYear(0, { month: month.value(0), year: year.value(0) + (isNext ? 1 : -1), fromNav: true });
245+
};
246+
221247
const getSidebarProps = () => {
222248
return {
223249
modelValue,
@@ -241,5 +267,8 @@
241267
handleArrow,
242268
updateMonthYear,
243269
getSidebarProps,
270+
changeMonth,
271+
changeYear,
272+
selectWeekDate,
244273
});
245274
</script>

src/VueDatePicker/components/DatePicker/date-picker.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const useDatePicker = (
4848
const lastScrollTime = ref(new Date());
4949
const clickedDate = ref<ICalendarDay | undefined>();
5050

51-
const { modelValue, calendars, time } = useModel(props, emit);
51+
const { modelValue, calendars, time, today } = useModel(props, emit);
5252
const {
5353
defaultedMultiCalendars,
5454
defaultedStartTime,
@@ -664,6 +664,7 @@ export const useDatePicker = (
664664
year,
665665
time,
666666
disabledTimesConfig,
667+
today,
667668
validateTime,
668669
getCalendarDays,
669670
getMarker,

src/VueDatePicker/components/DatepickerMenu.vue

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@keydown.up.prevent="handleArrowKey('up')"
1515
@keydown.down.prevent="handleArrowKey('down')"
1616
@keydown.right.prevent="handleArrowKey('right')"
17-
@keydown="checkShiftKey"
17+
@keydown="onKeyDown"
1818
>
1919
<div v-if="((disabled || readonly) && defaultedInline.enabled) || loading" :class="disabledReadonlyOverlay">
2020
<div v-if="loading" class="dp--menu-load-container">
@@ -370,6 +370,19 @@
370370
callChildFn('updateMonthYear', instance, value);
371371
};
372372
373+
const onKeyDown = (ev: KeyboardEvent) => {
374+
checkShiftKey(ev);
375+
if (ev.key === 'Home' || ev.key === 'End') {
376+
return callChildFn('selectWeekDate', ev.key === 'Home');
377+
}
378+
if (ev.key === 'PageUp' || ev.key === 'PageDown') {
379+
if (ev.shiftKey) {
380+
return callChildFn('changeYear', ev.key === 'PageUp');
381+
}
382+
return callChildFn('changeMonth', ev.key === 'PageUp');
383+
}
384+
};
385+
373386
defineExpose({
374387
updateMonthYear,
375388
switchView,

src/VueDatePicker/composables/model.ts

+1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ export const useModel = (props: PickerBasePropsType, emit: VueEmit) => {
5151
modelValue,
5252
month,
5353
year,
54+
today,
5455
};
5556
};

src/VueDatePicker/interfaces.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,15 @@ export interface HeaderSelectionBtn {
233233
ariaLabel: string;
234234
}
235235

236-
export type MenuExposedFn = 'selectCurrentDate' | 'presetDate' | 'clearHoverDate' | 'handleArrow' | 'updateMonthYear';
236+
export type MenuExposedFn =
237+
| 'selectCurrentDate'
238+
| 'presetDate'
239+
| 'clearHoverDate'
240+
| 'handleArrow'
241+
| 'updateMonthYear'
242+
| 'selectWeekDate'
243+
| 'changeYear'
244+
| 'changeMonth';
237245

238246
export type OptionEnabled = boolean | number | string;
239247

0 commit comments

Comments
 (0)