@aarhus-university/au-lib-react-components 11.5.1 → 11.6.0

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 (100) hide show
  1. package/.eslintrc.js +34 -34
  2. package/.storybook/main.js +20 -20
  3. package/.storybook/preview.js +17 -17
  4. package/README.md +19 -19
  5. package/__tests__/jest/AUButtonComponent.test.tsx +165 -165
  6. package/__tests__/jest/AUErrorComponent.test.tsx +142 -142
  7. package/__tests__/jest/AUModalComponent.test.tsx +186 -186
  8. package/__tests__/jest/AUNotificationComponent.test.tsx +115 -115
  9. package/__tests__/jest/AUSpinnerComponent.test.tsx +57 -57
  10. package/__tests__/jest/AUToolbarComponent.test.tsx +46 -46
  11. package/__tests__/jest/context.test.ts +25 -25
  12. package/__tests__/jest/helpers.test.ts +15 -15
  13. package/__tests__/jest/setupTests.ts +2 -2
  14. package/babel.config.js +8 -8
  15. package/build/umd/all.css +2 -2
  16. package/build/umd/all.js +1 -1
  17. package/build/umd/alphabox.js +1 -1
  18. package/build/umd/databox.js +1 -1
  19. package/build/umd/diagramme.js +1 -1
  20. package/build/umd/flowbox.js +1 -1
  21. package/build/umd/universe.js +1 -1
  22. package/build-storybook.log +367 -367
  23. package/package.json +114 -114
  24. package/src/components/AUAlertComponent.tsx +113 -113
  25. package/src/components/AUAutoSuggestComponent.js +158 -158
  26. package/src/components/AUButtonComponent.tsx +87 -87
  27. package/src/components/AUCalendarComponent.tsx +493 -493
  28. package/src/components/AUContentToggleComponent.tsx +47 -47
  29. package/src/components/AUDatepickerComponent.tsx +121 -121
  30. package/src/components/AUEditFilterComponent.tsx +24 -67
  31. package/src/components/AUErrorComponent.tsx +78 -78
  32. package/src/components/AUMobilePrefixComponent.tsx +15 -15
  33. package/src/components/AUModalComponent.tsx +68 -68
  34. package/src/components/AUNotificationComponent.tsx +43 -43
  35. package/src/components/AUReceiptComponent.tsx +33 -33
  36. package/src/components/AUSpinnerComponent.tsx +36 -36
  37. package/src/components/AUStepComponent.tsx +47 -47
  38. package/src/components/AUSubNavComponent.tsx +48 -48
  39. package/src/components/AUSubmitButtonContainerComponent.tsx +29 -29
  40. package/src/components/AUTabbedContentComponent.tsx +145 -145
  41. package/src/components/AUTableComponent.tsx +24 -24
  42. package/src/components/AUToastComponent.tsx +104 -104
  43. package/src/components/AUToolbarComponent.tsx +101 -106
  44. package/src/components/AUTruncatorComponent.tsx +124 -124
  45. package/src/components/wrapping/AUEmbedComponent.js +47 -47
  46. package/src/layout-2016/components/alphabox/AlphaBoxComponent.js +143 -143
  47. package/src/layout-2016/components/alphabox/AlphaBoxContentComponent.js +136 -136
  48. package/src/layout-2016/components/common/AUCollapsibleComponent.js +152 -152
  49. package/src/layout-2016/components/common/AUSpinnerComponent.js +103 -103
  50. package/src/layout-2016/components/databox/DataBoxAlphabetComponent.js +144 -144
  51. package/src/layout-2016/components/databox/DataBoxAssociationComponent.js +122 -122
  52. package/src/layout-2016/components/databox/DataBoxButtonComponent.js +157 -157
  53. package/src/layout-2016/components/databox/DataBoxComponent.js +297 -297
  54. package/src/layout-2016/components/databox/DataBoxGroupingComponent.js +64 -64
  55. package/src/layout-2016/components/databox/DataBoxSearchResultComponent.js +36 -36
  56. package/src/layout-2016/components/databox/DataBoxStackedAssociationComponent.js +54 -54
  57. package/src/layout-2016/components/databox/DataBoxSuggestionComponent.js +39 -39
  58. package/src/layout-2016/components/diagramme/AUDiagrammeComponent.js +309 -309
  59. package/src/layout-2016/components/flowbox/FlowBoxComponent.js +126 -126
  60. package/src/layout-2016/components/flowbox/FlowBoxPhoneComponent.js +104 -104
  61. package/src/layout-2016/components/profile/AUProfileAvatar2016Component.js +103 -103
  62. package/src/layout-2016/components/universe/StaffTopComponent.js +363 -363
  63. package/src/layout-2016/components/universe/StudentTopComponent.js +137 -137
  64. package/src/layout-2016/components/universe/UniverseContainerComponent.js +65 -65
  65. package/src/layout-2016/lib/all.js +3 -3
  66. package/src/layout-2016/lib/au-alphabox.js +100 -100
  67. package/src/layout-2016/lib/au-databox.js +400 -400
  68. package/src/layout-2016/lib/au-diagramme.js +85 -85
  69. package/src/layout-2016/lib/au-flowbox.js +93 -93
  70. package/src/layout-2016/lib/universe.js +9 -9
  71. package/src/lib/context.tsx +51 -51
  72. package/src/lib/dates.ts +50 -50
  73. package/src/lib/helpers.ts +208 -208
  74. package/src/lib/hooks.ts +75 -75
  75. package/src/lib/i18n.ts +600 -600
  76. package/src/lib/portals.tsx +145 -119
  77. package/src/lib/tracking.ts +69 -69
  78. package/src/lib/wrapping.ts +21 -21
  79. package/src/styles/_settings.scss +10 -10
  80. package/src/styles/alphabox.scss +222 -222
  81. package/src/styles/app.scss +7 -7
  82. package/src/styles/autosuggest.scss +57 -57
  83. package/src/styles/databox.scss +563 -563
  84. package/src/styles/diagramme.scss +119 -119
  85. package/src/styles/flowbox.scss +72 -72
  86. package/src/styles/maps.scss +395 -395
  87. package/stories/AUAlertComponent.stories.tsx +120 -120
  88. package/stories/AUButtonComponent.stories.tsx +127 -127
  89. package/stories/AUContentToggleComponent.stories.tsx +63 -63
  90. package/stories/AUEditFilterComponent.stories.tsx +29 -29
  91. package/stories/AUErrorComponent.stories.tsx +98 -98
  92. package/stories/AUModalComponent.stories.tsx +84 -84
  93. package/stories/AUNotificationComponent.stories.tsx +116 -116
  94. package/stories/AUSpinnerComponent.stories.tsx +41 -41
  95. package/stories/AUStepComponent.stories.tsx +40 -40
  96. package/stories/AUToolbarComponent.stories.tsx +326 -327
  97. package/stories/AUTruncatorComponent.stories.tsx +101 -101
  98. package/stories/lib/helpers.tsx +128 -128
  99. package/tsconfig.json +46 -46
  100. package/webpack.config.js +89 -89
