@transferwise/components 0.0.0-experimental-b77dac7 → 0.0.0-experimental-fa3aa98

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.
Files changed (154) hide show
  1. package/build/Calendar/Calendar.js +94 -16
  2. package/build/Calendar/Calendar.js.map +1 -1
  3. package/build/Calendar/Calendar.messages.js +12 -0
  4. package/build/Calendar/Calendar.messages.js.map +1 -1
  5. package/build/Calendar/Calendar.messages.mjs +12 -0
  6. package/build/Calendar/Calendar.messages.mjs.map +1 -1
  7. package/build/Calendar/Calendar.mjs +95 -17
  8. package/build/Calendar/Calendar.mjs.map +1 -1
  9. package/build/Calendar/DateHeader/DateHeader.js +5 -1
  10. package/build/Calendar/DateHeader/DateHeader.js.map +1 -1
  11. package/build/Calendar/DateHeader/DateHeader.mjs +5 -1
  12. package/build/Calendar/DateHeader/DateHeader.mjs.map +1 -1
  13. package/build/Calendar/DayCalendar/DayCalendar.js +14 -0
  14. package/build/Calendar/DayCalendar/DayCalendar.js.map +1 -1
  15. package/build/Calendar/DayCalendar/DayCalendar.mjs +14 -0
  16. package/build/Calendar/DayCalendar/DayCalendar.mjs.map +1 -1
  17. package/build/Calendar/DayCalendar/Table/DayCalendarTable.js +53 -19
  18. package/build/Calendar/DayCalendar/Table/DayCalendarTable.js.map +1 -1
  19. package/build/Calendar/DayCalendar/Table/DayCalendarTable.mjs +53 -19
  20. package/build/Calendar/DayCalendar/Table/DayCalendarTable.mjs.map +1 -1
  21. package/build/Calendar/MonthCalendar/MonthCalendar.js +6 -0
  22. package/build/Calendar/MonthCalendar/MonthCalendar.js.map +1 -1
  23. package/build/Calendar/MonthCalendar/MonthCalendar.mjs +6 -0
  24. package/build/Calendar/MonthCalendar/MonthCalendar.mjs.map +1 -1
  25. package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.js +8 -3
  26. package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.js.map +1 -1
  27. package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.mjs +8 -3
  28. package/build/Calendar/MonthCalendar/Table/MonthCalendarTable.mjs.map +1 -1
  29. package/build/Calendar/{tableLink → TableLink}/TableLink.js +26 -11
  30. package/build/Calendar/TableLink/TableLink.js.map +1 -0
  31. package/build/Calendar/{tableLink → TableLink}/TableLink.mjs +26 -11
  32. package/build/Calendar/TableLink/TableLink.mjs.map +1 -0
  33. package/build/Calendar/YearCalendar/Table/YearCalendarTable.js +8 -3
  34. package/build/Calendar/YearCalendar/Table/YearCalendarTable.js.map +1 -1
  35. package/build/Calendar/YearCalendar/Table/YearCalendarTable.mjs +8 -3
  36. package/build/Calendar/YearCalendar/Table/YearCalendarTable.mjs.map +1 -1
  37. package/build/Calendar/YearCalendar/YearCalendar.js +7 -0
  38. package/build/Calendar/YearCalendar/YearCalendar.js.map +1 -1
  39. package/build/Calendar/YearCalendar/YearCalendar.mjs +7 -0
  40. package/build/Calendar/YearCalendar/YearCalendar.mjs.map +1 -1
  41. package/build/DateLookup/DateLookup.js +20 -9
  42. package/build/DateLookup/DateLookup.js.map +1 -1
  43. package/build/DateLookup/DateLookup.mjs +20 -9
  44. package/build/DateLookup/DateLookup.mjs.map +1 -1
  45. package/build/DefinitionList/DefinitionList.js +1 -2
  46. package/build/DefinitionList/DefinitionList.js.map +1 -1
  47. package/build/DefinitionList/DefinitionList.mjs +1 -2
  48. package/build/DefinitionList/DefinitionList.mjs.map +1 -1
  49. package/build/common/DateUtils/hasDisabledDatesInMonth/hasDisabledDatesInMonth.js +33 -0
  50. package/build/common/DateUtils/hasDisabledDatesInMonth/hasDisabledDatesInMonth.js.map +1 -0
  51. package/build/common/DateUtils/hasDisabledDatesInMonth/hasDisabledDatesInMonth.mjs +31 -0
  52. package/build/common/DateUtils/hasDisabledDatesInMonth/hasDisabledDatesInMonth.mjs.map +1 -0
  53. package/build/common/DateUtils/isDateDisabled/isDateDisabled.js +40 -0
  54. package/build/common/DateUtils/isDateDisabled/isDateDisabled.js.map +1 -0
  55. package/build/common/DateUtils/isDateDisabled/isDateDisabled.mjs +38 -0
  56. package/build/common/DateUtils/isDateDisabled/isDateDisabled.mjs.map +1 -0
  57. package/build/i18n/en.json +4 -0
  58. package/build/i18n/en.json.js +4 -0
  59. package/build/i18n/en.json.js.map +1 -1
  60. package/build/i18n/en.json.mjs +4 -0
  61. package/build/i18n/en.json.mjs.map +1 -1
  62. package/build/index.js +2 -2
  63. package/build/index.mjs +1 -1
  64. package/build/main.css +123 -73
  65. package/build/styles/Calendar/Calendar.css +112 -37
  66. package/build/styles/Calendar/TableLink/TableLink.css +115 -0
  67. package/build/styles/DefinitionList/DefinitionList.css +0 -30
  68. package/build/styles/main.css +123 -73
  69. package/build/types/Calendar/Calendar.d.ts +16 -3
  70. package/build/types/Calendar/Calendar.d.ts.map +1 -1
  71. package/build/types/Calendar/Calendar.messages.d.ts +20 -0
  72. package/build/types/Calendar/Calendar.messages.d.ts.map +1 -1
  73. package/build/types/Calendar/Calendar.types.d.ts +32 -0
  74. package/build/types/Calendar/Calendar.types.d.ts.map +1 -0
  75. package/build/types/Calendar/DateHeader/DateHeader.d.ts +2 -0
  76. package/build/types/Calendar/DateHeader/DateHeader.d.ts.map +1 -1
  77. package/build/types/Calendar/DayCalendar/DayCalendar.d.ts +5 -1
  78. package/build/types/Calendar/DayCalendar/DayCalendar.d.ts.map +1 -1
  79. package/build/types/Calendar/DayCalendar/Table/DayCalendarTable.d.ts +4 -0
  80. package/build/types/Calendar/DayCalendar/Table/DayCalendarTable.d.ts.map +1 -1
  81. package/build/types/Calendar/MonthCalendar/MonthCalendar.d.ts +1 -0
  82. package/build/types/Calendar/MonthCalendar/MonthCalendar.d.ts.map +1 -1
  83. package/build/types/Calendar/MonthCalendar/Table/MonthCalendarTable.d.ts +2 -1
  84. package/build/types/Calendar/MonthCalendar/Table/MonthCalendarTable.d.ts.map +1 -1
  85. package/build/types/Calendar/{tableLink → TableLink}/TableLink.d.ts +3 -1
  86. package/build/types/Calendar/TableLink/TableLink.d.ts.map +1 -0
  87. package/build/types/Calendar/TableLink/index.d.ts.map +1 -0
  88. package/build/types/Calendar/YearCalendar/Table/YearCalendarTable.d.ts +2 -1
  89. package/build/types/Calendar/YearCalendar/Table/YearCalendarTable.d.ts.map +1 -1
  90. package/build/types/Calendar/YearCalendar/YearCalendar.d.ts +1 -0
  91. package/build/types/Calendar/YearCalendar/YearCalendar.d.ts.map +1 -1
  92. package/build/types/Calendar/index.d.ts +1 -0
  93. package/build/types/Calendar/index.d.ts.map +1 -1
  94. package/build/types/DateLookup/DateLookup.d.ts +24 -5
  95. package/build/types/DateLookup/DateLookup.d.ts.map +1 -1
  96. package/build/types/DateLookup/index.d.ts +1 -0
  97. package/build/types/DateLookup/index.d.ts.map +1 -1
  98. package/build/types/DefinitionList/DefinitionList.d.ts.map +1 -1
  99. package/build/types/common/DateUtils/hasDisabledDatesInMonth/hasDisabledDatesInMonth.d.ts +3 -0
  100. package/build/types/common/DateUtils/hasDisabledDatesInMonth/hasDisabledDatesInMonth.d.ts.map +1 -0
  101. package/build/types/common/DateUtils/index.d.ts +2 -0
  102. package/build/types/common/DateUtils/index.d.ts.map +1 -1
  103. package/build/types/common/DateUtils/isDateDisabled/isDateDisabled.d.ts +3 -0
  104. package/build/types/common/DateUtils/isDateDisabled/isDateDisabled.d.ts.map +1 -0
  105. package/build/types/index.d.ts +3 -2
  106. package/build/types/index.d.ts.map +1 -1
  107. package/build/types/test-utils/index.d.ts +8 -0
  108. package/build/types/test-utils/index.d.ts.map +1 -1
  109. package/package.json +1 -1
  110. package/src/Calendar/Calendar.css +112 -37
  111. package/src/Calendar/Calendar.disabledDates.test.tsx +518 -0
  112. package/src/Calendar/Calendar.less +5 -35
  113. package/src/Calendar/Calendar.messages.ts +21 -0
  114. package/src/Calendar/Calendar.test.tsx +288 -27
  115. package/src/Calendar/Calendar.tsx +136 -19
  116. package/src/Calendar/Calendar.types.ts +35 -0
  117. package/src/Calendar/DateHeader/DateHeader.tsx +6 -0
  118. package/src/Calendar/DayCalendar/DayCalendar.tsx +33 -1
  119. package/src/Calendar/DayCalendar/Table/DayCalendarTable.tsx +62 -28
  120. package/src/Calendar/MonthCalendar/MonthCalendar.tsx +8 -0
  121. package/src/Calendar/MonthCalendar/Table/MonthCalendarTable.tsx +15 -3
  122. package/src/Calendar/TableLink/TableLink.css +115 -0
  123. package/src/Calendar/TableLink/TableLink.less +68 -0
  124. package/src/Calendar/{tableLink → TableLink}/TableLink.tsx +29 -14
  125. package/src/Calendar/YearCalendar/Table/YearCalendarTable.tsx +15 -3
  126. package/src/Calendar/YearCalendar/YearCalendar.tsx +9 -0
  127. package/src/Calendar/_stories/Calendar.accessibility.docs.mdx +118 -0
  128. package/src/Calendar/_stories/Calendar.story.tsx +138 -4
  129. package/src/Calendar/_stories/Calendar.test.story.tsx +74 -0
  130. package/src/Calendar/index.ts +7 -0
  131. package/src/DateLookup/DateLookup.disabledDates.test.tsx +293 -0
  132. package/src/DateLookup/DateLookup.test.tsx +34 -37
  133. package/src/DateLookup/DateLookup.tsx +37 -13
  134. package/src/DateLookup/_stories/DateLookup.accessibility.docs.mdx +108 -0
  135. package/src/DateLookup/_stories/DateLookup.story.tsx +124 -2
  136. package/src/DateLookup/index.ts +6 -0
  137. package/src/DefinitionList/DefinitionList.css +0 -30
  138. package/src/DefinitionList/DefinitionList.less +0 -24
  139. package/src/DefinitionList/DefinitionList.tsx +0 -1
  140. package/src/common/DateUtils/hasDisabledDatesInMonth/hasDisabledDatesInMonth.test.ts +98 -0
  141. package/src/common/DateUtils/hasDisabledDatesInMonth/hasDisabledDatesInMonth.ts +45 -0
  142. package/src/common/DateUtils/index.ts +2 -0
  143. package/src/common/DateUtils/isDateDisabled/isDateDisabled.test.ts +169 -0
  144. package/src/common/DateUtils/isDateDisabled/isDateDisabled.ts +59 -0
  145. package/src/i18n/en.json +4 -0
  146. package/src/index.ts +3 -2
  147. package/src/main.css +123 -73
  148. package/build/Calendar/tableLink/TableLink.js.map +0 -1
  149. package/build/Calendar/tableLink/TableLink.mjs.map +0 -1
  150. package/build/types/Calendar/tableLink/TableLink.d.ts.map +0 -1
  151. package/build/types/Calendar/tableLink/index.d.ts.map +0 -1
  152. package/src/DefinitionList/_stories/DefinitionList.test.story.tsx +0 -168
  153. /package/build/types/Calendar/{tableLink → TableLink}/index.d.ts +0 -0
  154. /package/src/Calendar/{tableLink → TableLink}/index.ts +0 -0
