Skip to content

Commit

Permalink
Accessibility improvements (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feshchenko authored Dec 1, 2023
1 parent 8d51e2b commit 3433c9b
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/datepicker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rehookify/datepicker",
"version": "6.4.1",
"version": "6.5.0",
"description": "The ultimate tool to create a date, range and time picker in your React applications.",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.mjs",
Expand Down
8 changes: 7 additions & 1 deletion packages/datepicker/src/__test__/create-month.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ describe('createMonth', () => {
expect(active.length).toBe(1);
expect(now.length).toBe(1);

// This manipulation is needed to prevent selecting month in the next year
// for example if today is December then next month will be January
// and test will fail because in current year selected only 1 month 😅
// eslint-disable-next-line playwright/no-conditional-in-test
const nextMonth = M === 11 ? M - 1 : M + 1;

// with 2 selected dates
months = createMonths(NOW, [NOW, newDate(Y, M + 1, D)], locale, dates);
months = createMonths(NOW, [NOW, newDate(Y, nextMonth, D)], locale, dates);

let selected = months.filter(({ selected }) => selected);

Expand Down
1 change: 1 addition & 0 deletions packages/datepicker/src/types/day.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type DPDayRange =

export interface DPDay {
$date: Date;
active: boolean;
day: string;
disabled: boolean;
inCurrentMonth: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/datepicker/src/use-days.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useDaysPropGetters: DPUseDaysPropGetters = ({

const dayButton = useCallback(
(
{ $date, selected, disabled }: DPDay,
{ $date, selected, disabled, active }: DPDay,
{ onClick, disabled: disabledProps, ...rest }: DPPropsGetterConfig = {},
) =>
createPropGetter(
Expand Down Expand Up @@ -76,6 +76,7 @@ export const useDaysPropGetters: DPUseDaysPropGetters = ({
setRangeEndAction(dispatch, $date);
},
}),
tabIndex: active ? 0 : -1,
},
selected,
),
Expand Down
7 changes: 5 additions & 2 deletions packages/datepicker/src/use-months.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ export const useMonths: DPUseMonths = ({
export const useMonthsPropGetters: DPUseMonthsPropGetters = (dpState) => {
const monthButton = useCallback(
(
{ $date, disabled, selected }: DPMonth,
{ $date, disabled, selected, active }: DPMonth,
{ onClick, disabled: disabledProps, ...rest }: DPPropsGetterConfig = {},
) =>
createPropGetter(
!!disabledProps || disabled,
(evt) => callAll(onClick, skipFirst(setDPOffset(dpState)))(evt, $date),
rest,
{
...rest,
tabIndex: active ? 0 : -1,
},
selected,
),
[dpState],
Expand Down
7 changes: 5 additions & 2 deletions packages/datepicker/src/use-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const useTimePropGetter: DPUseTimePropGetter = ({
}) => {
const timeButton = useCallback(
(
{ $date, selected, disabled }: DPTime,
{ $date, selected, disabled, now }: DPTime,
{ onClick, disabled: disabledProps, ...rest }: DPPropsGetterConfig = {},
) =>
createPropGetter(
Expand All @@ -46,7 +46,10 @@ export const useTimePropGetter: DPUseTimePropGetter = ({
}),
)(evt, $date);
},
rest,
{
...rest,
tabIndex: selected || now ? 0 : -1,
},
selected,
),
[selectedDates, onDatesChange, dispatch, focusDate],
Expand Down
7 changes: 5 additions & 2 deletions packages/datepicker/src/use-years.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ export const useYearsPropGetters: DPUseYearsPropGetters = (dpState) => {

const yearButton = useCallback(
(
{ $date, disabled, selected }: DPYear,
{ $date, disabled, selected, active }: DPYear,
{ onClick, disabled: disabledProps, ...rest }: DPPropsGetterConfig = {},
) =>
createPropGetter(
!!disabledProps || disabled,
(evt) => callAll(onClick, skipFirst(setDPOffset(dpState)))(evt, $date),
rest,
{
...rest,
tabIndex: active ? 0 : -1,
},
selected,
),
[dpState],
Expand Down
9 changes: 6 additions & 3 deletions packages/datepicker/src/utils/create-calendars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {

const createCalendar = (
offsetDate: Date,
calendarStartDate: Date,
selectedDates: Date[],
{ rangeEnd }: DPReducerState,
config: DPConfig,
Expand All @@ -36,7 +37,7 @@ const createCalendar = (
exclude,
} = config;
const { locale: localeStr, day, year } = locale;
const { M, Y } = getDateParts(offsetDate);
const { M, Y } = getDateParts(calendarStartDate);
const { start, length } = getCalendarMonthParams(M, Y, calendar);

const days: DPDay[] = Array(length)
Expand All @@ -46,6 +47,7 @@ const createCalendar = (

return {
$date,
active: isSame(offsetDate, $date),
day: toLocaleDateString($date, localeStr, { day }),
now: isSame(getCleanDate(newDate()), $date),
range: getDateRangeState($date, rangeEnd, selectedDates, mode),
Expand All @@ -59,8 +61,8 @@ const createCalendar = (
});

return {
year: toLocaleDateString(offsetDate, localeStr, { year }),
month: formatMonthName(offsetDate, locale),
year: toLocaleDateString(calendarStartDate, localeStr, { year }),
month: formatMonthName(calendarStartDate, locale),
days,
};
};
Expand All @@ -73,6 +75,7 @@ export const createCalendars = ({
}: DPState): DPCalendar[] => {
return config.calendar.offsets.map((offset) =>
createCalendar(
offsetDate,
addToDate(offsetDate, offset, 'month'),
selectedDates,
state,
Expand Down

0 comments on commit 3433c9b

Please sign in to comment.