@eifi1/ui-kit 0.8.1 → 0.9.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 (101) hide show
  1. package/README.md +4 -0
  2. package/dist/components/amount-input.d.ts +2 -0
  3. package/dist/components/calculator.d.ts +2 -0
  4. package/dist/components/chip.d.ts +27 -5
  5. package/dist/components/chip.js +6 -2
  6. package/dist/components/chip.js.map +1 -1
  7. package/dist/components/data-table-filter-popover.d.ts +1 -1
  8. package/dist/components/data-table-filters.d.ts +1 -1
  9. package/dist/components/data-table.d.ts +1 -1
  10. package/dist/components/data-table.js +122 -91
  11. package/dist/components/data-table.js.map +1 -1
  12. package/dist/components/date-picker.d.ts +69 -3
  13. package/dist/components/date-picker.js +152 -65
  14. package/dist/components/date-picker.js.map +1 -1
  15. package/dist/components/disclosure.d.ts +11 -2
  16. package/dist/components/disclosure.js +10 -4
  17. package/dist/components/disclosure.js.map +1 -1
  18. package/dist/components/floating-panel.d.ts +29 -2
  19. package/dist/components/floating-panel.js +16 -2
  20. package/dist/components/floating-panel.js.map +1 -1
  21. package/dist/components/number-field.d.ts +2 -0
  22. package/dist/components/number-input.d.ts +2 -0
  23. package/dist/components/numpad-sheet.d.ts +2 -0
  24. package/dist/components/progress-bar.d.ts +16 -2
  25. package/dist/components/progress-bar.js +4 -2
  26. package/dist/components/progress-bar.js.map +1 -1
  27. package/dist/components/ui.d.ts +37 -14
  28. package/dist/components/ui.js +28 -22
  29. package/dist/components/ui.js.map +1 -1
  30. package/dist/components/use-table-state.d.ts +1 -1
  31. package/dist/{data-table-filters-CF1PXqjQ.d.ts → data-table-filters-Dh9uF_S-.d.ts} +33 -2
  32. package/dist/data-table.d.ts +1 -1
  33. package/dist/hooks/use-overlay-history.js +84 -4
  34. package/dist/hooks/use-overlay-history.js.map +1 -1
  35. package/dist/i18n/defaults.d.ts +2 -0
  36. package/dist/i18n/defaults.js +2 -0
  37. package/dist/i18n/defaults.js.map +1 -1
  38. package/dist/i18n/kit-labels.d.ts +3 -0
  39. package/dist/i18n/kit-labels.js.map +1 -1
  40. package/dist/i18n/locales/de-CH-informal.d.ts +2 -0
  41. package/dist/i18n/locales/de-CH.d.ts +2 -0
  42. package/dist/i18n/locales/de-informal.d.ts +2 -0
  43. package/dist/i18n/locales/de.d.ts +2 -0
  44. package/dist/i18n/locales/de.js +10 -0
  45. package/dist/i18n/locales/de.js.map +1 -1
  46. package/dist/i18n/locales/es.d.ts +2 -0
  47. package/dist/i18n/locales/es.js +10 -0
  48. package/dist/i18n/locales/es.js.map +1 -1
  49. package/dist/i18n/locales/fr.d.ts +2 -0
  50. package/dist/i18n/locales/fr.js +10 -0
  51. package/dist/i18n/locales/fr.js.map +1 -1
  52. package/dist/i18n/locales/hu.d.ts +2 -0
  53. package/dist/i18n/locales/hu.js +10 -0
  54. package/dist/i18n/locales/hu.js.map +1 -1
  55. package/dist/i18n/locales/it.d.ts +2 -0
  56. package/dist/i18n/locales/it.js +10 -0
  57. package/dist/i18n/locales/it.js.map +1 -1
  58. package/dist/i18n/locales/zh.d.ts +2 -0
  59. package/dist/i18n/locales/zh.js +10 -0
  60. package/dist/i18n/locales/zh.js.map +1 -1
  61. package/dist/index.d.ts +5 -3
  62. package/dist/index.js +2 -0
  63. package/dist/index.js.map +1 -1
  64. package/dist/search/command-palette.d.ts +49 -1
  65. package/dist/search/command-palette.js +132 -29
  66. package/dist/search/command-palette.js.map +1 -1
  67. package/dist/search/global-search.d.ts +141 -0
  68. package/dist/search/global-search.js +277 -0
  69. package/dist/search/global-search.js.map +1 -0
  70. package/dist/search/search-index.d.ts +105 -0
  71. package/dist/search/search-index.js +142 -0
  72. package/dist/search/search-index.js.map +1 -0
  73. package/dist/search.d.ts +2 -0
  74. package/dist/search.js +2 -0
  75. package/dist/search.js.map +1 -1
  76. package/dist/wizard/stepper-nav.d.ts +10 -1
  77. package/dist/wizard/stepper-nav.js +2 -1
  78. package/dist/wizard/stepper-nav.js.map +1 -1
  79. package/package.json +12 -3
  80. package/src/components/chip.tsx +36 -6
  81. package/src/components/data-table.tsx +81 -5
  82. package/src/components/date-picker.tsx +328 -117
  83. package/src/components/disclosure.tsx +24 -8
  84. package/src/components/floating-panel.tsx +46 -2
  85. package/src/components/progress-bar.tsx +27 -3
  86. package/src/components/ui.tsx +88 -37
  87. package/src/hooks/use-overlay-history.ts +181 -4
  88. package/src/i18n/defaults.ts +2 -0
  89. package/src/i18n/kit-labels.tsx +2 -0
  90. package/src/i18n/locales/de.ts +10 -0
  91. package/src/i18n/locales/es.ts +10 -0
  92. package/src/i18n/locales/fr.ts +10 -0
  93. package/src/i18n/locales/hu.ts +10 -0
  94. package/src/i18n/locales/it.ts +10 -0
  95. package/src/i18n/locales/zh.ts +10 -0
  96. package/src/index.ts +4 -0
  97. package/src/search/command-palette.tsx +200 -27
  98. package/src/search/global-search.tsx +484 -0
  99. package/src/search/search-index.ts +295 -0
  100. package/src/search.ts +2 -0
  101. package/src/wizard/stepper-nav.tsx +11 -1
