@aarhus-university/au-lib-react-components 10.15.2 → 10.17.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.
- package/__tests__/jest/AUErrorComponent.test.tsx +142 -142
- package/__tests__/jest/AUNotificationComponent.test.tsx +115 -115
- package/__tests__/jest/context.test.ts +25 -25
- 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/package.json +1 -1
- package/src/components/AUErrorComponent.tsx +78 -78
- package/src/components/AUNotificationComponent.tsx +43 -43
- package/src/lib/context.tsx +43 -38
- package/src/lib/dates.ts +50 -50
- package/src/lib/hooks.ts +18 -1
- package/src/lib/portals.ts +37 -36
- package/stories/lib/helpers.tsx +57 -57
- package/tsconfig.json +1 -1
package/src/lib/dates.ts
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import dayjs from 'dayjs';
|
|
2
|
-
import 'dayjs/locale/da';
|
|
3
|
-
import isoWeek from 'dayjs/plugin/isoWeek';
|
|
4
|
-
import weekday from 'dayjs/plugin/weekday';
|
|
5
|
-
import advancedFormat from 'dayjs/plugin/advancedFormat';
|
|
6
|
-
import updateLocale from 'dayjs/plugin/updateLocale';
|
|
7
|
-
|
|
8
|
-
dayjs.extend(isoWeek);
|
|
9
|
-
dayjs.extend(weekday);
|
|
10
|
-
dayjs.extend(advancedFormat);
|
|
11
|
-
dayjs.extend(updateLocale);
|
|
12
|
-
|
|
13
|
-
dayjs.updateLocale('da', {
|
|
14
|
-
weekdaysShort: 'søn_man_tir_ons_tor_fre_lør'.split('_'),
|
|
15
|
-
monthsShort: 'jan_feb_mar_apr_maj_jun_jul_aug_sep_okt_nov_dec'.split('_'),
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
export const formatTime = (date: Date, format = 'HH:mm'): string => dayjs(date).format(format);
|
|
19
|
-
export const getLocale = (): string => dayjs.locale();
|
|
20
|
-
export const setLocale = (locale: string): string => dayjs.locale(locale);
|
|
21
|
-
export const getThisYear = (): number => dayjs().year();
|
|
22
|
-
export const getYearToString = (date: Date): string => formatTime(date, 'YYYY');
|
|
23
|
-
export const getNumberOfWeeksInYear = (year: number): number => dayjs(`${year}-12-31`).isoWeek();
|
|
24
|
-
export const skipWeeks = (date: Date, multiplier: number): Date => dayjs(date).clone().add(multiplier * 7, 'days').toDate();
|
|
25
|
-
export const getWeek = (date: Date): number => dayjs(date).isoWeek();
|
|
26
|
-
// eslint-disable-next-line max-len
|
|
27
|
-
export const getDateByWeekday = (date: Date, index: number, locale = 'da'): Date => {
|
|
28
|
-
const currentLocale = getLocale();
|
|
29
|
-
setLocale(locale);
|
|
30
|
-
const newDate = dayjs(date).weekday(index).toDate();
|
|
31
|
-
if (currentLocale !== locale) {
|
|
32
|
-
setLocale(currentLocale);
|
|
33
|
-
}
|
|
34
|
-
return newDate;
|
|
35
|
-
};
|
|
36
|
-
const dateIsSameMonthAsCurrent = (date: Date): boolean => formatTime(new Date(), 'M') === formatTime(date, 'M');
|
|
37
|
-
export const getLongDate = (date: Date): string => formatTime(date, 'dddd, Do MMMM YYYY');
|
|
38
|
-
export const getShortDate = (date: Date): string => formatTime(date, `ddd D${(dateIsSameMonthAsCurrent(date) ? '' : `${getLocale() === 'da' ? '.' : ''} MMM`)}`);
|
|
39
|
-
export const isToday = (date: Date): boolean => dayjs(date).isSame(dayjs());
|
|
40
|
-
export const multipleDaysFormatTime = (date: Date, index: number, format = 'ddd HH:mm', firstLetterToUpper = true): string => {
|
|
41
|
-
if (index > 0) {
|
|
42
|
-
const formatted = formatTime(date, format);
|
|
43
|
-
if (firstLetterToUpper) {
|
|
44
|
-
return formatted.charAt(0).toUpperCase() + formatted.slice(1);
|
|
45
|
-
}
|
|
46
|
-
return formatted;
|
|
47
|
-
}
|
|
48
|
-
return formatTime(date);
|
|
49
|
-
};
|
|
50
|
-
export const formatDayOfWeek = (date: Date): string => formatTime(date, 'dddd').charAt(0).toUpperCase() + formatTime(date, 'dddd').slice(1);
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
import 'dayjs/locale/da';
|
|
3
|
+
import isoWeek from 'dayjs/plugin/isoWeek';
|
|
4
|
+
import weekday from 'dayjs/plugin/weekday';
|
|
5
|
+
import advancedFormat from 'dayjs/plugin/advancedFormat';
|
|
6
|
+
import updateLocale from 'dayjs/plugin/updateLocale';
|
|
7
|
+
|
|
8
|
+
dayjs.extend(isoWeek);
|
|
9
|
+
dayjs.extend(weekday);
|
|
10
|
+
dayjs.extend(advancedFormat);
|
|
11
|
+
dayjs.extend(updateLocale);
|
|
12
|
+
|
|
13
|
+
dayjs.updateLocale('da', {
|
|
14
|
+
weekdaysShort: 'søn_man_tir_ons_tor_fre_lør'.split('_'),
|
|
15
|
+
monthsShort: 'jan_feb_mar_apr_maj_jun_jul_aug_sep_okt_nov_dec'.split('_'),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const formatTime = (date: Date, format = 'HH:mm'): string => dayjs(date).format(format);
|
|
19
|
+
export const getLocale = (): string => dayjs.locale();
|
|
20
|
+
export const setLocale = (locale: string): string => dayjs.locale(locale);
|
|
21
|
+
export const getThisYear = (): number => dayjs().year();
|
|
22
|
+
export const getYearToString = (date: Date): string => formatTime(date, 'YYYY');
|
|
23
|
+
export const getNumberOfWeeksInYear = (year: number): number => dayjs(`${year}-12-31`).isoWeek();
|
|
24
|
+
export const skipWeeks = (date: Date, multiplier: number): Date => dayjs(date).clone().add(multiplier * 7, 'days').toDate();
|
|
25
|
+
export const getWeek = (date: Date): number => dayjs(date).isoWeek();
|
|
26
|
+
// eslint-disable-next-line max-len
|
|
27
|
+
export const getDateByWeekday = (date: Date, index: number, locale = 'da'): Date => {
|
|
28
|
+
const currentLocale = getLocale();
|
|
29
|
+
setLocale(locale);
|
|
30
|
+
const newDate = dayjs(date).weekday(index).toDate();
|
|
31
|
+
if (currentLocale !== locale) {
|
|
32
|
+
setLocale(currentLocale);
|
|
33
|
+
}
|
|
34
|
+
return newDate;
|
|
35
|
+
};
|
|
36
|
+
const dateIsSameMonthAsCurrent = (date: Date): boolean => formatTime(new Date(), 'M') === formatTime(date, 'M');
|
|
37
|
+
export const getLongDate = (date: Date): string => formatTime(date, 'dddd, Do MMMM YYYY');
|
|
38
|
+
export const getShortDate = (date: Date): string => formatTime(date, `ddd D${(dateIsSameMonthAsCurrent(date) ? '' : `${getLocale() === 'da' ? '.' : ''} MMM`)}`);
|
|
39
|
+
export const isToday = (date: Date): boolean => dayjs(date).isSame(dayjs());
|
|
40
|
+
export const multipleDaysFormatTime = (date: Date, index: number, format = 'ddd HH:mm', firstLetterToUpper = true): string => {
|
|
41
|
+
if (index > 0) {
|
|
42
|
+
const formatted = formatTime(date, format);
|
|
43
|
+
if (firstLetterToUpper) {
|
|
44
|
+
return formatted.charAt(0).toUpperCase() + formatted.slice(1);
|
|
45
|
+
}
|
|
46
|
+
return formatted;
|
|
47
|
+
}
|
|
48
|
+
return formatTime(date);
|
|
49
|
+
};
|
|
50
|
+
export const formatDayOfWeek = (date: Date): string => formatTime(date, 'dddd').charAt(0).toUpperCase() + formatTime(date, 'dddd').slice(1);
|
package/src/lib/hooks.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-empty-function */
|
|
2
2
|
/* eslint-disable import/prefer-default-export */
|
|
3
|
-
import { useEffect } from 'react';
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
4
|
import { showModal, hideModal } from '@aarhus-university/au-designsystem-delphinus/source/js/components/modal-view';
|
|
5
5
|
|
|
6
6
|
const useModal = (
|
|
@@ -32,6 +32,23 @@ const useModal = (
|
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
+
const useTranslation = <T>(lang: string, filename: string, initialObject: T): T => {
|
|
36
|
+
const [translations, setTranslations] = useState<T>(initialObject);
|
|
37
|
+
const loadTranslation = async (): Promise<void> => {
|
|
38
|
+
const [translation] = await Promise.all([
|
|
39
|
+
import(`../i18n/${lang}/${filename}.json`),
|
|
40
|
+
]);
|
|
41
|
+
setTranslations(translation.default);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
loadTranslation();
|
|
46
|
+
}, []);
|
|
47
|
+
|
|
48
|
+
return translations;
|
|
49
|
+
};
|
|
50
|
+
|
|
35
51
|
export {
|
|
36
52
|
useModal,
|
|
53
|
+
useTranslation,
|
|
37
54
|
};
|
package/src/lib/portals.ts
CHANGED
|
@@ -1,36 +1,37 @@
|
|
|
1
|
-
/* eslint-disable import/prefer-default-export */
|
|
2
|
-
const impersonationLabels = {
|
|
3
|
-
IMPERSONATION_SELECT_OPTION_CPR: { da: 'CPR-nr.', en: 'CPR no.' },
|
|
4
|
-
IMPERSONATION_SELECT_OPTION_EMAIL: { da: 'E-mail', en: 'Email' },
|
|
5
|
-
IMPERSONATION_SELECT_OPTION_AUID: { da: 'AU-id', en: 'AU ID' },
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
declare global {
|
|
9
|
-
interface Window {
|
|
10
|
-
IMPERSONATE_AUID: string,
|
|
11
|
-
IMPERSONATE_VALUE: string;
|
|
12
|
-
IMPERSONATE_TYPE: string;
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
1
|
+
/* eslint-disable import/prefer-default-export */
|
|
2
|
+
const impersonationLabels = {
|
|
3
|
+
IMPERSONATION_SELECT_OPTION_CPR: { da: 'CPR-nr.', en: 'CPR no.' },
|
|
4
|
+
IMPERSONATION_SELECT_OPTION_EMAIL: { da: 'E-mail', en: 'Email' },
|
|
5
|
+
IMPERSONATION_SELECT_OPTION_AUID: { da: 'AU-id', en: 'AU ID' },
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
declare global {
|
|
9
|
+
interface Window {
|
|
10
|
+
IMPERSONATE_AUID: string,
|
|
11
|
+
IMPERSONATE_VALUE: string;
|
|
12
|
+
IMPERSONATE_TYPE: string;
|
|
13
|
+
authenticated: boolean,
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const portals: (lang: string) => AU.IPortalContext[] = (lang: string) => [
|
|
18
|
+
{
|
|
19
|
+
name: 'mitstudie',
|
|
20
|
+
impersonationSetup: {
|
|
21
|
+
auid: window.IMPERSONATE_AUID,
|
|
22
|
+
value: window.IMPERSONATE_VALUE,
|
|
23
|
+
type: window.IMPERSONATE_TYPE,
|
|
24
|
+
},
|
|
25
|
+
impersonationOptions: [
|
|
26
|
+
{
|
|
27
|
+
value: 'auid',
|
|
28
|
+
label: impersonationLabels.IMPERSONATION_SELECT_OPTION_AUID[lang],
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
export const getPortalByName = (
|
|
35
|
+
lang: string,
|
|
36
|
+
name: string,
|
|
37
|
+
): AU.IPortalContext | null => portals(lang).find((portal) => portal.name === name) ?? null;
|
package/stories/lib/helpers.tsx
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
import React, { useEffect, StrictMode } from 'react';
|
|
2
|
-
|
|
3
|
-
const globalTheme = {
|
|
4
|
-
theme: {
|
|
5
|
-
name: 'Theme',
|
|
6
|
-
description: 'Global theme for components',
|
|
7
|
-
defaultValue: 'light',
|
|
8
|
-
toolbar: {
|
|
9
|
-
icon: 'circlehollow',
|
|
10
|
-
items: ['normal', 'dark'],
|
|
11
|
-
showName: true,
|
|
12
|
-
dynamicTitle: true,
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const globalLang = (items = ['da', 'en'], defaultValue = 'da') => ({
|
|
18
|
-
lang: {
|
|
19
|
-
name: 'Language',
|
|
20
|
-
description: 'Global language for components',
|
|
21
|
-
defaultValue,
|
|
22
|
-
toolbar: {
|
|
23
|
-
icon: 'circlehollow',
|
|
24
|
-
items,
|
|
25
|
-
showName: true,
|
|
26
|
-
dynamicTitle: true,
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
const ThemeWrapper = ({ theme, children }) => {
|
|
32
|
-
useEffect(() => {
|
|
33
|
-
const body = document.querySelector('body');
|
|
34
|
-
if (body) {
|
|
35
|
-
body.classList.remove('theme--normal');
|
|
36
|
-
body.classList.remove('theme--dark');
|
|
37
|
-
body.classList.add(`theme--${theme}`);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
return (
|
|
41
|
-
<StrictMode>
|
|
42
|
-
<main className={`theme--${theme}`}>
|
|
43
|
-
<div className="page">
|
|
44
|
-
<div className="page__content__block">
|
|
45
|
-
{children}
|
|
46
|
-
</div>
|
|
47
|
-
</div>
|
|
48
|
-
</main>
|
|
49
|
-
</StrictMode>
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export {
|
|
54
|
-
globalTheme,
|
|
55
|
-
globalLang,
|
|
56
|
-
ThemeWrapper,
|
|
57
|
-
};
|
|
1
|
+
import React, { useEffect, StrictMode } from 'react';
|
|
2
|
+
|
|
3
|
+
const globalTheme = {
|
|
4
|
+
theme: {
|
|
5
|
+
name: 'Theme',
|
|
6
|
+
description: 'Global theme for components',
|
|
7
|
+
defaultValue: 'light',
|
|
8
|
+
toolbar: {
|
|
9
|
+
icon: 'circlehollow',
|
|
10
|
+
items: ['normal', 'dark'],
|
|
11
|
+
showName: true,
|
|
12
|
+
dynamicTitle: true,
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const globalLang = (items = ['da', 'en'], defaultValue = 'da') => ({
|
|
18
|
+
lang: {
|
|
19
|
+
name: 'Language',
|
|
20
|
+
description: 'Global language for components',
|
|
21
|
+
defaultValue,
|
|
22
|
+
toolbar: {
|
|
23
|
+
icon: 'circlehollow',
|
|
24
|
+
items,
|
|
25
|
+
showName: true,
|
|
26
|
+
dynamicTitle: true,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const ThemeWrapper = ({ theme, children }) => {
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const body = document.querySelector('body');
|
|
34
|
+
if (body) {
|
|
35
|
+
body.classList.remove('theme--normal');
|
|
36
|
+
body.classList.remove('theme--dark');
|
|
37
|
+
body.classList.add(`theme--${theme}`);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return (
|
|
41
|
+
<StrictMode>
|
|
42
|
+
<main className={`theme--${theme}`}>
|
|
43
|
+
<div className="page">
|
|
44
|
+
<div className="page__content__block">
|
|
45
|
+
{children}
|
|
46
|
+
</div>
|
|
47
|
+
</div>
|
|
48
|
+
</main>
|
|
49
|
+
</StrictMode>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export {
|
|
54
|
+
globalTheme,
|
|
55
|
+
globalLang,
|
|
56
|
+
ThemeWrapper,
|
|
57
|
+
};
|