@aarhus-university/au-lib-react-components 11.4.0 → 11.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +34 -34
- package/.storybook/main.js +20 -20
- package/.storybook/preview.js +17 -17
- package/README.md +19 -19
- package/__tests__/jest/AUButtonComponent.test.tsx +165 -165
- package/__tests__/jest/AUErrorComponent.test.tsx +142 -142
- package/__tests__/jest/AUModalComponent.test.tsx +186 -186
- package/__tests__/jest/AUNotificationComponent.test.tsx +115 -115
- package/__tests__/jest/AUSpinnerComponent.test.tsx +57 -57
- package/__tests__/jest/AUToolbarComponent.test.tsx +46 -46
- package/__tests__/jest/context.test.ts +25 -25
- package/__tests__/jest/helpers.test.ts +15 -15
- package/__tests__/jest/setupTests.ts +2 -2
- package/babel.config.js +8 -8
- package/build/umd/all.css +2 -2
- package/build/umd/all.js +1 -1
- package/build/umd/alphabox.js +1 -1
- package/build/umd/databox.js +1 -1
- package/build/umd/diagramme.js +1 -1
- package/build/umd/flowbox.js +1 -1
- package/build/umd/universe.js +1 -1
- package/build-storybook.log +367 -367
- package/package.json +114 -114
- package/src/components/AUAlertComponent.tsx +70 -70
- package/src/components/AUAutoSuggestComponent.js +158 -158
- package/src/components/AUButtonComponent.tsx +84 -84
- package/src/components/AUCalendarComponent.tsx +493 -493
- package/src/components/AUContentToggleComponent.tsx +47 -47
- package/src/components/AUDatepickerComponent.tsx +121 -121
- package/src/components/AUErrorComponent.tsx +78 -78
- package/src/components/AUMobilePrefixComponent.tsx +15 -15
- package/src/components/AUModalComponent.tsx +68 -68
- package/src/components/AUNotificationComponent.tsx +43 -43
- package/src/components/AUReceiptComponent.tsx +33 -33
- package/src/components/AUSpinnerComponent.tsx +36 -36
- package/src/components/AUStepComponent.tsx +47 -47
- package/src/components/AUSubNavComponent.tsx +48 -48
- package/src/components/AUSubmitButtonContainerComponent.tsx +29 -29
- package/src/components/AUTabbedContentComponent.tsx +145 -145
- package/src/components/AUTableComponent.tsx +24 -24
- package/src/components/AUToastComponent.tsx +104 -104
- package/src/components/AUToolbarComponent.tsx +101 -101
- package/src/components/AUTruncatorComponent.tsx +124 -124
- package/src/components/wrapping/AUEmbedComponent.js +47 -47
- package/src/layout-2016/components/alphabox/AlphaBoxComponent.js +143 -143
- package/src/layout-2016/components/alphabox/AlphaBoxContentComponent.js +136 -136
- package/src/layout-2016/components/common/AUCollapsibleComponent.js +152 -152
- package/src/layout-2016/components/common/AUSpinnerComponent.js +103 -103
- package/src/layout-2016/components/databox/DataBoxAlphabetComponent.js +144 -144
- package/src/layout-2016/components/databox/DataBoxAssociationComponent.js +122 -122
- package/src/layout-2016/components/databox/DataBoxButtonComponent.js +157 -157
- package/src/layout-2016/components/databox/DataBoxComponent.js +297 -297
- package/src/layout-2016/components/databox/DataBoxGroupingComponent.js +64 -64
- package/src/layout-2016/components/databox/DataBoxSearchResultComponent.js +36 -36
- package/src/layout-2016/components/databox/DataBoxStackedAssociationComponent.js +54 -54
- package/src/layout-2016/components/databox/DataBoxSuggestionComponent.js +39 -39
- package/src/layout-2016/components/diagramme/AUDiagrammeComponent.js +309 -309
- package/src/layout-2016/components/flowbox/FlowBoxComponent.js +126 -126
- package/src/layout-2016/components/flowbox/FlowBoxPhoneComponent.js +104 -104
- package/src/layout-2016/components/profile/AUProfileAvatar2016Component.js +103 -103
- package/src/layout-2016/components/universe/StaffTopComponent.js +363 -363
- package/src/layout-2016/components/universe/StudentTopComponent.js +137 -137
- package/src/layout-2016/components/universe/UniverseContainerComponent.js +65 -65
- package/src/layout-2016/lib/all.js +3 -3
- package/src/layout-2016/lib/au-alphabox.js +100 -100
- package/src/layout-2016/lib/au-databox.js +400 -400
- package/src/layout-2016/lib/au-diagramme.js +85 -85
- package/src/layout-2016/lib/au-flowbox.js +93 -93
- package/src/layout-2016/lib/universe.js +9 -9
- package/src/lib/context.tsx +51 -51
- package/src/lib/dates.ts +50 -50
- package/src/lib/helpers.ts +208 -208
- package/src/lib/hooks.ts +75 -75
- package/src/lib/i18n.ts +600 -600
- package/src/lib/portals.tsx +119 -119
- package/src/lib/tracking.ts +69 -69
- package/src/lib/wrapping.ts +21 -21
- package/src/styles/_settings.scss +10 -10
- package/src/styles/alphabox.scss +222 -222
- package/src/styles/app.scss +7 -7
- package/src/styles/autosuggest.scss +57 -57
- package/src/styles/databox.scss +563 -563
- package/src/styles/diagramme.scss +119 -119
- package/src/styles/flowbox.scss +72 -72
- package/src/styles/maps.scss +395 -395
- package/stories/AUButtonComponent.stories.tsx +127 -127
- package/stories/AUContentToggleComponent.stories.tsx +63 -63
- package/stories/AUErrorComponent.stories.tsx +98 -98
- package/stories/AUModalComponent.stories.tsx +83 -83
- package/stories/AUNotificationComponent.stories.tsx +116 -116
- package/stories/AUSpinnerComponent.stories.tsx +41 -41
- package/stories/AUStepComponent.stories.tsx +40 -40
- package/stories/AUToolbarComponent.stories.tsx +326 -326
- package/stories/AUTruncatorComponent.stories.tsx +101 -101
- package/stories/lib/helpers.tsx +128 -128
- package/tsconfig.json +46 -46
- package/webpack.config.js +89 -89
package/src/lib/helpers.ts
CHANGED
|
@@ -1,208 +1,208 @@
|
|
|
1
|
-
/* eslint-env browser */
|
|
2
|
-
/* NO IMPORTS! */
|
|
3
|
-
|
|
4
|
-
// eslint-disable-next-line no-control-regex
|
|
5
|
-
const emailRegex = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;
|
|
6
|
-
|
|
7
|
-
const sortAlphaObj = (a, b) => {
|
|
8
|
-
if (typeof a.name !== 'undefined' && typeof b.name !== 'undefined') {
|
|
9
|
-
return a.name.toLowerCase().localeCompare(b.name.toLowerCase(), 'da', { ignorePunctuation: true });
|
|
10
|
-
}
|
|
11
|
-
return 0;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const isElementInViewport = (element: HTMLElement): boolean => {
|
|
15
|
-
if (element === null) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const rect = element.getBoundingClientRect();
|
|
20
|
-
|
|
21
|
-
return rect.bottom > 0
|
|
22
|
-
&& rect.right > 0
|
|
23
|
-
&& rect.left < (window.innerWidth || document.documentElement.clientWidth)
|
|
24
|
-
&& rect.top < (window.innerHeight || document.documentElement.clientHeight);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const isElementPartlyInViewport = (element: HTMLElement): boolean => {
|
|
28
|
-
let top = element.offsetTop;
|
|
29
|
-
let left = element.offsetLeft;
|
|
30
|
-
const width = element.offsetWidth;
|
|
31
|
-
const height = element.offsetHeight;
|
|
32
|
-
|
|
33
|
-
let offsetElement = element;
|
|
34
|
-
while (offsetElement.offsetParent) {
|
|
35
|
-
offsetElement = element.offsetParent as HTMLElement;
|
|
36
|
-
top += element.offsetTop;
|
|
37
|
-
left += element.offsetLeft;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return (
|
|
41
|
-
top < (window.pageYOffset + window.innerHeight)
|
|
42
|
-
&& left < (window.pageXOffset + window.innerWidth)
|
|
43
|
-
&& (top + height) > window.pageYOffset
|
|
44
|
-
&& (left + width) > window.pageXOffset
|
|
45
|
-
);
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const setCookie = (
|
|
49
|
-
cookieName: string,
|
|
50
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
|
-
value: any,
|
|
52
|
-
maxAge: number | null = null,
|
|
53
|
-
exdays: number | null = null,
|
|
54
|
-
path = ';path=/;domain=au.dk',
|
|
55
|
-
): void => {
|
|
56
|
-
let cookieValue = encodeURI(value);
|
|
57
|
-
if (exdays != null) {
|
|
58
|
-
const exdate = new Date();
|
|
59
|
-
exdate.setDate(exdate.getDate() + exdays);
|
|
60
|
-
cookieValue = `${cookieValue}; expires=${exdate.toUTCString()}`;
|
|
61
|
-
} else if (maxAge != null) {
|
|
62
|
-
cookieValue = `${cookieValue}; max-age=${maxAge}`;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
document.cookie = `${cookieName}=${cookieValue}${path}`;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const getCookie = (cookieName: string): string | null | boolean => {
|
|
69
|
-
let cookieValue: string | null | boolean = document.cookie;
|
|
70
|
-
let cookieStart = cookieValue.indexOf(` ${cookieName}=`);
|
|
71
|
-
if (cookieStart === -1) {
|
|
72
|
-
cookieStart = cookieValue.indexOf(`${cookieName}=`);
|
|
73
|
-
}
|
|
74
|
-
if (cookieStart === -1) {
|
|
75
|
-
cookieValue = null;
|
|
76
|
-
} else {
|
|
77
|
-
cookieStart = cookieValue.indexOf('=', cookieStart) + 1;
|
|
78
|
-
let cookieEnd = cookieValue.indexOf(';', cookieStart);
|
|
79
|
-
if (cookieEnd === -1) {
|
|
80
|
-
cookieEnd = cookieValue.length;
|
|
81
|
-
}
|
|
82
|
-
cookieValue = decodeURI(cookieValue.substring(cookieStart, cookieEnd));
|
|
83
|
-
}
|
|
84
|
-
return cookieValue;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const addToSessionStorage = (key: string, data: unknown): unknown => {
|
|
88
|
-
if (window.sessionStorage) {
|
|
89
|
-
sessionStorage.setItem(key, JSON.stringify(data));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return data;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
const getFromSessionStorage = (key: string): unknown | null => {
|
|
96
|
-
if (window.sessionStorage) {
|
|
97
|
-
const data = sessionStorage.getItem(key);
|
|
98
|
-
if (data) {
|
|
99
|
-
return JSON.parse(data);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return null;
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const addToLocalStorage = (key: string, data: unknown): unknown => {
|
|
107
|
-
if (window.localStorage) {
|
|
108
|
-
localStorage.setItem(key, JSON.stringify(data));
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return data;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
const getFromLocalStorage = (key: string): unknown | null => {
|
|
115
|
-
if (window.localStorage) {
|
|
116
|
-
const data = localStorage.getItem(key);
|
|
117
|
-
if (data) {
|
|
118
|
-
return JSON.parse(data);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return null;
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
// Credits: http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
|
|
126
|
-
const setCaretPosition = (element: HTMLInputElement, pos: number): void => {
|
|
127
|
-
// Modern browsers
|
|
128
|
-
if (element && element.setSelectionRange) {
|
|
129
|
-
element.focus();
|
|
130
|
-
element.setSelectionRange(pos, pos);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
const splitPhoneNumber = (
|
|
135
|
-
countryCodes: AU.ICountryCode[],
|
|
136
|
-
phoneNumber: string,
|
|
137
|
-
): AU.IPhoneNumber => {
|
|
138
|
-
const countryCodeFound = (code: string) => countryCodes.filter(
|
|
139
|
-
(x) => x.code === parseInt(code, 10),
|
|
140
|
-
).length > 0;
|
|
141
|
-
|
|
142
|
-
const findPrefix = (): string => {
|
|
143
|
-
if (countryCodes.length > 0) {
|
|
144
|
-
const skip = phoneNumber.length > 0 ? '' : '+'; // Hackish...
|
|
145
|
-
let count = 1;
|
|
146
|
-
let prefix = `${countryCodes.find((x) => x.important)?.code}`;
|
|
147
|
-
while (count < phoneNumber.length) {
|
|
148
|
-
const split = phoneNumber.substring(skip.length, skip.length + count);
|
|
149
|
-
if (countryCodeFound(split)) {
|
|
150
|
-
prefix = split;
|
|
151
|
-
}
|
|
152
|
-
count += 1;
|
|
153
|
-
}
|
|
154
|
-
return skip + prefix;
|
|
155
|
-
}
|
|
156
|
-
return '';
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
const prefix = findPrefix();
|
|
160
|
-
return {
|
|
161
|
-
prefix,
|
|
162
|
-
number: phoneNumber.replace(prefix, ''),
|
|
163
|
-
};
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
const prettyPrintPhone = (countryCodes: AU.ICountryCode[], phone: string) => {
|
|
167
|
-
if (phone) {
|
|
168
|
-
const { prefix, number } = splitPhoneNumber(countryCodes, phone);
|
|
169
|
-
if (prefix) {
|
|
170
|
-
return `${prefix} ${number?.match(/\d{1,2}/g)?.join(' ')}`;
|
|
171
|
-
}
|
|
172
|
-
return number?.match(/\d{1,2}/g)?.join(' ');
|
|
173
|
-
}
|
|
174
|
-
return '';
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
const scrollTo = (x = 0, y = 0) => window.scrollTo(x, y);
|
|
178
|
-
|
|
179
|
-
const replace = (text: string, find: string[], replaceWith: string[]): string => {
|
|
180
|
-
if (find.length !== replaceWith.length) {
|
|
181
|
-
return text;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
let newText = text;
|
|
185
|
-
find.forEach((placeholder, index) => {
|
|
186
|
-
newText = newText.replace(`{{${placeholder}}}`, replaceWith[index]);
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
return newText;
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
export {
|
|
193
|
-
emailRegex,
|
|
194
|
-
sortAlphaObj,
|
|
195
|
-
isElementInViewport,
|
|
196
|
-
isElementPartlyInViewport,
|
|
197
|
-
setCookie,
|
|
198
|
-
getCookie,
|
|
199
|
-
addToLocalStorage,
|
|
200
|
-
getFromLocalStorage,
|
|
201
|
-
addToSessionStorage,
|
|
202
|
-
getFromSessionStorage,
|
|
203
|
-
setCaretPosition,
|
|
204
|
-
splitPhoneNumber,
|
|
205
|
-
prettyPrintPhone,
|
|
206
|
-
scrollTo,
|
|
207
|
-
replace,
|
|
208
|
-
};
|
|
1
|
+
/* eslint-env browser */
|
|
2
|
+
/* NO IMPORTS! */
|
|
3
|
+
|
|
4
|
+
// eslint-disable-next-line no-control-regex
|
|
5
|
+
const emailRegex = /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;
|
|
6
|
+
|
|
7
|
+
const sortAlphaObj = (a, b) => {
|
|
8
|
+
if (typeof a.name !== 'undefined' && typeof b.name !== 'undefined') {
|
|
9
|
+
return a.name.toLowerCase().localeCompare(b.name.toLowerCase(), 'da', { ignorePunctuation: true });
|
|
10
|
+
}
|
|
11
|
+
return 0;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const isElementInViewport = (element: HTMLElement): boolean => {
|
|
15
|
+
if (element === null) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const rect = element.getBoundingClientRect();
|
|
20
|
+
|
|
21
|
+
return rect.bottom > 0
|
|
22
|
+
&& rect.right > 0
|
|
23
|
+
&& rect.left < (window.innerWidth || document.documentElement.clientWidth)
|
|
24
|
+
&& rect.top < (window.innerHeight || document.documentElement.clientHeight);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const isElementPartlyInViewport = (element: HTMLElement): boolean => {
|
|
28
|
+
let top = element.offsetTop;
|
|
29
|
+
let left = element.offsetLeft;
|
|
30
|
+
const width = element.offsetWidth;
|
|
31
|
+
const height = element.offsetHeight;
|
|
32
|
+
|
|
33
|
+
let offsetElement = element;
|
|
34
|
+
while (offsetElement.offsetParent) {
|
|
35
|
+
offsetElement = element.offsetParent as HTMLElement;
|
|
36
|
+
top += element.offsetTop;
|
|
37
|
+
left += element.offsetLeft;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
top < (window.pageYOffset + window.innerHeight)
|
|
42
|
+
&& left < (window.pageXOffset + window.innerWidth)
|
|
43
|
+
&& (top + height) > window.pageYOffset
|
|
44
|
+
&& (left + width) > window.pageXOffset
|
|
45
|
+
);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const setCookie = (
|
|
49
|
+
cookieName: string,
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
|
+
value: any,
|
|
52
|
+
maxAge: number | null = null,
|
|
53
|
+
exdays: number | null = null,
|
|
54
|
+
path = ';path=/;domain=au.dk',
|
|
55
|
+
): void => {
|
|
56
|
+
let cookieValue = encodeURI(value);
|
|
57
|
+
if (exdays != null) {
|
|
58
|
+
const exdate = new Date();
|
|
59
|
+
exdate.setDate(exdate.getDate() + exdays);
|
|
60
|
+
cookieValue = `${cookieValue}; expires=${exdate.toUTCString()}`;
|
|
61
|
+
} else if (maxAge != null) {
|
|
62
|
+
cookieValue = `${cookieValue}; max-age=${maxAge}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
document.cookie = `${cookieName}=${cookieValue}${path}`;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const getCookie = (cookieName: string): string | null | boolean => {
|
|
69
|
+
let cookieValue: string | null | boolean = document.cookie;
|
|
70
|
+
let cookieStart = cookieValue.indexOf(` ${cookieName}=`);
|
|
71
|
+
if (cookieStart === -1) {
|
|
72
|
+
cookieStart = cookieValue.indexOf(`${cookieName}=`);
|
|
73
|
+
}
|
|
74
|
+
if (cookieStart === -1) {
|
|
75
|
+
cookieValue = null;
|
|
76
|
+
} else {
|
|
77
|
+
cookieStart = cookieValue.indexOf('=', cookieStart) + 1;
|
|
78
|
+
let cookieEnd = cookieValue.indexOf(';', cookieStart);
|
|
79
|
+
if (cookieEnd === -1) {
|
|
80
|
+
cookieEnd = cookieValue.length;
|
|
81
|
+
}
|
|
82
|
+
cookieValue = decodeURI(cookieValue.substring(cookieStart, cookieEnd));
|
|
83
|
+
}
|
|
84
|
+
return cookieValue;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const addToSessionStorage = (key: string, data: unknown): unknown => {
|
|
88
|
+
if (window.sessionStorage) {
|
|
89
|
+
sessionStorage.setItem(key, JSON.stringify(data));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return data;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const getFromSessionStorage = (key: string): unknown | null => {
|
|
96
|
+
if (window.sessionStorage) {
|
|
97
|
+
const data = sessionStorage.getItem(key);
|
|
98
|
+
if (data) {
|
|
99
|
+
return JSON.parse(data);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return null;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const addToLocalStorage = (key: string, data: unknown): unknown => {
|
|
107
|
+
if (window.localStorage) {
|
|
108
|
+
localStorage.setItem(key, JSON.stringify(data));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return data;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const getFromLocalStorage = (key: string): unknown | null => {
|
|
115
|
+
if (window.localStorage) {
|
|
116
|
+
const data = localStorage.getItem(key);
|
|
117
|
+
if (data) {
|
|
118
|
+
return JSON.parse(data);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return null;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// Credits: http://blog.vishalon.net/index.php/javascript-getting-and-setting-caret-position-in-textarea/
|
|
126
|
+
const setCaretPosition = (element: HTMLInputElement, pos: number): void => {
|
|
127
|
+
// Modern browsers
|
|
128
|
+
if (element && element.setSelectionRange) {
|
|
129
|
+
element.focus();
|
|
130
|
+
element.setSelectionRange(pos, pos);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const splitPhoneNumber = (
|
|
135
|
+
countryCodes: AU.ICountryCode[],
|
|
136
|
+
phoneNumber: string,
|
|
137
|
+
): AU.IPhoneNumber => {
|
|
138
|
+
const countryCodeFound = (code: string) => countryCodes.filter(
|
|
139
|
+
(x) => x.code === parseInt(code, 10),
|
|
140
|
+
).length > 0;
|
|
141
|
+
|
|
142
|
+
const findPrefix = (): string => {
|
|
143
|
+
if (countryCodes.length > 0) {
|
|
144
|
+
const skip = phoneNumber.length > 0 ? '' : '+'; // Hackish...
|
|
145
|
+
let count = 1;
|
|
146
|
+
let prefix = `${countryCodes.find((x) => x.important)?.code}`;
|
|
147
|
+
while (count < phoneNumber.length) {
|
|
148
|
+
const split = phoneNumber.substring(skip.length, skip.length + count);
|
|
149
|
+
if (countryCodeFound(split)) {
|
|
150
|
+
prefix = split;
|
|
151
|
+
}
|
|
152
|
+
count += 1;
|
|
153
|
+
}
|
|
154
|
+
return skip + prefix;
|
|
155
|
+
}
|
|
156
|
+
return '';
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const prefix = findPrefix();
|
|
160
|
+
return {
|
|
161
|
+
prefix,
|
|
162
|
+
number: phoneNumber.replace(prefix, ''),
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const prettyPrintPhone = (countryCodes: AU.ICountryCode[], phone: string) => {
|
|
167
|
+
if (phone) {
|
|
168
|
+
const { prefix, number } = splitPhoneNumber(countryCodes, phone);
|
|
169
|
+
if (prefix) {
|
|
170
|
+
return `${prefix} ${number?.match(/\d{1,2}/g)?.join(' ')}`;
|
|
171
|
+
}
|
|
172
|
+
return number?.match(/\d{1,2}/g)?.join(' ');
|
|
173
|
+
}
|
|
174
|
+
return '';
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const scrollTo = (x = 0, y = 0) => window.scrollTo(x, y);
|
|
178
|
+
|
|
179
|
+
const replace = (text: string, find: string[], replaceWith: string[]): string => {
|
|
180
|
+
if (find.length !== replaceWith.length) {
|
|
181
|
+
return text;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let newText = text;
|
|
185
|
+
find.forEach((placeholder, index) => {
|
|
186
|
+
newText = newText.replace(`{{${placeholder}}}`, replaceWith[index]);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
return newText;
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
export {
|
|
193
|
+
emailRegex,
|
|
194
|
+
sortAlphaObj,
|
|
195
|
+
isElementInViewport,
|
|
196
|
+
isElementPartlyInViewport,
|
|
197
|
+
setCookie,
|
|
198
|
+
getCookie,
|
|
199
|
+
addToLocalStorage,
|
|
200
|
+
getFromLocalStorage,
|
|
201
|
+
addToSessionStorage,
|
|
202
|
+
getFromSessionStorage,
|
|
203
|
+
setCaretPosition,
|
|
204
|
+
splitPhoneNumber,
|
|
205
|
+
prettyPrintPhone,
|
|
206
|
+
scrollTo,
|
|
207
|
+
replace,
|
|
208
|
+
};
|
package/src/lib/hooks.ts
CHANGED
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
3
|
-
/* eslint-disable import/prefer-default-export */
|
|
4
|
-
import { useEffect, useState } from 'react';
|
|
5
|
-
import { showModal, hideModal } from '@aarhus-university/au-designsystem-delphinus/source/js/components/modal-view';
|
|
6
|
-
|
|
7
|
-
const modalIsVisible = (modalElement: Element | null) => (!modalElement ? false : getComputedStyle(modalElement).display !== 'none');
|
|
8
|
-
|
|
9
|
-
const useModal = (
|
|
10
|
-
domId: string,
|
|
11
|
-
sender: HTMLButtonElement | null,
|
|
12
|
-
show = true,
|
|
13
|
-
closeable = true,
|
|
14
|
-
onClose: () => void = () => { },
|
|
15
|
-
): void => {
|
|
16
|
-
useEffect(() => {
|
|
17
|
-
let cleanUp: ExtendedCleanUpPair[] = [];
|
|
18
|
-
const modalView = document.getElementById(domId);
|
|
19
|
-
if (modalView) {
|
|
20
|
-
const header = modalView.querySelector('.modal-view__header');
|
|
21
|
-
if (header && !header.getAttribute('id')) {
|
|
22
|
-
header.setAttribute('id', `${domId}-header`);
|
|
23
|
-
modalView.setAttribute('aria-labelledby', `${domId}-header`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (sender && show) {
|
|
28
|
-
cleanUp = showModal(domId, sender, () => {
|
|
29
|
-
onClose();
|
|
30
|
-
}, closeable);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (sender && !show && modalIsVisible(modalView)) {
|
|
34
|
-
hideModal(domId, sender);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return () => {
|
|
38
|
-
cleanUp.forEach((c) => {
|
|
39
|
-
if (c.clickEvent) {
|
|
40
|
-
c.element.removeEventListener('click', c.clickEvent);
|
|
41
|
-
}
|
|
42
|
-
if (c.keyUpEvent) {
|
|
43
|
-
c.element.removeEventListener('keyup', c.keyUpEvent);
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
};
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const useTranslation = <T>(
|
|
51
|
-
initialObject: T,
|
|
52
|
-
promises: [Promise<any>, Promise<any> | null],
|
|
53
|
-
lang: string,
|
|
54
|
-
): T => {
|
|
55
|
-
const [translations, setTranslations] = useState<T>(initialObject);
|
|
56
|
-
const loadTranslation = async (): Promise<void> => {
|
|
57
|
-
const translation = await Promise.all(promises);
|
|
58
|
-
if (translation[1] == null) {
|
|
59
|
-
setTranslations(translation[0].default);
|
|
60
|
-
} else {
|
|
61
|
-
setTranslations({ ...translation[0].default, ...translation[1].default });
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
useEffect(() => {
|
|
66
|
-
loadTranslation();
|
|
67
|
-
}, [lang]);
|
|
68
|
-
|
|
69
|
-
return translations;
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
export {
|
|
73
|
-
useModal,
|
|
74
|
-
useTranslation,
|
|
75
|
-
};
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
3
|
+
/* eslint-disable import/prefer-default-export */
|
|
4
|
+
import { useEffect, useState } from 'react';
|
|
5
|
+
import { showModal, hideModal } from '@aarhus-university/au-designsystem-delphinus/source/js/components/modal-view';
|
|
6
|
+
|
|
7
|
+
const modalIsVisible = (modalElement: Element | null) => (!modalElement ? false : getComputedStyle(modalElement).display !== 'none');
|
|
8
|
+
|
|
9
|
+
const useModal = (
|
|
10
|
+
domId: string,
|
|
11
|
+
sender: HTMLButtonElement | null,
|
|
12
|
+
show = true,
|
|
13
|
+
closeable = true,
|
|
14
|
+
onClose: () => void = () => { },
|
|
15
|
+
): void => {
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
let cleanUp: ExtendedCleanUpPair[] = [];
|
|
18
|
+
const modalView = document.getElementById(domId);
|
|
19
|
+
if (modalView) {
|
|
20
|
+
const header = modalView.querySelector('.modal-view__header');
|
|
21
|
+
if (header && !header.getAttribute('id')) {
|
|
22
|
+
header.setAttribute('id', `${domId}-header`);
|
|
23
|
+
modalView.setAttribute('aria-labelledby', `${domId}-header`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (sender && show) {
|
|
28
|
+
cleanUp = showModal(domId, sender, () => {
|
|
29
|
+
onClose();
|
|
30
|
+
}, closeable);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (sender && !show && modalIsVisible(modalView)) {
|
|
34
|
+
hideModal(domId, sender);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return () => {
|
|
38
|
+
cleanUp.forEach((c) => {
|
|
39
|
+
if (c.clickEvent) {
|
|
40
|
+
c.element.removeEventListener('click', c.clickEvent);
|
|
41
|
+
}
|
|
42
|
+
if (c.keyUpEvent) {
|
|
43
|
+
c.element.removeEventListener('keyup', c.keyUpEvent);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const useTranslation = <T>(
|
|
51
|
+
initialObject: T,
|
|
52
|
+
promises: [Promise<any>, Promise<any> | null],
|
|
53
|
+
lang: string,
|
|
54
|
+
): T => {
|
|
55
|
+
const [translations, setTranslations] = useState<T>(initialObject);
|
|
56
|
+
const loadTranslation = async (): Promise<void> => {
|
|
57
|
+
const translation = await Promise.all(promises);
|
|
58
|
+
if (translation[1] == null) {
|
|
59
|
+
setTranslations(translation[0].default);
|
|
60
|
+
} else {
|
|
61
|
+
setTranslations({ ...translation[0].default, ...translation[1].default });
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
loadTranslation();
|
|
67
|
+
}, [lang]);
|
|
68
|
+
|
|
69
|
+
return translations;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export {
|
|
73
|
+
useModal,
|
|
74
|
+
useTranslation,
|
|
75
|
+
};
|