Skip to content

Commit 4305248

Browse files
author
Kubit
committed
Enhance Calendar Component: Accessibility, Key Movement Refactor, and Test Updates
Accessibility Improvements: Added aria-label to TableStyled in CalendarStandAloneComponent to improve screen reader support. File: app/src/components/calendar/calendarStandAlone.tsx Key Movement Refactoring: Refactored key movement functions (keyLeftMove, keyRightMove, keyUpMove, keyDownMove) to handle edge cases and improve maintainability. Files: app/src/components/calendar/list/list.tsx app/src/components/calendar/selector/monthSelector/monthSelector.tsx app/src/components/calendar/selector/yearSelector/yearSelector.tsx Test Updates: Updated tests to reflect changes in key movement logic and added new test cases for edge scenarios. Files: app/src/components/calendar/selector/monthSelector/__tests__/monthSelector.test.tsx app/src/components/calendar/selector/monthSelector/utils/monthSelector.test.ts Type Adjustments: Modified IMonthSelector interface to make minDate and maxDate required. File: app/src/components/calendar/selector/monthSelector/types/monthSelector.ts
1 parent 361ac6e commit 4305248

29 files changed

+1271
-354
lines changed

src/components/calendar/calendarStandAlone.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ const CalendarStandAloneComponent = (
5050
/>
5151
<CalendarSelectorStyled>
5252
{!showMonthSelector && !showYearSelector && showDaySelector && (
53-
<TableStyled role={ROLES.GRID}>
53+
<TableStyled
54+
aria-label={props.currentDate.toLocaleDateString(props.locale, {
55+
timeZone: 'UTC',
56+
month: 'long',
57+
year: 'numeric',
58+
})}
59+
role={ROLES.GRID}
60+
>
5461
<Header
5562
formatWeekDayOption={props.formatWeekDayOption}
5663
isSundayFirst={props.sundayFirst}

src/components/calendar/list/list.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ import { NEUTRAL_DATE } from '@/types';
99
import { WEEK_DAYS } from '../constants/constants';
1010
import {
1111
getAllDaysInMonth,
12-
getAvailableDaysAfterCurrentDate,
13-
getAvailableDaysBeforeCurrentDate,
12+
getDaysAndEmptyDaysUntilMaxDate,
1413
getFirstDayOfMonth,
14+
getFirstEmptyAndDisabledDays,
1515
getStateDay,
1616
groupDaysByWeeks,
17-
handleKeyDownAndRightMove,
17+
handleKeyDownMove,
18+
handleKeyLeftMove,
19+
handleKeyPageDownMove,
20+
handleKeyPageUpMove,
21+
handleKeyRightMove,
1822
handleKeyTabMove,
19-
handleKeyUpAndLeftMove,
23+
handleKeyUpMove,
2024
} from '../utils/';
2125
// styles
2226
import {
@@ -51,18 +55,16 @@ export const List = ({
5155
const [ghostDateSelected, setGhostDateSelected] = React.useState<Date | 0>(0);
5256

5357
const handleKeyMoveConfig = {
54-
emptyDaysList,
5558
dayList,
56-
isAfter: dateHelpers.isAfter,
5759
maxDate: props.maxDate,
5860
currentDate: props.currentDate,
5961
minDate: props.minDate,
60-
availableDaysBeforeCurrentDate: getAvailableDaysBeforeCurrentDate(
62+
firstEmptyAndDisabledDays: getFirstEmptyAndDisabledDays(
6163
emptyDaysList,
6264
props.minDate,
6365
props.currentDate
6466
),
65-
availableDaysAfterCurrentDate: getAvailableDaysAfterCurrentDate(
67+
daysAndEmptyDaysUntilMaxDate: getDaysAndEmptyDaysUntilMaxDate(
6668
emptyDaysList,
6769
props.maxDate,
6870
props.currentDate
@@ -73,11 +75,13 @@ export const List = ({
7375
() => ({
7476
calendarBlankDaysSize: emptyDaysList.length,
7577
size: dayList.length,
76-
keyDownMove: handleKeyDownAndRightMove(handleKeyMoveConfig),
77-
keyUpMove: handleKeyUpAndLeftMove(handleKeyMoveConfig),
78-
keyRightMove: handleKeyDownAndRightMove(handleKeyMoveConfig),
79-
keyLeftMove: handleKeyUpAndLeftMove(handleKeyMoveConfig),
78+
keyDownMove: handleKeyDownMove(handleKeyMoveConfig),
79+
keyUpMove: handleKeyUpMove(handleKeyMoveConfig),
80+
keyRightMove: handleKeyRightMove(handleKeyMoveConfig),
81+
keyLeftMove: handleKeyLeftMove(handleKeyMoveConfig),
8082
keyTabMove: handleKeyTabMove,
83+
keyPageDownMove: handleKeyPageDownMove(handleKeyMoveConfig),
84+
keyPageUpMove: handleKeyPageUpMove(handleKeyMoveConfig),
8185
currentFocusSelected:
8286
(selectedDate[0] ? selectedDate[0].getDate() : new Date().getDate()) +
8387
emptyDaysList.length -

src/components/calendar/selector/monthSelector/__tests__/monthSelector.test.tsx

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import { renderProvider } from '@/tests/renderProvider/renderProvider.utility';
77
import { windowMatchMedia } from '@/tests/windowMatchMedia';
88

99
import { MonthSelector } from '../monthSelector';
10-
import { keyLeftMove, keyRightMove, keyTabMove } from '../utils';
1110

1211
window.matchMedia = windowMatchMedia();
1312

1413
const mockProps = {
1514
currentDate: new Date(2022, 11, 17),
15+
minDate: new Date(2023, 0, 1),
16+
maxDate: new Date(2023, 0, 31),
1617
setCurrentDate: jest.fn(),
1718
today: new Date(),
1819
};
@@ -36,33 +37,4 @@ describe('MonthSelector', () => {
3637

3738
expect(setCurrentDate).toHaveBeenCalled();
3839
});
39-
it('MonthSelector utils - left key pressed should be move to a previous position', () => {
40-
const result = keyLeftMove(5);
41-
42-
expect(result).toBe(4);
43-
});
44-
45-
it('MonthSelector utils - left key pressed should be move to the end', () => {
46-
const result = keyLeftMove(0);
47-
48-
expect(result).toBe(new Date().getMonth());
49-
});
50-
51-
it('MonthSelector utils - right key pressed should be move to a next position', () => {
52-
const result = keyRightMove(5)(6);
53-
54-
expect(result).toBe(6);
55-
});
56-
57-
it('MonthSelector utils - right key pressed should be move to the start', () => {
58-
const result = keyRightMove(12)(12);
59-
60-
expect(result).toBe(0);
61-
});
62-
63-
it('MonthSelector utils - tab key pressed should be move outside month list', () => {
64-
const result = keyTabMove(5);
65-
66-
expect(result).toBe(5);
67-
});
6840
});

src/components/calendar/selector/monthSelector/monthSelector.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,28 @@ import { setMonth } from '../../utils/setMonth';
1212
import { MonthElementStyled, MonthListStyled, MonthSelectorStyled } from './monthSelector.styled';
1313
import { IMonthSelector } from './types/monthSelector';
1414
import { MonthSelectorStateType } from './types/state';
15-
import { keyLeftMove, keyRightMove, keyTabMove } from './utils';
15+
import { keyDownMove, keyLeftMove, keyRightMove, keyTabMove, keyUpMove } from './utils';
1616

1717
export const MonthSelector = (props: IMonthSelector): JSX.Element => {
1818
const { dateHelpers, transformDate } = useUtilsProvider();
1919

20+
const handleKeyMoveConfig = {
21+
currentDate: props.currentDate,
22+
maxDate: props.maxDate,
23+
minDate: props.minDate,
24+
};
25+
2026
const roveFocusProps = React.useMemo(
2127
() => ({
2228
size: dateHelpers.getAllMonthName('long').length,
23-
keyLeftMove,
24-
keyRightMove: keyRightMove(new Date().getMonth()),
29+
keyLeftMove: keyLeftMove(handleKeyMoveConfig),
30+
keyRightMove: keyRightMove(handleKeyMoveConfig),
31+
keyUpMove: keyUpMove(handleKeyMoveConfig),
32+
keyDownMove: keyDownMove(handleKeyMoveConfig),
33+
keyTabMove,
2534
currentFocusSelected: props.currentDate
2635
? props.currentDate.getMonth()
2736
: new Date().getMonth(),
28-
keyUpMove: 0,
29-
keyDownMove: 0,
30-
keyTabMove,
3137
}),
3238
[dateHelpers.getAllMonthName('long').length, props.currentDate]
3339
);
@@ -65,6 +71,7 @@ export const MonthSelector = (props: IMonthSelector): JSX.Element => {
6571
<MonthListStyled
6672
key={index}
6773
$disabled={setDisabledMonths(index)}
74+
aria-selected={state === MonthSelectorStateType.SELECTED ? true : undefined}
6875
state={state}
6976
styles={props.styles}
7077
>

src/components/calendar/selector/monthSelector/types/monthSelector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { CalendarContainerStylesType } from '@/components/calendar/types/calendarTheme';
22

33
export interface IMonthSelector {
4-
minDate?: Date;
5-
maxDate?: Date;
4+
minDate: Date;
5+
maxDate: Date;
66
currentDate: Date;
77
setCurrentDate: (date) => void;
88
onMonthClick?: (value?: string) => void;

0 commit comments

Comments
 (0)