@@ -1,493 +1,493 @@
1
- import React, { useState, FC } from 'react';
2
- import dayjs from 'dayjs';
3
- import 'dayjs/locale/da';
4
- import advancedFormat from 'dayjs/plugin/advancedFormat';
5
-
6
- const months = {
7
- da: ['Januar', 'Februar', 'Marts', 'April', 'Maj', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'December'],
8
- en: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
9
- };
10
- const dayTitles = {
11
- da: ['Man', 'Tir', 'Ons', 'Tor', 'Fre', 'Lør', 'Søn'],
12
- en: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
13
- };
14
-
15
- const daysInMonth = (year: number, month: number): number => new Date(year, month, 0).getDate();
16
-
17
- // eslint-disable-next-line max-len
18
- const addDays = (days: number, date: Date): Date => new Date(date.getTime() + days * 60 * 24 * 60000);
19
-
20
- const calcEaster = (year: number): Date => {
21
- const a = year % 19;
22
- const b = Math.floor(year / 100);
23
- const c = year % 100;
24
- const d = Math.floor(b / 4);
25
- const e = b % 4;
26
- const f = Math.floor((b + 8) / 25);
27
- const g = Math.floor((b - f + 1) / 3);
28
- const h = (19 * a + b - d - g + 15) % 30;
29
- const i = Math.floor(c / 4);
30
- const k = c % 4;
31
- const l = (32 + 2 * e + 2 * i - h - k) % 7;
32
- const m = Math.floor((a + 11 * h + 22 * l) / 451);
33
- const n0 = (h + l + 7 * m + 114);
34
- const n = Math.floor(n0 / 31) - 1;
35
- const p = (n0 % 31) + 1;
36
- const date = new Date(year, n, p);
37
- return date;
38
- };
39
-
40
- const isHoliday = (date: Date): boolean => {
41
- const year = date.getFullYear();
42
- const holidays: Date[] = [];
43
- const newYear = new Date(year, 0, 1);
44
- holidays.push(newYear);
45
- const easter = calcEaster(year);
46
- holidays.push(easter);
47
- const easterThu = addDays(-3, easter);
48
- holidays.push(easterThu);
49
- const easterFri = addDays(-2, easter);
50
- holidays.push(easterFri);
51
- const easter2 = addDays(1, easter);
52
- holidays.push(easter2);
53
- const stBededag = addDays(26, easter);
54
- holidays.push(stBededag);
55
- const kristiHf = addDays(39, easter);
56
- holidays.push(kristiHf);
57
- const whitsun = addDays(49, easter);
58
- holidays.push(whitsun);
59
- const whitsun2 = addDays(1, whitsun);
60
- holidays.push(whitsun2);
61
- const grundlov = new Date(year, 5, 5);
62
- holidays.push(grundlov);
63
- const christmas = new Date(year, 11, 25);
64
- holidays.push(christmas);
65
- const christmas2 = addDays(1, christmas);
66
- holidays.push(christmas2);
67
-
68
- return holidays.map((h) => h.toString()).filter((x) => x === date.toString()).length > 0;
69
- };
70
-
71
- const dateAllowed = (date: Date, limitStart: Date, limitEnd: Date): boolean => {
72
- if (limitStart && !limitEnd) {
73
- return date >= limitStart;
74
- }
75
-
76
- if (!limitStart && limitEnd) {
77
- return date <= limitEnd;
78
- }
79
-
80
- if (limitStart && limitEnd) {
81
- return date >= limitStart && date <= limitEnd;
82
- }
83
-
84
- return true;
85
- };
86
-
87
- const skipDays = [6, 0, 1, 2, 3, 4, 5];
88
-
89
- const AUCalendarComponent: FC<AUCalendarComponentProps> = ({
90
- lang,
91
- day,
92
- month,
93
- year,
94
- hour,
95
- minute,
96
- selected,
97
- onSelected,
98
- highlightWeekend,
99
- highlightHolidays,
100
- yearSpanMinus,
101
- yearSpanPlus,
102
- minuteInterval,
103
- showTime,
104
- closeable,
105
- onClose,
106
- limitStart,
107
- limitEnd,
108
- controls,
109
- }: AUCalendarComponentProps) => {
110
- dayjs.locale(lang);
111
- dayjs.extend(advancedFormat);
112
-
113
- const [calendarState, setCalendarState] = useState<ICalendarState>({
114
- day,
115
- month,
116
- year,
117
- hour,
118
- minute,
119
- selected: selected as Date,
120
- });
121
-
122
- const {
123
- day: sDay,
124
- month: sMonth,
125
- year: sYear,
126
- hour: sHour,
127
- minute: sMinute,
128
- selected: sSelected,
129
- } = calendarState;
130
-
131
- const goBack = () => {
132
- const newMonth = sMonth - 1 < 1 ? 12 : (sMonth - 1);
133
- const newYear = newMonth === 12 ? (sYear - 1) : sYear;
134
- setCalendarState({
135
- ...calendarState,
136
- ...{
137
- month: newMonth,
138
- year: newYear,
139
- },
140
- });
141
- };
142
-
143
- const goForward = () => {
144
- const newMonth = sMonth + 1 > 12 ? 1 : (sMonth + 1);
145
- const newYear = newMonth === 1 ? (sYear + 1) : sYear;
146
- setCalendarState({
147
- ...calendarState,
148
- ...{
149
- month: newMonth,
150
- year: newYear,
151
- },
152
- });
153
- };
154
-
155
- const renderMonths = months[lang as string].map(
156
- (m: string, i: number) => (
157
- <option key={m} value={i + 1}>
158
- {m}
159
- </option>
160
- ),
161
- );
162
-
163
- let yearStart = yearSpanMinus;
164
- // eslint-disable-next-line max-len
165
- if (limitStart && limitStart.getFullYear() > (sSelected.getFullYear() - (yearSpanMinus as number))) {
166
- yearStart = sSelected.getFullYear() - limitStart.getFullYear();
167
- }
168
-
169
- let yearEnd = yearSpanPlus;
170
- if (limitEnd && limitEnd.getFullYear() < (sSelected.getFullYear() + (yearSpanPlus as number))) {
171
- yearEnd = limitEnd.getFullYear() + sSelected.getFullYear();
172
- }
173
-
174
- const yearArr: number[] = [];
175
- for (let y = sYear - (yearStart as number); y <= sYear + (yearEnd as number); y += 1) {
176
- yearArr.push(y);
177
- }
178
-
179
- const renderYears = yearArr.map(
180
- (y) => (
181
- <option key={y}>
182
- {y}
183
- </option>
184
- ),
185
- );
186
-
187
- const renderWeekDays = dayTitles[lang as string].map(
188
- (w: string) => (
189
- <span key={w} className="calendar__days__day">
190
- {w}
191
- </span>
192
- ),
193
- );
194
-
195
- const firstDayInMonth = new Date(sYear, sMonth - 1, 1);
196
- const emptyDays = skipDays[firstDayInMonth.getDay()];
197
- const prevYear = sMonth === 1 ? sYear - 1 : sYear;
198
- const prevMonth = sMonth === 1 ? 12 : sMonth - 1;
199
- const numberOfDaysInPrevMonth = daysInMonth(
200
- prevYear, prevMonth,
201
- );
202
-
203
- let rowCount = -1;
204
- const renderEmptyDaysBefore = Array(emptyDays).fill(null).map((_, i) => {
205
- rowCount = i;
206
- const beforeDay = numberOfDaysInPrevMonth - emptyDays + i + 1;
207
- const date = new Date(prevYear, prevMonth - 1, beforeDay);
208
- const isWeekend = (highlightWeekend && rowCount > 4)
209
- || (highlightHolidays
210
- && isHoliday(date));
211
- return (
212
- <button
213
- type="button"
214
- key={rowCount}
215
- disabled={(limitStart && date < limitStart) as boolean}
216
- className={`calendar__days__date ${isWeekend ? 'calendar__days__date--weekend calendar__days__date--empty' : 'calendar__days__date--empty'}`}
217
- onClick={() => {
218
- goBack();
219
- }}
220
- aria-label={dayjs(date).format('LL')}
221
- >
222
- {beforeDay}
223
- </button>
224
- );
225
- });
226
-
227
- const renderDays = Array(daysInMonth(sYear, sMonth)).fill(null).map((_, i) => {
228
- const key = i; // suck it
229
- if (rowCount === 6) {
230
- rowCount = 0;
231
- } else {
232
- rowCount += 1;
233
- }
234
- const newDay = i + 1;
235
- const isSelected = (
236
- newDay === sSelected.getDate()
237
- && sYear === sSelected.getFullYear()
238
- && sMonth === sSelected.getMonth() + 1
239
- );
240
- const isWeekend = (highlightWeekend && rowCount > 4)
241
- || (highlightHolidays && isHoliday(new Date(sYear, sMonth - 1, newDay)));
242
- let className = 'calendar__days__date';
243
- if (isSelected) {
244
- className = `${className} calendar__days__date--selected`;
245
- }
246
- if (isWeekend) {
247
- className = `${className} calendar__days__date--weekend`;
248
- }
249
- const date = new Date(sYear, sMonth - 1, newDay, sHour, sMinute);
250
- return (
251
- <button
252
- key={key}
253
- disabled={((limitStart && date < limitStart) || (limitEnd && date > limitEnd) as boolean)}
254
- type="button"
255
- className={className}
256
- onClick={() => {
257
- const newSelected = date;
258
- setCalendarState({
259
- ...calendarState,
260
- ...{
261
- day: newDay,
262
- selected: newSelected,
263
- },
264
- });
265
- onSelected(newSelected);
266
- }}
267
- aria-label={dayjs(date).format('LL')}
268
- >
269
- {i + 1}
270
- </button>
271
- );
272
- });
273
-
274
- const nextYear = sMonth === 12 ? (sYear + 1) : sYear;
275
- const nextMonth = sMonth === 12 ? 1 : (sMonth + 1);
276
- const renderEmptyDaysAfter = Array(6 - rowCount).fill(null).map((_, i) => {
277
- const key = i;
278
- const afterDay = i + 1;
279
- if (rowCount === 6) {
280
- rowCount = 0;
281
- } else {
282
- rowCount += 1;
283
- }
284
- const date = new Date(nextYear, nextMonth - 1, afterDay);
285
- const isWeekend = (highlightWeekend && rowCount > 4)
286
- || (highlightHolidays
287
- && isHoliday(date));
288
- return (
289
- <button
290
- type="button"
291
- key={key}
292
- disabled={(limitEnd && date > limitEnd) as boolean}
293
- className={`calendar__days__date ${isWeekend ? 'calendar__days__date--weekend calendar__days__date--empty' : 'calendar__days__date--empty'}`}
294
- onClick={() => {
295
- goForward();
296
- }}
297
- aria-label={dayjs(date).format('LL')}
298
- >
299
- {afterDay}
300
- </button>
301
- );
302
- });
303
-
304
- const renderHours = Array(24).fill(null).map((_, i) => {
305
- const key = i;
306
- const optHour = i < 10 ? `0${i}` : i;
307
- return (
308
- <option key={key} value={i}>
309
- {optHour}
310
- </option>
311
- );
312
- });
313
-
314
- const renderMinutes = Array(60).fill(null).map((_, i) => {
315
- const key = i;
316
- if (i % (minuteInterval as number) === 0) {
317
- const optMinute = i < 10 ? `0${i}` : i;
318
- return (
319
- <option key={key} value={i}>
320
- {optMinute}
321
- </option>
322
- );
323
- }
324
- return null;
325
- });
326
-
327
- return (
328
- <div id={`${controls}-calendar`} className="calendar" aria-controls={controls}>
329
- <div className="calendar__year-month">
330
- <div className="form__field">
331
- <select
332
- value={sMonth}
333
- title={lang === 'da' ? 'Vælg måned' : 'Select month'}
334
- onChange={(e) => {
335
- const newMonth = parseInt(e.target.value, 10);
336
- const newSelected = new Date(sYear, newMonth - 1, sDay, sHour, sMinute);
337
- if (dateAllowed(newSelected, limitStart as Date, limitEnd as Date)) {
338
- setCalendarState({
339
- ...calendarState,
340
- ...{
341
- month: newMonth,
342
- selected: newSelected,
343
- },
344
- });
345
- onSelected(newSelected);
346
- } else {
347
- setCalendarState({
348
- ...calendarState,
349
- ...{
350
- month: newMonth,
351
- },
352
- });
353
- }
354
- }}
355
- >
356
- {renderMonths}
357
- </select>
358
- </div>
359
- <div className="form__field">
360
- <select
361
- value={sYear}
362
- title={lang === 'da' ? 'Vælg år' : 'Select year'}
363
- onChange={(e) => {
364
- const newYear = parseInt(e.target.value, 10);
365
- const newSelected = new Date(newYear, sMonth - 1, sDay, sHour, sMinute);
366
- if (dateAllowed(newSelected, limitStart as Date, limitEnd as Date)) {
367
- setCalendarState({
368
- ...calendarState,
369
- ...{
370
- year: newYear,
371
- selected: newSelected,
372
- },
373
- });
374
- onSelected(newSelected);
375
- } else {
376
- setCalendarState({
377
- ...calendarState,
378
- ...{
379
- year: newYear,
380
- },
381
- });
382
- }
383
- }}
384
- >
385
- {renderYears}
386
- </select>
387
- </div>
388
- {
389
- closeable && (
390
- <button
391
- className="button button--icon--hide-label button--small button--icon"
392
- type="button"
393
- data-icon=""
394
- onClick={onClose}
395
- >
396
- {lang === 'da' ? 'Luk' : 'Close'}
397
- </button>
398
- )
399
- }
400
- </div>
401
- <div className="calendar__days">
402
- {renderWeekDays}
403
- {renderEmptyDaysBefore}
404
- {renderDays}
405
- {renderEmptyDaysAfter}
406
- </div>
407
- {
408
- showTime && (
409
- <div className="calendar__time">
410
- <div className="form__field">
411
- <select
412
- value={sHour}
413
- title={lang === 'da' ? 'Vælg time' : 'Select hour'}
414
- onChange={(e) => {
415
- const newHour = parseInt(e.target.value, 10);
416
- const newSelected = new Date(sYear, sMonth - 1, sDay, newHour, sMinute);
417
- if (dateAllowed(newSelected, limitStart as Date, limitEnd as Date)) {
418
- setCalendarState({
419
- ...calendarState,
420
- ...{
421
- hour: newHour,
422
- selected: newSelected,
423
- },
424
- });
425
- onSelected(newSelected);
426
- } else {
427
- setCalendarState({
428
- ...calendarState,
429
- ...{
430
- hour: newHour,
431
- },
432
- });
433
- }
434
- }}
435
- >
436
- {renderHours}
437
- </select>
438
- </div>
439
- <div className="form__field">
440
- <select
441
- value={sMinute}
442
- title={lang === 'da' ? 'Vælg minut' : 'Select minute'}
443
- onChange={(e) => {
444
- const newMinute = parseInt(e.target.value, 10);
445
- const newSelected = new Date(sYear, sMonth - 1, sDay, sHour, newMinute);
446
- if (dateAllowed(newSelected, limitStart as Date, limitEnd as Date)) {
447
- setCalendarState({
448
- ...calendarState,
449
- ...{
450
- minute: newMinute,
451
- selected: newSelected,
452
- },
453
- });
454
- onSelected(newSelected);
455
- } else {
456
- setCalendarState({
457
- ...calendarState,
458
- ...{
459
- minute: newMinute,
460
- },
461
- });
462
- }
463
- }}
464
- >
465
- {renderMinutes}
466
- </select>
467
- </div>
468
- </div>
469
- )
470
- }
471
- </div>
472
- );
473
- };
474
-
475
- AUCalendarComponent.defaultProps = {
476
- lang: 'da',
477
- selected: new Date(),
478
- // eslint-disable-next-line no-console
479
- highlightWeekend: true,
480
- highlightHolidays: true,
481
- yearSpanMinus: 5,
482
- yearSpanPlus: 5,
483
- minuteInterval: 5,
484
- showTime: true,
485
- closeable: false,
486
- // eslint-disable-next-line @typescript-eslint/no-empty-function
487
- onClose: () => { },
488
- limitStart: null,
489
- limitEnd: null,
490
- };
491
-
492
- AUCalendarComponent.displayName = 'AUCalendarComponent';
493
- export default AUCalendarComponent;
1
+ import React, { useState, FC } from 'react';
2
+ import dayjs from 'dayjs';
3
+ import 'dayjs/locale/da';
4
+ import advancedFormat from 'dayjs/plugin/advancedFormat';
5
+
6
+ const months = {
7
+ da: ['Januar', 'Februar', 'Marts', 'April', 'Maj', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'December'],
8
+ en: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
9
+ };
10
+ const dayTitles = {
11
+ da: ['Man', 'Tir', 'Ons', 'Tor', 'Fre', 'Lør', 'Søn'],
12
+ en: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
13
+ };
14
+
15
+ const daysInMonth = (year: number, month: number): number => new Date(year, month, 0).getDate();
16
+
17
+ // eslint-disable-next-line max-len
18
+ const addDays = (days: number, date: Date): Date => new Date(date.getTime() + days * 60 * 24 * 60000);
19
+
20
+ const calcEaster = (year: number): Date => {
21
+ const a = year % 19;
22
+ const b = Math.floor(year / 100);
23
+ const c = year % 100;
24
+ const d = Math.floor(b / 4);
25
+ const e = b % 4;
26
+ const f = Math.floor((b + 8) / 25);
27
+ const g = Math.floor((b - f + 1) / 3);
28
+ const h = (19 * a + b - d - g + 15) % 30;
29
+ const i = Math.floor(c / 4);
30
+ const k = c % 4;
31
+ const l = (32 + 2 * e + 2 * i - h - k) % 7;
32
+ const m = Math.floor((a + 11 * h + 22 * l) / 451);
33
+ const n0 = (h + l + 7 * m + 114);
34
+ const n = Math.floor(n0 / 31) - 1;
35
+ const p = (n0 % 31) + 1;
36
+ const date = new Date(year, n, p);
37
+ return date;
38
+ };
39
+
40
+ const isHoliday = (date: Date): boolean => {
41
+ const year = date.getFullYear();
42
+ const holidays: Date[] = [];
43
+ const newYear = new Date(year, 0, 1);
44
+ holidays.push(newYear);
45
+ const easter = calcEaster(year);
46
+ holidays.push(easter);
47
+ const easterThu = addDays(-3, easter);
48
+ holidays.push(easterThu);
49
+ const easterFri = addDays(-2, easter);
50
+ holidays.push(easterFri);
51
+ const easter2 = addDays(1, easter);
52
+ holidays.push(easter2);
53
+ const stBededag = addDays(26, easter);
54
+ holidays.push(stBededag);
55
+ const kristiHf = addDays(39, easter);
56
+ holidays.push(kristiHf);
57
+ const whitsun = addDays(49, easter);
58
+ holidays.push(whitsun);
59
+ const whitsun2 = addDays(1, whitsun);
60
+ holidays.push(whitsun2);
61
+ const grundlov = new Date(year, 5, 5);
62
+ holidays.push(grundlov);
63
+ const christmas = new Date(year, 11, 25);
64
+ holidays.push(christmas);
65
+ const christmas2 = addDays(1, christmas);
66
+ holidays.push(christmas2);
67
+
68
+ return holidays.map((h) => h.toString()).filter((x) => x === date.toString()).length > 0;
69
+ };
70
+
71
+ const dateAllowed = (date: Date, limitStart: Date, limitEnd: Date): boolean => {
72
+ if (limitStart && !limitEnd) {
73
+ return date >= limitStart;
74
+ }
75
+
76
+ if (!limitStart && limitEnd) {
77
+ return date <= limitEnd;
78
+ }
79
+
80
+ if (limitStart && limitEnd) {
81
+ return date >= limitStart && date <= limitEnd;
82
+ }
83
+
84
+ return true;
85
+ };
86
+
87
+ const skipDays = [6, 0, 1, 2, 3, 4, 5];
88
+
89
+ const AUCalendarComponent: FC<AUCalendarComponentProps> = ({
90
+ lang,
91
+ day,
92
+ month,
93
+ year,
94
+ hour,
95
+ minute,
96
+ selected,
97
+ onSelected,
98
+ highlightWeekend,
99
+ highlightHolidays,
100
+ yearSpanMinus,
101
+ yearSpanPlus,
102
+ minuteInterval,
103
+ showTime,
104
+ closeable,
105
+ onClose,
106
+ limitStart,
107
+ limitEnd,
108
+ controls,
109
+ }: AUCalendarComponentProps) => {
110
+ dayjs.locale(lang);
111
+ dayjs.extend(advancedFormat);
112
+
113
+ const [calendarState, setCalendarState] = useState<ICalendarState>({
114
+ day,
115
+ month,
116
+ year,
117
+ hour,
118
+ minute,
119
+ selected: selected as Date,
120
+ });
121
+
122
+ const {
123
+ day: sDay,
124
+ month: sMonth,
125
+ year: sYear,
126
+ hour: sHour,
127
+ minute: sMinute,
128
+ selected: sSelected,
129
+ } = calendarState;
130
+
131
+ const goBack = () => {
132
+ const newMonth = sMonth - 1 < 1 ? 12 : (sMonth - 1);
133
+ const newYear = newMonth === 12 ? (sYear - 1) : sYear;
134
+ setCalendarState({
135
+ ...calendarState,
136
+ ...{
137
+ month: newMonth,
138
+ year: newYear,
139
+ },
140
+ });
141
+ };
142
+
143
+ const goForward = () => {
144
+ const newMonth = sMonth + 1 > 12 ? 1 : (sMonth + 1);
145
+ const newYear = newMonth === 1 ? (sYear + 1) : sYear;
146
+ setCalendarState({
147
+ ...calendarState,
148
+ ...{
149
+ month: newMonth,
150
+ year: newYear,
151
+ },
152
+ });
153
+ };
154
+
155
+ const renderMonths = months[lang as string].map(
156
+ (m: string, i: number) => (
157
+ <option key={m} value={i + 1}>
158
+ {m}
159
+ </option>
160
+ ),
161
+ );
162
+
163
+ let yearStart = yearSpanMinus;
164
+ // eslint-disable-next-line max-len
165
+ if (limitStart && limitStart.getFullYear() > (sSelected.getFullYear() - (yearSpanMinus as number))) {
166
+ yearStart = sSelected.getFullYear() - limitStart.getFullYear();
167
+ }
168
+
169
+ let yearEnd = yearSpanPlus;
170
+ if (limitEnd && limitEnd.getFullYear() < (sSelected.getFullYear() + (yearSpanPlus as number))) {
171
+ yearEnd = limitEnd.getFullYear() + sSelected.getFullYear();
172
+ }
173
+
174
+ const yearArr: number[] = [];
175
+ for (let y = sYear - (yearStart as number); y <= sYear + (yearEnd as number); y += 1) {
176
+ yearArr.push(y);
177
+ }
178
+
179
+ const renderYears = yearArr.map(
180
+ (y) => (
181
+ <option key={y}>
182
+ {y}
183
+ </option>
184
+ ),
185
+ );
186
+
187
+ const renderWeekDays = dayTitles[lang as string].map(
188
+ (w: string) => (
189
+ <span key={w} className="calendar__days__day">
190
+ {w}
191
+ </span>
192
+ ),
193
+ );
194
+
195
+ const firstDayInMonth = new Date(sYear, sMonth - 1, 1);
196
+ const emptyDays = skipDays[firstDayInMonth.getDay()];
197
+ const prevYear = sMonth === 1 ? sYear - 1 : sYear;
198
+ const prevMonth = sMonth === 1 ? 12 : sMonth - 1;
199
+ const numberOfDaysInPrevMonth = daysInMonth(
200
+ prevYear, prevMonth,
201
+ );
202
+
203
+ let rowCount = -1;
204
+ const renderEmptyDaysBefore = Array(emptyDays).fill(null).map((_, i) => {
205
+ rowCount = i;
206
+ const beforeDay = numberOfDaysInPrevMonth - emptyDays + i + 1;
207
+ const date = new Date(prevYear, prevMonth - 1, beforeDay);
208
+ const isWeekend = (highlightWeekend && rowCount > 4)
209
+ || (highlightHolidays
210
+ && isHoliday(date));
211
+ return (
212
+ <button
213
+ type="button"
214
+ key={rowCount}
215
+ disabled={(limitStart && date < limitStart) as boolean}
216
+ className={`calendar__days__date ${isWeekend ? 'calendar__days__date--weekend calendar__days__date--empty' : 'calendar__days__date--empty'}`}
217
+ onClick={() => {
218
+ goBack();
219
+ }}
220
+ aria-label={dayjs(date).format('LL')}
221
+ >
222
+ {beforeDay}
223
+ </button>
224
+ );
225
+ });
226
+
227
+ const renderDays = Array(daysInMonth(sYear, sMonth)).fill(null).map((_, i) => {
228
+ const key = i; // suck it
229
+ if (rowCount === 6) {
230
+ rowCount = 0;
231
+ } else {
232
+ rowCount += 1;
233
+ }
234
+ const newDay = i + 1;
235
+ const isSelected = (
236
+ newDay === sSelected.getDate()
237
+ && sYear === sSelected.getFullYear()
238
+ && sMonth === sSelected.getMonth() + 1
239
+ );
240
+ const isWeekend = (highlightWeekend && rowCount > 4)
241
+ || (highlightHolidays && isHoliday(new Date(sYear, sMonth - 1, newDay)));
242
+ let className = 'calendar__days__date';
243
+ if (isSelected) {
244
+ className = `${className} calendar__days__date--selected`;
245
+ }
246
+ if (isWeekend) {
247
+ className = `${className} calendar__days__date--weekend`;
248
+ }
249
+ const date = new Date(sYear, sMonth - 1, newDay, sHour, sMinute);
250
+ return (
251
+ <button
252
+ key={key}
253
+ disabled={((limitStart && date < limitStart) || (limitEnd && date > limitEnd) as boolean)}
254
+ type="button"
255
+ className={className}
256
+ onClick={() => {
257
+ const newSelected = date;
258
+ setCalendarState({
259
+ ...calendarState,
260
+ ...{
261
+ day: newDay,
262
+ selected: newSelected,
263
+ },
264
+ });
265
+ onSelected(newSelected);
266
+ }}
267
+ aria-label={dayjs(date).format('LL')}
268
+ >
269
+ {i + 1}
270
+ </button>
271
+ );
272
+ });
273
+
274
+ const nextYear = sMonth === 12 ? (sYear + 1) : sYear;
275
+ const nextMonth = sMonth === 12 ? 1 : (sMonth + 1);
276
+ const renderEmptyDaysAfter = Array(6 - rowCount).fill(null).map((_, i) => {
277
+ const key = i;
278
+ const afterDay = i + 1;
279
+ if (rowCount === 6) {
280
+ rowCount = 0;
281
+ } else {
282
+ rowCount += 1;
283
+ }
284
+ const date = new Date(nextYear, nextMonth - 1, afterDay);
285
+ const isWeekend = (highlightWeekend && rowCount > 4)
286
+ || (highlightHolidays
287
+ && isHoliday(date));
288
+ return (
289
+ <button
290
+ type="button"
291
+ key={key}
292
+ disabled={(limitEnd && date > limitEnd) as boolean}
293
+ className={`calendar__days__date ${isWeekend ? 'calendar__days__date--weekend calendar__days__date--empty' : 'calendar__days__date--empty'}`}
294
+ onClick={() => {
295
+ goForward();
296
+ }}
297
+ aria-label={dayjs(date).format('LL')}
298
+ >
299
+ {afterDay}
300
+ </button>
301
+ );
302
+ });
303
+
304
+ const renderHours = Array(24).fill(null).map((_, i) => {
305
+ const key = i;
306
+ const optHour = i < 10 ? `0${i}` : i;
307
+ return (
308
+ <option key={key} value={i}>
309
+ {optHour}
310
+ </option>
311
+ );
312
+ });
313
+
314
+ const renderMinutes = Array(60).fill(null).map((_, i) => {
315
+ const key = i;
316
+ if (i % (minuteInterval as number) === 0) {
317
+ const optMinute = i < 10 ? `0${i}` : i;
318
+ return (
319
+ <option key={key} value={i}>
320
+ {optMinute}
321
+ </option>
322
+ );
323
+ }
324
+ return null;
325
+ });
326
+
327
+ return (
328
+ <div id={`${controls}-calendar`} className="calendar" aria-controls={controls}>
329
+ <div className="calendar__year-month">
330
+ <div className="form__field">
331
+ <select
332
+ value={sMonth}
333
+ title={lang === 'da' ? 'Vælg måned' : 'Select month'}
334
+ onChange={(e) => {
335
+ const newMonth = parseInt(e.target.value, 10);
336
+ const newSelected = new Date(sYear, newMonth - 1, sDay, sHour, sMinute);
337
+ if (dateAllowed(newSelected, limitStart as Date, limitEnd as Date)) {
338
+ setCalendarState({
339
+ ...calendarState,
340
+ ...{
341
+ month: newMonth,
342
+ selected: newSelected,
343
+ },
344
+ });
345
+ onSelected(newSelected);
346
+ } else {
347
+ setCalendarState({
348
+ ...calendarState,
349
+ ...{
350
+ month: newMonth,
351
+ },
352
+ });
353
+ }
354
+ }}
355
+ >
356
+ {renderMonths}
357
+ </select>
358
+ </div>
359
+ <div className="form__field">
360
+ <select
361
+ value={sYear}
362
+ title={lang === 'da' ? 'Vælg år' : 'Select year'}
363
+ onChange={(e) => {
364
+ const newYear = parseInt(e.target.value, 10);
365
+ const newSelected = new Date(newYear, sMonth - 1, sDay, sHour, sMinute);
366
+ if (dateAllowed(newSelected, limitStart as Date, limitEnd as Date)) {
367
+ setCalendarState({
368
+ ...calendarState,
369
+ ...{
370
+ year: newYear,
371
+ selected: newSelected,
372
+ },
373
+ });
374
+ onSelected(newSelected);
375
+ } else {
376
+ setCalendarState({
377
+ ...calendarState,
378
+ ...{
379
+ year: newYear,
380
+ },
381
+ });
382
+ }
383
+ }}
384
+ >
385
+ {renderYears}
386
+ </select>
387
+ </div>
388
+ {
389
+ closeable && (
390
+ <button
391
+ className="button button--icon--hide-label button--small button--icon"
392
+ type="button"
393
+ data-icon=""
394
+ onClick={onClose}
395
+ >
396
+ {lang === 'da' ? 'Luk' : 'Close'}
397
+ </button>
398
+ )
399
+ }
400
+ </div>
401
+ <div className="calendar__days">
402
+ {renderWeekDays}
403
+ {renderEmptyDaysBefore}
404
+ {renderDays}
405
+ {renderEmptyDaysAfter}
406
+ </div>
407
+ {
408
+ showTime && (
409
+ <div className="calendar__time">
410
+ <div className="form__field">
411
+ <select
412
+ value={sHour}
413
+ title={lang === 'da' ? 'Vælg time' : 'Select hour'}
414
+ onChange={(e) => {
415
+ const newHour = parseInt(e.target.value, 10);
416
+ const newSelected = new Date(sYear, sMonth - 1, sDay, newHour, sMinute);
417
+ if (dateAllowed(newSelected, limitStart as Date, limitEnd as Date)) {
418
+ setCalendarState({
419
+ ...calendarState,
420
+ ...{
421
+ hour: newHour,
422
+ selected: newSelected,
423
+ },
424
+ });
425
+ onSelected(newSelected);
426
+ } else {
427
+ setCalendarState({
428
+ ...calendarState,
429
+ ...{
430
+ hour: newHour,
431
+ },
432
+ });
433
+ }
434
+ }}
435
+ >
436
+ {renderHours}
437
+ </select>
438
+ </div>
439
+ <div className="form__field">
440
+ <select
441
+ value={sMinute}
442
+ title={lang === 'da' ? 'Vælg minut' : 'Select minute'}
443
+ onChange={(e) => {
444
+ const newMinute = parseInt(e.target.value, 10);
445
+ const newSelected = new Date(sYear, sMonth - 1, sDay, sHour, newMinute);
446
+ if (dateAllowed(newSelected, limitStart as Date, limitEnd as Date)) {
447
+ setCalendarState({
448
+ ...calendarState,
449
+ ...{
450
+ minute: newMinute,
451
+ selected: newSelected,
452
+ },
453
+ });
454
+ onSelected(newSelected);
455
+ } else {
456
+ setCalendarState({
457
+ ...calendarState,
458
+ ...{
459
+ minute: newMinute,
460
+ },
461
+ });
462
+ }
463
+ }}
464
+ >
465
+ {renderMinutes}
466
+ </select>
467
+ </div>
468
+ </div>
469
+ )
470
+ }
471
+ </div>
472
+ );
473
+ };
474
+
475
+ AUCalendarComponent.defaultProps = {
476
+ lang: 'da',
477
+ selected: new Date(),
478
+ // eslint-disable-next-line no-console
479
+ highlightWeekend: true,
480
+ highlightHolidays: true,
481
+ yearSpanMinus: 5,
482
+ yearSpanPlus: 5,
483
+ minuteInterval: 5,
484
+ showTime: true,
485
+ closeable: false,
486
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
487
+ onClose: () => { },
488
+ limitStart: null,
489
+ limitEnd: null,
490
+ };
491
+
492
+ AUCalendarComponent.displayName = 'AUCalendarComponent';
493
+ export default AUCalendarComponent;