@developer-overheid-nl/don-register-components 1.2.0 → 1.3.1

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 (89) hide show
  1. package/CHANGELOG.md +130 -85
  2. package/LICENSE.md +291 -291
  3. package/README.md +20 -20
  4. package/package.json +5 -7
  5. package/src/alert/Alert.tsx +19 -19
  6. package/src/alert/styles.module.css +3 -3
  7. package/src/alignBox/AlignBox.tsx +114 -114
  8. package/src/alignBox/styles.module.css +110 -110
  9. package/src/amountLabel/AmountLabel.tsx +16 -16
  10. package/src/amountLabel/styles.module.css +11 -11
  11. package/src/analytics/PiwikPro.tsx +94 -94
  12. package/src/analytics/index.ts +1 -1
  13. package/src/article/Article.tsx +10 -10
  14. package/src/badgeList/BadgeList.tsx +34 -26
  15. package/src/badgeList/styles.module.css +37 -24
  16. package/src/block/Block.tsx +35 -35
  17. package/src/block/styles.module.css +29 -29
  18. package/src/button/Button.tsx +3 -3
  19. package/src/cardAsLink/CardAsLink.tsx +21 -21
  20. package/src/cardAsLink/styles.module.css +23 -23
  21. package/src/cardsList/CardsList.tsx +56 -54
  22. package/src/cardsList/styles.module.css +19 -19
  23. package/src/client.ts +7 -7
  24. package/src/container/Container.tsx +10 -10
  25. package/src/container/styles.module.css +28 -28
  26. package/src/copyButton/CopyButton.tsx +39 -39
  27. package/src/copyButton/styles.module.css +13 -13
  28. package/src/dataBadgeLink/DataBadgeLink.tsx +53 -53
  29. package/src/dataBadgeLink/styles.module.css +89 -69
  30. package/src/dataSummary/DataSummary.tsx +22 -22
  31. package/src/dataSummary/DataSummaryItem.tsx +21 -21
  32. package/src/dataSummary/styles.module.css +75 -75
  33. package/src/facetFilters/FacetFilters.tsx +419 -313
  34. package/src/facetFilters/styles.module.css +3 -3
  35. package/src/fieldset/FieldSet.tsx +3 -3
  36. package/src/filters/Filters.tsx +116 -115
  37. package/src/filters/styles.module.css +26 -26
  38. package/src/footer/Footer.tsx +76 -76
  39. package/src/footer/styles.module.css +103 -103
  40. package/src/formFieldCheckboxGroup/FormFieldCheckboxGroup.tsx +16 -16
  41. package/src/formFieldCheckboxGroup/styles.module.css +5 -5
  42. package/src/formFieldCheckboxOption/FormFieldCheckboxOption.tsx +26 -26
  43. package/src/formFieldCheckboxOption/styles.module.css +22 -22
  44. package/src/formFieldLabel/FormFieldLabel.tsx +23 -23
  45. package/src/formFieldRadioGroup/FormFieldRadioGroup.tsx +13 -13
  46. package/src/formFieldRadioGroup/styles.module.css +5 -5
  47. package/src/formFieldRadioOption/FormFieldRadioOption.tsx +75 -75
  48. package/src/formFieldRadioOption/styles.module.css +49 -49
  49. package/src/formFieldSwitch/FormFieldSwitch.tsx +79 -79
  50. package/src/formFieldSwitch/styles.module.css +32 -32
  51. package/src/formFieldTextInput/FormFieldTextInput.tsx +3 -3
  52. package/src/header/Header.tsx +137 -137
  53. package/src/header/styles.module.css +71 -71
  54. package/src/heading/Heading.tsx +15 -15
  55. package/src/headingGroup/HeadingGroup.tsx +48 -48
  56. package/src/headingGroup/styles.module.css +3 -3
  57. package/src/i18n.ts +25 -25
  58. package/src/iconBadge/IconBadge.tsx +35 -35
  59. package/src/iconBadge/getAppearance.ts +53 -53
  60. package/src/iconBadge/styles.module.css +19 -19
  61. package/src/iconsSprite/Icon.tsx +17 -17
  62. package/src/iconsSprite/IconsSprite.tsx +5 -5
  63. package/src/index.ts +107 -106
  64. package/src/infoButton/InfoButton.tsx +45 -45
  65. package/src/infoButton/styles.module.css +43 -43
  66. package/src/link/Link.tsx +10 -10
  67. package/src/markdown/Markdown.tsx +42 -42
  68. package/src/overlay/Overlay.tsx +18 -18
  69. package/src/overlay/styles.module.css +15 -15
  70. package/src/pagination/Pagination.tsx +267 -144
  71. package/src/pagination/styles.module.css +28 -13
  72. package/src/paragraph/Paragraph.tsx +24 -24
  73. package/src/paragraph/styles.module.css +6 -6
  74. package/src/pillBadge/PillBadge.examples.tsx +107 -107
  75. package/src/pillBadge/PillBadge.tsx +51 -51
  76. package/src/pillBadge/styles.module.css +214 -214
  77. package/src/readOnlyTextInput/ReadOnlyTextInput.tsx +24 -24
  78. package/src/readOnlyTextInput/styles.module.css +19 -19
  79. package/src/scoreBadge/ScoreBadge.tsx +133 -133
  80. package/src/search/Search.tsx +82 -66
  81. package/src/search/styles.module.css +39 -39
  82. package/src/siteLogo/SiteLogo.tsx +24 -24
  83. package/src/switch/Switch.tsx +86 -86
  84. package/src/switch/styles.module.css +7 -7
  85. package/src/toolTip/ToolTip.tsx +55 -55
  86. package/src/toolTip/styles.module.css +49 -48
  87. package/src/topNavigation/TopNavigation.tsx +67 -67
  88. package/src/topNavigation/styles.module.css +54 -54
  89. package/tsconfig.json +121 -121
