@navikt/ds-react 8.5.0 → 8.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/cjs/data/table/helpers/table-grid-nav.d.ts +9 -15
  2. package/cjs/data/table/helpers/table-grid-nav.js +18 -25
  3. package/cjs/data/table/helpers/table-grid-nav.js.map +1 -1
  4. package/cjs/data/table/helpers/table-keyboard.d.ts +1 -1
  5. package/cjs/data/table/helpers/table-keyboard.js +1 -6
  6. package/cjs/data/table/helpers/table-keyboard.js.map +1 -1
  7. package/cjs/data/table/root/DataTableRoot.d.ts +41 -4
  8. package/cjs/data/table/root/DataTableRoot.js +10 -6
  9. package/cjs/data/table/root/DataTableRoot.js.map +1 -1
  10. package/cjs/data/table/root/useTableKeyboardNav.d.ts +1 -1
  11. package/cjs/data/table/root/useTableKeyboardNav.js +32 -19
  12. package/cjs/data/table/root/useTableKeyboardNav.js.map +1 -1
  13. package/cjs/data/table/td/DataTableTd.d.ts +5 -4
  14. package/cjs/data/table/td/DataTableTd.js +2 -2
  15. package/cjs/data/table/td/DataTableTd.js.map +1 -1
  16. package/cjs/data/token-filter/AutoSuggest.d.ts +9 -0
  17. package/cjs/data/token-filter/AutoSuggest.js +56 -0
  18. package/cjs/data/token-filter/AutoSuggest.js.map +1 -0
  19. package/cjs/data/token-filter/AutoSuggest.types.d.ts +12 -0
  20. package/cjs/data/token-filter/AutoSuggest.types.js +3 -0
  21. package/cjs/data/token-filter/AutoSuggest.types.js.map +1 -0
  22. package/cjs/data/token-filter/TokenFilter.d.ts +11 -0
  23. package/cjs/data/token-filter/TokenFilter.js +102 -0
  24. package/cjs/data/token-filter/TokenFilter.js.map +1 -0
  25. package/cjs/data/token-filter/TokenFilter.types.d.ts +52 -0
  26. package/cjs/data/token-filter/TokenFilter.types.js +3 -0
  27. package/cjs/data/token-filter/TokenFilter.types.js.map +1 -0
  28. package/cjs/data/token-filter/helpers/generate-autocomplete-options.d.ts +24 -0
  29. package/cjs/data/token-filter/helpers/generate-autocomplete-options.js +197 -0
  30. package/cjs/data/token-filter/helpers/generate-autocomplete-options.js.map +1 -0
  31. package/cjs/data/token-filter/helpers/grouping.d.ts +28 -0
  32. package/cjs/data/token-filter/helpers/grouping.js +61 -0
  33. package/cjs/data/token-filter/helpers/grouping.js.map +1 -0
  34. package/cjs/data/token-filter/helpers/operators.d.ts +22 -0
  35. package/cjs/data/token-filter/helpers/operators.js +66 -0
  36. package/cjs/data/token-filter/helpers/operators.js.map +1 -0
  37. package/cjs/data/token-filter/helpers/parse-query-text.d.ts +25 -0
  38. package/cjs/data/token-filter/helpers/parse-query-text.js +46 -0
  39. package/cjs/data/token-filter/helpers/parse-query-text.js.map +1 -0
  40. package/cjs/data/token-filter/helpers/query-builder.d.ts +20 -0
  41. package/cjs/data/token-filter/helpers/query-builder.js +38 -0
  42. package/cjs/data/token-filter/helpers/query-builder.js.map +1 -0
  43. package/cjs/data/token-filter/helpers/text-matching.d.ts +16 -0
  44. package/cjs/data/token-filter/helpers/text-matching.js +47 -0
  45. package/cjs/data/token-filter/helpers/text-matching.js.map +1 -0
  46. package/cjs/form/combobox/Input/InputController.js +1 -1
  47. package/cjs/form/combobox/Input/InputController.js.map +1 -1
  48. package/cjs/form/file-upload/dropzone/FileUploadDropzone.js +1 -1
  49. package/cjs/form/file-upload/dropzone/FileUploadDropzone.js.map +1 -1
  50. package/cjs/tooltip/Tooltip.js +1 -1
  51. package/cjs/tooltip/Tooltip.js.map +1 -1
  52. package/cjs/utils/i18n/locales/nb.d.ts +75 -154
  53. package/cjs/utils/i18n/locales/nb.js +75 -154
  54. package/cjs/utils/i18n/locales/nb.js.map +1 -1
  55. package/esm/data/table/helpers/table-grid-nav.d.ts +9 -15
  56. package/esm/data/table/helpers/table-grid-nav.js +18 -25
  57. package/esm/data/table/helpers/table-grid-nav.js.map +1 -1
  58. package/esm/data/table/helpers/table-keyboard.d.ts +1 -1
  59. package/esm/data/table/helpers/table-keyboard.js +1 -6
  60. package/esm/data/table/helpers/table-keyboard.js.map +1 -1
  61. package/esm/data/table/root/DataTableRoot.d.ts +41 -4
  62. package/esm/data/table/root/DataTableRoot.js +10 -6
  63. package/esm/data/table/root/DataTableRoot.js.map +1 -1
  64. package/esm/data/table/root/useTableKeyboardNav.d.ts +1 -1
  65. package/esm/data/table/root/useTableKeyboardNav.js +32 -19
  66. package/esm/data/table/root/useTableKeyboardNav.js.map +1 -1
  67. package/esm/data/table/td/DataTableTd.d.ts +5 -4
  68. package/esm/data/table/td/DataTableTd.js +2 -2
  69. package/esm/data/table/td/DataTableTd.js.map +1 -1
  70. package/esm/data/token-filter/AutoSuggest.d.ts +9 -0
  71. package/esm/data/token-filter/AutoSuggest.js +20 -0
  72. package/esm/data/token-filter/AutoSuggest.js.map +1 -0
  73. package/esm/data/token-filter/AutoSuggest.types.d.ts +12 -0
  74. package/esm/data/token-filter/AutoSuggest.types.js +2 -0
  75. package/esm/data/token-filter/AutoSuggest.types.js.map +1 -0
  76. package/esm/data/token-filter/TokenFilter.d.ts +11 -0
  77. package/esm/data/token-filter/TokenFilter.js +66 -0
  78. package/esm/data/token-filter/TokenFilter.js.map +1 -0
  79. package/esm/data/token-filter/TokenFilter.types.d.ts +52 -0
  80. package/esm/data/token-filter/TokenFilter.types.js +2 -0
  81. package/esm/data/token-filter/TokenFilter.types.js.map +1 -0
  82. package/esm/data/token-filter/helpers/generate-autocomplete-options.d.ts +24 -0
  83. package/esm/data/token-filter/helpers/generate-autocomplete-options.js +195 -0
  84. package/esm/data/token-filter/helpers/generate-autocomplete-options.js.map +1 -0
  85. package/esm/data/token-filter/helpers/grouping.d.ts +28 -0
  86. package/esm/data/token-filter/helpers/grouping.js +59 -0
  87. package/esm/data/token-filter/helpers/grouping.js.map +1 -0
  88. package/esm/data/token-filter/helpers/operators.d.ts +22 -0
  89. package/esm/data/token-filter/helpers/operators.js +60 -0
  90. package/esm/data/token-filter/helpers/operators.js.map +1 -0
  91. package/esm/data/token-filter/helpers/parse-query-text.d.ts +25 -0
  92. package/esm/data/token-filter/helpers/parse-query-text.js +44 -0
  93. package/esm/data/token-filter/helpers/parse-query-text.js.map +1 -0
  94. package/esm/data/token-filter/helpers/query-builder.d.ts +20 -0
  95. package/esm/data/token-filter/helpers/query-builder.js +34 -0
  96. package/esm/data/token-filter/helpers/query-builder.js.map +1 -0
  97. package/esm/data/token-filter/helpers/text-matching.d.ts +16 -0
  98. package/esm/data/token-filter/helpers/text-matching.js +45 -0
  99. package/esm/data/token-filter/helpers/text-matching.js.map +1 -0
  100. package/esm/form/combobox/Input/InputController.js +1 -1
  101. package/esm/form/combobox/Input/InputController.js.map +1 -1
  102. package/esm/form/file-upload/dropzone/FileUploadDropzone.js +1 -1
  103. package/esm/form/file-upload/dropzone/FileUploadDropzone.js.map +1 -1
  104. package/esm/tooltip/Tooltip.js +2 -2
  105. package/esm/tooltip/Tooltip.js.map +1 -1
  106. package/esm/utils/i18n/locales/nb.d.ts +75 -154
  107. package/esm/utils/i18n/locales/nb.js +75 -154
  108. package/esm/utils/i18n/locales/nb.js.map +1 -1
  109. package/package.json +3 -3
  110. package/src/data/table/helpers/table-grid-nav.test.ts +659 -0
  111. package/src/data/table/helpers/table-grid-nav.ts +19 -38
  112. package/src/data/table/helpers/table-keyboard.ts +1 -10
  113. package/src/data/table/root/DataTableRoot.tsx +50 -10
  114. package/src/data/table/root/useTableKeyboardNav.ts +35 -23
  115. package/src/data/table/td/DataTableTd.tsx +13 -6
  116. package/src/data/token-filter/AutoSuggest.tsx +55 -0
  117. package/src/data/token-filter/AutoSuggest.types.ts +14 -0
  118. package/src/data/token-filter/TokenFilter.tsx +129 -0
  119. package/src/data/token-filter/TokenFilter.types.ts +85 -0
  120. package/src/data/token-filter/helpers/generate-autocomplete-options.test.ts +896 -0
  121. package/src/data/token-filter/helpers/generate-autocomplete-options.ts +289 -0
  122. package/src/data/token-filter/helpers/grouping.test.ts +206 -0
  123. package/src/data/token-filter/helpers/grouping.ts +73 -0
  124. package/src/data/token-filter/helpers/operators.test.ts +281 -0
  125. package/src/data/token-filter/helpers/operators.ts +91 -0
  126. package/src/data/token-filter/helpers/parse-query-text.test.ts +201 -0
  127. package/src/data/token-filter/helpers/parse-query-text.ts +86 -0
  128. package/src/data/token-filter/helpers/query-builder.test.ts +126 -0
  129. package/src/data/token-filter/helpers/query-builder.ts +41 -0
  130. package/src/data/token-filter/helpers/text-matching.test.ts +125 -0
  131. package/src/data/token-filter/helpers/text-matching.ts +58 -0
  132. package/src/form/combobox/Input/InputController.tsx +0 -1
  133. package/src/form/file-upload/dropzone/FileUploadDropzone.tsx +0 -1
  134. package/src/tooltip/Tooltip.tsx +3 -3
  135. package/src/utils/i18n/locales/nb.ts +4 -83