@@ -0,0 +1,295 @@
1
+ import type { ReactNode } from "react";
2
+
3
+ /**
4
+ * The matcher behind {@link GlobalSearch} — pure, React-free, testable on its own.
5
+ *
6
+ * WHY IT EXISTS. `CommandPalette` is deliberately domain-free: it shows whatever a
7
+ * `search(query)` provider returns. So every app wrote that provider, and every app wrote
8
+ * the same one — `label.toLowerCase().includes(q)` over its pages and actions (kastlan's
9
+ * command palette, keksdose's global search) plus a second copy with keywords for the
10
+ * settings catalogue (keksdose's `matchSettingsEntries`). A substring test answers "does
11
+ * this string contain that one", which is not what a search box is asked:
12
+ *
13
+ * - "range date" finds nothing, because the words are in the other order;
14
+ * - "Uberweisung" misses "Überweisung", and "Tooltp" misses "Tooltip";
15
+ * - "del" matches "model" as happily as "delete", and every hit ranks the same, so the
16
+ * exact page name can sit under six unrelated rows.
17
+ *
18
+ * What it does instead, per entry:
19
+ *
20
+ * - NORMALISES both sides: lower case, accents stripped (NFD minus combining marks),
21
+ * `ß` → `ss`, punctuation as a word break, and camelCase split so `DateRangePicker`
22
+ * is also the words date, range, picker.
23
+ * - Requires EVERY query word to match SOMEWHERE in the entry — title, keywords or
24
+ * description, in any order.
25
+ * - A word matches a word of the entry exactly, as its prefix, inside it (four letters or
26
+ * more, for compounds: "bereich" in "Datumsbereich"), or — for words of five letters
27
+ * or more — at edit distance one, transpositions included ("tooltp", "calender").
28
+ * - RANKS in tiers: exact title > title prefix > all words in the title > some word only
29
+ * in the keywords > some word only in the description. Within a tier, better word
30
+ * matches (exact over prefix over infix over typo) score higher, and `weight` is added
31
+ * last. The tiers are {@link SEARCH_TIER_POINTS} points apart, so a weight below that reorders within
32
+ * a tier and never across one.
33
+ * - Caps each group (`groupLimit`, default 8) after ranking, so one broad group cannot
34
+ * push every other group off the list.
35
+ */
36
+
37
+ /** One searchable thing: a page, an action, a record, a settings row. */
38
+ export interface SearchEntry {
39
+ /** Unique across the entries handed to one index. */
40
+ id: string;
41
+ /** What the row says, and what ranks highest. */
42
+ title: string;
43
+ /** Other words or phrases that should find it — synonyms, a translated name, tags.
44
+ * Matched like the title, ranked below it. */
45
+ keywords?: readonly string[];
46
+ /** Longer text, matched last and ranked lowest. Not shown in the row. */
47
+ description?: string;
48
+ /** Heading the row is grouped under in the palette. */
49
+ group?: string;
50
+ /** Secondary text on the row's trailing side (the page it lives on, an amount). */
51
+ hint?: string;
52
+ /** Leading icon. */
53
+ icon?: ReactNode;
54
+ /** Where it leads — the app's own path, handed to `GlobalSearch`'s `navigate`. */
55
+ href?: string;
56
+ /** Run instead of navigating. Wins over `href` for a plain click and ↵; `href` still
57
+ * makes the row a real link for a middle-/⌘-click. */
58
+ onSelect?: () => void;
59
+ /** Added to the score. Below {@link SEARCH_TIER_POINTS} it reorders within a tier. */
60
+ weight?: number;
61
+ /** Masks the row for session replay (see `CommandItem.redact`). */
62
+ redact?: boolean;
63
+ }
64
+
65
+ export type SearchMatchField = "title" | "keywords" | "description";
66
+
67
+ export interface SearchHit<E extends SearchEntry = SearchEntry> {
68
+ entry: E;
69
+ score: number;
70
+ /** The weakest field any query word had to be found in. */
71
+ field: SearchMatchField;
72
+ }
73
+
74
+ export interface SearchIndexOptions {
75
+ /** Hits kept per `group` after ranking. Default 8; `Infinity` for no cap. */
76
+ groupLimit?: number;
77
+ /** Hits kept in total. Default: no cap beyond `groupLimit`. */
78
+ limit?: number;
79
+ /** Shortest query word allowed an edit-distance-1 match. Default 5 — below that a
80
+ * single typo is a different word ("cat" / "car"). */
81
+ typoMinLength?: number;
82
+ /** Shortest query word matched INSIDE an entry word. Default 4. */
83
+ infixMinLength?: number;
84
+ }
85
+
86
+ export interface SearchIndex<E extends SearchEntry = SearchEntry> {
87
+ /** Ranked hits for `query`; an empty query returns none. */
88
+ search: (query: string) => SearchHit<E>[];
89
+ readonly entries: readonly E[];
90
+ }
91
+
92
+ /** Points between two ranking tiers. */
93
+ export const SEARCH_TIER_POINTS = 1000;
94
+
95
+ const TIERS = {
96
+ exact: 5,
97
+ prefix: 4,
98
+ title: 3,
99
+ keywords: 2,
100
+ description: 1,
101
+ } as const;
102
+
103
+ /** How well one query word matched one entry word — higher is better. */
104
+ const WORD_SCORE = { exact: 40, prefix: 30, infix: 15, typo: 10 } as const;
105
+ /** How much the field a word matched in is worth, multiplied with the above. */
106
+ const FIELD_FACTOR: Record<SearchMatchField, number> = { title: 3, keywords: 2, description: 1 };
107
+
108
+ /**
109
+ * Lower case, accents off, punctuation to spaces — the form both sides are compared in.
110
+ * Exported so an app can normalise its own keys the same way.
111
+ */
112
+ export function normalizeSearchText(text: string): string {
113
+ return text
114
+ .normalize("NFD")
115
+ .replace(/\p{M}+/gu, "")
116
+ .toLowerCase()
117
+ .replace(/ß/g, "ss")
118
+ .replace(/[^\p{L}\p{N}]+/gu, " ")
119
+ .trim();
120
+ }
121
+
122
+ /** The words of a text, camelCase split too: "DateRangePicker" → daterangepicker, date,
123
+ * range, picker. The joined word stays so "daterange" still prefixes it. */
124
+ function words(text: string): string[] {
125
+ const out = new Set<string>();
126
+ // Split camelCase BEFORE lower-casing, or the boundaries are gone.
127
+ const spaced = text.replace(/(\p{Ll}|\p{N})(\p{Lu})/gu, "$1 $2").replace(/(\p{Lu})(\p{Lu}\p{Ll})/gu, "$1 $2");
128
+ for (const w of normalizeSearchText(text).split(" ")) if (w) out.add(w);
129
+ for (const w of normalizeSearchText(spaced).split(" ")) if (w) out.add(w);
130
+ return [...out];
131
+ }
132
+
133
+ /** Restricted Damerau–Levenshtein distance, early-exiting once it must exceed `max`. */
134
+ function withinDistance(a: string, b: string, max: number): boolean {
135
+ if (Math.abs(a.length - b.length) > max) return false;
136
+ if (a === b) return true;
137
+ const n = a.length;
138
+ const m = b.length;
139
+ let prev2: number[] = [];
140
+ let prev = Array.from({ length: m + 1 }, (_, j) => j);
141
+ for (let i = 1; i <= n; i++) {
142
+ const cur = [i];
143
+ let rowMin = i;
144
+ for (let j = 1; j <= m; j++) {
145
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
146
+ let v = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + cost);
147
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
148
+ v = Math.min(v, prev2[j - 2] + 1);
149
+ }
150
+ cur.push(v);
151
+ if (v < rowMin) rowMin = v;
152
+ }
153
+ if (rowMin > max) return false;
154
+ prev2 = prev;
155
+ prev = cur;
156
+ }
157
+ return prev[m] <= max;
158
+ }
159
+
160
+ type WordKind = keyof typeof WORD_SCORE;
161
+
162
+ function matchWord(q: string, word: string, opts: Required<Pick<SearchIndexOptions, "typoMinLength" | "infixMinLength">>): WordKind | null {
163
+ if (word === q) return "exact";
164
+ if (word.startsWith(q)) return "prefix";
165
+ if (q.length >= opts.infixMinLength && word.includes(q)) return "infix";
166
+ if (q.length >= opts.typoMinLength) {
167
+ // The whole word, or the word's start as long as the query (a typo in a prefix:
168
+ // "calender" is one edit from "calendar", the start of "calendars").
169
+ if (withinDistance(q, word, 1)) return "typo";
170
+ if (word.length > q.length && withinDistance(q, word.slice(0, q.length), 1)) return "typo";
171
+ }
172
+ return null;
173
+ }
174
+
175
+ interface Prepared<E> {
176
+ entry: E;
177
+ title: string;
178
+ titleCompact: string;
179
+ fields: Array<[SearchMatchField, string[]]>;
180
+ }
181
+
182
+ /**
183
+ * Build an index over `entries`. Normalisation happens once here, so `search` is a scan
184
+ * over prepared words — a few thousand entries per keystroke is nothing.
185
+ */
186
+ export function createSearchIndex<E extends SearchEntry>(
187
+ entries: readonly E[],
188
+ options: SearchIndexOptions = {},
189
+ ): SearchIndex<E> {
190
+ const { groupLimit = 8, limit = Infinity, typoMinLength = 5, infixMinLength = 4 } = options;
191
+ const wordOpts = { typoMinLength, infixMinLength };
192
+ const prepared: Prepared<E>[] = entries.map((entry) => {
193
+ const title = normalizeSearchText(entry.title);
194
+ return {
195
+ entry,
196
+ title,
197
+ titleCompact: title.replace(/ /g, ""),
198
+ fields: [
199
+ ["title", words(entry.title)],
200
+ ["keywords", (entry.keywords ?? []).flatMap(words)],
201
+ ["description", entry.description ? words(entry.description) : []],
202
+ ],
203
+ };
204
+ });
205
+
206
+ const search = (query: string): SearchHit<E>[] => {
207
+ const q = normalizeSearchText(query);
208
+ if (!q) return [];
209
+ const qWords = [...new Set(q.split(" "))];
210
+ const qCompact = q.replace(/ /g, "");
211
+ const hits: SearchHit<E>[] = [];
212
+
213
+ for (const p of prepared) {
214
+ let quality = 0;
215
+ let weakest: SearchMatchField = "title";
216
+ let ok = true;
217
+ for (const qw of qWords) {
218
+ let best = 0;
219
+ let bestField: SearchMatchField | null = null;
220
+ for (const [field, fieldWords] of p.fields) {
221
+ for (const w of fieldWords) {
222
+ const kind = matchWord(qw, w, wordOpts);
223
+ if (!kind) continue;
224
+ const s = WORD_SCORE[kind] * FIELD_FACTOR[field];
225
+ if (s > best) {
226
+ best = s;
227
+ bestField = field;
228
+ }
229
+ }
230
+ }
231
+ if (!bestField) {
232
+ ok = false;
233
+ break;
234
+ }
235
+ quality += best;
236
+ if (FIELD_FACTOR[bestField] < FIELD_FACTOR[weakest]) weakest = bestField;
237
+ }
238
+ if (!ok) continue;
239
+
240
+ let tier: number = TIERS[weakest];
241
+ if (p.title === q || p.titleCompact === qCompact) tier = TIERS.exact;
242
+ else if (
243
+ p.title.startsWith(q) ||
244
+ p.titleCompact.startsWith(qCompact) ||
245
+ // The whole title, mistyped once ("Tooltp") — as good as a prefix, not exact.
246
+ (qCompact.length >= typoMinLength && withinDistance(qCompact, p.titleCompact, 1))
247
+ ) {
248
+ tier = TIERS.prefix;
249
+ }
250
+ // Within a tier: how well the words matched (≤ 120 each, averaged so a long query
251
+ // cannot climb by word count), and how much of the title the query covers — so
252
+ // "Tooltip" beats "Popovers, menus & tooltips" for the same one-word match.
253
+ const coverage = Math.min(1, qCompact.length / Math.max(1, p.titleCompact.length)) * 40;
254
+ const score =
255
+ tier * SEARCH_TIER_POINTS +
256
+ Math.min(quality / qWords.length + coverage, SEARCH_TIER_POINTS / 2 - 1) +
257
+ (p.entry.weight ?? 0);
258
+ hits.push({ entry: p.entry, score, field: weakest });
259
+ }
260
+
261
+ hits.sort((a, b) => b.score - a.score || a.entry.title.localeCompare(b.entry.title));
262
+ return capByGroup(hits, groupLimit, limit);
263
+ };
264
+
265
+ return { search, entries };
266
+ }
267
+
268
+ function capByGroup<E extends SearchEntry>(hits: SearchHit<E>[], groupLimit: number, limit: number): SearchHit<E>[] {
269
+ const perGroup = new Map<string, number>();
270
+ const out: SearchHit<E>[] = [];
271
+ for (const hit of hits) {
272
+ if (out.length >= limit) break;
273
+ const g = hit.entry.group ?? "";
274
+ const n = perGroup.get(g) ?? 0;
275
+ if (n >= groupLimit) continue;
276
+ perGroup.set(g, n + 1);
277
+ out.push(hit);
278
+ }
279
+ return out;
280
+ }
281
+
282
+ /**
283
+ * One-shot form of {@link createSearchIndex}: the matching entries, best first. For a
284
+ * list that changes on every call (a server response, a store selector); build an index
285
+ * once when the entries are stable.
286
+ */
287
+ export function matchEntries<E extends SearchEntry>(
288
+ entries: readonly E[],
289
+ query: string,
290
+ options?: SearchIndexOptions,
291
+ ): E[] {
292
+ return createSearchIndex(entries, options)
293
+ .search(query)
294
+ .map((hit) => hit.entry);
295
+ }
package/src/search.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  // `@eifi1/ui-kit/search` — the command palette / global search.
2
2
  // A re-slicing of the main barrel, not a new API.
