@charcuterie/ui 4.28.3 → 4.29.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.
- package/dist/CatalogPicker/CatalogPicker.d.ts +105 -0
- package/dist/CatalogPicker/CatalogPicker.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPicker.js +152 -0
- package/dist/CatalogPicker/CatalogPicker.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerMark.d.ts +29 -0
- package/dist/CatalogPicker/CatalogPickerMark.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerMark.js +35 -0
- package/dist/CatalogPicker/CatalogPickerMark.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerOption.d.ts +40 -0
- package/dist/CatalogPicker/CatalogPickerOption.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerOption.js +42 -0
- package/dist/CatalogPicker/CatalogPickerOption.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerPanel.d.ts +60 -0
- package/dist/CatalogPicker/CatalogPickerPanel.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerPanel.js +409 -0
- package/dist/CatalogPicker/CatalogPickerPanel.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerRail.d.ts +50 -0
- package/dist/CatalogPicker/CatalogPickerRail.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerRail.js +114 -0
- package/dist/CatalogPicker/CatalogPickerRail.js.map +1 -0
- package/dist/CatalogPicker/catalogPickerDrawings.d.ts +10 -0
- package/dist/CatalogPicker/catalogPickerDrawings.d.ts.map +1 -0
- package/dist/CatalogPicker/catalogPickerDrawings.js +29 -0
- package/dist/CatalogPicker/catalogPickerDrawings.js.map +1 -0
- package/dist/CatalogPicker/catalogPickerView.d.ts +173 -0
- package/dist/CatalogPicker/catalogPickerView.d.ts.map +1 -0
- package/dist/CatalogPicker/catalogPickerView.js +235 -0
- package/dist/CatalogPicker/catalogPickerView.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/CatalogPicker/CatalogPicker.mdx +157 -0
- package/src/CatalogPicker/CatalogPicker.stories.tsx +218 -0
- package/src/CatalogPicker/CatalogPicker.test.tsx +1352 -0
- package/src/CatalogPicker/CatalogPicker.tsx +376 -0
- package/src/CatalogPicker/CatalogPickerMark.tsx +89 -0
- package/src/CatalogPicker/CatalogPickerOption.tsx +138 -0
- package/src/CatalogPicker/CatalogPickerPanel.tsx +780 -0
- package/src/CatalogPicker/CatalogPickerRail.tsx +214 -0
- package/src/CatalogPicker/catalogPicker.storyHelpers.tsx +276 -0
- package/src/CatalogPicker/catalogPickerDrawings.tsx +51 -0
- package/src/CatalogPicker/catalogPickerView.test.ts +346 -0
- package/src/CatalogPicker/catalogPickerView.ts +457 -0
- package/src/Dialog/Dialog.test.tsx +20 -289
- package/src/Field/Field.test.tsx +11 -405
- package/src/index.ts +6 -0
- package/src/sourceRules.test.ts +5 -1
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { expect, test } from "vitest"
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
buildCatalogSections,
|
|
5
|
+
buildCatalogTabs,
|
|
6
|
+
type CatalogPickerGroup,
|
|
7
|
+
type CatalogPickerItem,
|
|
8
|
+
FAVORITES_TAB_KEY,
|
|
9
|
+
isCatalogItemMatch,
|
|
10
|
+
pickInitialTabKey,
|
|
11
|
+
resolveFavoriteItems,
|
|
12
|
+
toGroupTabKey,
|
|
13
|
+
toInitials,
|
|
14
|
+
toUniqueCatalog,
|
|
15
|
+
} from "./catalogPickerView.ts"
|
|
16
|
+
|
|
17
|
+
const GROUPS: readonly CatalogPickerGroup[] = [
|
|
18
|
+
{ id: "alpha", label: "Alpha" },
|
|
19
|
+
// `"favorites"` as a group id must not collide with the Favorites tab.
|
|
20
|
+
{ id: "favorites", label: "Beta Works" },
|
|
21
|
+
{ id: "empty", label: "Empty" },
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
const ITEMS: readonly CatalogPickerItem[] = [
|
|
25
|
+
{
|
|
26
|
+
groupId: null,
|
|
27
|
+
label: "Default",
|
|
28
|
+
pinned: "start",
|
|
29
|
+
value: "default",
|
|
30
|
+
},
|
|
31
|
+
{ groupId: "alpha", label: "One", value: "a1" },
|
|
32
|
+
{
|
|
33
|
+
description: "slug-two",
|
|
34
|
+
groupId: "alpha",
|
|
35
|
+
label: "Two",
|
|
36
|
+
value: "a2",
|
|
37
|
+
},
|
|
38
|
+
{ groupId: "favorites", label: "Three", value: "b3" },
|
|
39
|
+
{
|
|
40
|
+
groupId: "favorites",
|
|
41
|
+
isDisabled: true,
|
|
42
|
+
label: "Four",
|
|
43
|
+
value: "b4",
|
|
44
|
+
},
|
|
45
|
+
{ groupId: null, label: "Loose", value: "loose" },
|
|
46
|
+
// Pinned rows do not give a group a tab.
|
|
47
|
+
{
|
|
48
|
+
groupId: "empty",
|
|
49
|
+
label: "Last",
|
|
50
|
+
pinned: "end",
|
|
51
|
+
value: "last",
|
|
52
|
+
},
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
const tabKeys = (favorites: readonly string[]) =>
|
|
56
|
+
buildCatalogTabs({
|
|
57
|
+
favorites,
|
|
58
|
+
favoritesLabel: "Favorites",
|
|
59
|
+
groups: GROUPS,
|
|
60
|
+
items: ITEMS,
|
|
61
|
+
}).map((tab) => tab.key)
|
|
62
|
+
|
|
63
|
+
const names = (
|
|
64
|
+
sections: ReturnType<typeof buildCatalogSections>,
|
|
65
|
+
) =>
|
|
66
|
+
sections.map((section) => ({
|
|
67
|
+
heading: section.heading,
|
|
68
|
+
values: section.items.map((item) => item.value),
|
|
69
|
+
}))
|
|
70
|
+
|
|
71
|
+
test("the rail is Favorites, then each group with an unpinned row, in order", () => {
|
|
72
|
+
expect(tabKeys(["b3"])).toEqual([
|
|
73
|
+
FAVORITES_TAB_KEY,
|
|
74
|
+
toGroupTabKey("alpha"),
|
|
75
|
+
toGroupTabKey("favorites"),
|
|
76
|
+
])
|
|
77
|
+
|
|
78
|
+
expect(tabKeys([])).toEqual([
|
|
79
|
+
toGroupTabKey("alpha"),
|
|
80
|
+
toGroupTabKey("favorites"),
|
|
81
|
+
])
|
|
82
|
+
|
|
83
|
+
// A favorite that names nothing is no Favorites tab.
|
|
84
|
+
expect(tabKeys(["missing"])).toEqual([
|
|
85
|
+
toGroupTabKey("alpha"),
|
|
86
|
+
toGroupTabKey("favorites"),
|
|
87
|
+
])
|
|
88
|
+
|
|
89
|
+
// A pinned favorite is a favorite: it alone is a Favorites tab.
|
|
90
|
+
expect(tabKeys(["missing", "default"])).toEqual([
|
|
91
|
+
FAVORITES_TAB_KEY,
|
|
92
|
+
toGroupTabKey("alpha"),
|
|
93
|
+
toGroupTabKey("favorites"),
|
|
94
|
+
])
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test("favorites resolve in their own order, once each, pinned rows included", () => {
|
|
98
|
+
expect(
|
|
99
|
+
resolveFavoriteItems({
|
|
100
|
+
favorites: ["b3", "missing", "a1", "b3", "default"],
|
|
101
|
+
items: ITEMS,
|
|
102
|
+
}).map((item) => item.value),
|
|
103
|
+
).toEqual(["b3", "a1", "default"])
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test("opening picks Favorites, then the chosen group, then Favorites, then the first group", () => {
|
|
107
|
+
const tabs = buildCatalogTabs({
|
|
108
|
+
favorites: ["b3"],
|
|
109
|
+
favoritesLabel: "Favorites",
|
|
110
|
+
groups: GROUPS,
|
|
111
|
+
items: ITEMS,
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
const pick = (
|
|
115
|
+
value: string | undefined,
|
|
116
|
+
favorites: readonly string[] = ["b3"],
|
|
117
|
+
) =>
|
|
118
|
+
pickInitialTabKey({
|
|
119
|
+
favorites,
|
|
120
|
+
items: ITEMS,
|
|
121
|
+
tabs:
|
|
122
|
+
favorites.length === 0
|
|
123
|
+
? tabs.filter((tab) => tab.kind === "group")
|
|
124
|
+
: tabs,
|
|
125
|
+
value,
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
expect(pick("b3")).toBe(FAVORITES_TAB_KEY)
|
|
129
|
+
expect(pick("default")).toBe(FAVORITES_TAB_KEY)
|
|
130
|
+
expect(pick("a2")).toBe(toGroupTabKey("alpha"))
|
|
131
|
+
expect(pick("loose")).toBe(FAVORITES_TAB_KEY)
|
|
132
|
+
expect(pick(undefined)).toBe(FAVORITES_TAB_KEY)
|
|
133
|
+
expect(pick("default", [])).toBe(toGroupTabKey("alpha"))
|
|
134
|
+
expect(pick("b4", [])).toBe(toGroupTabKey("favorites"))
|
|
135
|
+
expect(pick("unknown", [])).toBe(toGroupTabKey("alpha"))
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test("browsing a tab frames its rows with the pinned rows", () => {
|
|
139
|
+
const [, alphaTab] = buildCatalogTabs({
|
|
140
|
+
favorites: ["b3", "a2"],
|
|
141
|
+
favoritesLabel: "Favorites",
|
|
142
|
+
groups: GROUPS,
|
|
143
|
+
items: ITEMS,
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
expect(
|
|
147
|
+
names(
|
|
148
|
+
buildCatalogSections({
|
|
149
|
+
favorites: ["b3", "a2"],
|
|
150
|
+
groups: GROUPS,
|
|
151
|
+
items: ITEMS,
|
|
152
|
+
query: " ",
|
|
153
|
+
tab: alphaTab,
|
|
154
|
+
}),
|
|
155
|
+
),
|
|
156
|
+
).toEqual([
|
|
157
|
+
{ heading: undefined, values: ["default"] },
|
|
158
|
+
// Ungrouped, so on every tab: never out of reach.
|
|
159
|
+
{ heading: undefined, values: ["loose"] },
|
|
160
|
+
{ heading: undefined, values: ["a1", "a2"] },
|
|
161
|
+
{ heading: undefined, values: ["last"] },
|
|
162
|
+
])
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
test("the Favorites tab lists favorites in favorites order", () => {
|
|
166
|
+
const [favoritesTab] = buildCatalogTabs({
|
|
167
|
+
favorites: ["b3", "a2"],
|
|
168
|
+
favoritesLabel: "Favorites",
|
|
169
|
+
groups: GROUPS,
|
|
170
|
+
items: ITEMS,
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
expect(
|
|
174
|
+
names(
|
|
175
|
+
buildCatalogSections({
|
|
176
|
+
favorites: ["b3", "a2"],
|
|
177
|
+
groups: GROUPS,
|
|
178
|
+
items: ITEMS,
|
|
179
|
+
query: "",
|
|
180
|
+
tab: favoritesTab,
|
|
181
|
+
}),
|
|
182
|
+
)[2],
|
|
183
|
+
).toEqual({ heading: undefined, values: ["b3", "a2"] })
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test("a pinned or ungrouped favorite is on the Favorites tab once, where every tab lists it", () => {
|
|
187
|
+
const favorites = ["default", "loose"]
|
|
188
|
+
|
|
189
|
+
const [favoritesTab] = buildCatalogTabs({
|
|
190
|
+
favorites,
|
|
191
|
+
favoritesLabel: "Favorites",
|
|
192
|
+
groups: GROUPS,
|
|
193
|
+
items: ITEMS,
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
expect(favoritesTab?.kind).toBe("favorites")
|
|
197
|
+
|
|
198
|
+
expect(
|
|
199
|
+
names(
|
|
200
|
+
buildCatalogSections({
|
|
201
|
+
favorites,
|
|
202
|
+
groups: GROUPS,
|
|
203
|
+
items: ITEMS,
|
|
204
|
+
query: "",
|
|
205
|
+
tab: favoritesTab,
|
|
206
|
+
}),
|
|
207
|
+
),
|
|
208
|
+
).toEqual([
|
|
209
|
+
{ heading: undefined, values: ["default"] },
|
|
210
|
+
{ heading: undefined, values: ["loose"] },
|
|
211
|
+
{ heading: undefined, values: ["last"] },
|
|
212
|
+
])
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
test("an ungrouped chosen row is in the opening view, and so is one whose group is unknown", () => {
|
|
216
|
+
const items: readonly CatalogPickerItem[] = [
|
|
217
|
+
...ITEMS,
|
|
218
|
+
{ groupId: "gone", label: "Stray", value: "stray" },
|
|
219
|
+
]
|
|
220
|
+
|
|
221
|
+
const tabs = buildCatalogTabs({
|
|
222
|
+
favorites: [],
|
|
223
|
+
favoritesLabel: "Favorites",
|
|
224
|
+
groups: GROUPS,
|
|
225
|
+
items,
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
for (const value of ["loose", "stray"]) {
|
|
229
|
+
const tabKey = pickInitialTabKey({
|
|
230
|
+
favorites: [],
|
|
231
|
+
items,
|
|
232
|
+
tabs,
|
|
233
|
+
value,
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
const values = buildCatalogSections({
|
|
237
|
+
favorites: [],
|
|
238
|
+
groups: GROUPS,
|
|
239
|
+
items,
|
|
240
|
+
query: "",
|
|
241
|
+
tab: tabs.find((tab) => tab.key === tabKey),
|
|
242
|
+
}).flatMap((section) =>
|
|
243
|
+
section.items.map((item) => item.value),
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
expect(values).toContain(value)
|
|
247
|
+
expect(
|
|
248
|
+
values.filter((shown) => shown === value),
|
|
249
|
+
).toHaveLength(1)
|
|
250
|
+
}
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
test("a repeated item value or group id is dropped, and the first one wins", () => {
|
|
254
|
+
const unique = toUniqueCatalog({
|
|
255
|
+
groups: [
|
|
256
|
+
...GROUPS,
|
|
257
|
+
{ id: "alpha", label: "Alpha again" },
|
|
258
|
+
],
|
|
259
|
+
items: [
|
|
260
|
+
...ITEMS,
|
|
261
|
+
{
|
|
262
|
+
groupId: "favorites",
|
|
263
|
+
label: "One again",
|
|
264
|
+
value: "a1",
|
|
265
|
+
},
|
|
266
|
+
],
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
expect(unique.duplicateGroupIds).toEqual(["alpha"])
|
|
270
|
+
expect(unique.duplicateItemValues).toEqual(["a1"])
|
|
271
|
+
expect(unique.groups).toEqual(GROUPS)
|
|
272
|
+
expect(unique.items).toEqual(ITEMS)
|
|
273
|
+
|
|
274
|
+
// A clean catalog comes back as the same arrays, so memos hold.
|
|
275
|
+
const clean = toUniqueCatalog({
|
|
276
|
+
groups: GROUPS,
|
|
277
|
+
items: ITEMS,
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
expect(clean.groups).toBe(GROUPS)
|
|
281
|
+
expect(clean.items).toBe(ITEMS)
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
test("an empty rail browses every unpinned row", () => {
|
|
285
|
+
expect(
|
|
286
|
+
names(
|
|
287
|
+
buildCatalogSections({
|
|
288
|
+
favorites: [],
|
|
289
|
+
groups: [],
|
|
290
|
+
items: ITEMS,
|
|
291
|
+
query: "",
|
|
292
|
+
tab: undefined,
|
|
293
|
+
}),
|
|
294
|
+
)[1]?.values,
|
|
295
|
+
).toEqual(["a1", "a2", "b3", "b4", "loose"])
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
test("a search crosses every group, headed in rail order, pinned rows outside", () => {
|
|
299
|
+
expect(
|
|
300
|
+
names(
|
|
301
|
+
buildCatalogSections({
|
|
302
|
+
favorites: ["b3"],
|
|
303
|
+
groups: GROUPS,
|
|
304
|
+
items: ITEMS,
|
|
305
|
+
query: "o",
|
|
306
|
+
tab: undefined,
|
|
307
|
+
}),
|
|
308
|
+
),
|
|
309
|
+
).toEqual([
|
|
310
|
+
{ heading: undefined, values: ["loose"] },
|
|
311
|
+
{ heading: "Alpha", values: ["a1", "a2"] },
|
|
312
|
+
// "Three" matches on its group's label, "Beta Works".
|
|
313
|
+
{ heading: "Beta Works", values: ["b3", "b4"] },
|
|
314
|
+
])
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
test("every word must match the label, description or group label, in any case", () => {
|
|
318
|
+
const match = (query: string, item: CatalogPickerItem) =>
|
|
319
|
+
isCatalogItemMatch({
|
|
320
|
+
groupLabel: "Contoso (Work)",
|
|
321
|
+
item,
|
|
322
|
+
words: query
|
|
323
|
+
.toLowerCase()
|
|
324
|
+
.split(/\s+/u)
|
|
325
|
+
.filter(Boolean),
|
|
326
|
+
})
|
|
327
|
+
|
|
328
|
+
const item: CatalogPickerItem = {
|
|
329
|
+
description: "ct-lumen-4",
|
|
330
|
+
groupId: "contoso-work",
|
|
331
|
+
label: "Lumen 4",
|
|
332
|
+
value: "x",
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
expect(match("LUMEN", item)).toBe(true)
|
|
336
|
+
expect(match("work lumen", item)).toBe(true)
|
|
337
|
+
expect(match("ct-lum", item)).toBe(true)
|
|
338
|
+
expect(match("lumen prism", item)).toBe(false)
|
|
339
|
+
})
|
|
340
|
+
|
|
341
|
+
test("initials are the first letter or digit of the first two words", () => {
|
|
342
|
+
expect(toInitials("Northwind")).toBe("N")
|
|
343
|
+
expect(toInitials("tailspin labs inc")).toBe("TL")
|
|
344
|
+
expect(toInitials(" Contoso (Work) ")).toBe("CW")
|
|
345
|
+
expect(toInitials("4 Seasons")).toBe("4S")
|
|
346
|
+
})
|