@aarhus-university/au-lib-react-components 10.6.0 → 10.7.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 (97) hide show
  1. package/.eslintrc.js +34 -34
  2. package/.storybook/main.js +20 -7
  3. package/.storybook/preview.js +10 -10
  4. package/README.md +19 -19
  5. package/__tests__/jest/AUButtonComponent.test.tsx +165 -163
  6. package/__tests__/jest/AUModalComponent.test.tsx +116 -0
  7. package/__tests__/jest/AUSpinnerComponent.test.tsx +57 -57
  8. package/__tests__/jest/AUToolbarComponent.test.tsx +46 -0
  9. package/__tests__/jest/helpers.test.ts +15 -15
  10. package/__tests__/jest/setupTests.ts +2 -2
  11. package/babel.config.js +8 -8
  12. package/build/umd/all.css +2 -2
  13. package/build/umd/all.js +1 -1
  14. package/build/umd/alphabox.js +1 -1
  15. package/build/umd/databox.js +1 -1
  16. package/build/umd/diagramme.js +1 -1
  17. package/build/umd/flowbox.js +1 -1
  18. package/build/umd/universe.js +1 -1
  19. package/package.json +107 -107
  20. package/src/components/AUAlertComponent.tsx +70 -70
  21. package/src/components/AUAutoSuggestComponent.js +158 -158
  22. package/src/components/AUButtonComponent.tsx +83 -77
  23. package/src/components/AUCalendarComponent.tsx +493 -493
  24. package/src/components/AUContentToggleComponent.tsx +33 -33
  25. package/src/components/AUDatepickerComponent.tsx +117 -117
  26. package/src/components/AUMobilePrefixComponent.tsx +15 -15
  27. package/src/components/AUModalComponent.tsx +64 -80
  28. package/src/components/AUReceiptComponent.tsx +33 -33
  29. package/src/components/AUSpinnerComponent.tsx +33 -33
  30. package/src/components/AUSubNavComponent.tsx +48 -48
  31. package/src/components/AUSubmitButtonContainerComponent.tsx +31 -31
  32. package/src/components/AUTabbedContentComponent.tsx +145 -145
  33. package/src/components/AUTableComponent.tsx +24 -24
  34. package/src/components/AUToastComponent.tsx +103 -103
  35. package/src/components/AUToolbarComponent.tsx +52 -45
  36. package/src/components/profile/AUProfileActions.js +128 -128
  37. package/src/components/profile/AUProfileAvatarComponent.js +83 -83
  38. package/src/components/profile/AUProfileAvatarV2Component.js +91 -91
  39. package/src/components/profile/AUProfileAvatarV3Component.tsx +42 -42
  40. package/src/components/profile/AUProfileContainerComponent.js +283 -283
  41. package/src/components/profile/AUProfileHooks.js +30 -30
  42. package/src/components/profile/AUProfileItemComponent.js +54 -54
  43. package/src/components/profile/AUProfileLanguageComponent.js +131 -131
  44. package/src/components/profile/AUProfileLoginComponent.tsx +26 -26
  45. package/src/components/profile/AUProfileMailComponent.js +307 -307
  46. package/src/components/profile/AUProfileMobileComponent.js +164 -164
  47. package/src/components/profile/AUProfileNameComponent.js +253 -253
  48. package/src/components/profile/AUProfileNextOfKinComponent.js +216 -216
  49. package/src/components/profile/AUProfileReducer.js +230 -230
  50. package/src/components/profile/AUProfileWidgetComponent.js +95 -95
  51. package/src/components/profile/AUProfileWidgetV2Component.js +116 -116
  52. package/src/components/profile/AUProfileWidgetV3Component.tsx +122 -122
  53. package/src/components/wrapping/AUEmbedComponent.js +47 -47
  54. package/src/layout-2016/components/alphabox/AlphaBoxComponent.js +143 -143
  55. package/src/layout-2016/components/alphabox/AlphaBoxContentComponent.js +136 -136
  56. package/src/layout-2016/components/common/AUCollapsibleComponent.js +152 -152
  57. package/src/layout-2016/components/common/AUSpinnerComponent.js +103 -103
  58. package/src/layout-2016/components/databox/DataBoxAlphabetComponent.js +144 -144
  59. package/src/layout-2016/components/databox/DataBoxAssociationComponent.js +122 -122
  60. package/src/layout-2016/components/databox/DataBoxButtonComponent.js +157 -157
  61. package/src/layout-2016/components/databox/DataBoxComponent.js +297 -297
  62. package/src/layout-2016/components/databox/DataBoxGroupingComponent.js +64 -64
  63. package/src/layout-2016/components/databox/DataBoxSearchResultComponent.js +36 -36
  64. package/src/layout-2016/components/databox/DataBoxStackedAssociationComponent.js +54 -54
  65. package/src/layout-2016/components/databox/DataBoxSuggestionComponent.js +39 -39
  66. package/src/layout-2016/components/diagramme/AUDiagrammeComponent.js +309 -309
  67. package/src/layout-2016/components/flowbox/FlowBoxComponent.js +126 -126
  68. package/src/layout-2016/components/flowbox/FlowBoxPhoneComponent.js +104 -104
  69. package/src/layout-2016/components/profile/AUProfileAvatar2016Component.js +103 -103
  70. package/src/layout-2016/components/universe/StaffTopComponent.js +363 -363
  71. package/src/layout-2016/components/universe/StudentTopComponent.js +137 -137
  72. package/src/layout-2016/components/universe/UniverseContainerComponent.js +65 -65
  73. package/src/layout-2016/lib/all.js +3 -3
  74. package/src/layout-2016/lib/au-alphabox.js +100 -100
  75. package/src/layout-2016/lib/au-databox.js +400 -400
  76. package/src/layout-2016/lib/au-diagramme.js +85 -85
  77. package/src/layout-2016/lib/au-flowbox.js +93 -93
  78. package/src/layout-2016/lib/universe.js +9 -9
  79. package/src/lib/helpers.ts +194 -194
  80. package/src/lib/hooks.ts +37 -33
  81. package/src/lib/i18n.ts +600 -600
  82. package/src/lib/tracking.ts +69 -69
  83. package/src/lib/wrapping.ts +21 -21
  84. package/src/styles/_settings.scss +10 -10
  85. package/src/styles/alphabox.scss +222 -222
  86. package/src/styles/app.scss +7 -7
  87. package/src/styles/autosuggest.scss +57 -57
  88. package/src/styles/databox.scss +563 -563
  89. package/src/styles/diagramme.scss +119 -119
  90. package/src/styles/flowbox.scss +72 -72
  91. package/src/styles/maps.scss +395 -395
  92. package/stories/AUButtonComponent.stories.tsx +85 -85
  93. package/stories/AUModalComponent.stories.tsx +92 -0
  94. package/stories/AUSpinnerComponent.stories.tsx +41 -41
  95. package/stories/AUToolbarComponent.stories.tsx +96 -0
  96. package/tsconfig.json +46 -46
  97. 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;