3
3
  export * from "./search/command-palette";
4
+ export * from "./search/search-index";
5
+ export * from "./search/global-search";
@@ -42,6 +42,7 @@ export function StepperNav<TData extends Record<string, unknown>>({
42
42
  labels,
43
43
  finishVariant = "brand",
44
44
  finishDisabled = false,
45
+ doneDisabled = false,
45
46
  renderFinish,
46
47
  }: {
47
48
  wizard: UseWizardReturn<TData>;
@@ -56,6 +57,15 @@ export function StepperNav<TData extends Record<string, unknown>>({
56
57
  /** Disable Finish for a reason of the app's own, on top of the kit's gates
57
58
  * (`canFinish`, submitting, validating). */
58
59
  finishDisabled?: boolean;
60
+ /**
61
+ * Disable Done — the button of the last step after a commit — while the step still
62
+ * has work of its own running. keksdose's YNAB import ends on a recurring step whose
63
+ * convert loop writes one rule at a time (ynab-import-panel.tsx): leaving mid-loop
64
+ * abandons the rest. Until now the panel dropped `onDone` for the duration, which
65
+ * takes the button away instead of saying "not yet": disabled, it stays where the
66
+ * eye left it and comes back to life when the loop ends.
67
+ */
68
+ doneDisabled?: boolean;
59
69
  /**
60
70
  * Wrap the Finish button: receives the kit's button element — already labelled,
61
71
  * gated and wired to `wizard.finish` — and returns what to render in its place.
@@ -117,7 +127,7 @@ export function StepperNav<TData extends Record<string, unknown>>({
117
127
  // The last step after a commit. Nothing to finish; Done only when the app gave
118
128
  // an `onDone` — otherwise the step carries its own way out.
119
129
  forwardButton = wizard.canDone ? (
120
- <Button variant="brand" data-tour="wizard-done" onClick={wizard.done}>
130
+ <Button variant="brand" data-tour="wizard-done" onClick={wizard.done} disabled={doneDisabled}>
121
131
  {stepNextLabel ?? l.done ?? DEFAULT_WIZARD_LABELS.done}
122
132
  </Button>
123
133
  ) : null;