@transferwise/components 0.0.0-experimental-0df72a3 → 0.0.0-experimental-fe160ab
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/Calendar/Calendar.constants.js +10 -0
- package/build/Calendar/Calendar.constants.js.map +1 -0
- package/build/Calendar/Calendar.constants.mjs +8 -0
- package/build/Calendar/Calendar.constants.mjs.map +1 -0
- package/build/Calendar/Calendar.js +116 -16
- package/build/Calendar/Calendar.js.map +1 -1
- package/build/Calendar/Calendar.messages.js +12 -0
- package/build/Calendar/Calendar.messages.js.map +1 -1
- package/build/Calendar/Calendar.messages.mjs +12 -0
- package/build/Calendar/Calendar.messages.mjs.map +1 -1
- package/build/Calendar/Calendar.mjs +117 -17
- package/build/Calendar/Calendar.mjs.map +1 -1
- package/build/Calendar/DateHeader/DateHeader.js +5 -1
- package/build/Calendar/DateHeader/DateHeader.js.map +1 -1
- package/build/Calendar/DateHeader/DateHeader.mjs +5 -1
- package/build/Calendar/DateHeader/DateHeader.mjs.map +1 -1
- package/build/Calendar/DayCalendar/DayCalendar.js +20 -2
- package/build/Calendar/DayCalendar/DayCalendar.js.map +1 -1
- package/build/Calendar/DayCalendar/DayCalendar.mjs +20 -2
- package/build/Calendar/DayCalendar/DayCalendar.mjs.map +1 -1
- package/build/Calendar/DayCalendar/Table/DayCalendarTable.js +60 -19
- package/build/Calendar/DayCalendar/Table/DayCalendarTable.js.map +1 -1
- package/build/Calendar/DayCalendar/Table/DayCalendarTable.mjs +60 -19
- package/build/Calendar/DayCalendar/Table/DayCalendarTable.mjs.map +1 -1
- package/build/Calendar/MonthCalendar/MonthCalendar.js +6 -0
- package/build/Calendar/MonthCalendar/MonthCalendar.js.map +1 -1
- package/build/Calendar/MonthCalendar/MonthCalendar.mjs +6 -0
- package/build/Calendar/MonthCalendar/MonthCalendar.mjs.map +1 -1
- package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.js +8 -3
- package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.js.map +1 -1
- package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.mjs +8 -3
- package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.mjs.map +1 -1
- package/build/Calendar/TableLink/TableLink.js +91 -0
- package/build/Calendar/TableLink/TableLink.js.map +1 -0
- package/build/Calendar/TableLink/TableLink.mjs +87 -0
- package/build/Calendar/TableLink/TableLink.mjs.map +1 -0
- package/build/Calendar/YearCalendar/Table/YearCalendarTable.js +8 -3
- package/build/Calendar/YearCalendar/Table/YearCalendarTable.js.map +1 -1
- package/build/Calendar/YearCalendar/Table/YearCalendarTable.mjs +8 -3
- package/build/Calendar/YearCalendar/Table/YearCalendarTable.mjs.map +1 -1
- package/build/Calendar/YearCalendar/YearCalendar.js +7 -0
- package/build/Calendar/YearCalendar/YearCalendar.js.map +1 -1
- package/build/Calendar/YearCalendar/YearCalendar.mjs +7 -0
- package/build/Calendar/YearCalendar/YearCalendar.mjs.map +1 -1
- package/build/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.js +33 -0
- package/build/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.js.map +1 -0
- package/build/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.mjs +31 -0
- package/build/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.mjs.map +1 -0
- package/build/Calendar/_helpers/isDateDisabled/isDateDisabled.js +40 -0
- package/build/Calendar/_helpers/isDateDisabled/isDateDisabled.js.map +1 -0
- package/build/Calendar/_helpers/isDateDisabled/isDateDisabled.mjs +38 -0
- package/build/Calendar/_helpers/isDateDisabled/isDateDisabled.mjs.map +1 -0
- package/build/DateLookup/DateLookup.js +20 -9
- package/build/DateLookup/DateLookup.js.map +1 -1
- package/build/DateLookup/DateLookup.mjs +20 -9
- package/build/DateLookup/DateLookup.mjs.map +1 -1
- package/build/i18n/en.json +4 -0
- package/build/i18n/en.json.js +4 -0
- package/build/i18n/en.json.js.map +1 -1
- package/build/i18n/en.json.mjs +4 -0
- package/build/i18n/en.json.mjs.map +1 -1
- package/build/i18n/hu.json +1 -1
- package/build/i18n/hu.json.js +1 -1
- package/build/i18n/hu.json.mjs +1 -1
- package/build/index.js +2 -2
- package/build/index.mjs +1 -1
- package/build/main.css +123 -43
- package/build/styles/Calendar/Calendar.css +112 -37
- package/build/styles/Calendar/TableLink/TableLink.css +115 -0
- package/build/styles/main.css +123 -43
- package/build/types/Calendar/Calendar.constants.d.ts +2 -0
- package/build/types/Calendar/Calendar.constants.d.ts.map +1 -0
- package/build/types/Calendar/Calendar.d.ts +16 -3
- package/build/types/Calendar/Calendar.d.ts.map +1 -1
- package/build/types/Calendar/Calendar.messages.d.ts +20 -0
- package/build/types/Calendar/Calendar.messages.d.ts.map +1 -1
- package/build/types/Calendar/Calendar.types.d.ts +32 -0
- package/build/types/Calendar/Calendar.types.d.ts.map +1 -0
- package/build/types/Calendar/DateHeader/DateHeader.d.ts +2 -0
- package/build/types/Calendar/DateHeader/DateHeader.d.ts.map +1 -1
- package/build/types/Calendar/DayCalendar/DayCalendar.d.ts +7 -1
- package/build/types/Calendar/DayCalendar/DayCalendar.d.ts.map +1 -1
- package/build/types/Calendar/DayCalendar/Table/DayCalendarTable.d.ts +6 -0
- package/build/types/Calendar/DayCalendar/Table/DayCalendarTable.d.ts.map +1 -1
- package/build/types/Calendar/MonthCalendar/MonthCalendar.d.ts +1 -0
- package/build/types/Calendar/MonthCalendar/MonthCalendar.d.ts.map +1 -1
- package/build/types/Calendar/MonthCalendar/Table/MonthCalendarTable.d.ts +2 -1
- package/build/types/Calendar/MonthCalendar/Table/MonthCalendarTable.d.ts.map +1 -1
- package/build/types/Calendar/{tableLink → TableLink}/TableLink.d.ts +5 -1
- package/build/types/Calendar/TableLink/TableLink.d.ts.map +1 -0
- package/build/types/Calendar/TableLink/index.d.ts.map +1 -0
- package/build/types/Calendar/YearCalendar/Table/YearCalendarTable.d.ts +2 -1
- package/build/types/Calendar/YearCalendar/Table/YearCalendarTable.d.ts.map +1 -1
- package/build/types/Calendar/YearCalendar/YearCalendar.d.ts +1 -0
- package/build/types/Calendar/YearCalendar/YearCalendar.d.ts.map +1 -1
- package/build/types/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.d.ts +3 -0
- package/build/types/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.d.ts.map +1 -0
- package/build/types/Calendar/_helpers/index.d.ts +3 -0
- package/build/types/Calendar/_helpers/index.d.ts.map +1 -0
- package/build/types/Calendar/_helpers/isDateDisabled/isDateDisabled.d.ts +3 -0
- package/build/types/Calendar/_helpers/isDateDisabled/isDateDisabled.d.ts.map +1 -0
- package/build/types/Calendar/index.d.ts +1 -0
- package/build/types/Calendar/index.d.ts.map +1 -1
- package/build/types/DateLookup/DateLookup.d.ts +24 -5
- package/build/types/DateLookup/DateLookup.d.ts.map +1 -1
- package/build/types/DateLookup/index.d.ts +1 -0
- package/build/types/DateLookup/index.d.ts.map +1 -1
- package/build/types/index.d.ts +3 -2
- package/build/types/index.d.ts.map +1 -1
- package/build/types/test-utils/index.d.ts +8 -0
- package/build/types/test-utils/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/Calendar/Calendar.constants.ts +5 -0
- package/src/Calendar/Calendar.css +112 -37
- package/src/Calendar/Calendar.disabledDates.test.tsx +593 -0
- package/src/Calendar/Calendar.less +5 -35
- package/src/Calendar/Calendar.messages.ts +21 -0
- package/src/Calendar/Calendar.test.tsx +310 -28
- package/src/Calendar/Calendar.tsx +159 -18
- package/src/Calendar/Calendar.types.ts +35 -0
- package/src/Calendar/DateHeader/DateHeader.tsx +6 -0
- package/src/Calendar/DayCalendar/DayCalendar.tsx +39 -1
- package/src/Calendar/DayCalendar/Table/DayCalendarTable.tsx +67 -27
- package/src/Calendar/MonthCalendar/MonthCalendar.tsx +8 -0
- package/src/Calendar/MonthCalendar/Table/MonthCalendarTable.tsx +15 -3
- package/src/Calendar/TableLink/TableLink.css +115 -0
- package/src/Calendar/TableLink/TableLink.less +68 -0
- package/src/Calendar/TableLink/TableLink.tsx +112 -0
- package/src/Calendar/YearCalendar/Table/YearCalendarTable.tsx +15 -3
- package/src/Calendar/YearCalendar/YearCalendar.tsx +9 -0
- package/src/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.test.ts +98 -0
- package/src/Calendar/_helpers/hasDisabledDatesInMonth/hasDisabledDatesInMonth.ts +45 -0
- package/src/Calendar/_helpers/index.ts +2 -0
- package/src/Calendar/_helpers/isDateDisabled/isDateDisabled.test.ts +169 -0
- package/src/Calendar/_helpers/isDateDisabled/isDateDisabled.ts +59 -0
- package/src/Calendar/_stories/Calendar.accessibility.docs.mdx +118 -0
- package/src/Calendar/_stories/Calendar.story.tsx +138 -4
- package/src/Calendar/_stories/Calendar.test.story.tsx +114 -0
- package/src/Calendar/index.ts +7 -0
- package/src/DateLookup/DateLookup.disabledDates.test.tsx +293 -0
- package/src/DateLookup/DateLookup.test.tsx +34 -37
- package/src/DateLookup/DateLookup.tsx +37 -13
- package/src/DateLookup/_stories/DateLookup.accessibility.docs.mdx +108 -0
- package/src/DateLookup/_stories/DateLookup.story.tsx +124 -2
- package/src/DateLookup/index.ts +6 -0
- package/src/i18n/en.json +4 -0
- package/src/i18n/hu.json +1 -1
- package/src/index.ts +3 -2
- package/src/main.css +123 -43
- package/build/Calendar/tableLink/TableLink.js +0 -61
- package/build/Calendar/tableLink/TableLink.js.map +0 -1
- package/build/Calendar/tableLink/TableLink.mjs +0 -57
- package/build/Calendar/tableLink/TableLink.mjs.map +0 -1
- package/build/types/Calendar/tableLink/TableLink.d.ts.map +0 -1
- package/build/types/Calendar/tableLink/index.d.ts.map +0 -1
- package/src/Calendar/tableLink/TableLink.tsx +0 -77
- /package/build/types/Calendar/{tableLink → TableLink}/index.d.ts +0 -0
- /package/src/Calendar/{tableLink → TableLink}/index.ts +0 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { isDateDisabled } from './isDateDisabled';
|
|
2
|
+
import type { DisabledRules } from '../../Calendar.types';
|
|
3
|
+
|
|
4
|
+
describe('isDateDisabled', () => {
|
|
5
|
+
describe('when no rules provided', () => {
|
|
6
|
+
it('returns false when disabledRules is undefined', () => {
|
|
7
|
+
expect(isDateDisabled(new Date('2024-07-15'), undefined)).toBe(false);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('returns false when disabledRules has only reason', () => {
|
|
11
|
+
const rules: DisabledRules = { reason: 'some reason' };
|
|
12
|
+
expect(isDateDisabled(new Date('2024-07-15'), rules)).toBe(false);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('weekdays', () => {
|
|
17
|
+
const rules: DisabledRules = {
|
|
18
|
+
weekdays: ['sat', 'sun'],
|
|
19
|
+
reason: 'Closed on weekends',
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
it('returns true for a Saturday', () => {
|
|
23
|
+
// 2024-07-13 is a Saturday
|
|
24
|
+
expect(isDateDisabled(new Date(2024, 6, 13), rules)).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('returns true for a Sunday', () => {
|
|
28
|
+
// 2024-07-14 is a Sunday
|
|
29
|
+
expect(isDateDisabled(new Date(2024, 6, 14), rules)).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('returns false for a Monday', () => {
|
|
33
|
+
// 2024-07-15 is a Monday
|
|
34
|
+
expect(isDateDisabled(new Date(2024, 6, 15), rules)).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('returns false for a Friday', () => {
|
|
38
|
+
// 2024-07-12 is a Friday
|
|
39
|
+
expect(isDateDisabled(new Date(2024, 6, 12), rules)).toBe(false);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('handles single weekday rule', () => {
|
|
43
|
+
const singleDay: DisabledRules = { weekdays: ['wed'], reason: 'Closed' };
|
|
44
|
+
// 2024-07-17 is a Wednesday
|
|
45
|
+
expect(isDateDisabled(new Date(2024, 6, 17), singleDay)).toBe(true);
|
|
46
|
+
// 2024-07-18 is a Thursday
|
|
47
|
+
expect(isDateDisabled(new Date(2024, 6, 18), singleDay)).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('specific dates', () => {
|
|
52
|
+
const rules: DisabledRules = {
|
|
53
|
+
dates: [new Date(2024, 6, 11), new Date(2025, 1, 12), new Date(2025, 4, 1)],
|
|
54
|
+
reason: 'Bank holidays',
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
it('returns true for a disabled date', () => {
|
|
58
|
+
expect(isDateDisabled(new Date(2024, 6, 11), rules)).toBe(true);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('returns false for a non-disabled date', () => {
|
|
62
|
+
expect(isDateDisabled(new Date(2024, 6, 12), rules)).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('compares date-only, ignoring time', () => {
|
|
66
|
+
const dateWithTime = new Date(2024, 6, 11, 14, 30, 0);
|
|
67
|
+
expect(isDateDisabled(dateWithTime, rules)).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('handles disabled dates that have non-zero time components', () => {
|
|
71
|
+
const rulesWithTime: DisabledRules = {
|
|
72
|
+
dates: [new Date(2024, 6, 11, 23, 59, 59)],
|
|
73
|
+
reason: 'Holiday',
|
|
74
|
+
};
|
|
75
|
+
expect(isDateDisabled(new Date(2024, 6, 11), rulesWithTime)).toBe(true);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('ranges', () => {
|
|
80
|
+
const rules: DisabledRules = {
|
|
81
|
+
ranges: [
|
|
82
|
+
{ start: new Date(2024, 2, 29), end: new Date(2024, 3, 3) },
|
|
83
|
+
{ start: new Date(2024, 11, 24), end: new Date(2024, 11, 26) },
|
|
84
|
+
],
|
|
85
|
+
reason: 'Scheduled maintenance',
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
it('returns true for start of range (inclusive)', () => {
|
|
89
|
+
expect(isDateDisabled(new Date(2024, 2, 29), rules)).toBe(true);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('returns true for end of range (inclusive)', () => {
|
|
93
|
+
expect(isDateDisabled(new Date(2024, 3, 3), rules)).toBe(true);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('returns true for date within range', () => {
|
|
97
|
+
expect(isDateDisabled(new Date(2024, 2, 31), rules)).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('returns false for date before range', () => {
|
|
101
|
+
expect(isDateDisabled(new Date(2024, 2, 28), rules)).toBe(false);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('returns false for date after range', () => {
|
|
105
|
+
expect(isDateDisabled(new Date(2024, 3, 4), rules)).toBe(false);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('handles second range', () => {
|
|
109
|
+
expect(isDateDisabled(new Date(2024, 11, 25), rules)).toBe(true);
|
|
110
|
+
expect(isDateDisabled(new Date(2024, 11, 27), rules)).toBe(false);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('correctly checks a date with non-midnight time against a range', () => {
|
|
114
|
+
const dateWithTime = new Date(2024, 2, 31, 14, 30, 0);
|
|
115
|
+
expect(isDateDisabled(dateWithTime, rules)).toBe(true);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('correctly excludes a date with non-midnight time outside a range', () => {
|
|
119
|
+
const dateWithTime = new Date(2024, 2, 28, 23, 59, 59);
|
|
120
|
+
expect(isDateDisabled(dateWithTime, rules)).toBe(false);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('combined rules (additive)', () => {
|
|
125
|
+
const rules: DisabledRules = {
|
|
126
|
+
weekdays: ['sat', 'sun'],
|
|
127
|
+
dates: [new Date(2024, 6, 15)], // Monday
|
|
128
|
+
ranges: [{ start: new Date(2024, 6, 22), end: new Date(2024, 6, 24) }],
|
|
129
|
+
reason: 'Multiple rules',
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
it('disables by weekday', () => {
|
|
133
|
+
// 2024-07-13 is Saturday
|
|
134
|
+
expect(isDateDisabled(new Date(2024, 6, 13), rules)).toBe(true);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('disables by specific date', () => {
|
|
138
|
+
// 2024-07-15 is Monday but explicitly disabled
|
|
139
|
+
expect(isDateDisabled(new Date(2024, 6, 15), rules)).toBe(true);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('disables by range', () => {
|
|
143
|
+
// 2024-07-23 is Tuesday but in range
|
|
144
|
+
expect(isDateDisabled(new Date(2024, 6, 23), rules)).toBe(true);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('allows date not matching any rule', () => {
|
|
148
|
+
// 2024-07-16 is Tuesday, not in dates or ranges
|
|
149
|
+
expect(isDateDisabled(new Date(2024, 6, 16), rules)).toBe(false);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('edge cases', () => {
|
|
154
|
+
it('handles empty weekdays array', () => {
|
|
155
|
+
const rules: DisabledRules = { weekdays: [], reason: 'Empty' };
|
|
156
|
+
expect(isDateDisabled(new Date(2024, 6, 13), rules)).toBe(false);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('handles empty dates array', () => {
|
|
160
|
+
const rules: DisabledRules = { dates: [], reason: 'Empty' };
|
|
161
|
+
expect(isDateDisabled(new Date(2024, 6, 13), rules)).toBe(false);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('handles empty ranges array', () => {
|
|
165
|
+
const rules: DisabledRules = { ranges: [], reason: 'Empty' };
|
|
166
|
+
expect(isDateDisabled(new Date(2024, 6, 13), rules)).toBe(false);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { DisabledRules, WeekDayType } from '../../Calendar.types';
|
|
2
|
+
|
|
3
|
+
const DAY_INDEX_TO_WEEKDAY: Record<number, WeekDayType> = {
|
|
4
|
+
0: 'sun',
|
|
5
|
+
1: 'mon',
|
|
6
|
+
2: 'tue',
|
|
7
|
+
3: 'wed',
|
|
8
|
+
4: 'thu',
|
|
9
|
+
5: 'fri',
|
|
10
|
+
6: 'sat',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function isDateDisabled(date: Date, disabledRules?: DisabledRules): boolean {
|
|
14
|
+
if (!disabledRules) return false;
|
|
15
|
+
|
|
16
|
+
if (disabledRules.weekdays?.length) {
|
|
17
|
+
const weekday = DAY_INDEX_TO_WEEKDAY[date.getDay()];
|
|
18
|
+
if (disabledRules.weekdays.includes(weekday)) return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (disabledRules.dates?.length) {
|
|
22
|
+
const year = date.getFullYear();
|
|
23
|
+
const month = date.getMonth();
|
|
24
|
+
const day = date.getDate();
|
|
25
|
+
if (
|
|
26
|
+
disabledRules.dates.some(
|
|
27
|
+
(disabledDate) =>
|
|
28
|
+
disabledDate.getFullYear() === year &&
|
|
29
|
+
disabledDate.getMonth() === month &&
|
|
30
|
+
disabledDate.getDate() === day,
|
|
31
|
+
)
|
|
32
|
+
) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (disabledRules.ranges?.length) {
|
|
38
|
+
const dateStart = date.getTime();
|
|
39
|
+
if (
|
|
40
|
+
disabledRules.ranges.some((range) => {
|
|
41
|
+
const rangeStart = new Date(
|
|
42
|
+
range.start.getFullYear(),
|
|
43
|
+
range.start.getMonth(),
|
|
44
|
+
range.start.getDate(),
|
|
45
|
+
).getTime();
|
|
46
|
+
const rangeEnd = new Date(
|
|
47
|
+
range.end.getFullYear(),
|
|
48
|
+
range.end.getMonth(),
|
|
49
|
+
range.end.getDate(),
|
|
50
|
+
).getTime();
|
|
51
|
+
return dateStart >= rangeStart && dateStart <= rangeEnd;
|
|
52
|
+
})
|
|
53
|
+
) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Meta } from '@storybook/addon-docs/blocks';
|
|
2
|
+
|
|
3
|
+
<Meta title="Forms/Calendar/Accessibility" />
|
|
4
|
+
|
|
5
|
+
# Accessibility
|
|
6
|
+
|
|
7
|
+
Calendar is a fully keyboard-accessible date picker grid. All dates — including disabled ones — are reachable via keyboard so that screen reader users can discover which dates are unavailable and why.
|
|
8
|
+
|
|
9
|
+
## Keyboard Support
|
|
10
|
+
|
|
11
|
+
<table>
|
|
12
|
+
<thead>
|
|
13
|
+
<tr>
|
|
14
|
+
<th>Key</th>
|
|
15
|
+
<th>Action</th>
|
|
16
|
+
</tr>
|
|
17
|
+
</thead>
|
|
18
|
+
<tbody>
|
|
19
|
+
<tr>
|
|
20
|
+
<td>
|
|
21
|
+
<code>ArrowLeft</code>
|
|
22
|
+
</td>
|
|
23
|
+
<td>Move focus to previous day / month / year</td>
|
|
24
|
+
</tr>
|
|
25
|
+
<tr>
|
|
26
|
+
<td>
|
|
27
|
+
<code>ArrowRight</code>
|
|
28
|
+
</td>
|
|
29
|
+
<td>Move focus to next day / month / year</td>
|
|
30
|
+
</tr>
|
|
31
|
+
<tr>
|
|
32
|
+
<td>
|
|
33
|
+
<code>ArrowUp</code>
|
|
34
|
+
</td>
|
|
35
|
+
<td>Move focus up by one week (7 days) / 4 months / 4 years</td>
|
|
36
|
+
</tr>
|
|
37
|
+
<tr>
|
|
38
|
+
<td>
|
|
39
|
+
<code>ArrowDown</code>
|
|
40
|
+
</td>
|
|
41
|
+
<td>Move focus down by one week (7 days) / 4 months / 4 years</td>
|
|
42
|
+
</tr>
|
|
43
|
+
<tr>
|
|
44
|
+
<td>
|
|
45
|
+
<code>Enter</code> / <code>Space</code>
|
|
46
|
+
</td>
|
|
47
|
+
<td>Select the focused date (if enabled)</td>
|
|
48
|
+
</tr>
|
|
49
|
+
</tbody>
|
|
50
|
+
</table>
|
|
51
|
+
|
|
52
|
+
<br /> Navigation **does not skip** disabled dates. Users can arrow through every date in the grid
|
|
53
|
+
regardless of its enabled state.
|
|
54
|
+
|
|
55
|
+
## Screen Reader Announcements
|
|
56
|
+
|
|
57
|
+
Each date cell is a `<button>` with an `aria-label` in a human-readable long format (e.g. "6 July 2024") constructed via `Intl.DateTimeFormat` for full locale support.
|
|
58
|
+
|
|
59
|
+
### Active (Selected) Date
|
|
60
|
+
|
|
61
|
+
The currently selected date includes "selected day" in its aria-label:
|
|
62
|
+
|
|
63
|
+
> "6 July 2024, selected day"
|
|
64
|
+
|
|
65
|
+
### Disabled Dates (via `disabledRules`)
|
|
66
|
+
|
|
67
|
+
Dates disabled via `disabledRules` remain focusable and keyboard-navigable. They are announced with additional context:
|
|
68
|
+
|
|
69
|
+
- `aria-disabled="true"` signals the date is not selectable
|
|
70
|
+
- The aria-label appends "not available" (e.g. "6 July 2024, not available")
|
|
71
|
+
- `aria-describedby` links to the InlinePrompt element containing the reason text
|
|
72
|
+
|
|
73
|
+
This means a screen reader will announce something like:
|
|
74
|
+
|
|
75
|
+
> "6 July 2024, not available — We are closed on weekends. button, dimmed."
|
|
76
|
+
|
|
77
|
+
### Disabled + Selected
|
|
78
|
+
|
|
79
|
+
If the focused date is both active and disabled:
|
|
80
|
+
|
|
81
|
+
> "6 July 2024, selected day, not available"
|
|
82
|
+
|
|
83
|
+
### Out-of-Range Dates (via `min`/`max`)
|
|
84
|
+
|
|
85
|
+
Dates outside `min`/`max` boundaries use `aria-disabled="true"` (not the native HTML `disabled` attribute). They are rendered as interactive buttons in the grid: they can be clicked and the `aria-label` appends "not available". Arrow key navigation clamps at the boundary, so keyboard users cannot navigate into out-of-range cells — but they remain visible and clickable for pointer users.
|
|
86
|
+
|
|
87
|
+
This distinction is intentional: `min`/`max` defines a hard boundary (the calendar simply doesn't extend beyond it), while `disabledRules` marks dates that exist but are unavailable (users should be able to discover them and understand why).
|
|
88
|
+
|
|
89
|
+
## InlinePrompt Behaviour
|
|
90
|
+
|
|
91
|
+
An `InlinePrompt` renders below the day grid when the current month contains disabled dates. It displays the `disabledRules.reason` text.
|
|
92
|
+
|
|
93
|
+
### When It Appears
|
|
94
|
+
|
|
95
|
+
The prompt is visible when **all** of the following are true:
|
|
96
|
+
|
|
97
|
+
1. The Calendar is in day view
|
|
98
|
+
2. `disabledRules.reason` is provided
|
|
99
|
+
3. The currently displayed month contains at least one disabled date
|
|
100
|
+
|
|
101
|
+
### How It's Announced
|
|
102
|
+
|
|
103
|
+
The prompt is linked to disabled date cells via `aria-describedby`. This means the reason text is read as a description when a disabled cell receives focus — screen readers announce it after the label and role.
|
|
104
|
+
|
|
105
|
+
The prompt element has an `id` that matches the `aria-describedby` on each disabled cell. It is **not** a live region (`role="alert"` or `role="status"`) because:
|
|
106
|
+
|
|
107
|
+
- It does not change dynamically while focus is on a disabled cell
|
|
108
|
+
- Announcing it on every arrow key press would be disruptive
|
|
109
|
+
- The `aria-describedby` relationship already ensures it's read on focus
|
|
110
|
+
|
|
111
|
+
### Sentiment
|
|
112
|
+
|
|
113
|
+
The prompt's visual sentiment changes based on the currently focused date:
|
|
114
|
+
|
|
115
|
+
- **Negative** (red) — when the focused date is disabled
|
|
116
|
+
- **Neutral** — when the focused date is enabled
|
|
117
|
+
|
|
118
|
+
This is a visual-only change and does not affect screen reader output.
|
|
@@ -1,22 +1,47 @@
|
|
|
1
1
|
import { useState } from 'react';
|
|
2
2
|
import { StoryObj } from '@storybook/react-webpack5';
|
|
3
|
-
import { userEvent, within } from 'storybook/test';
|
|
3
|
+
import { fn, userEvent, within } from 'storybook/test';
|
|
4
4
|
import {
|
|
5
5
|
createSandboxStory,
|
|
6
6
|
globalScope,
|
|
7
7
|
} from '../../../.storybook/components/sandbox/SandboxEditor';
|
|
8
|
+
import { storySourceWithoutNoise } from '../../../.storybook/helpers';
|
|
8
9
|
import Calendar, { type CalendarProps } from '../Calendar';
|
|
9
10
|
import Modal from '../../Modal';
|
|
10
11
|
import Button from '../../Button';
|
|
12
|
+
import { action } from 'storybook/actions';
|
|
11
13
|
|
|
12
|
-
/** Inline calendar for selecting a single date, with optional min/max bounds. */
|
|
13
14
|
export default {
|
|
14
15
|
component: Calendar,
|
|
15
16
|
title: 'Forms/Calendar',
|
|
17
|
+
args: {
|
|
18
|
+
value: undefined,
|
|
19
|
+
min: undefined,
|
|
20
|
+
max: undefined,
|
|
21
|
+
monthFormat: undefined,
|
|
22
|
+
placeholder: undefined,
|
|
23
|
+
disabledRules: undefined,
|
|
24
|
+
onChange: fn(),
|
|
25
|
+
onDateFocus: fn(),
|
|
26
|
+
},
|
|
16
27
|
argTypes: {
|
|
17
28
|
monthFormat: { control: 'select', options: ['long', 'short'] },
|
|
18
29
|
min: { control: 'date' },
|
|
19
30
|
max: { control: 'date' },
|
|
31
|
+
disabledRules: {
|
|
32
|
+
control: false,
|
|
33
|
+
table: {
|
|
34
|
+
type: {
|
|
35
|
+
summary: 'DisabledRules',
|
|
36
|
+
detail: `{
|
|
37
|
+
weekdays?: ('mon'|'tue'|'wed'|'thu'|'fri'|'sat'|'sun')[]
|
|
38
|
+
dates?: Date[]
|
|
39
|
+
ranges?: { start: Date; end: Date }[]
|
|
40
|
+
reason: string // required — shown in prompt, linked via aria-describedby
|
|
41
|
+
}`,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
},
|
|
20
45
|
},
|
|
21
46
|
};
|
|
22
47
|
|
|
@@ -30,12 +55,17 @@ thePast.setUTCDate(epoch.getUTCDate() - 10);
|
|
|
30
55
|
|
|
31
56
|
export const Sandbox = createSandboxStory({
|
|
32
57
|
code: `const App = () => {
|
|
33
|
-
const [value, setValue] = React.useState(new Date(
|
|
58
|
+
const [value, setValue] = React.useState(new Date(2024, 5, 18));
|
|
34
59
|
|
|
35
60
|
return (
|
|
36
61
|
<Calendar
|
|
37
62
|
monthFormat="long"
|
|
38
63
|
value={value}
|
|
64
|
+
disabledRules={{
|
|
65
|
+
weekdays: ['sat', 'sun'],
|
|
66
|
+
dates: [new Date(2024, 5, 19)],
|
|
67
|
+
reason: 'Weekends and holidays are unavailable.',
|
|
68
|
+
}}
|
|
39
69
|
onChange={(date) => setValue(date)}
|
|
40
70
|
/>
|
|
41
71
|
);
|
|
@@ -52,7 +82,13 @@ export const Basic: Story = {
|
|
|
52
82
|
min={args.min ? new Date(args.min) : thePast}
|
|
53
83
|
max={args.max ? new Date(args.max) : theFuture}
|
|
54
84
|
value={value}
|
|
55
|
-
|
|
85
|
+
onDateFocus={(date, options) => {
|
|
86
|
+
action('onDateFocus')(date, options);
|
|
87
|
+
}}
|
|
88
|
+
onChange={(date) => {
|
|
89
|
+
action('onChange')(date);
|
|
90
|
+
setValue(date);
|
|
91
|
+
}}
|
|
56
92
|
/>
|
|
57
93
|
);
|
|
58
94
|
},
|
|
@@ -102,3 +138,101 @@ export const InsideModal: Story = {
|
|
|
102
138
|
await userEvent.click(canvas.getByRole('button', { name: 'Open modal' }));
|
|
103
139
|
},
|
|
104
140
|
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Use `disabledRules` to disable specific weekdays, individual dates, or date ranges.
|
|
144
|
+
* Disabled dates remain focusable and keyboard-navigable — clicking or focusing one
|
|
145
|
+
* fires `onDateFocus` with `isEnabled: false` and shows a negative inline prompt with
|
|
146
|
+
* the `reason` text. `min`/`max` out-of-range dates show a built-in boundary prompt
|
|
147
|
+
* (e.g. "Please select a date between 4 December 2024 and 18 January 2025.").
|
|
148
|
+
*
|
|
149
|
+
* The `reason` field is **required for accessibility**: it renders as an InlinePrompt
|
|
150
|
+
* below the grid and is linked to every disabled cell via `aria-describedby`, so screen
|
|
151
|
+
* reader users understand why a date is unavailable. For per-date reason customisation
|
|
152
|
+
* see [DisabledDatesWithDynamicReason](#disabled-dates-with-dynamic-reason).
|
|
153
|
+
*/
|
|
154
|
+
export const DisabledDates: Story = storySourceWithoutNoise({
|
|
155
|
+
render: function Render() {
|
|
156
|
+
const [value, setValue] = useState<Date | null>(new Date(2024, 11, 13));
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<Calendar
|
|
160
|
+
value={value}
|
|
161
|
+
min={new Date(2024, 11, 4)}
|
|
162
|
+
max={new Date(2025, 0, 18)}
|
|
163
|
+
disabledRules={{
|
|
164
|
+
weekdays: ['sat', 'sun'],
|
|
165
|
+
dates: [new Date(2024, 11, 10), new Date(2025, 0, 15)],
|
|
166
|
+
ranges: [{ start: new Date(2024, 11, 23), end: new Date(2024, 11, 27) }],
|
|
167
|
+
reason: 'Some dates are unavailable for this service.',
|
|
168
|
+
}}
|
|
169
|
+
onDateFocus={(date, { isEnabled }) => {
|
|
170
|
+
action('onDateFocus')(date, { isEnabled });
|
|
171
|
+
}}
|
|
172
|
+
onChange={(date) => {
|
|
173
|
+
action('onChange')(date);
|
|
174
|
+
setValue(date);
|
|
175
|
+
}}
|
|
176
|
+
/>
|
|
177
|
+
);
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* When different disabled dates warrant different explanations, drive the `reason`
|
|
183
|
+
* dynamically from `onDateFocus`. The `isEnabled` flag tells you whether the focused
|
|
184
|
+
* date is selectable; from there you can look up the specific reason and update state.
|
|
185
|
+
*
|
|
186
|
+
* `min`/`max` out-of-range dates are handled automatically — the component shows a
|
|
187
|
+
* built-in prompt with the boundary dates, so `determineReason` only needs to cover
|
|
188
|
+
* rule-disabled dates.
|
|
189
|
+
*/
|
|
190
|
+
export const DisabledDatesWithDynamicReason: Story = storySourceWithoutNoise({
|
|
191
|
+
render: function Render() {
|
|
192
|
+
const DEFAULT_REASON = 'Some dates are unavailable for this service.';
|
|
193
|
+
const min = new Date(2024, 11, 4);
|
|
194
|
+
const max = new Date(2025, 0, 18);
|
|
195
|
+
const maintenanceDays = [new Date(2024, 11, 10), new Date(2025, 0, 15)];
|
|
196
|
+
const [value, setValue] = useState<Date | null>(new Date(2024, 11, 13));
|
|
197
|
+
const [reason, setReason] = useState(DEFAULT_REASON);
|
|
198
|
+
|
|
199
|
+
function determineReason(date: Date, isEnabled: boolean): string {
|
|
200
|
+
if (isEnabled) return DEFAULT_REASON;
|
|
201
|
+
if (
|
|
202
|
+
maintenanceDays.some(
|
|
203
|
+
(maintenanceDay) => maintenanceDay.toDateString() === date.toDateString(),
|
|
204
|
+
)
|
|
205
|
+
) {
|
|
206
|
+
return 'This date is a maintenance day.';
|
|
207
|
+
}
|
|
208
|
+
if (date.getDay() === 0 || date.getDay() === 6) {
|
|
209
|
+
return 'We do not operate on weekends.';
|
|
210
|
+
}
|
|
211
|
+
return 'This date is unavailable.';
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return (
|
|
215
|
+
<Calendar
|
|
216
|
+
value={value}
|
|
217
|
+
min={min}
|
|
218
|
+
max={max}
|
|
219
|
+
disabledRules={{
|
|
220
|
+
weekdays: ['sat', 'sun'],
|
|
221
|
+
dates: maintenanceDays,
|
|
222
|
+
ranges: [{ start: new Date(2024, 11, 23), end: new Date(2024, 11, 27) }],
|
|
223
|
+
reason,
|
|
224
|
+
}}
|
|
225
|
+
onDateFocus={(date, { isEnabled }) => {
|
|
226
|
+
action('onDateFocus')(date, { isEnabled });
|
|
227
|
+
if (date) {
|
|
228
|
+
setReason(determineReason(date, isEnabled));
|
|
229
|
+
}
|
|
230
|
+
}}
|
|
231
|
+
onChange={(date) => {
|
|
232
|
+
action('onChange')(date);
|
|
233
|
+
setValue(date);
|
|
234
|
+
}}
|
|
235
|
+
/>
|
|
236
|
+
);
|
|
237
|
+
},
|
|
238
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react-webpack5';
|
|
2
|
+
import { expect, waitFor, within, userEvent } from 'storybook/test';
|
|
3
|
+
|
|
4
|
+
import Calendar from '../Calendar';
|
|
5
|
+
import type { DisabledRules } from '../Calendar.types';
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
component: Calendar,
|
|
9
|
+
title: 'Forms/Calendar/Tests',
|
|
10
|
+
tags: ['!autodocs', '!manifest'],
|
|
11
|
+
} satisfies Meta<typeof Calendar>;
|
|
12
|
+
|
|
13
|
+
type Story = StoryObj<typeof Calendar>;
|
|
14
|
+
|
|
15
|
+
// min: March 2024, max: October 2024
|
|
16
|
+
const min = new Date(2024, 2, 1);
|
|
17
|
+
const max = new Date(2024, 9, 31);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Year view: years within min/max are enabled, years outside are disabled (aria-disabled),
|
|
21
|
+
* and both navigation arrows are disabled because min and max fall within the same 2020–2039 block.
|
|
22
|
+
*/
|
|
23
|
+
export const YearViewBoundaries: Story = {
|
|
24
|
+
render: () => <Calendar value={new Date(2024, 5, 15)} min={min} max={max} />,
|
|
25
|
+
play: async ({ canvasElement }) => {
|
|
26
|
+
const canvas = within(canvasElement);
|
|
27
|
+
|
|
28
|
+
await userEvent.click(canvas.getByRole('button', { name: /year view/iu }));
|
|
29
|
+
|
|
30
|
+
// Selected year (within range) is enabled
|
|
31
|
+
await expect(
|
|
32
|
+
canvas.getByRole('button', { name: /2024.*selected year/iu }),
|
|
33
|
+
).not.toHaveAttribute('aria-disabled');
|
|
34
|
+
|
|
35
|
+
// Adjacent years outside range are disabled
|
|
36
|
+
await expect(
|
|
37
|
+
canvas.getByRole('button', { name: /^2023.*not available/iu }),
|
|
38
|
+
).toHaveAttribute('aria-disabled', 'true');
|
|
39
|
+
await expect(
|
|
40
|
+
canvas.getByRole('button', { name: /^2025.*not available/iu }),
|
|
41
|
+
).toHaveAttribute('aria-disabled', 'true');
|
|
42
|
+
|
|
43
|
+
// Both nav arrows disabled — min and max are both in the 2020–2039 block
|
|
44
|
+
await expect(canvas.getByRole('button', { name: /previous 20 years/iu })).toBeDisabled();
|
|
45
|
+
await expect(canvas.getByRole('button', { name: /next 20 years/iu })).toBeDisabled();
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Month view: months within min/max are enabled, months outside are disabled (aria-disabled),
|
|
51
|
+
* and both navigation arrows are disabled because min and max are both in 2024.
|
|
52
|
+
*/
|
|
53
|
+
export const MonthViewBoundaries: Story = {
|
|
54
|
+
render: () => <Calendar value={new Date(2024, 5, 15)} min={min} max={max} />,
|
|
55
|
+
play: async ({ canvasElement }) => {
|
|
56
|
+
const canvas = within(canvasElement);
|
|
57
|
+
|
|
58
|
+
// Navigate: year view → click 2024 → month view
|
|
59
|
+
await userEvent.click(canvas.getByRole('button', { name: /year view/iu }));
|
|
60
|
+
await userEvent.click(canvas.getByRole('button', { name: /2024.*selected year/iu }));
|
|
61
|
+
|
|
62
|
+
// Selected month (within range) is enabled
|
|
63
|
+
await expect(
|
|
64
|
+
canvas.getByRole('button', { name: /^Jun.*selected month/iu }),
|
|
65
|
+
).not.toHaveAttribute('aria-disabled');
|
|
66
|
+
|
|
67
|
+
// Boundary months (min=Mar, max=Oct) are enabled
|
|
68
|
+
await expect(
|
|
69
|
+
canvas.getByRole('button', { name: /^Mar(?!.*not available)/iu }),
|
|
70
|
+
).not.toHaveAttribute('aria-disabled');
|
|
71
|
+
await expect(
|
|
72
|
+
canvas.getByRole('button', { name: /^Oct(?!.*not available)/iu }),
|
|
73
|
+
).not.toHaveAttribute('aria-disabled');
|
|
74
|
+
|
|
75
|
+
// Months outside range are disabled
|
|
76
|
+
await expect(
|
|
77
|
+
canvas.getByRole('button', { name: /^Feb.*not available/iu }),
|
|
78
|
+
).toHaveAttribute('aria-disabled', 'true');
|
|
79
|
+
await expect(
|
|
80
|
+
canvas.getByRole('button', { name: /^Nov.*not available/iu }),
|
|
81
|
+
).toHaveAttribute('aria-disabled', 'true');
|
|
82
|
+
|
|
83
|
+
// Both nav arrows disabled — min and max are both in 2024
|
|
84
|
+
await expect(canvas.getByRole('button', { name: /previous year/iu })).toBeDisabled();
|
|
85
|
+
await expect(canvas.getByRole('button', { name: /next year/iu })).toBeDisabled();
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const disabledRules: DisabledRules = {
|
|
90
|
+
weekdays: ['sat', 'sun'],
|
|
91
|
+
reason: 'We are closed on weekends.',
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* When a disabled cell receives direct DOM focus (e.g. screen reader virtual cursor),
|
|
96
|
+
* the inline prompt updates to negative sentiment showing the disabled reason.
|
|
97
|
+
*/
|
|
98
|
+
export const DisabledDateFocusUpdatesPrompt: Story = {
|
|
99
|
+
render: () => <Calendar value={new Date(2024, 6, 5)} disabledRules={disabledRules} />,
|
|
100
|
+
play: async ({ canvasElement }) => {
|
|
101
|
+
const canvas = within(canvasElement);
|
|
102
|
+
|
|
103
|
+
// Initially prompt is neutral (focused date is enabled)
|
|
104
|
+
const prompt = canvasElement.querySelector('.wds-inline-prompt')!;
|
|
105
|
+
await expect(prompt).toHaveClass('wds-prompt--neutral');
|
|
106
|
+
|
|
107
|
+
// Simulate screen reader focus on a disabled Saturday (matches both "6 July 2024" and "July 6, 2024")
|
|
108
|
+
const saturday = canvas.getByLabelText(/(?:6 July|July 6).*2024/u);
|
|
109
|
+
saturday.focus();
|
|
110
|
+
|
|
111
|
+
// Prompt should switch to negative after React re-render
|
|
112
|
+
await waitFor(() => expect(prompt).toHaveClass('wds-prompt--negative'));
|
|
113
|
+
},
|
|
114
|
+
};
|
package/src/Calendar/index.ts
CHANGED