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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/CHANGELOG.md +118 -85
  2. package/LICENSE.md +291 -291
  3. package/README.md +20 -20
  4. package/package.json +2 -2
  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 +54 -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 +367 -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,367 @@
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
+ export interface FacetFiltersProps extends HTMLProps<HTMLDivElement> {
78
+ title?: string;
79
+ startHeadingLevel?: HeadingProps["level"];
80
+ filters: FilterData[] | null | undefined;
81
+ /**
82
+ * Callback function called when filter option has changed.
83
+ * @param selectedFilters current selected filters at the moment of change
84
+ * @returns
85
+ */
86
+ onFilterChange?: (selectedFilters: SelectedFilters) => void;
87
+ /**
88
+ * Types of filters for which the onFilterChange callback should be debounced. Default: ["date", "single-select"]
89
+ */
90
+ debounceTypes?: Array<FilterType>;
91
+ /**
92
+ * Delay for debouncing the onFilterChange callback, in milliseconds. Default: 600
93
+ */
94
+ debounceDelay?: number;
95
+ }
96
+
97
+ /**
98
+ * @deprecated Use getSelectedFiltersMap, that will return a map with keys as a tuple with key and label
99
+ * @param filters
100
+ * @param withLabels Note: keys have no labels, use the map version
101
+ * @returns object
102
+ */
103
+ export const getSelectedFilters = (
104
+ filters: FilterData[] | null | undefined,
105
+ withLabels = false,
106
+ ): SelectedFilters => {
107
+ return (
108
+ filters?.reduce((acc, filter) => {
109
+ if (filter.type === FilterType.Multi) {
110
+ const keys: (string | [string, string])[] = filter.options
111
+ ?.filter((option) => option.selected)
112
+ .map((option) =>
113
+ withLabels ? [option.value, option.label] : option.value,
114
+ );
115
+ if (keys?.length > 0) {
116
+ acc[filter.key] = keys;
117
+ }
118
+ } else if (filter.type === FilterType.Toggle && filter.value) {
119
+ acc[filter.key] = withLabels ? [["true", filter.label]] : ["true"];
120
+ } else if (filter.type === FilterType.Date && filter.value) {
121
+ acc[filter.key] = withLabels
122
+ ? [[filter.value, filter.label]]
123
+ : [filter.value];
124
+ } else if (filter.type === FilterType.Single) {
125
+ const selectedOption = filter.options?.find(
126
+ (option) => option.selected,
127
+ );
128
+ if (selectedOption) {
129
+ acc[filter.key] = withLabels
130
+ ? [[selectedOption.value, selectedOption.label]]
131
+ : [selectedOption.value];
132
+ }
133
+ }
134
+ return acc;
135
+ }, {} as SelectedFilters) ?? {}
136
+ );
137
+ };
138
+
139
+ export const getSelectedFiltersMap = (
140
+ filters: FilterData[] | null | undefined,
141
+ withLabels = false,
142
+ ): SelectedFiltersMap => {
143
+ return (
144
+ filters?.reduce((acc, filter) => {
145
+ if (filter.type === FilterType.Multi) {
146
+ const keys: (string | [string, string])[] = filter.options
147
+ ?.filter((option) => option.selected)
148
+ .map((option) =>
149
+ withLabels ? [option.value, option.label] : option.value,
150
+ );
151
+ if (keys?.length > 0) {
152
+ withLabels
153
+ ? acc.set([filter.key, filter.label], keys)
154
+ : acc.set(filter.key, keys);
155
+ }
156
+ } else if (filter.type === FilterType.Toggle && filter.value) {
157
+ withLabels
158
+ ? acc.set([filter.key, filter.label], [["true", filter.label]])
159
+ : acc.set(filter.key, ["true"]);
160
+ } else if (filter.type === FilterType.Date && filter.value) {
161
+ withLabels
162
+ ? acc.set([filter.key, filter.label], [[filter.value, filter.label]])
163
+ : acc.set(filter.key, [filter.value]);
164
+ } else if (filter.type === FilterType.Single) {
165
+ const selectedOption = filter.options?.find(
166
+ (option) => option.selected,
167
+ );
168
+ if (selectedOption) {
169
+ withLabels
170
+ ? acc.set(
171
+ [filter.key, filter.label],
172
+ [[selectedOption.value, selectedOption.label]],
173
+ )
174
+ : acc.set(filter.key, [selectedOption.value]);
175
+ }
176
+ }
177
+ return acc;
178
+ }, new Map() as SelectedFiltersMap) ?? new Map()
179
+ );
180
+ };
181
+
182
+ const updateFilters = (
183
+ selectedFilters: SelectedFilters,
184
+ filterKey: string,
185
+ filterValue: string,
186
+ checked: boolean,
187
+ ) => {
188
+ const newSelectedFilters = {
189
+ ...selectedFilters,
190
+ [filterKey]: checked
191
+ ? [...(selectedFilters[filterKey] || []), filterValue]
192
+ : (selectedFilters[filterKey] || []).filter(
193
+ (value) => value !== filterValue,
194
+ ),
195
+ };
196
+ return newSelectedFilters;
197
+ };
198
+
199
+ const debounce = <T extends unknown[]>(
200
+ fn: (...args: T) => void,
201
+ delay: number,
202
+ ) => {
203
+ let timeoutId: ReturnType<typeof setTimeout> | null = null;
204
+ return (...args: T) => {
205
+ if (timeoutId) {
206
+ clearTimeout(timeoutId);
207
+ }
208
+ timeoutId = setTimeout(() => {
209
+ fn(...args);
210
+ }, delay);
211
+ };
212
+ };
213
+
214
+ const FacetFilters = (props: PropsWithChildren<FacetFiltersProps>) => {
215
+ const { t } = useTranslation();
216
+ const thisRef = useRef<HTMLDivElement>(null);
217
+ const {
218
+ title,
219
+ startHeadingLevel = 2,
220
+ filters,
221
+ onFilterChange = () => {},
222
+ debounceTypes = [FilterType.Date, FilterType.Single],
223
+ debounceDelay = 600,
224
+ ...restProps
225
+ } = props;
226
+
227
+ const selectedFilters = getSelectedFilters(filters);
228
+
229
+ // Callback function (for React) and CustomEvent (for non-React) to notify about filter changes
230
+ const emitChange = (newSelectedFilters: SelectedFilters) => {
231
+ onFilterChange(newSelectedFilters);
232
+ const customEvent = new CustomEvent("onFilterChange", {
233
+ detail: newSelectedFilters,
234
+ });
235
+ if (thisRef.current) thisRef.current.dispatchEvent(customEvent);
236
+ };
237
+
238
+ const debouncedEmitChange = useRef(
239
+ debounce(emitChange, debounceDelay),
240
+ ).current;
241
+
242
+ const handleChange = (
243
+ event: ChangeEvent<HTMLInputElement>,
244
+ type: FilterType,
245
+ ) => {
246
+ const newSelectedFilters = updateFilters(
247
+ selectedFilters,
248
+ event.target.name,
249
+ event.target.value,
250
+ event.target.checked,
251
+ );
252
+
253
+ if (debounceTypes.includes(type)) {
254
+ debouncedEmitChange(newSelectedFilters);
255
+ } else {
256
+ emitChange(newSelectedFilters);
257
+ }
258
+ };
259
+
260
+ return (
261
+ <div
262
+ ref={thisRef}
263
+ className={clsx([styles.facetFilters, props.className])}
264
+ {...restProps}
265
+ >
266
+ {title ? (
267
+ <Heading level={startHeadingLevel} appearanceLevel={3}>
268
+ {title}
269
+ </Heading>
270
+ ) : null}
271
+ {/* Temporary filter on `date` types */}
272
+ {filters
273
+ ?.filter(({ type }) => type !== "date")
274
+ .map((facet) => (
275
+ <div className={styles.facet} key={facet.key}>
276
+ <Heading
277
+ level={
278
+ title
279
+ ? clampHeadingLevel(startHeadingLevel + 1)
280
+ : startHeadingLevel
281
+ }
282
+ appearanceLevel={5}
283
+ >
284
+ {facet.label}
285
+ </Heading>
286
+ <Paragraph id={`${facet.key}-label`} purpose="short">
287
+ {facet.description}
288
+ </Paragraph>
289
+ {facet.type === "toggle" ? (
290
+ <FormFieldSwitch
291
+ label=""
292
+ labelledById={`${facet.key}-label`}
293
+ name={facet.key}
294
+ defaultChecked={facet.value}
295
+ value="true"
296
+ amount={facet.count || 0}
297
+ labels={{
298
+ on: t("components.filter-on"),
299
+ off: t("components.filter-off"),
300
+ }}
301
+ onChange={(event) => handleChange(event, facet.type)}
302
+ />
303
+ ) : null}
304
+ {/* TODO: Date selector
305
+ {facet.type === "date" ? <div>TODO</div> : null}
306
+ */}
307
+ {facet.type === "multi-select" ? (
308
+ <FormFieldCheckboxGroup>
309
+ {facet.options?.map((option) => (
310
+ <FormFieldCheckboxOption
311
+ key={option.value}
312
+ label={option.label}
313
+ name={facet.key}
314
+ value={option.value}
315
+ defaultChecked={option.selected}
316
+ amount={option.count}
317
+ onChange={(event) => handleChange(event, facet.type)}
318
+ >
319
+ {option.description ? (
320
+ <ToolTip
321
+ text={option.description}
322
+ aria-label={t("components.info-about", {
323
+ subject: option.label,
324
+ })}
325
+ />
326
+ ) : null}
327
+ </FormFieldCheckboxOption>
328
+ ))}
329
+ </FormFieldCheckboxGroup>
330
+ ) : null}
331
+ {facet.type === "single-select" ? (
332
+ <FormFieldRadioGroup>
333
+ <FormFieldRadioOption
334
+ key="none"
335
+ label={t("components.filter-all")}
336
+ name={facet.key}
337
+ value=""
338
+ onChange={(event) => handleChange(event, facet.type)}
339
+ />
340
+ {facet.options?.map((option) => (
341
+ <FormFieldRadioOption
342
+ key={option.value}
343
+ label={option.label}
344
+ name={facet.key}
345
+ value={option.value}
346
+ defaultChecked={option.selected}
347
+ amount={option.count}
348
+ onChange={(event) => handleChange(event, facet.type)}
349
+ />
350
+ ))}
351
+ </FormFieldRadioGroup>
352
+ ) : null}
353
+ </div>
354
+ ))}
355
+ </div>
356
+ );
357
+ };
358
+
359
+ const TranslatedFacetFilters = (props: FacetFiltersProps) => {
360
+ return (
361
+ <I18nextProvider i18n={i18n}>
362
+ <FacetFilters {...props} />
363
+ </I18nextProvider>
364
+ );
365
+ };
366
+
367
+ export default TranslatedFacetFilters;