@@ -1,313 +1,419 @@
1
- "use client";
2
-
3
- import clsx from "clsx";
4
- import type { ChangeEvent } from "react";
5
- import { type HTMLProps, type PropsWithChildren, useRef } from "react";
6
- import { I18nextProvider, useTranslation } from "react-i18next";
7
- import FormFieldCheckboxGroup from "../formFieldCheckboxGroup/FormFieldCheckboxGroup";
8
- import FormFieldCheckboxOption from "../formFieldCheckboxOption/FormFieldCheckboxOption";
9
- import FormFieldRadioGroup from "../formFieldRadioGroup/FormFieldRadioGroup";
10
- import FormFieldRadioOption from "../formFieldRadioOption/FormFieldRadioOption";
11
- import FormFieldSwitch from "../formFieldSwitch/FormFieldSwitch";
12
- import Heading, {
13
- clampHeadingLevel,
14
- type HeadingProps,
15
- } from "../heading/Heading";
16
- import i18n from "../i18n";
17
- import Paragraph from "../paragraph/Paragraph";
18
- import ToolTip from "../toolTip/ToolTip";
19
- import styles from "./styles.module.css";
20
-
21
- enum FilterType {
22
- Toggle = "toggle",
23
- Date = "date",
24
- Multi = "multi-select",
25
- Single = "single-select",
26
- }
27
-
28
- export interface SelectOption {
29
- value: string;
30
- label: string;
31
- description?: string | null;
32
- count: number;
33
- selected: boolean;
34
- }
35
-
36
- interface BaseFilterItems {
37
- key: string;
38
- label: string;
39
- description: string;
40
- }
41
-
42
- interface ToggleFilterData extends BaseFilterItems {
43
- type: FilterType.Toggle;
44
- value?: boolean;
45
- count?: number;
46
- }
47
-
48
- interface DateFilterData extends BaseFilterItems {
49
- type: FilterType.Date;
50
- value?: string;
51
- count?: number;
52
- }
53
-
54
- interface MultiSelectFilterData extends BaseFilterItems {
55
- type: FilterType.Multi;
56
- options: SelectOption[];
57
- }
58
-
59
- interface SingleSelectFilterData extends BaseFilterItems {
60
- type: FilterType.Single;
61
- options: SelectOption[];
62
- }
63
-
64
- export type FilterData =
65
- | ToggleFilterData
66
- | DateFilterData
67
- | MultiSelectFilterData
68
- | SingleSelectFilterData;
69
-
70
- type SelectedFilters = Record<string, Array<string | [string, string]>>;
71
-
72
- export interface FacetFiltersProps extends HTMLProps<HTMLDivElement> {
73
- title?: string;
74
- startHeadingLevel?: HeadingProps["level"];
75
- filters: FilterData[] | null | undefined;
76
- /**
77
- * Callback function called when filter option has changed.
78
- * @param selectedFilters current selected filters at the moment of change
79
- * @returns
80
- */
81
- onFilterChange?: (selectedFilters: SelectedFilters) => void;
82
- /**
83
- * Types of filters for which the onFilterChange callback should be debounced. Default: ["date", "single-select"]
84
- */
85
- debounceTypes?: Array<FilterType>;
86
- /**
87
- * Delay for debouncing the onFilterChange callback, in milliseconds. Default: 600
88
- */
89
- debounceDelay?: number;
90
- }
91
-
92
- export const getSelectedFilters = (
93
- filters: FilterData[] | null | undefined,
94
- withLabels = false,
95
- ): SelectedFilters => {
96
- return (
97
- filters?.reduce((acc, filter) => {
98
- if (filter.type === FilterType.Multi) {
99
- const keys: (string | [string, string])[] = filter.options
100
- ?.filter((option) => option.selected)
101
- .map((option) =>
102
- withLabels ? [option.value, option.label] : option.value,
103
- );
104
- if (keys?.length > 0) {
105
- acc[filter.key] = keys;
106
- }
107
- } else if (filter.type === FilterType.Toggle && filter.value) {
108
- acc[filter.key] = withLabels ? [["true", filter.label]] : ["true"];
109
- } else if (filter.type === FilterType.Date && filter.value) {
110
- acc[filter.key] = withLabels
111
- ? [[filter.value, filter.label]]
112
- : [filter.value];
113
- } else if (filter.type === FilterType.Single) {
114
- const selectedOption = filter.options?.find(
115
- (option) => option.selected,
116
- );
117
- if (selectedOption) {
118
- acc[filter.key] = withLabels
119
- ? [[selectedOption.value, selectedOption.label]]
120
- : [selectedOption.value];
121
- }
122
- }
123
- return acc;
124
- }, {} as SelectedFilters) ?? {}
125
- );
126
- };
127
-
128
- const updateFilters = (
129
- selectedFilters: SelectedFilters,
130
- filterKey: string,
131
- filterValue: string,
132
- checked: boolean,
133
- ) => {
134
- const newSelectedFilters = {
135
- ...selectedFilters,
136
- [filterKey]: checked
137
- ? [...(selectedFilters[filterKey] || []), filterValue]
138
- : (selectedFilters[filterKey] || []).filter(
139
- (value) => value !== filterValue,
140
- ),
141
- };
142
- return newSelectedFilters;
143
- };
144
-
145
- const debounce = <T extends unknown[]>(
146
- fn: (...args: T) => void,
147
- delay: number,
148
- ) => {
149
- let timeoutId: ReturnType<typeof setTimeout> | null = null;
150
- return (...args: T) => {
151
- if (timeoutId) {
152
- clearTimeout(timeoutId);
153
- }
154
- timeoutId = setTimeout(() => {
155
- fn(...args);
156
- }, delay);
157
- };
158
- };
159
-
160
- const FacetFilters = (props: PropsWithChildren<FacetFiltersProps>) => {
161
- const { t } = useTranslation();
162
- const thisRef = useRef<HTMLDivElement>(null);
163
- const {
164
- title,
165
- startHeadingLevel = 2,
166
- filters,
167
- onFilterChange = () => {},
168
- debounceTypes = [FilterType.Date, FilterType.Single],
169
- debounceDelay = 600,
170
- ...restProps
171
- } = props;
172
-
173
- const selectedFilters = getSelectedFilters(filters);
174
-
175
- // Callback function (for React) and CustomEvent (for non-React) to notify about filter changes
176
- const emitChange = (newSelectedFilters: SelectedFilters) => {
177
- onFilterChange(newSelectedFilters);
178
- const customEvent = new CustomEvent("onFilterChange", {
179
- detail: newSelectedFilters,
180
- });
181
- if (thisRef.current) thisRef.current.dispatchEvent(customEvent);
182
- };
183
-
184
- const debouncedEmitChange = useRef(
185
- debounce(emitChange, debounceDelay),
186
- ).current;
187
-
188
- const handleChange = (
189
- event: ChangeEvent<HTMLInputElement>,
190
- type: FilterType,
191
- ) => {
192
- const newSelectedFilters = updateFilters(
193
- selectedFilters,
194
- event.target.name,
195
- event.target.value,
196
- event.target.checked,
197
- );
198
-
199
- if (debounceTypes.includes(type)) {
200
- debouncedEmitChange(newSelectedFilters);
201
- } else {
202
- emitChange(newSelectedFilters);
203
- }
204
- };
205
-
206
- return (
207
- <div
208
- ref={thisRef}
209
- className={clsx([styles.facetFilters, props.className])}
210
- {...restProps}
211
- >
212
- {title ? (
213
- <Heading level={startHeadingLevel} appearanceLevel={3}>
214
- {title}
215
- </Heading>
216
- ) : null}
217
- {/* Temporary filter on `date` types */}
218
- {filters
219
- ?.filter(({ type }) => type !== "date")
220
- .map((facet) => (
221
- <div className={styles.facet} key={facet.key}>
222
- <Heading
223
- level={
224
- title
225
- ? clampHeadingLevel(startHeadingLevel + 1)
226
- : startHeadingLevel
227
- }
228
- appearanceLevel={5}
229
- >
230
- {facet.label}
231
- </Heading>
232
- <Paragraph id={`${facet.key}-label`} purpose="short">
233
- {facet.description}
234
- </Paragraph>
235
- {facet.type === "toggle" ? (
236
- <FormFieldSwitch
237
- label=""
238
- labelledById={`${facet.key}-label`}
239
- name={facet.key}
240
- defaultChecked={facet.value}
241
- value="true"
242
- amount={facet.count || 0}
243
- labels={{
244
- on: t("components.filter-on"),
245
- off: t("components.filter-off"),
246
- }}
247
- onChange={(event) => handleChange(event, facet.type)}
248
- />
249
- ) : null}
250
- {/* TODO: Date selector
251
- {facet.type === "date" ? <div>TODO</div> : null}
252
- */}
253
- {facet.type === "multi-select" ? (
254
- <FormFieldCheckboxGroup>
255
- {facet.options?.map((option) => (
256
- <FormFieldCheckboxOption
257
- key={option.value}
258
- label={option.label}
259
- name={facet.key}
260
- value={option.value}
261
- defaultChecked={option.selected}
262
- amount={option.count}
263
- onChange={(event) => handleChange(event, facet.type)}
264
- >
265
- {option.description ? (
266
- <ToolTip
267
- text={option.description}
268
- aria-label={t("components.info-about", {
269
- subject: option.label,
270
- })}
271
- />
272
- ) : null}
273
- </FormFieldCheckboxOption>
274
- ))}
275
- </FormFieldCheckboxGroup>
276
- ) : null}
277
- {facet.type === "single-select" ? (
278
- <FormFieldRadioGroup>
279
- <FormFieldRadioOption
280
- key="none"
281
- label={t("components.filter-all")}
282
- name={facet.key}
283
- value=""
284
- onChange={(event) => handleChange(event, facet.type)}
285
- />
286
- {facet.options?.map((option) => (
287
- <FormFieldRadioOption
288
- key={option.value}
289
- label={option.label}
290
- name={facet.key}
291
- value={option.value}
292
- defaultChecked={option.selected}
293
- amount={option.count}
294
- onChange={(event) => handleChange(event, facet.type)}
295
- />
296
- ))}
297
- </FormFieldRadioGroup>
298
- ) : null}
299
- </div>
300
- ))}
301
- </div>
302
- );
303
- };
304
-
305
- const TranslatedFacetFilters = (props: FacetFiltersProps) => {
306
- return (
307
- <I18nextProvider i18n={i18n}>
308
- <FacetFilters {...props} />
309
- </I18nextProvider>
310
- );
311
- };
312
-
313
- export default TranslatedFacetFilters;
1
+ "use client";
2
+
3
+ import clsx from "clsx";
4
+ import type { ChangeEvent } from "react";
5
+ import { type HTMLProps, type PropsWithChildren, useRef } from "react";
6
+ import { I18nextProvider, useTranslation } from "react-i18next";
7
+ import FormFieldCheckboxGroup from "../formFieldCheckboxGroup/FormFieldCheckboxGroup";
8
+ import FormFieldCheckboxOption from "../formFieldCheckboxOption/FormFieldCheckboxOption";
9
+ import FormFieldRadioGroup from "../formFieldRadioGroup/FormFieldRadioGroup";
10
+ import FormFieldRadioOption from "../formFieldRadioOption/FormFieldRadioOption";
11
+ import FormFieldSwitch from "../formFieldSwitch/FormFieldSwitch";
12
+ import Heading, {
13
+ clampHeadingLevel,
14
+ type HeadingProps,
15
+ } from "../heading/Heading";
16
+ import i18n from "../i18n";
17
+ import Paragraph from "../paragraph/Paragraph";
18
+ import ToolTip from "../toolTip/ToolTip";
19
+ import styles from "./styles.module.css";
20
+
21
+ enum FilterType {
22
+ Toggle = "toggle",
23
+ Date = "date",
24
+ Multi = "multi-select",
25
+ Single = "single-select",
26
+ }
27
+
28
+ export interface SelectOption {
29
+ value: string;
30
+ label: string;
31
+ description?: string | null;
32
+ count: number;
33
+ selected: boolean;
34
+ }
35
+
36
+ interface BaseFilterItems {
37
+ key: string;
38
+ label: string;
39
+ description: string;
40
+ }
41
+
42
+ interface ToggleFilterData extends BaseFilterItems {
43
+ type: FilterType.Toggle;
44
+ value?: boolean;
45
+ count?: number;
46
+ }
47
+
48
+ interface DateFilterData extends BaseFilterItems {
49
+ type: FilterType.Date;
50
+ value?: string;
51
+ count?: number;
52
+ }
53
+
54
+ interface MultiSelectFilterData extends BaseFilterItems {
55
+ type: FilterType.Multi;
56
+ options: SelectOption[];
57
+ }
58
+
59
+ interface SingleSelectFilterData extends BaseFilterItems {
60
+ type: FilterType.Single;
61
+ options: SelectOption[];
62
+ }
63
+
64
+ export type FilterData =
65
+ | ToggleFilterData
66
+ | DateFilterData
67
+ | MultiSelectFilterData
68
+ | SingleSelectFilterData;
69
+
70
+ type SelectedFilters = Record<string, Array<string | [string, string]>>;
71
+
72
+ type SelectedFiltersMap = Map<
73
+ string | [string, string],
74
+ Array<string | [string, string]>
75
+ >;
76
+
77
+ type ActiveFilters = Record<string, string | string[] | undefined>;
78
+
79
+ export interface FacetFiltersProps extends HTMLProps<HTMLDivElement> {
80
+ title?: string;
81
+ startHeadingLevel?: HeadingProps["level"];
82
+ filters: FilterData[] | null | undefined;
83
+ /**
84
+ * Callback function called when filter option has changed.
85
+ * @param selectedFilters current selected filters at the moment of change
86
+ * @returns
87
+ */
88
+ onFilterChange?: (selectedFilters: SelectedFilters) => void;
89
+ /**
90
+ * Types of filters for which the onFilterChange callback should be debounced. Default: ["date", "single-select"]
91
+ */
92
+ debounceTypes?: Array<FilterType>;
93
+ /**
94
+ * Delay for debouncing the onFilterChange callback, in milliseconds. Default: 600
95
+ */
96
+ debounceDelay?: number;
97
+ }
98
+
99
+ /**
100
+ * @deprecated Use getSelectedFiltersMap, that will return a map with keys as a tuple with key and label
101
+ * @param filters
102
+ * @param withLabels Note: keys have no labels, use the map version
103
+ * @returns object
104
+ */
105
+ export const getSelectedFilters = (
106
+ filters: FilterData[] | null | undefined,
107
+ withLabels = false,
108
+ ): SelectedFilters => {
109
+ return (
110
+ filters?.reduce((acc, filter) => {
111
+ if (filter.type === FilterType.Multi) {
112
+ const keys: (string | [string, string])[] = filter.options
113
+ ?.filter((option) => option.selected)
114
+ .map((option) =>
115
+ withLabels ? [option.value, option.label] : option.value,
116
+ );
117
+ if (keys?.length > 0) {
118
+ acc[filter.key] = keys;
119
+ }
120
+ } else if (filter.type === FilterType.Toggle && filter.value) {
121
+ acc[filter.key] = withLabels ? [["true", filter.label]] : ["true"];
122
+ } else if (filter.type === FilterType.Date && filter.value) {
123
+ acc[filter.key] = withLabels
124
+ ? [[filter.value, filter.label]]
125
+ : [filter.value];
126
+ } else if (filter.type === FilterType.Single) {
127
+ const selectedOption = filter.options?.find(
128
+ (option) => option.selected,
129
+ );
130
+ if (selectedOption) {
131
+ acc[filter.key] = withLabels
132
+ ? [[selectedOption.value, selectedOption.label]]
133
+ : [selectedOption.value];
134
+ }
135
+ }
136
+ return acc;
137
+ }, {} as SelectedFilters) ?? {}
138
+ );
139
+ };
140
+
141
+ export const getSelectedFiltersMap = (
142
+ filters: FilterData[] | null | undefined,
143
+ withLabels = false,
144
+ activeFilters: ActiveFilters = {},
145
+ ): SelectedFiltersMap => {
146
+ return (
147
+ filters?.reduce((acc, filter) => {
148
+ if (filter.type === FilterType.Multi) {
149
+ const keys: (string | [string, string])[] = filter.options
150
+ ?.filter((option) => option.selected)
151
+ .map((option) =>
152
+ withLabels ? [option.value, option.label] : option.value,
153
+ );
154
+ if (keys?.length > 0) {
155
+ withLabels
156
+ ? acc.set([filter.key, filter.label], keys)
157
+ : acc.set(filter.key, keys);
158
+ }
159
+ } else if (filter.type === FilterType.Toggle) {
160
+ const activeValue = activeFilters[filter.key];
161
+ const values = Array.isArray(activeValue) ? activeValue : [activeValue];
162
+ const isExplicitFalse = values.includes("false");
163
+
164
+ if (filter.value || isExplicitFalse) {
165
+ const value = filter.value ? "true" : "false";
166
+ const label =
167
+ value === "true"
168
+ ? filter.label
169
+ : filter.label.replace(/^Heeft\s+/i, "Heeft geen ");
170
+
171
+ withLabels
172
+ ? acc.set([filter.key, filter.label], [[value, label]])
173
+ : acc.set(filter.key, [value]);
174
+ }
175
+ } else if (filter.type === FilterType.Date && filter.value) {
176
+ withLabels
177
+ ? acc.set([filter.key, filter.label], [[filter.value, filter.label]])
178
+ : acc.set(filter.key, [filter.value]);
179
+ } else if (filter.type === FilterType.Single) {
180
+ const selectedOption = filter.options?.find(
181
+ (option) => option.selected,
182
+ );
183
+ if (selectedOption) {
184
+ withLabels
185
+ ? acc.set(
186
+ [filter.key, filter.label],
187
+ [[selectedOption.value, selectedOption.label]],
188
+ )
189
+ : acc.set(filter.key, [selectedOption.value]);
190
+ }
191
+ }
192
+ return acc;
193
+ }, new Map() as SelectedFiltersMap) ?? new Map()
194
+ );
195
+ };
196
+
197
+ const updateFilters = (
198
+ selectedFilters: SelectedFilters,
199
+ filterKey: string,
200
+ filterValue: string,
201
+ checked: boolean,
202
+ type: FilterType,
203
+ ) => {
204
+ if (type === FilterType.Toggle) {
205
+ return {
206
+ ...selectedFilters,
207
+ [filterKey]: [checked ? filterValue : "false"],
208
+ };
209
+ }
210
+
211
+ return {
212
+ ...selectedFilters,
213
+ [filterKey]: checked
214
+ ? [...(selectedFilters[filterKey] || []), filterValue]
215
+ : (selectedFilters[filterKey] || []).filter(
216
+ (value) => value !== filterValue,
217
+ ),
218
+ };
219
+ };
220
+
221
+ const debounce = <T extends unknown[]>(
222
+ fn: (...args: T) => void,
223
+ delay: number,
224
+ ) => {
225
+ let timeoutId: ReturnType<typeof setTimeout> | null = null;
226
+ return (...args: T) => {
227
+ if (timeoutId) {
228
+ clearTimeout(timeoutId);
229
+ }
230
+ timeoutId = setTimeout(() => {
231
+ fn(...args);
232
+ }, delay);
233
+ };
234
+ };
235
+
236
+ const FacetFilters = (props: PropsWithChildren<FacetFiltersProps>) => {
237
+ const { t } = useTranslation();
238
+ const thisRef = useRef<HTMLDivElement>(null);
239
+ const {
240
+ title,
241
+ startHeadingLevel = 2,
242
+ filters,
243
+ onFilterChange = () => {},
244
+ debounceTypes = [FilterType.Date, FilterType.Single],
245
+ debounceDelay = 600,
246
+ ...restProps
247
+ } = props;
248
+
249
+ const selectedFilters = getSelectedFilters(filters);
250
+
251
+ // Callback function (for React) and CustomEvent (for non-React) to notify about filter changes
252
+ const emitChange = (newSelectedFilters: SelectedFilters) => {
253
+ onFilterChange(newSelectedFilters);
254
+ const customEvent = new CustomEvent("onFilterChange", {
255
+ detail: newSelectedFilters,
256
+ });
257
+ if (thisRef.current) thisRef.current.dispatchEvent(customEvent);
258
+ };
259
+
260
+ const debouncedEmitChange = useRef(
261
+ debounce(emitChange, debounceDelay),
262
+ ).current;
263
+
264
+ const handleChange = (
265
+ event: ChangeEvent<HTMLInputElement>,
266
+ type: FilterType,
267
+ filterKey = event.target.name,
268
+ ) => {
269
+ const newSelectedFilters = updateFilters(
270
+ selectedFilters,
271
+ filterKey,
272
+ event.target.value,
273
+ event.target.checked,
274
+ type,
275
+ );
276
+
277
+ if (debounceTypes.includes(type)) {
278
+ debouncedEmitChange(newSelectedFilters);
279
+ } else {
280
+ emitChange(newSelectedFilters);
281
+ }
282
+ };
283
+
284
+ const handleToggleChange = (
285
+ event: ChangeEvent<HTMLInputElement>,
286
+ filterKey: string,
287
+ ) => {
288
+ const hiddenInput = event.target.form?.elements.namedItem(filterKey);
289
+
290
+ if (hiddenInput instanceof HTMLInputElement) {
291
+ hiddenInput.value = event.target.checked ? "true" : "false";
292
+ }
293
+
294
+ handleChange(event, FilterType.Toggle, filterKey);
295
+ };
296
+
297
+ return (
298
+ <div
299
+ ref={thisRef}
300
+ className={clsx([styles.facetFilters, props.className])}
301
+ {...restProps}
302
+ >
303
+ {title ? (
304
+ <Heading level={startHeadingLevel} appearanceLevel={3}>
305
+ {title}
306
+ </Heading>
307
+ ) : null}
308
+ {/* Temporary filter on `date` types */}
309
+ {filters
310
+ ?.filter(({ type }) => type !== "date")
311
+ .map((facet) => (
312
+ <div className={styles.facet} key={facet.key}>
313
+ <Heading
314
+ level={
315
+ title
316
+ ? clampHeadingLevel(startHeadingLevel + 1)
317
+ : startHeadingLevel
318
+ }
319
+ appearanceLevel={5}
320
+ >
321
+ {facet.label}
322
+ </Heading>
323
+ <Paragraph id={`${facet.key}-label`} purpose="short">
324
+ {facet.description}
325
+ </Paragraph>
326
+ {facet.type === "toggle" ? (
327
+ <>
328
+ <input
329
+ type="hidden"
330
+ name={facet.key}
331
+ defaultValue={facet.value ? "true" : "false"}
332
+ />
333
+ <FormFieldSwitch
334
+ label=""
335
+ labelledById={`${facet.key}-label`}
336
+ defaultChecked={facet.value}
337
+ value="true"
338
+ amount={facet.count || 0}
339
+ labels={{
340
+ on: t("components.filter-on"),
341
+ off: t("components.filter-off"),
342
+ }}
343
+ onChange={(event) => handleToggleChange(event, facet.key)}
344
+ />
345
+ </>
346
+ ) : null}
347
+ {/* TODO: Date selector
348
+ {facet.type === "date" ? <div>TODO</div> : null}
349
+ */}
350
+ {facet.type === "multi-select" ? (
351
+ <FormFieldCheckboxGroup>
352
+ {facet.options?.map((option) => (
353
+ <FormFieldCheckboxOption
354
+ key={option.value}
355
+ label={option.label}
356
+ name={facet.key}
357
+ value={option.value}
358
+ defaultChecked={option.selected}
359
+ amount={option.count}
360
+ onChange={(event) => handleChange(event, facet.type)}
361
+ >
362
+ {option.description ? (
363
+ <ToolTip
364
+ text={option.description}
365
+ aria-label={t("components.info-about", {
366
+ subject: option.label,
367
+ })}
368
+ />
369
+ ) : null}
370
+ </FormFieldCheckboxOption>
371
+ ))}
372
+ </FormFieldCheckboxGroup>
373
+ ) : null}
374
+ {facet.type === "single-select" ? (
375
+ <FormFieldRadioGroup>
376
+ <FormFieldRadioOption
377
+ key="none"
378
+ label={t("components.filter-all")}
379
+ name={facet.key}
380
+ value=""
381
+ onChange={(event) => handleChange(event, facet.type)}
382
+ />
383
+ {facet.options?.map((option) => (
384
+ <FormFieldRadioOption
385
+ key={option.value}
386
+ label={option.label}
387
+ name={facet.key}
388
+ value={option.value}
389
+ defaultChecked={option.selected}
390
+ amount={option.count}
391
+ onChange={(event) => handleChange(event, facet.type)}
392
+ >
393
+ {option.description ? (
394
+ <ToolTip
395
+ text={option.description}
396
+ aria-label={t("components.info-about", {
397
+ subject: option.label,
398
+ })}
399
+ />
400
+ ) : null}
401
+ </FormFieldRadioOption>
402
+ ))}
403
+ </FormFieldRadioGroup>
404
+ ) : null}
405
+ </div>
406
+ ))}
407
+ </div>
408
+ );
409
+ };
410
+
411
+ const TranslatedFacetFilters = (props: FacetFiltersProps) => {
412
+ return (
413
+ <I18nextProvider i18n={i18n}>
414
+ <FacetFilters {...props} />
415
+ </I18nextProvider>
416
+ );
417
+ };
418
+
419
+ export default TranslatedFacetFilters;