@@ -0,0 +1,289 @@
1
+ import type { AutoCompleteOption, OptionGroup } from "../AutoSuggest.types";
2
+ import type {
3
+ ParsedOption,
4
+ ParsedProperty,
5
+ QueryFilterOperator,
6
+ } from "../TokenFilter.types";
7
+ import { createGroups } from "./grouping";
8
+ import { QUERY_OPERATORS } from "./operators";
9
+ import { type ParsedText } from "./parse-query-text";
10
+ import { OPERATOR_LABELS, buildQueryString } from "./query-builder";
11
+ import { matchesFilterText } from "./text-matching";
12
+
13
+ /**
14
+ * Generates "options" to be used as autosuggest-ottion based on the current query state.
15
+ *
16
+ * The query parser recognizes three states:
17
+ * - "property": User has selected/matched a property and operator ("Status = active")
18
+ * - "operator": User has matched a property but is typing the operator ("Status" or "Status !")
19
+ * - "free-text": User is typing freely without a property match (e.g., "act" or "!: test")
20
+ *
21
+ * @returns
22
+ * - value: The canonical query string representation for the current state.
23
+ * Used by the UI to determine cursor position and input replacement.
24
+ * - options: Grouped suggestions to display (properties, operators, or values).
25
+ */
26
+ function generateAutoCompleteOptions(
27
+ queryState: ParsedText,
28
+ filteringProperties: ParsedProperty[] = [],
29
+ filteringOptions: ParsedOption[] = [],
30
+ ) {
31
+ /* State: Property and operator are matched, suggest values */
32
+ if (queryState.step === "property") {
33
+ const filterText = queryState.value || "";
34
+
35
+ return {
36
+ value: queryState.value,
37
+ options: createValueSuggestions(
38
+ filteringOptions,
39
+ queryState.operator,
40
+ filterText,
41
+ queryState.property,
42
+ ),
43
+ };
44
+ }
45
+
46
+ /* State: Property matched, but operator is incomplete */
47
+ if (queryState.step === "operator") {
48
+ const operators = filterOperatorsByPrefix(
49
+ getValidOperatorsForProperty(queryState.property),
50
+ queryState.operatorPrefix,
51
+ );
52
+
53
+ const partialQuery = buildQueryString(
54
+ queryState.property.propertyLabel,
55
+ queryState.operatorPrefix,
56
+ "",
57
+ );
58
+
59
+ /**
60
+ * Edge case: User typed an invalid operator prefix that doesn't match any operators.
61
+ * This can happen when typing characters that don't start any valid operator.
62
+ * Return empty suggestions gracefully - the UI will show "no results".
63
+ *
64
+ * TODO: When per-property operator configuration is implemented,
65
+ * this could also occur when a property restricts which operators are valid.
66
+ */
67
+ if (operators.length === 0) {
68
+ return {
69
+ value: partialQuery,
70
+ options: [],
71
+ };
72
+ }
73
+
74
+ return {
75
+ value: partialQuery,
76
+ options: generateOperatorSuggestions(
77
+ queryState.property,
78
+ queryState.operatorPrefix,
79
+ ),
80
+ };
81
+ }
82
+
83
+ /*
84
+ * Edge case: Input starts with operator but has no value yet (user typed just "!=")
85
+ * Wait for value before showing suggestions
86
+ */
87
+ if (!queryState.value && queryState.operator) {
88
+ return {
89
+ value: "",
90
+ options: [],
91
+ };
92
+ }
93
+
94
+ /* Empty input: Show all properties */
95
+ if (!queryState.value) {
96
+ return {
97
+ value: "",
98
+ options: generatePropertySuggestions(filteringProperties),
99
+ };
100
+ }
101
+
102
+ /*
103
+ * Free-text search: Show matching values across all properties
104
+ * Use the detected operator if input started with one (e.g., "!= test"), otherwise default to "="
105
+ */
106
+ return {
107
+ value: queryState.value,
108
+ options: createValueSuggestions(
109
+ filteringOptions,
110
+ queryState.operator ?? "=",
111
+ queryState.value,
112
+ ),
113
+ };
114
+ }
115
+
116
+ /**
117
+ * Returns the valid operators for a given property.
118
+ * Extracts operators from the property's custom operator configuration.
119
+ * If none are configured, falls back to all available operators.
120
+ *
121
+ * The QueryFilteringScopedOperator can be a simple string (e.g., "=")
122
+ * or an object with operator and tokenType (e.g., { operator: ":", tokenType: "single" }).
123
+ * This function normalizes both formats and returns just the operator strings.
124
+ *
125
+ * @returns Array of valid operators for the property
126
+ *
127
+ * TODO: We omit passing the tokenType for now since it's not currently used in the UI. But will be needed for single/multi-selection.
128
+ */
129
+ function getValidOperatorsForProperty(
130
+ property: ParsedProperty,
131
+ ): QueryFilterOperator[] {
132
+ const { operators } = property;
133
+
134
+ /* If no operators configured, return all available operators */
135
+ if (!operators || operators.length === 0) {
136
+ return QUERY_OPERATORS;
137
+ }
138
+
139
+ /*
140
+ * Extract operator strings from QueryFilteringScopedOperator format
141
+ * Handle both simple strings and objects with operator property
142
+ */
143
+ const operatorStrings = operators.map((op) =>
144
+ typeof op === "string" ? op : op.operator,
145
+ );
146
+
147
+ /* Filter to only valid QUERY_OPERATORS to ensure type safety */
148
+ return operatorStrings.filter((op) =>
149
+ QUERY_OPERATORS.includes(op as QueryFilterOperator),
150
+ ) as QueryFilterOperator[];
151
+ }
152
+
153
+ /**
154
+ * Filters the list of operators based on the provided prefix.
155
+ * If the prefix is empty, all operators are returned.
156
+ */
157
+ function filterOperatorsByPrefix(
158
+ operators: QueryFilterOperator[],
159
+ prefix: string,
160
+ ): QueryFilterOperator[] {
161
+ if (!prefix) {
162
+ return operators;
163
+ }
164
+
165
+ return operators.filter((operator) => operator.startsWith(prefix));
166
+ }
167
+
168
+ function generatePropertySuggestions(
169
+ filteringProperties: ParsedProperty[] = [],
170
+ filterText = "",
171
+ ): OptionGroup<ParsedProperty>[] {
172
+ const filteredProperties: ParsedProperty[] = [];
173
+
174
+ for (const property of filteringProperties) {
175
+ if (!property) {
176
+ continue;
177
+ }
178
+
179
+ if (
180
+ matchesFilterText(
181
+ [
182
+ property.propertyLabel,
183
+ property.groupValuesLabel,
184
+ property.propertyGroup,
185
+ ].filter(Boolean),
186
+ filterText,
187
+ )
188
+ ) {
189
+ filteredProperties.push(property);
190
+ }
191
+ }
192
+
193
+ return createGroups(
194
+ filteredProperties,
195
+ (property) => property.propertyGroup,
196
+ "Properties",
197
+ );
198
+ }
199
+
200
+ function generateOperatorSuggestions(
201
+ property: ParsedProperty,
202
+ operatorPrefix = "",
203
+ ): OptionGroup<AutoCompleteOption>[] {
204
+ const operators = filterOperatorsByPrefix(
205
+ getValidOperatorsForProperty(property),
206
+ operatorPrefix,
207
+ );
208
+
209
+ if (operators.length === 0) {
210
+ return [];
211
+ }
212
+
213
+ return [
214
+ {
215
+ label: "Operators",
216
+ options: operators.map((operator) => ({
217
+ value: buildQueryString(property.propertyLabel, operator, ""),
218
+ label: buildQueryString(property.propertyLabel, operator, ""),
219
+ description: OPERATOR_LABELS[operator] ?? "",
220
+ })),
221
+ },
222
+ ];
223
+ }
224
+
225
+ /**
226
+ * Creates value suggestions for autocomplete.
227
+ * When scopedProperty is provided, only shows values for that property (single group).
228
+ * When scopedProperty is omitted, searches across all properties (multiple groups).
229
+ */
230
+ function createValueSuggestions(
231
+ filteringOptions: ParsedOption[] = [],
232
+ operator: QueryFilterOperator,
233
+ filterText = "",
234
+ scopedProperty?: ParsedProperty,
235
+ ): OptionGroup<AutoCompleteOption>[] {
236
+ const groups: Record<string, OptionGroup<AutoCompleteOption>> = {};
237
+
238
+ for (const option of filteringOptions) {
239
+ if (!option?.property) {
240
+ continue;
241
+ }
242
+
243
+ /* If scoped to a property, filter to only that property's options */
244
+ if (scopedProperty && option.property !== scopedProperty) {
245
+ continue;
246
+ }
247
+
248
+ /* Build search fields */
249
+ const searchFields = [
250
+ option.label,
251
+ ...(option.tags ?? []),
252
+ ...(option.filteringTags ?? []),
253
+ ];
254
+
255
+ if (!scopedProperty) {
256
+ searchFields.push(option.property.propertyLabel);
257
+ }
258
+
259
+ const matches = matchesFilterText(searchFields.filter(Boolean), filterText);
260
+
261
+ if (!matches) {
262
+ continue;
263
+ }
264
+
265
+ const groupLabel = option.property.groupValuesLabel || "Values";
266
+
267
+ if (!groups[groupLabel]) {
268
+ groups[groupLabel] = {
269
+ label: groupLabel,
270
+ options: [],
271
+ };
272
+ }
273
+
274
+ groups[groupLabel].options.push({
275
+ value: buildQueryString(
276
+ option.property.propertyLabel,
277
+ operator,
278
+ option.value,
279
+ ),
280
+ label: option.label,
281
+ tags: option.tags,
282
+ filteringTags: option.filteringTags,
283
+ });
284
+ }
285
+
286
+ return Object.values(groups).filter((group) => group.options.length > 0);
287
+ }
288
+
289
+ export { generateAutoCompleteOptions };
@@ -0,0 +1,206 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { createGroups } from "./grouping";
3
+
4
+ interface TestItem {
5
+ name: string;
6
+ group?: string;
7
+ value: string;
8
+ }
9
+
10
+ describe("createGroups", () => {
11
+ describe("basic grouping", () => {
12
+ test("groups items by label", () => {
13
+ const items: TestItem[] = [
14
+ { name: "Item 1", group: "Group A", value: "1" },
15
+ { name: "Item 2", group: "Group B", value: "2" },
16
+ { name: "Item 3", group: "Group A", value: "3" },
17
+ ];
18
+
19
+ const groups = createGroups(items, (item) => item.group);
20
+
21
+ expect(groups).toHaveLength(2);
22
+ expect(groups[0].label).toBe("Group A");
23
+ expect(groups[0].options).toHaveLength(2);
24
+ expect(groups[1].label).toBe("Group B");
25
+ expect(groups[1].options).toHaveLength(1);
26
+ });
27
+
28
+ test("uses default group for items without group label", () => {
29
+ const items: TestItem[] = [
30
+ { name: "Item 1", group: "Group A", value: "1" },
31
+ { name: "Item 2", value: "2" },
32
+ ];
33
+
34
+ const groups = createGroups(items, (item) => item.group, "Default");
35
+
36
+ expect(groups).toHaveLength(2);
37
+ expect(groups[0].label).toBe("Group A");
38
+ expect(groups[1].label).toBe("Default");
39
+ expect(groups[1].options).toHaveLength(1);
40
+ });
41
+
42
+ test("places default group last", () => {
43
+ const items: TestItem[] = [
44
+ { name: "No group", value: "0" },
45
+ { name: "Item 1", group: "Group A", value: "1" },
46
+ { name: "Item 2", group: "Group B", value: "2" },
47
+ ];
48
+
49
+ const groups = createGroups(items, (item) => item.group, "Default");
50
+
51
+ expect(groups).toHaveLength(3);
52
+ expect(groups[2].label).toBe("Default");
53
+ });
54
+
55
+ test("returns empty array when no items", () => {
56
+ const groups = createGroups([], (item: TestItem) => item.group);
57
+
58
+ expect(groups).toEqual([]);
59
+ });
60
+ });
61
+
62
+ describe("empty string handling", () => {
63
+ test("treats empty string as default group", () => {
64
+ const items: TestItem[] = [
65
+ { name: "Item 1", group: "Group A", value: "1" },
66
+ { name: "Item 2", group: "", value: "2" },
67
+ ];
68
+
69
+ const groups = createGroups(items, (item) => item.group, "Properties");
70
+
71
+ expect(groups).toHaveLength(2);
72
+ expect(groups[0].label).toBe("Group A");
73
+ expect(groups[1].label).toBe("Properties");
74
+ expect(groups[1].options).toHaveLength(1);
75
+ });
76
+
77
+ test("treats whitespace-only string as default group", () => {
78
+ const items: TestItem[] = [
79
+ { name: "Item 1", group: "Group A", value: "1" },
80
+ { name: "Item 2", group: " ", value: "2" },
81
+ ];
82
+
83
+ const groups = createGroups(items, (item) => item.group, "Default");
84
+
85
+ expect(groups).toHaveLength(2);
86
+ expect(groups[1].label).toBe("Default");
87
+ expect(groups[1].options).toHaveLength(1);
88
+ });
89
+ });
90
+
91
+ describe("null/undefined handling", () => {
92
+ test("treats undefined group as default group", () => {
93
+ const items: TestItem[] = [
94
+ { name: "Item 1", group: "Group A", value: "1" },
95
+ { name: "Item 2", group: undefined, value: "2" },
96
+ ];
97
+
98
+ const groups = createGroups(items, (item) => item.group, "Default");
99
+
100
+ expect(groups).toHaveLength(2);
101
+ expect(groups[1].label).toBe("Default");
102
+ });
103
+
104
+ test("treats null group as default group", () => {
105
+ const items: TestItem[] = [
106
+ { name: "Item 1", group: "Group A", value: "1" },
107
+ { name: "Item 2", group: null as any, value: "2" },
108
+ ];
109
+
110
+ const groups = createGroups(items, (item) => item.group, "Default");
111
+
112
+ expect(groups).toHaveLength(2);
113
+ expect(groups[1].label).toBe("Default");
114
+ });
115
+
116
+ test("skips null/undefined items", () => {
117
+ const items = [
118
+ { name: "Item 1", group: "Group A", value: "1" },
119
+ null as any,
120
+ undefined as any,
121
+ { name: "Item 2", group: "Group A", value: "2" },
122
+ ];
123
+
124
+ const groups = createGroups(items, (item) => item.group);
125
+
126
+ expect(groups).toHaveLength(1);
127
+ expect(groups[0].options).toHaveLength(2);
128
+ });
129
+ });
130
+
131
+ describe("edge cases", () => {
132
+ test("handles all items in default group", () => {
133
+ const items: TestItem[] = [
134
+ { name: "Item 1", value: "1" },
135
+ { name: "Item 2", value: "2" },
136
+ ];
137
+
138
+ const groups = createGroups(items, (item) => item.group, "All");
139
+
140
+ expect(groups).toHaveLength(1);
141
+ expect(groups[0].label).toBe("All");
142
+ expect(groups[0].options).toHaveLength(2);
143
+ });
144
+
145
+ test("handles single item", () => {
146
+ const items: TestItem[] = [
147
+ { name: "Item 1", group: "Group A", value: "1" },
148
+ ];
149
+
150
+ const groups = createGroups(items, (item) => item.group);
151
+
152
+ expect(groups).toHaveLength(1);
153
+ expect(groups[0].label).toBe("Group A");
154
+ expect(groups[0].options).toHaveLength(1);
155
+ });
156
+
157
+ test("preserves item order within groups", () => {
158
+ const items: TestItem[] = [
159
+ { name: "Item 3", group: "Group A", value: "3" },
160
+ { name: "Item 1", group: "Group A", value: "1" },
161
+ { name: "Item 2", group: "Group A", value: "2" },
162
+ ];
163
+
164
+ const groups = createGroups(items, (item) => item.group);
165
+
166
+ expect(groups[0].options[0].value).toBe("3");
167
+ expect(groups[0].options[1].value).toBe("1");
168
+ expect(groups[0].options[2].value).toBe("2");
169
+ });
170
+
171
+ test("trims group labels", () => {
172
+ const items: TestItem[] = [
173
+ { name: "Item 1", group: " Group A ", value: "1" },
174
+ { name: "Item 2", group: "Group A", value: "2" },
175
+ ];
176
+
177
+ const groups = createGroups(items, (item) => item.group);
178
+
179
+ expect(groups).toHaveLength(1);
180
+ expect(groups[0].label).toBe("Group A");
181
+ expect(groups[0].options).toHaveLength(2);
182
+ });
183
+ });
184
+
185
+ describe("custom default group label", () => {
186
+ test("uses custom default group label", () => {
187
+ const items: TestItem[] = [{ name: "Item 1", value: "1" }];
188
+
189
+ const groups = createGroups(
190
+ items,
191
+ (item) => item.group,
192
+ "Custom Default",
193
+ );
194
+
195
+ expect(groups[0].label).toBe("Custom Default");
196
+ });
197
+
198
+ test("defaults to 'Default' when not provided", () => {
199
+ const items: TestItem[] = [{ name: "Item 1", value: "1" }];
200
+
201
+ const groups = createGroups(items, (item) => item.group);
202
+
203
+ expect(groups[0].label).toBe("Default");
204
+ });
205
+ });
206
+ });
@@ -0,0 +1,73 @@
1
+ import type { OptionGroup } from "../AutoSuggest.types";
2
+
3
+ /**
4
+ * Groups items into labeled groups with support for a default group.
5
+ * Empty or whitespace-only group labels are treated as belonging to the default group.
6
+ *
7
+ * @returns Array of groups with non-empty options
8
+ *
9
+ * @example
10
+ * const properties = [
11
+ * { name: "Status", group: "Metadata" },
12
+ * { name: "Region", group: "Location" },
13
+ * { name: "Type", group: "" },
14
+ * ];
15
+ * const groups = createGroups(
16
+ * properties,
17
+ * (p) => p.group,
18
+ * "Properties"
19
+ * );
20
+ *
21
+ * Returns:
22
+ * [
23
+ * { label: "Metadata", options: [{ name: "Status", group: "Metadata" }] },
24
+ * { label: "Location", options: [{ name: "Region", group: "Location" }] },
25
+ * { label: "Properties", options: [{ name: "Type", group: "" }] }
26
+ * ]
27
+ */
28
+ function createGroups<T>(
29
+ items: T[],
30
+ getGroupLabel: (item: T) => string | undefined | null,
31
+ defaultGroupLabel = "Default",
32
+ ): OptionGroup<T>[] {
33
+ const defaultGroup: OptionGroup<T> = {
34
+ label: defaultGroupLabel,
35
+ options: [],
36
+ };
37
+ const customGroups: Record<string, OptionGroup<T>> = {};
38
+
39
+ for (const item of items) {
40
+ if (!item) {
41
+ continue;
42
+ }
43
+
44
+ const rawLabel = getGroupLabel(item);
45
+ const groupLabel = rawLabel?.trim();
46
+
47
+ /* Empty string after trim or falsy values go to default group */
48
+ if (!groupLabel) {
49
+ defaultGroup.options.push(item);
50
+ continue;
51
+ }
52
+
53
+ if (!customGroups[groupLabel]) {
54
+ customGroups[groupLabel] = {
55
+ label: groupLabel,
56
+ options: [],
57
+ };
58
+ }
59
+
60
+ customGroups[groupLabel].options.push(item);
61
+ }
62
+
63
+ /* Custom groups first, then default group if it has items */
64
+ const groups: OptionGroup<T>[] = [...Object.values(customGroups)];
65
+
66
+ if (defaultGroup.options.length > 0) {
67
+ groups.push(defaultGroup);
68
+ }
69
+
70
+ return groups;
71
+ }
72
+
73
+ export { createGroups };