@lotics/ui 47.7.1 → 47.8.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/AGENTS.md +6 -0
- package/docs/catalog.md +31 -2
- package/docs/composition.md +20 -0
- package/package.json +3 -1
- package/src/search_match.ts +18 -0
- package/src/switcher.tsx +48 -3
- package/src/table.tsx +91 -25
- package/src/use_option_list.ts +1 -11
- package/src/use_persisted_state.ts +131 -0
package/AGENTS.md
CHANGED
|
@@ -35,6 +35,12 @@ CURRENT major only — upgrading an app across majors is `MIGRATION.md`.
|
|
|
35
35
|
nearest `SizeBoundary`; wrap one around any region whose width stops tracking its parent's.
|
|
36
36
|
`useScreenSize()` answers a question about the DEVICE.
|
|
37
37
|
→ [catalog.md §"Utility hooks & plumbing"](./docs/catalog.md).
|
|
38
|
+
- **A view the reader ARRANGED survives the reload.** Filters, grouping, sort and folded bands go
|
|
39
|
+
through `usePersistedState(key, fallback, codec)` — browser storage, per app origin, so it is a
|
|
40
|
+
per-viewer convenience and never a shared or durable fact. Its `codec.read` is a required
|
|
41
|
+
ALLOWLIST: a stored option key whose option has since been retired must restore as "off", not
|
|
42
|
+
as an empty register.
|
|
43
|
+
→ [catalog.md §"Utility hooks & plumbing"](./docs/catalog.md).
|
|
38
44
|
- **No user-facing string is a literal in `src/` when a locale slice already names it.** Every
|
|
39
45
|
string a component renders resolves prop → `useLoticsLocale()` slice → the English pack.
|
|
40
46
|
→ [composition.md §"Localization — a rendered string is WIRED"](./docs/composition.md).
|
package/docs/catalog.md
CHANGED
|
@@ -1049,7 +1049,14 @@ source (`src/<module>.tsx`/`.ts`) is the API reference.
|
|
|
1049
1049
|
entity/workspace switcher. A square `Avatar` appears on the trigger and every row as soon as
|
|
1050
1050
|
ONE item carries `imageUri`, the rest falling back to initials, so an unpictured entry still
|
|
1051
1051
|
lines up. `minItemsToRender` defaults to 2 (a single choice is clutter); lower it to 1 where
|
|
1052
|
-
the switcher states a scope the reader has no other way to see.
|
|
1052
|
+
the switcher states a scope the reader has no other way to see. It FILTERS past
|
|
1053
|
+
`SWITCHER_SEARCH_FROM` (8) items, and the THRESHOLD is the point: `searchable` is a
|
|
1054
|
+
default rather than an opt-in because the failure it prevents is silent — eight rows
|
|
1055
|
+
is a list, forty is a scroll with no way to jump, and nothing about the second says a
|
|
1056
|
+
prop was missed. Pass `false` on a list that is long but ORDERED (a year of months,
|
|
1057
|
+
where scanning beats typing), `true` on a short one whose labels are hard to tell
|
|
1058
|
+
apart. The query clears on close, so reopening never shows a list narrowed by
|
|
1059
|
+
something typed earlier and no longer on screen.
|
|
1053
1060
|
- **`count`** — `Count`: a 20px count disc that grows into a pill past one digit (`color` highlight|muted|red).
|
|
1054
1061
|
- **`shortcut_badge`** — `ShortcutBadge`: the keycap hint pill — a zinc-50 badge rendering a
|
|
1055
1062
|
shortcut from a raw string or `ShortcutDescriptor` (⌘B on Mac, Ctrl+B elsewhere); null on
|
|
@@ -1743,7 +1750,7 @@ source (`src/<module>.tsx`/`.ts`) is the API reference.
|
|
|
1743
1750
|
`TabOption.status` (a `ColorName` dot). Pressing the tab IS the filter, so the count is also the
|
|
1744
1751
|
reason to press it. → [composition.md](./composition.md) §"A partitioning control states its band's size".
|
|
1745
1752
|
- **`table`** rhythm — rows are separated by their own height and the hover wash, never by a hairline; the register's one line is the band capping the columns. 72px rows, overridable per row with `TableRow`'s `minHeight` when a surface genuinely needs density. There is no `spacing` prop: a table of VALUES rather than of objects is a different component (`Matrix`, `DataGrid`). → [composition.md](./composition.md) §"The register's rhythm".
|
|
1746
|
-
- **`table` › `TableGroup`** — a titled BAND of rows inside a `Table` (`label`, `count?`, `color?` for a single valence dot). Turns a register that presents records into one that triages them: group by what implies a different ACTION, order the bands by what needs the reader now, and let air rather than a rule separate them. → [composition.md](./composition.md) §"A register that TRIAGES".
|
|
1753
|
+
- **`table` › `TableGroup`** — a titled BAND of rows inside a `Table` (`label`, `count?`, `color?` for a single valence dot, `collapsed?` + `onToggleCollapse?`, `footer?`). **Reach for this rather than `DataGrid` whenever the rows are pressable** — both group, but a `DataGrid` row is a plain view by design (its cells hold live editors), so banding a browse-and-drill register with it silently costs the row press and nothing says so until somebody clicks. **Collapse is CONTROLLED**: `collapsed` + `onToggleCollapse` live with the control that grouped the rows, and omitting `onToggleCollapse` leaves an inert band — one that cannot fold must not wear a chevron. A folded band is only safe while it still says how much it is holding, which is what `count` is for. **`footer` is the band's subtotal**, composed from `TableCell`s so its figures land under the columns they total, and rendered only while the band is open. The band spans the register, so it aligns to the register's left edge — never indented to the first column's text. Turns a register that presents records into one that triages them: group by what implies a different ACTION, order the bands by what needs the reader now, and let air rather than a rule separate them. → [composition.md](./composition.md) §"A register that TRIAGES".
|
|
1747
1754
|
- **`sort_header`** — `SortHeader` + `SortState`/`SortDir` + `cycleSort` + `sortBy` +
|
|
1748
1755
|
`SortHeaderLabels`: the sortable column header and the sort-state helpers `Table`/
|
|
1749
1756
|
`DataGrid` consumers drive. `sortBy` copies rather than mutates, and compares strings with
|
|
@@ -2683,6 +2690,28 @@ component rather than showing it at zero.
|
|
|
2683
2690
|
usually needs both (what is being typed vs. what the results answer). `.cancel()` is for when
|
|
2684
2691
|
a trailing call has been overtaken — a picked option, a cleared box. `Combobox` searches
|
|
2685
2692
|
through it.
|
|
2693
|
+
- **`use_persisted_state`** — `usePersistedState(key, fallback, codec)` + the codecs `asText`,
|
|
2694
|
+
`oneOf`, `asStringSet`, `asSortState`: `useState` that survives a RELOAD, for the things a
|
|
2695
|
+
reader arranged and would otherwise arrange again — which filters are on, how a register is
|
|
2696
|
+
grouped, which bands are folded. Same signature as `useState`, functional updater included, so
|
|
2697
|
+
a screen adopts it by swapping the call. Storage is the browser's, per origin — and every app
|
|
2698
|
+
is served from its own origin, so what a screen stores is private to that app on that machine.
|
|
2699
|
+
**That is also the whole limit: it never reaches another viewer, the same person's other
|
|
2700
|
+
device, or the server.** A convenience belongs here; anything the work depends on belongs in a
|
|
2701
|
+
record. Every access is wrapped, so a private window or a browser that blocks site data costs
|
|
2702
|
+
the arrangement and nothing else.
|
|
2703
|
+
**The `codec.read` is REQUIRED, and that is the point** — what comes back was written by an
|
|
2704
|
+
earlier version of the page, so `JSON.parse` typed as `T` is a lie that surfaces later as a
|
|
2705
|
+
filter set to a value nothing matches. Validate against an ALLOWLIST (`oneOf`, `asSortState`)
|
|
2706
|
+
wherever the value is an option key or a column name, so a retired option restores as "off"
|
|
2707
|
+
rather than as an empty register that reads like missing data. Never persist a value the
|
|
2708
|
+
screen cannot show the reader: a filter whose chip is on screen is legible, a hidden one is a
|
|
2709
|
+
bug report.
|
|
2710
|
+
- **`search_match`** — `normalizeForSearch(s)`: fold case + diacritics for substring matching, so
|
|
2711
|
+
`cafe` finds `Café` and `da giac` finds `Đa Giác`. ONE definition, and every list that filters
|
|
2712
|
+
uses it — two rules is not a style problem: a reader who found a row by typing unaccented text
|
|
2713
|
+
in one list and finds nothing in the next reads the second as missing data. `OptionList`,
|
|
2714
|
+
`Combobox` and `Switcher` all match through it.
|
|
2686
2715
|
- **`use_async_fn`** — `useAsyncFn`: wrap an async function into a manual-trigger mutation —
|
|
2687
2716
|
`[run, {loading, data, error}]`, unmount-safe, the error lands in state AND rethrows; the
|
|
2688
2717
|
pending-state engine for a submit/download/upload action.
|
package/docs/composition.md
CHANGED
|
@@ -369,6 +369,14 @@ formatted into it, and that is the whole rule: `"Trên tàu (4)"` is a second co
|
|
|
369
369
|
screen already computes, and it is the copy that goes stale — the band re-counts on every filter
|
|
370
370
|
and the string does not.
|
|
371
371
|
|
|
372
|
+
**A grouping control CLEARS — it never offers "no grouping" as a choice.** Flat is the
|
|
373
|
+
register's resting state, not one option among N, so putting *None* / *Không nhóm* in the list
|
|
374
|
+
prices the way back at the same click as a way in and leaves the control looking engaged while it
|
|
375
|
+
is doing nothing. Give the chip a clear affordance (the ✕ every other filter already has) and let
|
|
376
|
+
its summary state the dimension only while one is picked. Same reason a grouping chip sits FIRST
|
|
377
|
+
in the band: it changes the register's shape, where the filters beside it only change its
|
|
378
|
+
contents.
|
|
379
|
+
|
|
372
380
|
It also decides where the count may live. A register whose bands already state their sizes must
|
|
373
381
|
not carry a strip of chips above it repeating the same three numbers; that is one fact twice. And
|
|
374
382
|
a count is a reason to PRESS — a control that states how many rows are behind it and does not
|
|
@@ -1567,6 +1575,18 @@ column is a sort wearing chrome: it spends a band per value to repeat the cell b
|
|
|
1567
1575
|
that is not there.
|
|
1568
1576
|
- **At most one band carries a dot.** A valence mark on every band is decoration; on the one band
|
|
1569
1577
|
whose urgency comes from a CLOCK rather than from something someone wrote down, it is a signal.
|
|
1578
|
+
- **Grouping never costs the row press.** A register whose rows open something keeps `Table` +
|
|
1579
|
+
`TableGroup`; reaching for `DataGrid` to get bands trades the press away, because its rows are
|
|
1580
|
+
plain views by design. The tell is that the screen still looks right — bands, counts and columns
|
|
1581
|
+
all render — and only a click reveals the rows are inert. Grouping is a presentation OF rows,
|
|
1582
|
+
not a different kind of screen, so it must not decide which kind you get.
|
|
1583
|
+
- **The band aligns to the REGISTER, not to a column.** It spans every column, so its heading
|
|
1584
|
+
starts on the same left edge the column headings do. Indented to where the first column's text
|
|
1585
|
+
begins it reads as a value inside that column rather than a divider across all of them, and it
|
|
1586
|
+
drifts the day that column's leading chrome changes size.
|
|
1587
|
+
- **The air between bands is a margin OUTSIDE the heading, and the heading's own padding is
|
|
1588
|
+
symmetric.** Padding the top asymmetrically to make room bakes the gap into the pressable's box,
|
|
1589
|
+
so its hover wash and focus ring hang below the words they belong to.
|
|
1570
1590
|
- **Air separates bands, a hairline separates rows.** The band boundary is the largest break
|
|
1571
1591
|
inside the register and the row rules are the smallest — drawing both as lines makes them
|
|
1572
1592
|
compete and the grouping stops reading. No rule under a band heading either; that orphans the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lotics/ui",
|
|
3
|
-
"version": "47.
|
|
3
|
+
"version": "47.8.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./accordion": "./src/accordion.tsx",
|
|
@@ -239,6 +239,7 @@
|
|
|
239
239
|
"./screen_router": "./src/screen_router.tsx",
|
|
240
240
|
"./scroll_to_bottom": "./src/scroll_to_bottom.tsx",
|
|
241
241
|
"./search_input": "./src/search_input.tsx",
|
|
242
|
+
"./search_match": "./src/search_match.ts",
|
|
242
243
|
"./section_card": "./src/section_card.tsx",
|
|
243
244
|
"./section_heading": "./src/section_heading.tsx",
|
|
244
245
|
"./section_stack": "./src/section_stack.tsx",
|
|
@@ -304,6 +305,7 @@
|
|
|
304
305
|
"./use_gated_press": "./src/use_gated_press.ts",
|
|
305
306
|
"./use_hover": "./src/use_hover.ts",
|
|
306
307
|
"./use_image_rotation": "./src/use_image_rotation.ts",
|
|
308
|
+
"./use_persisted_state": "./src/use_persisted_state.ts",
|
|
307
309
|
"./use_paste_files": {
|
|
308
310
|
"react-native": "./src/use_paste_files.ts",
|
|
309
311
|
"default": "./src/use_paste_files.web.ts"
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Fold case + diacritics for matching, so search is forgiving: `cafe` matches
|
|
2
|
+
* `Café`, `dieu` matches `Điều`, `da giac` matches `Đa Giác`. Still
|
|
3
|
+
* substring-based (predictable — no subsequence/typo noise); the typed value
|
|
4
|
+
* keeps its original accents.
|
|
5
|
+
*
|
|
6
|
+
* ONE definition, because every list in the kit that filters must agree on what
|
|
7
|
+
* a query matches. Two of them is not a style problem: a reader who found a row
|
|
8
|
+
* by typing unaccented text in one list and finds nothing in the next reads the
|
|
9
|
+
* second as missing data, and nothing on either screen says the rule differed.
|
|
10
|
+
* `Đ`/`đ` needs the explicit fold — NFD leaves it a single codepoint with no
|
|
11
|
+
* combining mark to strip, so it survives the diacritic pass unchanged. */
|
|
12
|
+
export function normalizeForSearch(s: string): string {
|
|
13
|
+
return s
|
|
14
|
+
.toLowerCase()
|
|
15
|
+
.normalize("NFD")
|
|
16
|
+
.replace(/\p{Diacritic}/gu, "")
|
|
17
|
+
.replaceAll(String.fromCharCode(0x111), "d");
|
|
18
|
+
}
|
package/src/switcher.tsx
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
|
-
import React, { useCallback, useState } from "react";
|
|
1
|
+
import React, { useCallback, useMemo, useState } from "react";
|
|
2
2
|
import { StyleSheet } from "react-native";
|
|
3
3
|
import { Avatar } from "./avatar";
|
|
4
4
|
import { Icon } from "./icon";
|
|
5
5
|
import { MenuButton } from "./menu_button";
|
|
6
6
|
import { PressableHighlight } from "./pressable_highlight";
|
|
7
7
|
import { Popover, PopoverContent, PopoverTrigger } from "./popover";
|
|
8
|
+
import { normalizeForSearch } from "./search_match";
|
|
9
|
+
import { SearchInput } from "./search_input";
|
|
10
|
+
import { useLoticsLocale } from "./locale";
|
|
8
11
|
import { Stack } from "./stack";
|
|
9
12
|
import { Text } from "./text";
|
|
10
13
|
import { colors } from "./colors";
|
|
11
14
|
import { CONTROL_HEIGHT, CONTROL_RADIUS } from "./control_surface";
|
|
12
15
|
|
|
16
|
+
/** Where a popover list stops being scannable and starts being a scroll. Eight
|
|
17
|
+
* rows is about one popover's height at the default row rhythm — past it the
|
|
18
|
+
* reader is dragging rather than reading. */
|
|
19
|
+
export const SWITCHER_SEARCH_FROM = 8;
|
|
20
|
+
|
|
13
21
|
export interface SwitcherItem {
|
|
14
22
|
id: string;
|
|
15
23
|
label: string;
|
|
@@ -36,6 +44,16 @@ interface SwitcherProps {
|
|
|
36
44
|
align?: "start" | "center" | "end";
|
|
37
45
|
/** Trigger max width in pixels. Default 180. */
|
|
38
46
|
maxTriggerWidth?: number;
|
|
47
|
+
/**
|
|
48
|
+
* Filter the list by typing. Defaults to ON past {@link SWITCHER_SEARCH_FROM}
|
|
49
|
+
* items — the threshold rather than a flat `false`, because the failure this
|
|
50
|
+
* prevents is SILENT: a switcher over eight things is a short list, a switcher
|
|
51
|
+
* over forty is a scroll with no way to jump, and nothing about the second
|
|
52
|
+
* announces that a prop was missed. Pass `false` to suppress it on a list that
|
|
53
|
+
* is long but ordered (a year of months), or `true` to force it on a short one
|
|
54
|
+
* whose labels are hard to scan.
|
|
55
|
+
*/
|
|
56
|
+
searchable?: boolean;
|
|
39
57
|
}
|
|
40
58
|
|
|
41
59
|
/**
|
|
@@ -59,12 +77,23 @@ export function Switcher(props: SwitcherProps) {
|
|
|
59
77
|
side = "bottom",
|
|
60
78
|
align = "start",
|
|
61
79
|
maxTriggerWidth = 180,
|
|
80
|
+
searchable,
|
|
62
81
|
} = props;
|
|
63
82
|
const [open, setOpen] = useState(false);
|
|
83
|
+
const [q, setQ] = useState("");
|
|
84
|
+
const locale = useLoticsLocale();
|
|
85
|
+
|
|
86
|
+
const showSearch = searchable ?? items.length >= SWITCHER_SEARCH_FROM;
|
|
87
|
+
const shown = useMemo(() => {
|
|
88
|
+
const needle = normalizeForSearch(q.trim());
|
|
89
|
+
if (!showSearch || !needle) return items;
|
|
90
|
+
return items.filter((i) => normalizeForSearch(i.label).includes(needle));
|
|
91
|
+
}, [items, q, showSearch]);
|
|
64
92
|
|
|
65
93
|
const handleSelect = useCallback(
|
|
66
94
|
(id: string) => {
|
|
67
95
|
setOpen(false);
|
|
96
|
+
setQ("");
|
|
68
97
|
if (id === currentId) return;
|
|
69
98
|
onSelect(id);
|
|
70
99
|
},
|
|
@@ -88,7 +117,9 @@ export function Switcher(props: SwitcherProps) {
|
|
|
88
117
|
) : undefined;
|
|
89
118
|
|
|
90
119
|
return (
|
|
91
|
-
|
|
120
|
+
// The query resets on close, so reopening never shows a list narrowed by
|
|
121
|
+
// something typed a minute ago and no longer on screen.
|
|
122
|
+
<Popover open={open} onOpenChange={(o) => { setOpen(o); if (!o) setQ(""); }} side={side} align={align}>
|
|
92
123
|
<PopoverTrigger>
|
|
93
124
|
<PressableHighlight focusRing style={[styles.trigger, { maxWidth: maxTriggerWidth }]}>
|
|
94
125
|
{mark(current)}
|
|
@@ -100,7 +131,20 @@ export function Switcher(props: SwitcherProps) {
|
|
|
100
131
|
</PopoverTrigger>
|
|
101
132
|
<PopoverContent>
|
|
102
133
|
<Stack style={styles.popoverBody}>
|
|
103
|
-
{
|
|
134
|
+
{showSearch ? (
|
|
135
|
+
<SearchInput
|
|
136
|
+
value={q}
|
|
137
|
+
onChangeText={setQ}
|
|
138
|
+
placeholder={locale.optionList.searchPlaceholder}
|
|
139
|
+
autoFocus
|
|
140
|
+
/>
|
|
141
|
+
) : null}
|
|
142
|
+
{showSearch && shown.length === 0 ? (
|
|
143
|
+
<Text size="sm" color="muted" style={styles.noResults}>
|
|
144
|
+
{locale.optionList.noResults}
|
|
145
|
+
</Text>
|
|
146
|
+
) : null}
|
|
147
|
+
{shown.map((item) => (
|
|
104
148
|
<MenuButton
|
|
105
149
|
key={item.id}
|
|
106
150
|
icon={mark(item)}
|
|
@@ -131,4 +175,5 @@ const styles = StyleSheet.create({
|
|
|
131
175
|
popoverBody: {
|
|
132
176
|
minWidth: 220,
|
|
133
177
|
},
|
|
178
|
+
noResults: { paddingHorizontal: 10, paddingVertical: 8 },
|
|
134
179
|
});
|
package/src/table.tsx
CHANGED
|
@@ -13,6 +13,8 @@ import {
|
|
|
13
13
|
import { Animated, StyleSheet, View, type ViewStyle } from "react-native";
|
|
14
14
|
import { Text } from "./text";
|
|
15
15
|
import { colors, solid, type ColorName } from "./colors";
|
|
16
|
+
import { Icon } from "./icon";
|
|
17
|
+
import { FocusRingPressable } from "./focus_ring_pressable";
|
|
16
18
|
import { PressableRow } from "./pressable_row";
|
|
17
19
|
import { CONTROL_HEIGHT } from "./control_surface";
|
|
18
20
|
import { PressDoor } from "./press_door";
|
|
@@ -86,6 +88,9 @@ const TableContext = createContext<TableCtx | null>(null);
|
|
|
86
88
|
*/
|
|
87
89
|
const ORDINAL_W = 28;
|
|
88
90
|
|
|
91
|
+
/** The band's leading slot — sized to the chevron, the widest thing it holds. */
|
|
92
|
+
const GROUP_MARK_W = 16;
|
|
93
|
+
|
|
89
94
|
/**
|
|
90
95
|
* The row's left chrome: its number, then whatever the caller put in `leading`.
|
|
91
96
|
*
|
|
@@ -335,6 +340,17 @@ export interface TableGroupProps {
|
|
|
335
340
|
* label alone understates (a queue that is overdue). Omit for a neutral band —
|
|
336
341
|
* most bands are neutral, and a dot on every one is decoration. */
|
|
337
342
|
color?: ColorName;
|
|
343
|
+
/** Folded away. Controlled: which bands are open is the register's own state,
|
|
344
|
+
* and it belongs beside the control that grouped them. The COUNT is what makes
|
|
345
|
+
* folding safe — a closed band still says how much it is holding. */
|
|
346
|
+
collapsed?: boolean;
|
|
347
|
+
/** Omit for an inert band — a heading with no chevron and no tab stop. A band
|
|
348
|
+
* that cannot fold must not look like it can. */
|
|
349
|
+
onToggleCollapse?: () => void;
|
|
350
|
+
/** A subtotal row under the band's rows, composed from `TableCell`s so its
|
|
351
|
+
* figures land under the columns they total. Rendered only while open: a
|
|
352
|
+
* total under a folded band restates a number the band already carries. */
|
|
353
|
+
footer?: ReactNode;
|
|
338
354
|
/** The `TableRow`s in this band. */
|
|
339
355
|
children: ReactNode;
|
|
340
356
|
}
|
|
@@ -363,36 +379,73 @@ export interface TableGroupProps {
|
|
|
363
379
|
* "which of all".
|
|
364
380
|
*/
|
|
365
381
|
export function TableGroup(props: TableGroupProps) {
|
|
366
|
-
const { label, count, color, children } = props;
|
|
382
|
+
const { label, count, color, collapsed = false, onToggleCollapse, footer, children } = props;
|
|
367
383
|
const rows = Children.toArray(children).filter(isValidElement);
|
|
368
384
|
// A band separates its OWN rows the same way the table does. This rendered
|
|
369
385
|
// hairlines unconditionally, so an airy register lost its rules between bands
|
|
370
386
|
// and kept them inside — the grouping made the register look half-converted,
|
|
371
387
|
// and the rule that survived was the one with least reason to.
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
+
// THE BAND SPANS THE WHOLE REGISTER, so it aligns to the register's own left
|
|
389
|
+
// edge — the same line the column headings start on — not to any one column's
|
|
390
|
+
// text. A heading indented to the subject column reads as a value inside that
|
|
391
|
+
// column rather than a divider across all of them, and it drifts the moment the
|
|
392
|
+
// first column's chrome changes.
|
|
393
|
+
const heading = (
|
|
394
|
+
<View style={styles.groupHeading}>
|
|
395
|
+
{/* ONE slot for the band's leading mark, whatever it holds — the chevron, the
|
|
396
|
+
valence dot, or nothing. Fixed width so the label starts at the same x on
|
|
397
|
+
every band in a register: a collapsible band beside an inert one, or a
|
|
398
|
+
coloured band beside a neutral one, would otherwise step their titles
|
|
399
|
+
apart by the width of a glyph. (The dot already carried this reasoning for
|
|
400
|
+
colour alone; the chevron made it a second axis of the same problem.) */}
|
|
401
|
+
<View style={styles.groupMark}>
|
|
402
|
+
{onToggleCollapse ? (
|
|
403
|
+
<Icon name={collapsed ? "chevron-right" : "chevron-down"} size={15} color={colors.zinc[400]} />
|
|
404
|
+
) : color ? (
|
|
405
|
+
<View style={[styles.groupDot, { backgroundColor: solid(color) }]} />
|
|
388
406
|
) : null}
|
|
389
407
|
</View>
|
|
390
|
-
<
|
|
391
|
-
{
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
408
|
+
<Text size="sm" weight="semibold">
|
|
409
|
+
{label}
|
|
410
|
+
</Text>
|
|
411
|
+
{count != null ? (
|
|
412
|
+
<Text size="sm" color="muted" tabular>
|
|
413
|
+
{count}
|
|
414
|
+
</Text>
|
|
415
|
+
) : null}
|
|
416
|
+
</View>
|
|
417
|
+
);
|
|
418
|
+
|
|
419
|
+
return (
|
|
420
|
+
<View style={styles.groupBand}>
|
|
421
|
+
{onToggleCollapse ? (
|
|
422
|
+
<FocusRingPressable
|
|
423
|
+
onPress={onToggleCollapse}
|
|
424
|
+
accessibilityRole="button"
|
|
425
|
+
// Raw, not `accessibilityState` — this RNW build drops that silently, and a
|
|
426
|
+
// band that folds without announcing it is a control a screen reader reads
|
|
427
|
+
// as a plain button.
|
|
428
|
+
aria-expanded={!collapsed}
|
|
429
|
+
accessibilityLabel={label}
|
|
430
|
+
style={({ hovered }: { hovered?: boolean }) => [
|
|
431
|
+
styles.groupPress,
|
|
432
|
+
hovered ? styles.groupPressHover : null,
|
|
433
|
+
]}
|
|
434
|
+
>
|
|
435
|
+
{heading}
|
|
436
|
+
</FocusRingPressable>
|
|
437
|
+
) : (
|
|
438
|
+
heading
|
|
439
|
+
)}
|
|
440
|
+
{collapsed ? null : (
|
|
441
|
+
<View style={styles.body}>
|
|
442
|
+
{/* The row's own key, for the reason given in the wide variant above. */}
|
|
443
|
+
{rows.map((row, i) => (
|
|
444
|
+
<View key={row.key ?? i}>{row}</View>
|
|
445
|
+
))}
|
|
446
|
+
{footer}
|
|
447
|
+
</View>
|
|
448
|
+
)}
|
|
396
449
|
</View>
|
|
397
450
|
);
|
|
398
451
|
}
|
|
@@ -774,10 +827,23 @@ const styles = StyleSheet.create({
|
|
|
774
827
|
flexDirection: "row",
|
|
775
828
|
alignItems: "center",
|
|
776
829
|
gap: 8,
|
|
777
|
-
|
|
778
|
-
|
|
830
|
+
// SYMMETRIC, so the hover wash and the focus ring sit centred on the label.
|
|
831
|
+
// The air that separates one band from the band above is `groupBand`'s margin,
|
|
832
|
+
// OUTSIDE the pressable — baked into this padding instead it made every band
|
|
833
|
+
// look bottom-heavy, because the wash is drawn on the padding box.
|
|
834
|
+
paddingVertical: 7,
|
|
779
835
|
paddingHorizontal: ROW_GUTTER,
|
|
780
836
|
},
|
|
837
|
+
/** The separation between groups. Outside the pressable — see `groupHeading`. */
|
|
838
|
+
groupBand: { marginTop: 18 },
|
|
839
|
+
groupPress: { borderRadius: 8 },
|
|
840
|
+
/** ONE slot for the band's leading mark, whatever it holds — the chevron, the
|
|
841
|
+
* valence dot, or nothing. Fixed width so the label starts at the same x on
|
|
842
|
+
* every band in a register: a collapsible band beside an inert one, or a
|
|
843
|
+
* coloured band beside a neutral one, would otherwise step their titles apart
|
|
844
|
+
* by the width of a glyph. */
|
|
845
|
+
groupMark: { width: GROUP_MARK_W, alignItems: "center", justifyContent: "center" },
|
|
846
|
+
groupPressHover: { backgroundColor: colors.zinc[50] },
|
|
781
847
|
groupDot: {
|
|
782
848
|
width: 6,
|
|
783
849
|
height: 6,
|
package/src/use_option_list.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
|
|
2
2
|
import type { ScrollView } from "react-native";
|
|
3
|
+
import { normalizeForSearch } from "./search_match";
|
|
3
4
|
import { useListKeyboardNav } from "./use_list_keyboard_nav";
|
|
4
5
|
import type { PickerOption, PickerValue, PickerOnValueChange, PickerOnClose } from "./picker";
|
|
5
6
|
|
|
@@ -98,17 +99,6 @@ export interface UseOptionList<T extends string, D> {
|
|
|
98
99
|
|
|
99
100
|
const defaultCustomLabel = (query: string) => `Add "${query}"`;
|
|
100
101
|
|
|
101
|
-
/** Fold case + diacritics for matching, so option search is forgiving: `cafe`
|
|
102
|
-
* matches `Café`, `dieu` matches `Điều`. Still substring-based (predictable —
|
|
103
|
-
* no subsequence/typo noise); the typed value keeps its original accents. */
|
|
104
|
-
function normalizeForSearch(s: string): string {
|
|
105
|
-
return s
|
|
106
|
-
.toLowerCase()
|
|
107
|
-
.normalize("NFD")
|
|
108
|
-
.replace(/\p{Diacritic}/gu, "")
|
|
109
|
-
.replaceAll(String.fromCharCode(0x111), "d");
|
|
110
|
-
}
|
|
111
|
-
|
|
112
102
|
/**
|
|
113
103
|
* The headless core of the kit's option listbox — filtering, the custom/empty
|
|
114
104
|
* rows, single/multi selection + select-all, and keyboard nav + typeahead — with
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState, type Dispatch, type SetStateAction } from "react";
|
|
2
|
+
import type { SortState } from "./sort_header";
|
|
3
|
+
|
|
4
|
+
/** How one value survives a reload. `read` is REQUIRED and not optional: what
|
|
5
|
+
* comes back out of storage was written by some earlier version of this page,
|
|
6
|
+
* in some browser, possibly by hand — `JSON.parse` typed as `T` is a lie, and
|
|
7
|
+
* the lie surfaces later as a filter set to a value nothing matches. Validate
|
|
8
|
+
* the shape and return `undefined` to fall back. `write` is only needed when
|
|
9
|
+
* `T` is not something `JSON.stringify` round-trips (a `Set`, a `Map`, a
|
|
10
|
+
* `Date`). */
|
|
11
|
+
export interface PersistedCodec<T> {
|
|
12
|
+
read: (raw: unknown) => T | undefined;
|
|
13
|
+
write?: (value: T) => unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* `useState` that REMEMBERS — for the things a reader arranged and would have to
|
|
18
|
+
* arrange again: which filters are on, how the register is grouped, which bands
|
|
19
|
+
* are folded. Same signature as `useState` (the functional updater included), so
|
|
20
|
+
* a screen adopts it by swapping the call.
|
|
21
|
+
*
|
|
22
|
+
* Storage is per browser and per origin, and every app is served from its own
|
|
23
|
+
* origin, so what a screen stores is private to that app on that machine. It
|
|
24
|
+
* never reaches another viewer, the same person's other device, or the server —
|
|
25
|
+
* which is exactly why this is for CONVENIENCE and never for anything the work
|
|
26
|
+
* depends on. A shared or durable fact belongs in a record.
|
|
27
|
+
*
|
|
28
|
+
* Every access is wrapped: storage can be absent, full, or throw on the accessor
|
|
29
|
+
* itself (a private window, a browser set to block site data, a screenshot
|
|
30
|
+
* runner). The screen must render correctly with nothing stored, which is what
|
|
31
|
+
* `fallback` is for — so a failure here costs the arrangement and nothing else.
|
|
32
|
+
*/
|
|
33
|
+
export function usePersistedState<T>(
|
|
34
|
+
key: string,
|
|
35
|
+
fallback: T,
|
|
36
|
+
codec: PersistedCodec<T>,
|
|
37
|
+
): [T, Dispatch<SetStateAction<T>>] {
|
|
38
|
+
// The codec is inevitably a fresh object literal on every render — reading it
|
|
39
|
+
// through a ref keeps it out of the write effect's dependencies, so the effect
|
|
40
|
+
// fires on a VALUE change rather than on every render.
|
|
41
|
+
const codecRef = useRef(codec);
|
|
42
|
+
codecRef.current = codec;
|
|
43
|
+
|
|
44
|
+
const [value, setValue] = useState<T>(() => {
|
|
45
|
+
const stored = readStored(key);
|
|
46
|
+
if (stored === undefined) return fallback;
|
|
47
|
+
// `undefined` is the ONLY rejection — never `??`. A codec may legitimately
|
|
48
|
+
// revive `null` (a sort the reader cleared), and `??` would quietly restore
|
|
49
|
+
// the default instead, so clearing would be the one arrangement that never
|
|
50
|
+
// survives a reload.
|
|
51
|
+
const revived = codec.read(stored);
|
|
52
|
+
return revived === undefined ? fallback : revived;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const w = codecRef.current.write;
|
|
57
|
+
writeStored(key, w ? w(value) : value);
|
|
58
|
+
}, [key, value]);
|
|
59
|
+
|
|
60
|
+
// Stable, so a screen can pass it straight to a child without re-rendering it
|
|
61
|
+
// on every keystroke elsewhere.
|
|
62
|
+
const set = useCallback<Dispatch<SetStateAction<T>>>((next) => setValue(next), []);
|
|
63
|
+
return [value, set];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function store(): Storage | null {
|
|
67
|
+
try {
|
|
68
|
+
return typeof localStorage === "undefined" ? null : localStorage;
|
|
69
|
+
} catch {
|
|
70
|
+
// Accessing the accessor throws where site data is blocked outright.
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function readStored(key: string): unknown {
|
|
76
|
+
try {
|
|
77
|
+
const raw = store()?.getItem(key);
|
|
78
|
+
return raw === null || raw === undefined ? undefined : JSON.parse(raw);
|
|
79
|
+
} catch {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function writeStored(key: string, value: unknown): void {
|
|
85
|
+
try {
|
|
86
|
+
store()?.setItem(key, JSON.stringify(value));
|
|
87
|
+
} catch {
|
|
88
|
+
// Full, or blocked. The screen already holds the value in state.
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The codec for a plain string that must be one of a KNOWN set — a select
|
|
93
|
+
* option key, a grouping dimension. An allowlist rather than a `typeof` check,
|
|
94
|
+
* because a stored key whose option was deleted filters the register down to
|
|
95
|
+
* nothing and looks like missing data. `""` is always allowed: it is how every
|
|
96
|
+
* filter in the kit spells "off". */
|
|
97
|
+
export function oneOf<T extends string>(allowed: readonly T[]): PersistedCodec<T | ""> {
|
|
98
|
+
return {
|
|
99
|
+
read: (raw) =>
|
|
100
|
+
raw === "" ? "" : typeof raw === "string" && (allowed as readonly string[]).includes(raw) ? (raw as T) : undefined,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The codec for free text a reader typed. */
|
|
105
|
+
export const asText: PersistedCodec<string> = {
|
|
106
|
+
read: (raw) => (typeof raw === "string" ? raw : undefined),
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** The codec for a `Set` of strings — stored as an array, since `JSON` has no
|
|
110
|
+
* set. Used for the folded bands of a grouped register. */
|
|
111
|
+
export const asStringSet: PersistedCodec<Set<string>> = {
|
|
112
|
+
read: (raw) => (Array.isArray(raw) && raw.every((x) => typeof x === "string") ? new Set(raw) : undefined),
|
|
113
|
+
write: (v) => [...v],
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/** The codec for a `Table`'s single-column sort. Takes the columns that are
|
|
117
|
+
* actually sortable, so a stored key from a column since renamed or removed
|
|
118
|
+
* falls back to the register's own order instead of sorting on a field the
|
|
119
|
+
* screen no longer reads. */
|
|
120
|
+
export function asSortState(keys: readonly string[]): PersistedCodec<SortState | null> {
|
|
121
|
+
return {
|
|
122
|
+
read: (raw) => {
|
|
123
|
+
if (raw === null) return null;
|
|
124
|
+
if (typeof raw !== "object") return undefined;
|
|
125
|
+
const { key, dir } = raw as { key?: unknown; dir?: unknown };
|
|
126
|
+
if (typeof key !== "string" || !keys.includes(key)) return undefined;
|
|
127
|
+
if (dir !== "asc" && dir !== "desc") return undefined;
|
|
128
|
+
return { key, dir };
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|