@aarhus-university/au-lib-react-components 12.6.2 → 12.6.3
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 +35 -35
- package/.storybook/main.js +34 -34
- package/.storybook/preview.js +17 -17
- package/README.md +19 -19
- package/__tests__/jest/AUButtonComponent.test.tsx +165 -165
- package/__tests__/jest/AUDynamicContentComponent.test.tsx +386 -386
- 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/flowbox.js.map +1 -1
- package/build/umd/universe.js +1 -1
- package/build-storybook.log +386 -386
- package/esbuild.mjs +22 -22
- package/package.json +107 -107
- package/src/components/AUAlertComponent.tsx +128 -128
- package/src/components/AUAutoSuggestComponent.js +148 -148
- package/src/components/AUButtonComponent.tsx +99 -99
- package/src/components/AUCalendarComponent.tsx +497 -497
- package/src/components/AUCharacterCountComponent.tsx +56 -56
- package/src/components/AUComboBoxComponent.tsx +195 -195
- package/src/components/AUContentToggleComponent.tsx +50 -50
- package/src/components/AUDatepickerComponent.tsx +124 -124
- package/src/components/AUDialogModalComponent.tsx +124 -124
- package/src/components/AUDynamicContentComponent.tsx +137 -137
- package/src/components/AUEditorComponent.tsx +126 -126
- package/src/components/AUErrorComponent.tsx +73 -73
- package/src/components/AUMobilePrefixComponent.tsx +20 -20
- package/src/components/AUModalComponent.tsx +72 -72
- package/src/components/AUNotificationComponent.tsx +44 -44
- package/src/components/AUReceiptComponent.tsx +34 -34
- package/src/components/AUSpinnerComponent.tsx +40 -40
- package/src/components/AUStepComponent.tsx +75 -75
- package/src/components/AUSubNavComponent.tsx +57 -57
- package/src/components/AUSubmitButtonContainerComponent.tsx +38 -38
- package/src/components/AUTabbedContentComponent.tsx +154 -154
- package/src/components/AUTableComponent.tsx +29 -29
- package/src/components/AUToastComponent.tsx +104 -104
- package/src/components/AUToolbarComponent.tsx +108 -108
- package/src/components/AUTruncatorComponent.tsx +141 -141
- package/src/components/wrapping/AUEmbedComponent.js +47 -47
- package/src/layout-2016/components/alphabox/AlphaBoxComponent.js +142 -142
- 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/lib/all.js +3 -3
- package/src/layout-2016/lib/au-alphabox.js +99 -99
- package/src/layout-2016/lib/au-databox.js +399 -399
- package/src/layout-2016/lib/au-diagramme.js +85 -85
- package/src/layout-2016/lib/au-flowbox.js +116 -119
- package/src/lib/context.tsx +59 -59
- package/src/lib/dates.ts +52 -52
- package/src/lib/helpers.ts +208 -208
- package/src/lib/hooks.ts +157 -157
- package/src/lib/i18n.ts +600 -600
- package/src/lib/portals.tsx +150 -150
- package/src/lib/tinymce.ts +84 -84
- 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/AUAlertComponent.stories.tsx +133 -133
- package/stories/AUAutoSuggestComponent.stories.tsx +95 -95
- package/stories/AUButtonComponent.stories.tsx +139 -139
- package/stories/AUCharacterCountComponent.stories.tsx +121 -121
- package/stories/AUComboBoxComponent.stories.tsx +101 -101
- package/stories/AUContentToggleComponent.stories.tsx +87 -87
- package/stories/AUDialogModalComponent.stories.tsx +75 -75
- package/stories/AUDynamicContentComponent.stories.tsx +119 -119
- package/stories/AUEditorComponent.stories.tsx +66 -66
- package/stories/AUErrorComponent.stories.tsx +132 -132
- package/stories/AUModalComponent.stories.tsx +160 -160
- package/stories/AUNotificationComponent.stories.tsx +151 -151
- package/stories/AUSpinnerComponent.stories.tsx +44 -44
- package/stories/AUStepComponent.stories.tsx +91 -91
- package/stories/AUToolbarComponent.stories.tsx +389 -389
- package/stories/AUTruncatorComponent.stories.tsx +123 -123
- package/stories/lib/helpers.tsx +146 -146
- package/tsconfig.json +46 -46
- package/webpack.config.js +88 -88
- package/.claude/settings.local.json +0 -12
- package/.vscode/settings.json +0 -22
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
/* eslint-disable camelcase */
|
|
2
|
-
import React, {
|
|
3
|
-
FC,
|
|
4
|
-
} from 'react';
|
|
5
|
-
import { replace } from '../lib/helpers';
|
|
6
|
-
import { useCharacterCount } from '../lib/hooks';
|
|
7
|
-
|
|
8
|
-
type Props = {
|
|
9
|
-
children: React.ReactNode;
|
|
10
|
-
type: 'recommended' | 'allowed';
|
|
11
|
-
initialChars: number;
|
|
12
|
-
maxChars: number;
|
|
13
|
-
translations: IGenericTranslation['generics']['forms'];
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const AUCharacterCountComponent: FC<Props> = ({
|
|
17
|
-
children,
|
|
18
|
-
type,
|
|
19
|
-
initialChars,
|
|
20
|
-
maxChars,
|
|
21
|
-
translations,
|
|
22
|
-
}) => {
|
|
23
|
-
const {
|
|
24
|
-
ref, remaining, overflow,
|
|
25
|
-
} = useCharacterCount(type, initialChars, maxChars);
|
|
26
|
-
|
|
27
|
-
const { character_count } = translations;
|
|
28
|
-
|
|
29
|
-
return (
|
|
30
|
-
<div
|
|
31
|
-
ref={ref}
|
|
32
|
-
className="form__field__character-count"
|
|
33
|
-
>
|
|
34
|
-
{children}
|
|
35
|
-
<div
|
|
36
|
-
className="form__field__character-count__info"
|
|
37
|
-
aria-live="polite"
|
|
38
|
-
>
|
|
39
|
-
<span className="form__field__character-count__info__overflow-message">
|
|
40
|
-
{`${replace(character_count.overflow.message[type], ['maxchars'], [maxChars.toString()])}`}
|
|
41
|
-
</span>
|
|
42
|
-
{' '}
|
|
43
|
-
<span className="form__field__character-count__info__overflow-count">
|
|
44
|
-
{`${replace(character_count.overflow.count[type][overflow === 1 ? 'one' : 'other'], ['overflow'], [overflow.toString()])}`}
|
|
45
|
-
</span>
|
|
46
|
-
{' '}
|
|
47
|
-
<span className="form__field__character-count__info__count-down">
|
|
48
|
-
{`${replace(character_count.count_down[remaining === 1 ? 'one' : 'other'], ['remaining'], [remaining.toString()])}`}
|
|
49
|
-
</span>
|
|
50
|
-
</div>
|
|
51
|
-
</div>
|
|
52
|
-
);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
AUCharacterCountComponent.displayName = 'AUCharacterCountComponent';
|
|
56
|
-
export default AUCharacterCountComponent;
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
import React, {
|
|
3
|
+
FC,
|
|
4
|
+
} from 'react';
|
|
5
|
+
import { replace } from '../lib/helpers';
|
|
6
|
+
import { useCharacterCount } from '../lib/hooks';
|
|
7
|
+
|
|
8
|
+
type Props = {
|
|
9
|
+
children: React.ReactNode;
|
|
10
|
+
type: 'recommended' | 'allowed';
|
|
11
|
+
initialChars: number;
|
|
12
|
+
maxChars: number;
|
|
13
|
+
translations: IGenericTranslation['generics']['forms'];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const AUCharacterCountComponent: FC<Props> = ({
|
|
17
|
+
children,
|
|
18
|
+
type,
|
|
19
|
+
initialChars,
|
|
20
|
+
maxChars,
|
|
21
|
+
translations,
|
|
22
|
+
}) => {
|
|
23
|
+
const {
|
|
24
|
+
ref, remaining, overflow,
|
|
25
|
+
} = useCharacterCount(type, initialChars, maxChars);
|
|
26
|
+
|
|
27
|
+
const { character_count } = translations;
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<div
|
|
31
|
+
ref={ref}
|
|
32
|
+
className="form__field__character-count"
|
|
33
|
+
>
|
|
34
|
+
{children}
|
|
35
|
+
<div
|
|
36
|
+
className="form__field__character-count__info"
|
|
37
|
+
aria-live="polite"
|
|
38
|
+
>
|
|
39
|
+
<span className="form__field__character-count__info__overflow-message">
|
|
40
|
+
{`${replace(character_count.overflow.message[type], ['maxchars'], [maxChars.toString()])}`}
|
|
41
|
+
</span>
|
|
42
|
+
{' '}
|
|
43
|
+
<span className="form__field__character-count__info__overflow-count">
|
|
44
|
+
{`${replace(character_count.overflow.count[type][overflow === 1 ? 'one' : 'other'], ['overflow'], [overflow.toString()])}`}
|
|
45
|
+
</span>
|
|
46
|
+
{' '}
|
|
47
|
+
<span className="form__field__character-count__info__count-down">
|
|
48
|
+
{`${replace(character_count.count_down[remaining === 1 ? 'one' : 'other'], ['remaining'], [remaining.toString()])}`}
|
|
49
|
+
</span>
|
|
50
|
+
</div>
|
|
51
|
+
</div>
|
|
52
|
+
);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
AUCharacterCountComponent.displayName = 'AUCharacterCountComponent';
|
|
56
|
+
export default AUCharacterCountComponent;
|
|
@@ -1,195 +1,195 @@
|
|
|
1
|
-
/* eslint-disable max-len */
|
|
2
|
-
/* eslint-disable jsx-a11y/role-has-required-aria-props */
|
|
3
|
-
/* eslint-disable jsx-a11y/control-has-associated-label */
|
|
4
|
-
/* eslint-disable jsx-a11y/label-has-associated-control */
|
|
5
|
-
import React, {
|
|
6
|
-
FC, useState, useEffect, useRef,
|
|
7
|
-
} from 'react';
|
|
8
|
-
import { replace } from '../lib/helpers';
|
|
9
|
-
|
|
10
|
-
const setResults = (value: string, data: IComboBoxSearch[], setFiltered: (values: IComboBoxSearch[]) => void) => {
|
|
11
|
-
const filtered = data.filter((item) => item.name.toLowerCase().includes(value.toLowerCase()));
|
|
12
|
-
setFiltered(filtered);
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const isElementWithIdInEventPath = (event: MouseEvent, id: string): boolean => {
|
|
16
|
-
let found = false;
|
|
17
|
-
const path = event.composedPath();
|
|
18
|
-
path.forEach((p: EventTarget): void => {
|
|
19
|
-
if ((p as HTMLElement).parentElement != null && (p as HTMLElement).getAttribute('id') === id) {
|
|
20
|
-
found = true;
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
return found;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
type Props = {
|
|
27
|
-
inline: boolean;
|
|
28
|
-
data: IComboBoxSearch[];
|
|
29
|
-
label: string;
|
|
30
|
-
resultsText: {
|
|
31
|
-
one: string;
|
|
32
|
-
other: string;
|
|
33
|
-
};
|
|
34
|
-
noResultsText: string;
|
|
35
|
-
noResultsTextSecondary: string;
|
|
36
|
-
clearOnSelect?: boolean;
|
|
37
|
-
onItemSelect: (item: IComboBoxSearch) => void;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const AUComboBoxComponent: FC<Props> = ({
|
|
41
|
-
inline,
|
|
42
|
-
data,
|
|
43
|
-
label,
|
|
44
|
-
resultsText,
|
|
45
|
-
noResultsText,
|
|
46
|
-
noResultsTextSecondary,
|
|
47
|
-
clearOnSelect = false,
|
|
48
|
-
onItemSelect,
|
|
49
|
-
}) => {
|
|
50
|
-
const comboBoxRef = useRef<HTMLDivElement>(null);
|
|
51
|
-
const [filteredData, setFilteredData] = useState<IComboBoxSearch[]>([]);
|
|
52
|
-
const [resultIndex, setResultIndex] = useState<number>(-1);
|
|
53
|
-
const [selected, setSelected] = useState<boolean>(false);
|
|
54
|
-
const [selectedItem, setSelectedItem] = useState<string>('');
|
|
55
|
-
|
|
56
|
-
const id = label.split(' ').join('-').toLocaleLowerCase();
|
|
57
|
-
|
|
58
|
-
useEffect(() => {
|
|
59
|
-
document.addEventListener('click', (e) => {
|
|
60
|
-
if (comboBoxRef.current && !isElementWithIdInEventPath(e, `combo-box-${id}`)) {
|
|
61
|
-
setFilteredData([]);
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
}, []);
|
|
65
|
-
|
|
66
|
-
useEffect(() => {
|
|
67
|
-
const liItems = comboBoxRef.current?.querySelectorAll('li');
|
|
68
|
-
if (liItems && liItems.length > 0) {
|
|
69
|
-
liItems[resultIndex - 1]?.focus();
|
|
70
|
-
}
|
|
71
|
-
if (resultIndex === 0) {
|
|
72
|
-
comboBoxRef.current?.querySelector('input')?.focus();
|
|
73
|
-
}
|
|
74
|
-
}, [resultIndex]);
|
|
75
|
-
|
|
76
|
-
return (
|
|
77
|
-
<div className={`form__field${inline ? ' form__field--inline-label' : ''}`} ref={comboBoxRef}>
|
|
78
|
-
<label htmlFor={id}>{label}</label>
|
|
79
|
-
<select className="form__field__select--combo-box" id={id} aria-hidden hidden>
|
|
80
|
-
{data.map((item) => (
|
|
81
|
-
<option key={item.id} value={item.name}>
|
|
82
|
-
{item.name}
|
|
83
|
-
</option>
|
|
84
|
-
))}
|
|
85
|
-
</select>
|
|
86
|
-
<div className="form__field__combo-box" id={`combo-box-${id}`}>
|
|
87
|
-
<input
|
|
88
|
-
className="form__field__combo-box__field"
|
|
89
|
-
autoCapitalize="none"
|
|
90
|
-
type="text"
|
|
91
|
-
autoComplete="off"
|
|
92
|
-
aria-owns={`combo-box-options--${id}`}
|
|
93
|
-
aria-autocomplete="list"
|
|
94
|
-
role="combobox"
|
|
95
|
-
id={id}
|
|
96
|
-
aria-expanded="false"
|
|
97
|
-
value={selectedItem}
|
|
98
|
-
onChange={(e) => {
|
|
99
|
-
setSelectedItem(e.target.value);
|
|
100
|
-
setSelected(false);
|
|
101
|
-
setResults(e.target.value, data, setFilteredData);
|
|
102
|
-
}}
|
|
103
|
-
onFocus={(e) => {
|
|
104
|
-
if (!selected) {
|
|
105
|
-
const { value } = e.target;
|
|
106
|
-
if (!value) {
|
|
107
|
-
setFilteredData(data);
|
|
108
|
-
} else {
|
|
109
|
-
setResults(value, data, setFilteredData);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}}
|
|
113
|
-
onKeyDown={(e) => {
|
|
114
|
-
if (e.key === 'ArrowDown') {
|
|
115
|
-
e.preventDefault();
|
|
116
|
-
setResultIndex(1);
|
|
117
|
-
}
|
|
118
|
-
if (e.key === 'ArrowUp') {
|
|
119
|
-
e.preventDefault();
|
|
120
|
-
setResultIndex(0);
|
|
121
|
-
}
|
|
122
|
-
if (e.key === 'Tab') {
|
|
123
|
-
setFilteredData([]);
|
|
124
|
-
setResultIndex(0);
|
|
125
|
-
}
|
|
126
|
-
if (e.key === 'Enter') {
|
|
127
|
-
e.preventDefault();
|
|
128
|
-
}
|
|
129
|
-
}}
|
|
130
|
-
/>
|
|
131
|
-
{
|
|
132
|
-
!selected && (
|
|
133
|
-
<ul
|
|
134
|
-
id={`combo-box-options--${id}`}
|
|
135
|
-
role="listbox"
|
|
136
|
-
className="form__field__combo-box__options"
|
|
137
|
-
>
|
|
138
|
-
{filteredData.map((item, index) => (
|
|
139
|
-
<li
|
|
140
|
-
key={item.id}
|
|
141
|
-
tabIndex={-1}
|
|
142
|
-
aria-selected={index === resultIndex - 1}
|
|
143
|
-
role="option"
|
|
144
|
-
className="form__field__combo-box__option"
|
|
145
|
-
onClick={() => {
|
|
146
|
-
setSelectedItem(clearOnSelect ? '' : item.name);
|
|
147
|
-
setSelected(true);
|
|
148
|
-
setFilteredData([]);
|
|
149
|
-
setResultIndex(0);
|
|
150
|
-
onItemSelect(item);
|
|
151
|
-
}}
|
|
152
|
-
onKeyDown={(e) => {
|
|
153
|
-
if (e.key === 'ArrowDown') {
|
|
154
|
-
e.preventDefault();
|
|
155
|
-
setResultIndex(resultIndex + 1);
|
|
156
|
-
}
|
|
157
|
-
if (e.key === 'ArrowUp') {
|
|
158
|
-
e.preventDefault();
|
|
159
|
-
setResultIndex(resultIndex - 1);
|
|
160
|
-
}
|
|
161
|
-
}}
|
|
162
|
-
onKeyUp={(e) => {
|
|
163
|
-
if (e.key === 'Enter') {
|
|
164
|
-
setSelectedItem(clearOnSelect ? '' : item.name);
|
|
165
|
-
setSelected(true);
|
|
166
|
-
setFilteredData([]);
|
|
167
|
-
setResultIndex(0);
|
|
168
|
-
onItemSelect(item);
|
|
169
|
-
}
|
|
170
|
-
}}
|
|
171
|
-
>
|
|
172
|
-
{item.name}
|
|
173
|
-
</li>
|
|
174
|
-
))}
|
|
175
|
-
{(filteredData.length === 0 && selectedItem) && (
|
|
176
|
-
<li className="form__field__combo-box__option form__field__combo-box__option--no-results">
|
|
177
|
-
<div className="empty-state empty-state--inline">
|
|
178
|
-
<div className="empty-state__text">{noResultsText}</div>
|
|
179
|
-
<div className="empty-state__text-secondary">{noResultsTextSecondary}</div>
|
|
180
|
-
</div>
|
|
181
|
-
</li>
|
|
182
|
-
)}
|
|
183
|
-
</ul>
|
|
184
|
-
)
|
|
185
|
-
}
|
|
186
|
-
<div aria-live="polite" role="status" className="screenreader-only">
|
|
187
|
-
{replace(resultsText[filteredData.length === 1 ? 'one' : 'other'], ['dataLength'], [`${filteredData.length}`])}
|
|
188
|
-
</div>
|
|
189
|
-
</div>
|
|
190
|
-
</div>
|
|
191
|
-
);
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
AUComboBoxComponent.displayName = 'AUComboBoxComponent';
|
|
195
|
-
export default AUComboBoxComponent;
|
|
1
|
+
/* eslint-disable max-len */
|
|
2
|
+
/* eslint-disable jsx-a11y/role-has-required-aria-props */
|
|
3
|
+
/* eslint-disable jsx-a11y/control-has-associated-label */
|
|
4
|
+
/* eslint-disable jsx-a11y/label-has-associated-control */
|
|
5
|
+
import React, {
|
|
6
|
+
FC, useState, useEffect, useRef,
|
|
7
|
+
} from 'react';
|
|
8
|
+
import { replace } from '../lib/helpers';
|
|
9
|
+
|
|
10
|
+
const setResults = (value: string, data: IComboBoxSearch[], setFiltered: (values: IComboBoxSearch[]) => void) => {
|
|
11
|
+
const filtered = data.filter((item) => item.name.toLowerCase().includes(value.toLowerCase()));
|
|
12
|
+
setFiltered(filtered);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const isElementWithIdInEventPath = (event: MouseEvent, id: string): boolean => {
|
|
16
|
+
let found = false;
|
|
17
|
+
const path = event.composedPath();
|
|
18
|
+
path.forEach((p: EventTarget): void => {
|
|
19
|
+
if ((p as HTMLElement).parentElement != null && (p as HTMLElement).getAttribute('id') === id) {
|
|
20
|
+
found = true;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
return found;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type Props = {
|
|
27
|
+
inline: boolean;
|
|
28
|
+
data: IComboBoxSearch[];
|
|
29
|
+
label: string;
|
|
30
|
+
resultsText: {
|
|
31
|
+
one: string;
|
|
32
|
+
other: string;
|
|
33
|
+
};
|
|
34
|
+
noResultsText: string;
|
|
35
|
+
noResultsTextSecondary: string;
|
|
36
|
+
clearOnSelect?: boolean;
|
|
37
|
+
onItemSelect: (item: IComboBoxSearch) => void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const AUComboBoxComponent: FC<Props> = ({
|
|
41
|
+
inline,
|
|
42
|
+
data,
|
|
43
|
+
label,
|
|
44
|
+
resultsText,
|
|
45
|
+
noResultsText,
|
|
46
|
+
noResultsTextSecondary,
|
|
47
|
+
clearOnSelect = false,
|
|
48
|
+
onItemSelect,
|
|
49
|
+
}) => {
|
|
50
|
+
const comboBoxRef = useRef<HTMLDivElement>(null);
|
|
51
|
+
const [filteredData, setFilteredData] = useState<IComboBoxSearch[]>([]);
|
|
52
|
+
const [resultIndex, setResultIndex] = useState<number>(-1);
|
|
53
|
+
const [selected, setSelected] = useState<boolean>(false);
|
|
54
|
+
const [selectedItem, setSelectedItem] = useState<string>('');
|
|
55
|
+
|
|
56
|
+
const id = label.split(' ').join('-').toLocaleLowerCase();
|
|
57
|
+
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
document.addEventListener('click', (e) => {
|
|
60
|
+
if (comboBoxRef.current && !isElementWithIdInEventPath(e, `combo-box-${id}`)) {
|
|
61
|
+
setFilteredData([]);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}, []);
|
|
65
|
+
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
const liItems = comboBoxRef.current?.querySelectorAll('li');
|
|
68
|
+
if (liItems && liItems.length > 0) {
|
|
69
|
+
liItems[resultIndex - 1]?.focus();
|
|
70
|
+
}
|
|
71
|
+
if (resultIndex === 0) {
|
|
72
|
+
comboBoxRef.current?.querySelector('input')?.focus();
|
|
73
|
+
}
|
|
74
|
+
}, [resultIndex]);
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<div className={`form__field${inline ? ' form__field--inline-label' : ''}`} ref={comboBoxRef}>
|
|
78
|
+
<label htmlFor={id}>{label}</label>
|
|
79
|
+
<select className="form__field__select--combo-box" id={id} aria-hidden hidden>
|
|
80
|
+
{data.map((item) => (
|
|
81
|
+
<option key={item.id} value={item.name}>
|
|
82
|
+
{item.name}
|
|
83
|
+
</option>
|
|
84
|
+
))}
|
|
85
|
+
</select>
|
|
86
|
+
<div className="form__field__combo-box" id={`combo-box-${id}`}>
|
|
87
|
+
<input
|
|
88
|
+
className="form__field__combo-box__field"
|
|
89
|
+
autoCapitalize="none"
|
|
90
|
+
type="text"
|
|
91
|
+
autoComplete="off"
|
|
92
|
+
aria-owns={`combo-box-options--${id}`}
|
|
93
|
+
aria-autocomplete="list"
|
|
94
|
+
role="combobox"
|
|
95
|
+
id={id}
|
|
96
|
+
aria-expanded="false"
|
|
97
|
+
value={selectedItem}
|
|
98
|
+
onChange={(e) => {
|
|
99
|
+
setSelectedItem(e.target.value);
|
|
100
|
+
setSelected(false);
|
|
101
|
+
setResults(e.target.value, data, setFilteredData);
|
|
102
|
+
}}
|
|
103
|
+
onFocus={(e) => {
|
|
104
|
+
if (!selected) {
|
|
105
|
+
const { value } = e.target;
|
|
106
|
+
if (!value) {
|
|
107
|
+
setFilteredData(data);
|
|
108
|
+
} else {
|
|
109
|
+
setResults(value, data, setFilteredData);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}}
|
|
113
|
+
onKeyDown={(e) => {
|
|
114
|
+
if (e.key === 'ArrowDown') {
|
|
115
|
+
e.preventDefault();
|
|
116
|
+
setResultIndex(1);
|
|
117
|
+
}
|
|
118
|
+
if (e.key === 'ArrowUp') {
|
|
119
|
+
e.preventDefault();
|
|
120
|
+
setResultIndex(0);
|
|
121
|
+
}
|
|
122
|
+
if (e.key === 'Tab') {
|
|
123
|
+
setFilteredData([]);
|
|
124
|
+
setResultIndex(0);
|
|
125
|
+
}
|
|
126
|
+
if (e.key === 'Enter') {
|
|
127
|
+
e.preventDefault();
|
|
128
|
+
}
|
|
129
|
+
}}
|
|
130
|
+
/>
|
|
131
|
+
{
|
|
132
|
+
!selected && (
|
|
133
|
+
<ul
|
|
134
|
+
id={`combo-box-options--${id}`}
|
|
135
|
+
role="listbox"
|
|
136
|
+
className="form__field__combo-box__options"
|
|
137
|
+
>
|
|
138
|
+
{filteredData.map((item, index) => (
|
|
139
|
+
<li
|
|
140
|
+
key={item.id}
|
|
141
|
+
tabIndex={-1}
|
|
142
|
+
aria-selected={index === resultIndex - 1}
|
|
143
|
+
role="option"
|
|
144
|
+
className="form__field__combo-box__option"
|
|
145
|
+
onClick={() => {
|
|
146
|
+
setSelectedItem(clearOnSelect ? '' : item.name);
|
|
147
|
+
setSelected(true);
|
|
148
|
+
setFilteredData([]);
|
|
149
|
+
setResultIndex(0);
|
|
150
|
+
onItemSelect(item);
|
|
151
|
+
}}
|
|
152
|
+
onKeyDown={(e) => {
|
|
153
|
+
if (e.key === 'ArrowDown') {
|
|
154
|
+
e.preventDefault();
|
|
155
|
+
setResultIndex(resultIndex + 1);
|
|
156
|
+
}
|
|
157
|
+
if (e.key === 'ArrowUp') {
|
|
158
|
+
e.preventDefault();
|
|
159
|
+
setResultIndex(resultIndex - 1);
|
|
160
|
+
}
|
|
161
|
+
}}
|
|
162
|
+
onKeyUp={(e) => {
|
|
163
|
+
if (e.key === 'Enter') {
|
|
164
|
+
setSelectedItem(clearOnSelect ? '' : item.name);
|
|
165
|
+
setSelected(true);
|
|
166
|
+
setFilteredData([]);
|
|
167
|
+
setResultIndex(0);
|
|
168
|
+
onItemSelect(item);
|
|
169
|
+
}
|
|
170
|
+
}}
|
|
171
|
+
>
|
|
172
|
+
{item.name}
|
|
173
|
+
</li>
|
|
174
|
+
))}
|
|
175
|
+
{(filteredData.length === 0 && selectedItem) && (
|
|
176
|
+
<li className="form__field__combo-box__option form__field__combo-box__option--no-results">
|
|
177
|
+
<div className="empty-state empty-state--inline">
|
|
178
|
+
<div className="empty-state__text">{noResultsText}</div>
|
|
179
|
+
<div className="empty-state__text-secondary">{noResultsTextSecondary}</div>
|
|
180
|
+
</div>
|
|
181
|
+
</li>
|
|
182
|
+
)}
|
|
183
|
+
</ul>
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
<div aria-live="polite" role="status" className="screenreader-only">
|
|
187
|
+
{replace(resultsText[filteredData.length === 1 ? 'one' : 'other'], ['dataLength'], [`${filteredData.length}`])}
|
|
188
|
+
</div>
|
|
189
|
+
</div>
|
|
190
|
+
</div>
|
|
191
|
+
);
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
AUComboBoxComponent.displayName = 'AUComboBoxComponent';
|
|
195
|
+
export default AUComboBoxComponent;
|
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
useEffect,
|
|
3
|
-
useRef,
|
|
4
|
-
FC,
|
|
5
|
-
} from 'react';
|
|
6
|
-
import { setContentToggle } from '@aarhus-university/au-designsystem-delphinus/source/js/components/content-toggle';
|
|
7
|
-
|
|
8
|
-
type Props = {
|
|
9
|
-
toggled: boolean;
|
|
10
|
-
classNames?: string;
|
|
11
|
-
children: JSX.Element;
|
|
12
|
-
onClick?: ((content: HTMLElement, button: HTMLButtonElement) => void);
|
|
13
|
-
beforeCallback?: ((content: HTMLElement, button: HTMLButtonElement) => void);
|
|
14
|
-
afterCallback?: ((content: HTMLElement, button: HTMLButtonElement) => void);
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const AUContentToggleComponent: FC<Props> = ({
|
|
18
|
-
toggled,
|
|
19
|
-
classNames = undefined,
|
|
20
|
-
children,
|
|
21
|
-
onClick = undefined,
|
|
22
|
-
beforeCallback = undefined,
|
|
23
|
-
afterCallback = undefined,
|
|
24
|
-
}) => {
|
|
25
|
-
const toggleContainer = useRef<HTMLDivElement>(null);
|
|
26
|
-
useEffect(() => {
|
|
27
|
-
const toggle = toggleContainer?.current?.querySelector('.content-toggle__content');
|
|
28
|
-
let cleanUp: CleanUpPair[];
|
|
29
|
-
if (toggle) {
|
|
30
|
-
cleanUp = setContentToggle(toggle, onClick, beforeCallback, afterCallback);
|
|
31
|
-
if (cleanUp.length > 0) { // Den første er altid knappen (og ikke luk-knappen)
|
|
32
|
-
cleanUp[0].element.setAttribute('aria-expanded', toggled ? 'true' : 'false');
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return () => {
|
|
37
|
-
cleanUp.forEach((c) => {
|
|
38
|
-
c.element.removeEventListener('click', c.clickEvent);
|
|
39
|
-
});
|
|
40
|
-
};
|
|
41
|
-
}, []);
|
|
42
|
-
return (
|
|
43
|
-
<div ref={toggleContainer} className={classNames}>
|
|
44
|
-
{children}
|
|
45
|
-
</div>
|
|
46
|
-
);
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
AUContentToggleComponent.displayName = 'AUContentToggleComponent';
|
|
50
|
-
export default AUContentToggleComponent;
|
|
1
|
+
import React, {
|
|
2
|
+
useEffect,
|
|
3
|
+
useRef,
|
|
4
|
+
FC,
|
|
5
|
+
} from 'react';
|
|
6
|
+
import { setContentToggle } from '@aarhus-university/au-designsystem-delphinus/source/js/components/content-toggle';
|
|
7
|
+
|
|
8
|
+
type Props = {
|
|
9
|
+
toggled: boolean;
|
|
10
|
+
classNames?: string;
|
|
11
|
+
children: JSX.Element;
|
|
12
|
+
onClick?: ((content: HTMLElement, button: HTMLButtonElement) => void);
|
|
13
|
+
beforeCallback?: ((content: HTMLElement, button: HTMLButtonElement) => void);
|
|
14
|
+
afterCallback?: ((content: HTMLElement, button: HTMLButtonElement) => void);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const AUContentToggleComponent: FC<Props> = ({
|
|
18
|
+
toggled,
|
|
19
|
+
classNames = undefined,
|
|
20
|
+
children,
|
|
21
|
+
onClick = undefined,
|
|
22
|
+
beforeCallback = undefined,
|
|
23
|
+
afterCallback = undefined,
|
|
24
|
+
}) => {
|
|
25
|
+
const toggleContainer = useRef<HTMLDivElement>(null);
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const toggle = toggleContainer?.current?.querySelector('.content-toggle__content');
|
|
28
|
+
let cleanUp: CleanUpPair[];
|
|
29
|
+
if (toggle) {
|
|
30
|
+
cleanUp = setContentToggle(toggle, onClick, beforeCallback, afterCallback);
|
|
31
|
+
if (cleanUp.length > 0) { // Den første er altid knappen (og ikke luk-knappen)
|
|
32
|
+
cleanUp[0].element.setAttribute('aria-expanded', toggled ? 'true' : 'false');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return () => {
|
|
37
|
+
cleanUp.forEach((c) => {
|
|
38
|
+
c.element.removeEventListener('click', c.clickEvent);
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
}, []);
|
|
42
|
+
return (
|
|
43
|
+
<div ref={toggleContainer} className={classNames}>
|
|
44
|
+
{children}
|
|
45
|
+
</div>
|
|
46
|
+
);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
AUContentToggleComponent.displayName = 'AUContentToggleComponent';
|
|
50
|
+
export default AUContentToggleComponent;
|