@@ -1,16 +1,23 @@
1
1
  import { useState } from 'react';
2
2
  import { StoryObj } from '@storybook/react-webpack5';
3
+ import { fn } from 'storybook/test';
4
+ import { action } from 'storybook/actions';
3
5
  import {
4
6
  createSandboxStory,
5
7
  globalScope,
6
8
  } from '../../../.storybook/components/sandbox/SandboxEditor';
9
+ import { storySourceWithoutNoise } from '../../../.storybook/helpers';
7
10
  import { Size } from '../../common';
8
- import DateLookup, { type DateLookupProps } from '..';
9
11
  import Field from '../../Field';
12
+ import DateLookup, { type DateLookupProps } from '..';
10
13
 
11
14
  export default {
12
15
  component: DateLookup,
13
16
  title: 'Forms/DateLookup',
17
+ args: {
18
+ onFocus: fn(),
19
+ onBlur: fn(),
20
+ },
14
21
  argTypes: {
15
22
  size: { control: 'select', options: [Size.SMALL, Size.MEDIUM, Size.LARGE] },
16
23
  disabled: { control: 'boolean' },
@@ -34,7 +41,7 @@ thePast.setUTCDate(epoch.getUTCDate() - 10);
34
41
 
35
42
  export const Sandbox = createSandboxStory({
36
43
  code: `const App = () => {
37
- const [value, setValue] = React.useState(new Date('2024-06-15'));
44
+ const [value, setValue] = React.useState(new Date(2024, 5, 14));
38
45
 
39
46
  return (
40
47
  <DateLookup
@@ -44,6 +51,11 @@ export const Sandbox = createSandboxStory({
44
51
  placeholder="Choose date"
45
52
  clearable
46
53
  value={value}
54
+ disabledRules={{
55
+ weekdays: ['sat', 'sun'],
56
+ dates: [new Date(2024, 5, 19)],
57
+ reason: 'Weekends and holidays are unavailable.',
58
+ }}
47
59
  onChange={(date) => setValue(date)}
48
60
  />
49
61
  );
@@ -120,3 +132,113 @@ export const RightAligned: Story = {
120
132
  );
121
133
  },
122
134
  };
135
+
136
+ /**
137
+ * Use `disabledRules` to disable specific weekdays, individual dates, or date ranges.
138
+ * Disabled dates remain focusable and keyboard-navigable — clicking or focusing one
139
+ * keeps the panel open, fires `onDateSelect` with `isEnabled: false`, and shows a
140
+ * negative inline prompt with the `reason` text. `min`/`max` out-of-range dates show
141
+ * a built-in boundary prompt (e.g. "Please select a date between 9 December 2024 and
142
+ * 22 January 2025.").
143
+ *
144
+ * The `reason` field is **required for accessibility**: it renders as an InlinePrompt
145
+ * below the grid and is linked to every disabled cell via `aria-describedby`, so screen
146
+ * reader users understand why a date is unavailable. For per-date reason customisation
147
+ * see [DisabledDatesWithDynamicReason](#disabled-dates-with-dynamic-reason).
148
+ */
149
+ export const DisabledDates: Story = storySourceWithoutNoise({
150
+ args: {
151
+ size: Size.MEDIUM,
152
+ label: 'Select a date',
153
+ placeholder: 'Choose date',
154
+ monthFormat: 'long',
155
+ },
156
+ render: function Render(args: DateLookupProps) {
157
+ const min = new Date(2024, 11, 9);
158
+ const max = new Date(2025, 0, 22);
159
+ const bankHolidays = [new Date(2024, 11, 25), new Date(2024, 11, 26), new Date(2025, 0, 1)];
160
+ const [value, setValue] = useState<Date | null>(null);
161
+
162
+ return (
163
+ <DateLookup
164
+ {...args}
165
+ min={min}
166
+ max={max}
167
+ value={value}
168
+ disabledRules={{
169
+ weekdays: ['sat', 'sun'],
170
+ dates: bankHolidays,
171
+ ranges: [{ start: new Date(2024, 11, 23), end: new Date(2024, 11, 24) }],
172
+ reason: 'Some dates are unavailable for this service.',
173
+ }}
174
+ onChange={(date) => {
175
+ action('onChange')(date);
176
+ setValue(date);
177
+ }}
178
+ onDateSelect={({ date, isEnabled }) => {
179
+ action('onDateSelect')({ date, isEnabled });
180
+ }}
181
+ />
182
+ );
183
+ },
184
+ });
185
+
186
+ /**
187
+ * When different disabled dates warrant different explanations, drive the `reason`
188
+ * dynamically from `onDateSelect`. The `isEnabled` flag tells you whether the focused
189
+ * date is selectable; from there you can look up the specific reason and update state.
190
+ *
191
+ * `min`/`max` out-of-range dates are handled automatically — the component shows a
192
+ * built-in prompt with the boundary dates, so `determineReason` only needs to cover
193
+ * rule-disabled dates.
194
+ */
195
+ export const DisabledDatesWithDynamicReason: Story = storySourceWithoutNoise({
196
+ args: {
197
+ size: Size.MEDIUM,
198
+ label: 'Select a date',
199
+ placeholder: 'Choose date',
200
+ monthFormat: 'long',
201
+ },
202
+ render: function Render(args: DateLookupProps) {
203
+ const DEFAULT_REASON = 'Some dates are unavailable for this service.';
204
+ const min = new Date(2024, 11, 9);
205
+ const max = new Date(2025, 0, 22);
206
+ const bankHolidays = [new Date(2024, 11, 25), new Date(2024, 11, 26), new Date(2025, 0, 1)];
207
+ const [value, setValue] = useState<Date | null>(null);
208
+ const [reason, setReason] = useState(DEFAULT_REASON);
209
+
210
+ function determineReason(date: Date, isEnabled: boolean): string {
211
+ if (isEnabled) return DEFAULT_REASON;
212
+ if (bankHolidays.some((holiday) => holiday.toDateString() === date.toDateString())) {
213
+ return 'This date is a bank holiday.';
214
+ }
215
+ if (date.getDay() === 0 || date.getDay() === 6) {
216
+ return 'We do not operate on weekends.';
217
+ }
218
+ return 'This date is unavailable due to scheduled maintenance.';
219
+ }
220
+
221
+ return (
222
+ <DateLookup
223
+ {...args}
224
+ min={min}
225
+ max={max}
226
+ value={value}
227
+ disabledRules={{
228
+ weekdays: ['sat', 'sun'],
229
+ dates: bankHolidays,
230
+ ranges: [{ start: new Date(2024, 11, 23), end: new Date(2024, 11, 24) }],
231
+ reason,
232
+ }}
233
+ onChange={(date) => {
234
+ action('onChange')(date);
235
+ setValue(date);
236
+ }}
237
+ onDateSelect={({ date, isEnabled }) => {
238
+ action('onDateSelect')({ date, isEnabled });
239
+ setReason(determineReason(date, isEnabled));
240
+ }}
241
+ />
242
+ );
243
+ },
244
+ });
@@ -1,2 +1,8 @@
1
1
  export { default } from './DateLookup';
2
2
  export type { DateLookupProps } from './DateLookup';
3
+ export type {
4
+ DisabledRules,
5
+ DisabledDateRange,
6
+ WeekDayType,
7
+ OnDateSelectParams,
8
+ } from '../Calendar/Calendar.types';
@@ -46,36 +46,6 @@
46
46
  margin-bottom: 8px;
47
47
  margin-bottom: var(--size-8);
48
48
  }
49
- .tw-definition-list.tw-definition-list--horizontal-right-aligned {
50
- display: grid;
51
- grid-template-columns: minmax(0, max-content) minmax(0, max-content);
52
- grid-column-gap: 52px;
53
- grid-column-gap: var(--size-52);
54
- -moz-column-gap: 52px;
55
- column-gap: 52px;
56
- -moz-column-gap: var(--size-52);
57
- column-gap: var(--size-52);
58
- justify-content: space-between;
59
- }
60
- .tw-definition-list.tw-definition-list--horizontal-right-aligned .tw-definition-list__item {
61
- display: grid;
62
- grid-template-columns: subgrid;
63
- grid-column: 1 / -1;
64
- }
65
- .tw-definition-list.tw-definition-list--horizontal-right-aligned .tw-definition-list__item dt {
66
- max-width: none;
67
- min-width: 0;
68
- word-wrap: anywhere;
69
- margin-right: 0;
70
- }
71
- [dir="rtl"] .tw-definition-list.tw-definition-list--horizontal-right-aligned .tw-definition-list__item dt {
72
- margin-left: 0;
73
- margin-right: 0;
74
- margin-right: initial;
75
- }
76
- .tw-definition-list.tw-definition-list--horizontal-right-aligned .tw-definition-list__item dd {
77
- min-width: 0;
78
- }
79
49
  }
80
50
  .np-theme-personal .tw-definition-list dt {
81
51
  color: #5d7079;
@@ -52,30 +52,6 @@
52
52
  }
53
53
  }
54
54
  }
55
-
56
- &.tw-definition-list--horizontal-right-aligned {
57
- display: grid;
58
- grid-template-columns: minmax(0, max-content) minmax(0, max-content);
59
- column-gap: var(--size-52);
60
- justify-content: space-between;
61
-
62
- .tw-definition-list__item {
63
- display: grid;
64
- grid-template-columns: subgrid;
65
- grid-column: 1 / -1;
66
-
67
- dt {
68
- max-width: none;
69
- min-width: 0;
70
- overflow-wrap: anywhere;
71
- .margin(right, 0);
72
- }
73
-
74
- dd {
75
- min-width: 0;
76
- }
77
- }
78
- }
79
55
  }
80
56
 
81
57
  .np-theme-personal & {
@@ -69,7 +69,6 @@ export default function DefinitionList({
69
69
  'tw-definition-list--columns flex-column flex-row--sm':
70
70
  layout === Layout.VERTICAL_TWO_COLUMN,
71
71
  'tw-definition-list--horizontal flex-column': isLayoutHorizontal(layout),
72
- 'tw-definition-list--horizontal-right-aligned': layout === Layout.HORIZONTAL_RIGHT_ALIGNED,
73
72
  })}
74
73
  >
75
74
  {definitions
@@ -0,0 +1,98 @@
1
+ import { hasDisabledDatesInMonth } from './hasDisabledDatesInMonth';
2
+ import type { DisabledRules } from '../../../Calendar/Calendar.types';
3
+
4
+ describe('hasDisabledDatesInMonth', () => {
5
+ describe('weekdays', () => {
6
+ it('returns true when weekday rules exist (every month has all weekdays)', () => {
7
+ const rules: DisabledRules = { weekdays: ['sat', 'sun'], reason: 'Closed' };
8
+ expect(hasDisabledDatesInMonth(6, 2024, rules)).toBe(true);
9
+ });
10
+
11
+ it('returns true for any month when weekday rules exist', () => {
12
+ const rules: DisabledRules = { weekdays: ['mon'], reason: 'Closed' };
13
+ expect(hasDisabledDatesInMonth(0, 2024, rules)).toBe(true);
14
+ expect(hasDisabledDatesInMonth(11, 2024, rules)).toBe(true);
15
+ });
16
+
17
+ it('returns false when weekdays array is empty', () => {
18
+ const rules: DisabledRules = { weekdays: [], reason: 'None' };
19
+ expect(hasDisabledDatesInMonth(6, 2024, rules)).toBe(false);
20
+ });
21
+ });
22
+
23
+ describe('specific dates', () => {
24
+ const rules: DisabledRules = {
25
+ dates: [new Date(2024, 6, 11), new Date(2024, 6, 25), new Date(2025, 0, 1)],
26
+ reason: 'Bank holidays',
27
+ };
28
+
29
+ it('returns true when month contains a disabled date', () => {
30
+ expect(hasDisabledDatesInMonth(6, 2024, rules)).toBe(true);
31
+ });
32
+
33
+ it('returns false when month has no disabled dates', () => {
34
+ expect(hasDisabledDatesInMonth(5, 2024, rules)).toBe(false);
35
+ });
36
+
37
+ it('returns true for another year with disabled date', () => {
38
+ expect(hasDisabledDatesInMonth(0, 2025, rules)).toBe(true);
39
+ });
40
+ });
41
+
42
+ describe('ranges', () => {
43
+ const rules: DisabledRules = {
44
+ ranges: [{ start: new Date(2024, 2, 29), end: new Date(2024, 3, 3) }],
45
+ reason: 'Maintenance',
46
+ };
47
+
48
+ it('returns true when range starts in the month', () => {
49
+ expect(hasDisabledDatesInMonth(2, 2024, rules)).toBe(true);
50
+ });
51
+
52
+ it('returns true when range ends in the month', () => {
53
+ expect(hasDisabledDatesInMonth(3, 2024, rules)).toBe(true);
54
+ });
55
+
56
+ it('returns true when range spans the entire month', () => {
57
+ const spanRules: DisabledRules = {
58
+ ranges: [{ start: new Date(2024, 0, 1), end: new Date(2024, 11, 31) }],
59
+ reason: 'Year-long',
60
+ };
61
+ expect(hasDisabledDatesInMonth(6, 2024, spanRules)).toBe(true);
62
+ });
63
+
64
+ it('returns false when range does not overlap the month', () => {
65
+ expect(hasDisabledDatesInMonth(4, 2024, rules)).toBe(false);
66
+ });
67
+ });
68
+
69
+ describe('min/max constraints', () => {
70
+ it('returns false when all disabled dates in month are outside min/max', () => {
71
+ const rules: DisabledRules = {
72
+ dates: [new Date(2024, 6, 1), new Date(2024, 6, 2)],
73
+ reason: 'Early',
74
+ };
75
+ const min = new Date(2024, 6, 10);
76
+ const max = new Date(2024, 6, 31);
77
+ expect(hasDisabledDatesInMonth(6, 2024, rules, min, max)).toBe(false);
78
+ });
79
+
80
+ it('returns true when weekdays exist regardless of min/max', () => {
81
+ const rules: DisabledRules = { weekdays: ['sat'], reason: 'Closed' };
82
+ const min = new Date(2024, 6, 10);
83
+ const max = new Date(2024, 6, 20);
84
+ expect(hasDisabledDatesInMonth(6, 2024, rules, min, max)).toBe(true);
85
+ });
86
+ });
87
+
88
+ describe('no rules', () => {
89
+ it('returns false when disabledRules is undefined', () => {
90
+ expect(hasDisabledDatesInMonth(6, 2024, undefined)).toBe(false);
91
+ });
92
+
93
+ it('returns false when only reason is provided', () => {
94
+ const rules: DisabledRules = { reason: 'Reason only' };
95
+ expect(hasDisabledDatesInMonth(6, 2024, rules)).toBe(false);
96
+ });
97
+ });
98
+ });
@@ -0,0 +1,45 @@
1
+ import type { DisabledRules } from '../../../Calendar/Calendar.types';
2
+
3
+ export function hasDisabledDatesInMonth(
4
+ month: number,
5
+ year: number,
6
+ disabledRules?: DisabledRules,
7
+ min?: Date | null,
8
+ max?: Date | null,
9
+ ): boolean {
10
+ if (!disabledRules) return false;
11
+
12
+ if (disabledRules.weekdays?.length) {
13
+ return true;
14
+ }
15
+
16
+ const monthStart = new Date(year, month, 1);
17
+ const monthEnd = new Date(year, month + 1, 0);
18
+
19
+ if (disabledRules.dates?.length) {
20
+ const hasDateInMonth = disabledRules.dates.some((date) => {
21
+ if (date.getFullYear() !== year || date.getMonth() !== month) return false;
22
+ if (min && date < min) return false;
23
+ if (max && date > max) return false;
24
+ return true;
25
+ });
26
+ if (hasDateInMonth) return true;
27
+ }
28
+
29
+ if (disabledRules.ranges?.length) {
30
+ const hasOverlap = disabledRules.ranges.some((range) => {
31
+ const rangeStart = new Date(
32
+ range.start.getFullYear(),
33
+ range.start.getMonth(),
34
+ range.start.getDate(),
35
+ );
36
+ const rangeEnd = new Date(range.end.getFullYear(), range.end.getMonth(), range.end.getDate());
37
+ if (max && rangeStart > max) return false;
38
+ if (min && rangeEnd < min) return false;
39
+ return rangeStart <= monthEnd && rangeEnd >= monthStart;
40
+ });
41
+ if (hasOverlap) return true;
42
+ }
43
+
44
+ return false;
45
+ }
@@ -6,3 +6,5 @@ export { isDateValid } from './isDateValid/isDateValid';
6
6
  export { isMonthAndYearFormat } from './isMonthAndYearFormat/isMonthAndYearFormat';
7
7
  export { MDY, YMD } from './getFormatForLocale/getFormatForLocale';
8
8
  export { returnDateView } from './getDateView/getDateView';
9
+ export { isDateDisabled } from './isDateDisabled/isDateDisabled';
10
+ export { hasDisabledDatesInMonth } from './hasDisabledDatesInMonth/hasDisabledDatesInMonth';
@@ -0,0 +1,169 @@
1
+ import { isDateDisabled } from './isDateDisabled';
2
+ import type { DisabledRules } from '../../../Calendar/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/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
+ }
package/src/i18n/en.json CHANGED
@@ -12,6 +12,10 @@
12
12
  "neptune.DateLookup.goTo20YearView": "Go to 20 year view",
13
13
  "neptune.DateLookup.month": "month",
14
14
  "neptune.DateLookup.next": "next",
15
+ "neptune.DateLookup.notAvailable": "not available",
16
+ "neptune.DateLookup.outOfRangeAfter": "Please select a date on or after {min}.",
17
+ "neptune.DateLookup.outOfRangeBefore": "Please select a date on or before {max}.",
18
+ "neptune.DateLookup.outOfRangeBetween": "Please select a date between {min} and {max}.",
15
19
  "neptune.DateLookup.previous": "previous",
16
20
  "neptune.DateLookup.selected": "selected",
17
21
  "neptune.DateLookup.twentyYears": "20 years",
package/src/index.ts CHANGED
@@ -25,6 +25,8 @@ export type {
25
25
  ButtonProps as LegacyButtonProps,
26
26
  AnchorElementProps,
27
27
  } from './Button';
28
+ export type { CalendarProps } from './Calendar';
29
+ export type { DisabledRules, WeekDayType, DisabledDateRange } from './Calendar';
28
30
  export type { CardProps } from './_deprecated/Card';
29
31
  export type {
30
32
  CarouselProps,
@@ -55,7 +57,6 @@ export type {
55
57
  export type { BottomSheetProps } from './common/BottomSheet';
56
58
  export type { DateInputProps } from './DateInput';
57
59
  export type { DateLookupProps } from './DateLookup';
58
- export type { CalendarProps } from './Calendar';
59
60
  export type { DecisionProps } from './Decision';
60
61
  export type {
61
62
  DefinitionListProps,
@@ -201,6 +202,7 @@ export { default as Badge, BadgeAssets } from './_deprecated/Badge';
201
202
  export { default as Body } from './Body';
202
203
  export { default as Button } from './Button';
203
204
  export { default as IconButton } from './IconButton';
205
+ export { default as Calendar } from './Calendar';
204
206
  export { default as Carousel } from './Carousel';
205
207
  export { default as Card } from './_deprecated/Card';
206
208
  export { default as Checkbox } from './Checkbox';
@@ -216,7 +218,6 @@ export { default as BaseCard } from './common/BaseCard';
216
218
  export { default as CriticalCommsBanner } from './_deprecated/CriticalBanner';
217
219
  export { default as DateInput } from './DateInput';
218
220
  export { default as DateLookup } from './DateLookup';
219
- export { default as Calendar } from './Calendar';
220
221
  export { default as Decision } from './Decision';
221
222
  export { default as DefinitionList } from './DefinitionList';
222
223
  export { default as Dimmer } from './Dimmer';