@tristan2828/ui-foundation 4.86.0 → 4.88.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/README.md +1 -1
- package/conventions/docs/add-an-entity.md +52 -23
- package/conventions/docs/cell-patterns.md +49 -24
- package/conventions/docs/design-language.md +30 -3
- package/dist/components/app/app-shell-nav.d.ts +24 -0
- package/dist/components/app/app-shell-nav.js +30 -0
- package/dist/components/app/app-shell.d.ts +12 -1
- package/dist/components/app/app-shell.js +19 -5
- package/dist/components/app/choice-buttons.d.ts +25 -0
- package/dist/components/app/choice-buttons.js +17 -0
- package/dist/components/app/date-field.d.ts +28 -9
- package/dist/components/app/date-field.js +103 -10
- package/dist/components/app/edit-in-place-controls.d.ts +1 -3
- package/dist/components/app/edit-in-place-controls.js +4 -58
- package/dist/components/app/edit-in-place-presets.d.ts +38 -4
- package/dist/components/app/edit-in-place-presets.js +48 -27
- package/dist/components/app/edit-row.d.ts +7 -0
- package/dist/components/app/edit-row.js +23 -0
- package/dist/components/app/editable-value.d.ts +8 -1
- package/dist/components/app/editable-value.js +36 -4
- package/dist/components/app/entity-view.d.ts +11 -1
- package/dist/components/app/entity-view.js +19 -2
- package/dist/components/app/multi-pick.d.ts +8 -1
- package/dist/components/app/multi-pick.js +19 -5
- package/dist/components/app/multi-reference.d.ts +8 -1
- package/dist/components/app/multi-reference.js +44 -6
- package/dist/components/app/page-trail-context.d.ts +18 -0
- package/dist/components/app/page-trail-context.js +28 -0
- package/dist/components/app/page-trail.d.ts +1 -1
- package/dist/components/app/page-trail.js +1 -1
- package/dist/components/app/reference-combobox.d.ts +37 -0
- package/dist/components/app/reference-combobox.js +96 -0
- package/dist/components/app/search-palette.d.ts +29 -0
- package/dist/components/app/search-palette.js +160 -0
- package/dist/components/ui/field.d.ts +2 -2
- package/dist/components/ui/field.js +2 -1
- package/dist/hooks/use-recent-record.d.ts +10 -0
- package/dist/hooks/use-recent-record.js +20 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +6 -1
- package/dist/lib/names-record.d.ts +4 -0
- package/dist/lib/names-record.js +7 -0
- package/dist/lib/plain-date.d.ts +20 -0
- package/dist/lib/plain-date.js +72 -0
- package/dist/lib/search-palette.d.ts +50 -0
- package/dist/lib/search-palette.js +119 -0
- package/package.json +1 -1
|
@@ -92,14 +92,24 @@ import { PrintNotes } from './entity-view-print.js';
|
|
|
92
92
|
import { EditInPlaceContext } from './edit-in-place.js';
|
|
93
93
|
import { EditInPlaceStore } from './edit-in-place-store.js';
|
|
94
94
|
import { EditableList } from './editable-list.js';
|
|
95
|
+
import { editRowClass, onEditRowClick } from './edit-row.js';
|
|
95
96
|
import { EditableValue, LeaveGuard } from './editable-value.js';
|
|
96
97
|
import { EmptyText } from './empty-value.js';
|
|
97
98
|
import { EMPTY_DASH, isEmptyValue } from '../../lib/empty-value.js';
|
|
98
99
|
import { useRecordDialog } from './record-dialog-context.js';
|
|
100
|
+
import { PageTrail } from './page-trail.js';
|
|
101
|
+
import { usePageTrail } from './page-trail-context.js';
|
|
99
102
|
import { entityViewTabSlug } from '../../lib/entity-view-tab.js';
|
|
100
103
|
// The view's empty text and whether fields cards fold their empty ones,
|
|
101
104
|
// for every section without threading them through each layout.
|
|
102
105
|
const EmptyContext = createContext({ text: EMPTY_DASH, fold: false });
|
|
106
|
+
// The record's trail (#R2): the crumbs above it, then its own title
|
|
107
|
+
// once it has loaded; the list's crumb is the last until then.
|
|
108
|
+
function recordTrail(back, title) {
|
|
109
|
+
if (!back.trail)
|
|
110
|
+
return null;
|
|
111
|
+
return title ? [...back.trail, { label: title }] : [...back.trail];
|
|
112
|
+
}
|
|
103
113
|
function BackLink({ back }) {
|
|
104
114
|
return (_jsxs(Link, { to: back.to, className: "inline-flex w-fit items-center gap-1 rounded-sm type-label text-muted-foreground outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50", children: [_jsx(ArrowLeftIcon, { "aria-hidden": "true", className: "size-4" }), back.label] }));
|
|
105
115
|
}
|
|
@@ -124,7 +134,10 @@ const FIELD_ROW = {
|
|
|
124
134
|
};
|
|
125
135
|
function FieldRow({ field, place }) {
|
|
126
136
|
const value = fieldValue(field);
|
|
127
|
-
|
|
137
|
+
// An editable row is the target as a whole (4.88, F15/F18): the list's
|
|
138
|
+
// row hover, its pencil at the end, a chevron on a phone.
|
|
139
|
+
const editable = Boolean(field.edit);
|
|
140
|
+
return (_jsxs("div", { className: cn('flex flex-col gap-1 py-3 first:pt-0 last:pb-0', FIELD_ROW[place], editable && cn(editRowClass, '-mx-2 px-2 first:pt-2 last:pb-2')), onClick: editable ? onEditRowClick : undefined, children: [_jsx("dt", { className: "type-label text-muted-foreground", children: field.label }), _jsx("dd", { className: "min-w-0 type-body break-words text-foreground", children: field.edit ? (_jsx(EditableValue, { label: field.label, edit: field.edit, look: "row", children: _jsx(OrEmpty, { value: value, emptyLabel: field.emptyLabel }) })) : (_jsx(OrEmpty, { value: value, emptyLabel: field.emptyLabel })) })] }));
|
|
128
141
|
}
|
|
129
142
|
// A fields card's rows. Folded (`emptyFields="fold"`), the filled fields
|
|
130
143
|
// show and the empty ones wait behind "N empty", which opens them in their
|
|
@@ -248,6 +261,10 @@ export function EntityView({ title, documentTitle, titleEdit, leading, subtitle,
|
|
|
248
261
|
const dialog = useRecordDialog();
|
|
249
262
|
const editing = useSyncExternalStore(store.subscribe, () => store.editing, () => store.editing);
|
|
250
263
|
useDocumentTitle(isLoading || error || documentTitle === false ? undefined : (documentTitle ?? title));
|
|
264
|
+
const trail = recordTrail(back, isLoading || error ? undefined : (title ?? (documentTitle || undefined)));
|
|
265
|
+
// In the shell, the trail goes up in its bar; a dialog leaves the bar
|
|
266
|
+
// to the list under it.
|
|
267
|
+
const inShell = usePageTrail(dialog ? null : trail);
|
|
251
268
|
if (error?.kind === 'notfound') {
|
|
252
269
|
// A record that doesn't exist and one that belongs to another user are
|
|
253
270
|
// the same plain 404, so this never says which. Retrying can't help.
|
|
@@ -281,7 +298,7 @@ export function EntityView({ title, documentTitle, titleEdit, leading, subtitle,
|
|
|
281
298
|
const main = (_jsxs("div", { className: cn(RAIL_LAYOUT.main, railSections.length === 0 && RAIL_LAYOUT.mainAlone), children: [mainSections
|
|
282
299
|
.filter((section) => !tabbed(section))
|
|
283
300
|
.map((section) => (_jsx(ViewSection, { section: section }, section.title))), tabList] }));
|
|
284
|
-
return (_jsx(EditInPlaceContext.Provider, { value: store, children: _jsx(EmptyContext.Provider, { value: { text: emptyText, fold: emptyFields === 'fold' }, children: _jsxs("div", { className: layout === 'rail' ? RAIL_LAYOUT.page : 'flex max-w-4xl flex-col gap-6', "data-state": "success", "data-layout": layout, children: [editing && _jsx(LeaveGuard, { store: store }), !dialog && _jsx(BackLink, { back: back }), _jsxs("header", { className: "flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between", children: [_jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-3 sm:min-w-64 sm:gap-4", children: [leading && (_jsx("div", { "data-slot": "entity-view-leading", className: "flex shrink-0 items-center", children: leading })), _jsxs("div", { className: "flex min-w-0 flex-1 flex-col gap-2", children: [_jsxs("div", { "data-slot": "entity-view-title-line", className: "flex min-w-0 flex-wrap items-center gap-x-3 gap-y-2", children: [_jsx("h1", { id: dialog?.titleId, className: "min-w-0 max-w-full type-page-title break-words text-foreground", children: titleEdit ? (_jsx(EditableValue, { label: titleEdit.label, edit: titleEdit, layout: "inline", className: "w-full", children: title })) : (title) }), badges && (_jsx("div", { "data-slot": "entity-view-badges", className: "flex flex-wrap items-center gap-1.5", children: badges }))] }), subtitle && (_jsx("div", { "data-slot": "entity-view-subtitle", className: "type-body break-words text-muted-foreground", children: subtitle }))] })] }), (actions || printButton) && (_jsxs("div", { className: "flex min-w-0 flex-wrap items-center gap-2 sm:justify-end", children: [printButton, actions] }))] }), _jsx("div", { role: "status", "data-slot": "entity-view-notice", className: "empty:hidden", children: notice }), strip && _jsx("div", { "data-slot": "entity-view-strip", children: strip }), layout === 'rail' ? (_jsxs("div", { className: RAIL_LAYOUT.columns, children: [mobileOrder === 'main-first' && main, railSections.length > 0 && (_jsx(Rail, { label: railLabel ?? `${title ?? 'Record'} details`, sections: railSections, fields: railFields })), mobileOrder === 'rail-first' && main] })) : (_jsxs(_Fragment, { children: [sections
|
|
301
|
+
return (_jsx(EditInPlaceContext.Provider, { value: store, children: _jsx(EmptyContext.Provider, { value: { text: emptyText, fold: emptyFields === 'fold' }, children: _jsxs("div", { className: layout === 'rail' ? RAIL_LAYOUT.page : 'flex max-w-4xl flex-col gap-6', "data-state": "success", "data-layout": layout, children: [editing && _jsx(LeaveGuard, { store: store }), !dialog && !trail && _jsx(BackLink, { back: back }), !dialog && trail && !inShell && _jsx(PageTrail, { crumbs: trail }), _jsxs("header", { className: "flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between", children: [_jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-3 sm:min-w-64 sm:gap-4", children: [leading && (_jsx("div", { "data-slot": "entity-view-leading", className: "flex shrink-0 items-center", children: leading })), _jsxs("div", { className: "flex min-w-0 flex-1 flex-col gap-2", children: [_jsxs("div", { "data-slot": "entity-view-title-line", className: "flex min-w-0 flex-wrap items-center gap-x-3 gap-y-2", children: [_jsx("h1", { id: dialog?.titleId, className: "min-w-0 max-w-full type-page-title break-words text-foreground", children: titleEdit ? (_jsx(EditableValue, { label: titleEdit.label, edit: titleEdit, layout: "inline", className: "w-full", children: title })) : (title) }), badges && (_jsx("div", { "data-slot": "entity-view-badges", className: "flex flex-wrap items-center gap-1.5", children: badges }))] }), subtitle && (_jsx("div", { "data-slot": "entity-view-subtitle", className: "type-body break-words text-muted-foreground", children: subtitle }))] })] }), (actions || printButton) && (_jsxs("div", { className: "flex min-w-0 flex-wrap items-center gap-2 sm:justify-end", children: [printButton, actions] }))] }), _jsx("div", { role: "status", "data-slot": "entity-view-notice", className: "empty:hidden", children: notice }), strip && _jsx("div", { "data-slot": "entity-view-strip", children: strip }), layout === 'rail' ? (_jsxs("div", { className: RAIL_LAYOUT.columns, children: [mobileOrder === 'main-first' && main, railSections.length > 0 && (_jsx(Rail, { label: railLabel ?? `${title ?? 'Record'} details`, sections: railSections, fields: railFields })), mobileOrder === 'rail-first' && main] })) : (_jsxs(_Fragment, { children: [sections
|
|
285
302
|
.filter((section) => !tabbed(section))
|
|
286
303
|
.map((section) => (_jsx(ViewSection, { section: section }, section.title))), tabList] })), footer && (_jsx("footer", { "data-slot": "entity-view-footer", className: "type-caption text-muted-foreground", children: footer }))] }) }) }));
|
|
287
304
|
}
|
|
@@ -36,8 +36,15 @@ export type MultiPickProps<Id extends string | number> = {
|
|
|
36
36
|
placeholder?: string;
|
|
37
37
|
/** Shown when there are no options at all. */
|
|
38
38
|
emptyText?: string;
|
|
39
|
+
/**
|
|
40
|
+
* How many options show before "Show all N": past it, the picked ones
|
|
41
|
+
* and the first `limit`. Defaults to 30. A filter always searches them all.
|
|
42
|
+
*/
|
|
43
|
+
limit?: number;
|
|
44
|
+
/** The options are still loading (the app's query of its records): "Loading…" in place of the chips. */
|
|
45
|
+
loading?: boolean;
|
|
39
46
|
/** Shown, not changeable. */
|
|
40
47
|
readOnly?: boolean;
|
|
41
48
|
className?: string;
|
|
42
49
|
};
|
|
43
|
-
export declare function MultiPick<Id extends string | number>({ options, value, onValueChange, onPick, 'aria-label': label, 'aria-describedby': describedBy, id, placeholder, emptyText, readOnly, className, }: MultiPickProps<Id>): import("react").JSX.Element;
|
|
50
|
+
export declare function MultiPick<Id extends string | number>({ options, value, onValueChange, onPick, 'aria-label': label, 'aria-describedby': describedBy, id, placeholder, emptyText, limit, loading, readOnly, className, }: MultiPickProps<Id>): import("react").JSX.Element;
|
|
@@ -22,18 +22,26 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
22
22
|
// - Keyboard: the filter box first; Down goes to the chips, Left/Right
|
|
23
23
|
// (Home/End) move along them, Up goes back to the box, and Space or
|
|
24
24
|
// Enter toggles a chip.
|
|
25
|
+
// - Every multi-pick (4.88, the form standard's F13): the chips wrap at
|
|
26
|
+
// every width, under the filter box. Past `limit` options (30) it shows
|
|
27
|
+
// the picked ones and the first 30, then "Show all N"; a filter searches
|
|
28
|
+
// them all. The options can be the app's list of records (its query's
|
|
29
|
+
// rows as `{ id, label }`, `loading` while it loads), so a link to
|
|
30
|
+
// several other records uses it too.
|
|
25
31
|
import { CheckIcon } from 'lucide-react';
|
|
26
32
|
import { useEffect, useId, useRef, useState } from 'react';
|
|
27
33
|
import { toast } from 'sonner';
|
|
28
34
|
import { cn } from 'cn';
|
|
35
|
+
import { Button } from '../ui/button.js';
|
|
29
36
|
import { FieldError } from '../ui/field.js';
|
|
30
37
|
import { Input } from '../ui/input.js';
|
|
31
38
|
import { Spinner } from '../ui/spinner.js';
|
|
32
39
|
import { ToggleGroup, ToggleGroupItem } from '../ui/toggle-group.js';
|
|
33
40
|
import { messageOf } from './edit-in-place-store.js';
|
|
34
|
-
export function MultiPick({ options, value, onValueChange, onPick, 'aria-label': label, 'aria-describedby': describedBy, id, placeholder = 'Filter', emptyText = 'No options.', readOnly = false, className, }) {
|
|
41
|
+
export function MultiPick({ options, value, onValueChange, onPick, 'aria-label': label, 'aria-describedby': describedBy, id, placeholder = 'Filter', emptyText = 'No options.', limit = 30, loading = false, readOnly = false, className, }) {
|
|
35
42
|
const key = useId();
|
|
36
43
|
const [filter, setFilter] = useState('');
|
|
44
|
+
const [showAll, setShowAll] = useState(false);
|
|
37
45
|
const [pending, setPending] = useState(() => new Map());
|
|
38
46
|
const [refused, setRefused] = useState(() => new Map());
|
|
39
47
|
const filterBox = useRef(null);
|
|
@@ -91,7 +99,11 @@ export function MultiPick({ options, value, onValueChange, onPick, 'aria-label':
|
|
|
91
99
|
});
|
|
92
100
|
};
|
|
93
101
|
const needle = filter.trim().toLowerCase();
|
|
94
|
-
const
|
|
102
|
+
const matching = needle ? options.filter((option) => option.label.toLowerCase().includes(needle)) : options;
|
|
103
|
+
// A long set: the first `limit`, and any picked one past them, in the options' order.
|
|
104
|
+
const capped = !needle && !showAll && options.length > limit;
|
|
105
|
+
const visible = capped ? matching.filter((option, index) => index < limit || isPicked(option.id)) : matching;
|
|
106
|
+
const hidden = options.length - visible.length;
|
|
95
107
|
// Down from the box: into the chips, at the one the group would tab to.
|
|
96
108
|
const onFilterKeyDown = (event) => {
|
|
97
109
|
if (event.key !== 'ArrowDown')
|
|
@@ -110,11 +122,13 @@ export function MultiPick({ options, value, onValueChange, onPick, 'aria-label':
|
|
|
110
122
|
filterBox.current?.focus();
|
|
111
123
|
};
|
|
112
124
|
const errorId = (option) => `${key}-refused-${String(option)}`;
|
|
113
|
-
return (_jsxs("div", { "data-slot": "multi-pick", className: cn('flex w-full min-w-0 flex-col gap-2', className), children: [_jsx(Input, { ref: filterBox, id: id, type: "search", value: filter, onChange: (event) => setFilter(event.target.value), onKeyDown: onFilterKeyDown, placeholder: placeholder, "aria-label": `Filter ${label}`, "aria-describedby": describedBy, autoComplete: "off", readOnly: readOnly, className: "max-w-xs" }), visible.length === 0 ? (_jsx("p", { className: "type-caption text-muted-foreground", children: options.length === 0 ? emptyText : 'No matches.' })) : (_jsx(ToggleGroup, { ref: chips, multiple: true, variant: "outline", size: "sm", spacing: 1.5, value: shown.map(String), disabled: readOnly, onKeyDown: onChipsKeyDown, "aria-label": label, "aria-describedby": describedBy,
|
|
125
|
+
return (_jsxs("div", { "data-slot": "multi-pick", className: cn('flex w-full min-w-0 flex-col gap-2', className), children: [_jsx(Input, { ref: filterBox, id: id, type: "search", value: filter, onChange: (event) => setFilter(event.target.value), onKeyDown: onFilterKeyDown, placeholder: placeholder, "aria-label": `Filter ${label}`, "aria-describedby": describedBy, autoComplete: "off", readOnly: readOnly, className: "max-w-xs" }), loading ? (_jsx("p", { role: "status", className: "type-caption text-muted-foreground", children: "Loading\u2026" })) : visible.length === 0 ? (_jsx("p", { className: "type-caption text-muted-foreground", children: options.length === 0 ? emptyText : 'No matches.' })) : (_jsx(ToggleGroup, { ref: chips, multiple: true, variant: "outline", size: "sm", spacing: 1.5, value: shown.map(String), disabled: readOnly, onKeyDown: onChipsKeyDown, "aria-label": label, "aria-describedby": describedBy,
|
|
126
|
+
// Wraps at every width; a chip longer than the row ends in "…".
|
|
127
|
+
className: "w-full max-w-full min-w-0 flex-wrap", children: visible.map((option) => {
|
|
114
128
|
const picked = isPicked(option.id);
|
|
115
129
|
const saving = (pending.get(option.id)?.inFlight ?? 0) > 0;
|
|
116
|
-
return (_jsxs(ToggleGroupItem, { value: String(option.id), onPressedChange: (next) => toggle(option, next), "aria-busy": saving || undefined, "aria-describedby": refused.has(option.id) ? errorId(option.id) : undefined, children: [option.icon && (_jsx("span", { "aria-hidden": "true", className: "inline-flex shrink-0", children: option.icon })), option.label, _jsx("span", { "aria-hidden": "true", className: "inline-flex size-3.5 shrink-0 items-center justify-center", children: saving ? (_jsx(Spinner, { className: "size-3.5 text-muted-foreground" })) : (picked && _jsx(CheckIcon, { className: "size-3.5" })) })] }, option.id));
|
|
117
|
-
}) })), refused.size > 0 && (_jsx(FieldError, { className: "type-caption flex flex-col gap-0.5", children: [...refused].map(([option, reason]) => (_jsx("span", { id: errorId(option), children: reason }, String(option)))) }))] }));
|
|
130
|
+
return (_jsxs(ToggleGroupItem, { value: String(option.id), onPressedChange: (next) => toggle(option, next), "aria-busy": saving || undefined, "aria-describedby": refused.has(option.id) ? errorId(option.id) : undefined, title: option.label, className: "max-w-full min-w-0", children: [option.icon && (_jsx("span", { "aria-hidden": "true", className: "inline-flex shrink-0", children: option.icon })), _jsx("span", { className: "min-w-0 truncate", children: option.label }), _jsx("span", { "aria-hidden": "true", className: "inline-flex size-3.5 shrink-0 items-center justify-center", children: saving ? (_jsx(Spinner, { className: "size-3.5 text-muted-foreground" })) : (picked && _jsx(CheckIcon, { className: "size-3.5" })) })] }, option.id));
|
|
131
|
+
}) })), !loading && capped && hidden > 0 && (_jsxs(Button, { type: "button", variant: "link", size: "sm", className: "h-auto w-fit p-0", "aria-label": `Show all ${options.length} ${label}`, onClick: () => setShowAll(true), children: ["Show all ", options.length] })), refused.size > 0 && (_jsx(FieldError, { className: "type-caption flex flex-col gap-0.5", children: [...refused].map(([option, reason]) => (_jsx("span", { id: errorId(option), children: reason }, String(option)))) }))] }));
|
|
118
132
|
}
|
|
119
133
|
function without(map, key) {
|
|
120
134
|
if (!map.has(key))
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
|
|
2
|
+
import type { ReferenceRecord } from './reference-combobox.js';
|
|
3
|
+
export declare function MultiReference<Id extends string | number>({ options, value, onValueChange, getLabel, onSearchChange, id, placeholder, emptyText, 'aria-label': ariaLabel, 'aria-invalid': ariaInvalid, 'aria-describedby': ariaDescribedBy, readOnly, searching, onCreate, className, }: {
|
|
3
4
|
/** The records the current search returned, in display order. */
|
|
4
5
|
options: readonly {
|
|
5
6
|
id: Id;
|
|
@@ -35,5 +36,11 @@ export declare function MultiReference<Id extends string | number>({ options, va
|
|
|
35
36
|
* still picks. The list is `aria-busy` meanwhile.
|
|
36
37
|
*/
|
|
37
38
|
searching?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Offers Create "<typed>" when what's typed names no option: the app
|
|
41
|
+
* opens its small create form with the name and resolves with the saved
|
|
42
|
+
* record (added to the picks), or `null` when it was cancelled.
|
|
43
|
+
*/
|
|
44
|
+
onCreate?: (name: string) => Promise<ReferenceRecord<Id> | null>;
|
|
38
45
|
className?: string;
|
|
39
46
|
}): React.JSX.Element;
|
|
@@ -17,17 +17,47 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
17
17
|
// results, its first still highlighted. `searching` says so: Enter
|
|
18
18
|
// waits rather than picking from the old list (where it could even
|
|
19
19
|
// remove a pick: the highlighted item may be one already picked).
|
|
20
|
+
// - `onCreate` (4.88, F14): Create "<typed>" as the list's last item when
|
|
21
|
+
// what's typed names no option; the app's create form resolves with the
|
|
22
|
+
// new record, which joins the picks (its name kept here until the app's
|
|
23
|
+
// lookup has it).
|
|
20
24
|
import * as React from 'react';
|
|
21
25
|
import { Combobox, ComboboxChip, ComboboxChips, ComboboxChipsInput, ComboboxContent, ComboboxEmpty, ComboboxItem, ComboboxList, ComboboxValue, useComboboxAnchor, } from '../ui/combobox.js';
|
|
26
|
+
import { namesRecord } from '../../lib/names-record.js';
|
|
22
27
|
/** Shown on a chip whose name hasn't loaded yet, instead of a bare id. */
|
|
23
28
|
const PENDING_LABEL = '…';
|
|
24
|
-
|
|
29
|
+
/** The Create "<typed>" item. */
|
|
30
|
+
const CREATE = '__create__';
|
|
31
|
+
export function MultiReference({ options, value, onValueChange, getLabel, onSearchChange, id, placeholder, emptyText = 'No matches.', 'aria-label': ariaLabel, 'aria-invalid': ariaInvalid, 'aria-describedby': ariaDescribedBy, readOnly = false, searching = false, onCreate, className, }) {
|
|
25
32
|
const anchor = useComboboxAnchor();
|
|
26
|
-
const
|
|
33
|
+
const [typed, setTyped] = React.useState('');
|
|
34
|
+
const [created] = React.useState(() => new Map());
|
|
35
|
+
const [creating, setCreating] = React.useState(false);
|
|
36
|
+
const label = (option) => getLabel(option) ?? created.get(option) ?? PENDING_LABEL;
|
|
37
|
+
const name = typed.trim();
|
|
38
|
+
const offerCreate = Boolean(onCreate) && name !== '' && !searching && !namesRecord(options, name);
|
|
39
|
+
const items = [...options.map((option) => option.id), ...(offerCreate ? [CREATE] : [])];
|
|
40
|
+
const latest = React.useRef(value);
|
|
41
|
+
React.useEffect(() => {
|
|
42
|
+
latest.current = value;
|
|
43
|
+
});
|
|
44
|
+
const create = () => {
|
|
45
|
+
if (!onCreate)
|
|
46
|
+
return;
|
|
47
|
+
setCreating(true);
|
|
48
|
+
onCreate(name).then((record) => {
|
|
49
|
+
setCreating(false);
|
|
50
|
+
if (!record)
|
|
51
|
+
return;
|
|
52
|
+
created.set(record.id, record.label);
|
|
53
|
+
if (!latest.current.includes(record.id))
|
|
54
|
+
onValueChange([...latest.current, record.id]);
|
|
55
|
+
}, () => setCreating(false));
|
|
56
|
+
};
|
|
27
57
|
return (_jsxs(Combobox, { multiple: true, autoHighlight: true,
|
|
28
58
|
// Ids, not {id,label} objects: Base UI compares items to the value
|
|
29
59
|
// with Object.is, so the items must have the value's own shape.
|
|
30
|
-
items:
|
|
60
|
+
items: items, value: value, onValueChange: (next, details) => {
|
|
31
61
|
// Base UI empties the value on Escape while the list is closed, so
|
|
32
62
|
// a second Escape after a pick wiped every chip (issue #123). Escape
|
|
33
63
|
// never removes picks here: a chip's remove button, Backspace or
|
|
@@ -38,11 +68,19 @@ export function MultiReference({ options, value, onValueChange, getLabel, onSear
|
|
|
38
68
|
details.allowPropagation();
|
|
39
69
|
return;
|
|
40
70
|
}
|
|
41
|
-
|
|
42
|
-
|
|
71
|
+
const picks = next;
|
|
72
|
+
if (picks.includes(CREATE)) {
|
|
73
|
+
create();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
onValueChange(picks);
|
|
77
|
+
}, onInputValueChange: (text) => {
|
|
78
|
+
setTyped(text);
|
|
79
|
+
onSearchChange(text);
|
|
80
|
+
}, itemToStringLabel: label, filter: null, readOnly: readOnly || creating, children: [_jsx(ComboboxChips, { ref: anchor, className: className, children: _jsx(ComboboxValue, { children: (selected) => (_jsxs(React.Fragment, { children: [selected.map((option) => (_jsx(ComboboxChip, { children: label(option) }, option))), _jsx(ComboboxChipsInput, { id: id, onKeyDown: (event) => {
|
|
43
81
|
if (searching && event.key === 'Enter') {
|
|
44
82
|
event.preventDefault();
|
|
45
83
|
event.preventBaseUIHandler();
|
|
46
84
|
}
|
|
47
|
-
}, "aria-label": ariaLabel, "aria-invalid": ariaInvalid, "aria-describedby": ariaDescribedBy, placeholder: selected.length === 0 ? placeholder : undefined })] })) }) }), _jsxs(ComboboxContent, { anchor: anchor, children: [_jsx(ComboboxEmpty, { children: emptyText }), _jsx(ComboboxList, { "aria-busy": searching || undefined, children: (option) => (_jsx(ComboboxItem, { value: option, children: label(option) }, option)) })] })] }));
|
|
85
|
+
}, "aria-label": ariaLabel, "aria-invalid": ariaInvalid, "aria-describedby": ariaDescribedBy, "aria-busy": creating || undefined, "data-edit-hold": creating ? '' : undefined, placeholder: selected.length === 0 ? placeholder : undefined })] })) }) }), _jsxs(ComboboxContent, { anchor: anchor, children: [_jsx(ComboboxEmpty, { children: emptyText }), _jsx(ComboboxList, { "aria-busy": searching || undefined, children: (option) => (_jsx(ComboboxItem, { value: option, "data-create": option === CREATE ? '' : undefined, children: option === CREATE ? `Create "${name}"` : label(option) }, option)) })] })] }));
|
|
48
86
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PageCrumb } from './page-trail.js';
|
|
2
|
+
export type PageTrailSlot = {
|
|
3
|
+
/** Shows these crumbs in the bar, for the page holding `owner`. */
|
|
4
|
+
set: (owner: object, crumbs: readonly PageCrumb[]) => void;
|
|
5
|
+
/** Takes them down again, unless a later page has put up its own. */
|
|
6
|
+
clear: (owner: object) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare const PageTrailContext: import("react").Context<PageTrailSlot | null>;
|
|
9
|
+
/**
|
|
10
|
+
* Puts this page's trail in AppShell's bar: from the top (a module, say)
|
|
11
|
+
* down to the page itself, last, every crumb but the last with an `href`.
|
|
12
|
+
* Nothing (`undefined` or `null`) leaves the bar to the nav entry's
|
|
13
|
+
* label. Returns whether there is a bar to show it, so a view outside
|
|
14
|
+
* the shell can draw the trail itself.
|
|
15
|
+
*
|
|
16
|
+
* usePageTrail([{ label: 'Finance', href: '/finance' }, { label: 'Accounts', href: '/finance/accounts' }, { label: 'Update balances' }])
|
|
17
|
+
*/
|
|
18
|
+
export declare function usePageTrail(crumbs: readonly PageCrumb[] | null | undefined): boolean;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// The trail in AppShell's bar (#N5): a page names where it is with
|
|
2
|
+
// `usePageTrail`, and the shell draws it between the menu button and the
|
|
3
|
+
// search box. Pages that set none get their nav entry's (navTrail).
|
|
4
|
+
import { createContext, useContext, useLayoutEffect } from 'react';
|
|
5
|
+
export const PageTrailContext = createContext(null);
|
|
6
|
+
/**
|
|
7
|
+
* Puts this page's trail in AppShell's bar: from the top (a module, say)
|
|
8
|
+
* down to the page itself, last, every crumb but the last with an `href`.
|
|
9
|
+
* Nothing (`undefined` or `null`) leaves the bar to the nav entry's
|
|
10
|
+
* label. Returns whether there is a bar to show it, so a view outside
|
|
11
|
+
* the shell can draw the trail itself.
|
|
12
|
+
*
|
|
13
|
+
* usePageTrail([{ label: 'Finance', href: '/finance' }, { label: 'Accounts', href: '/finance/accounts' }, { label: 'Update balances' }])
|
|
14
|
+
*/
|
|
15
|
+
export function usePageTrail(crumbs) {
|
|
16
|
+
const slot = useContext(PageTrailContext);
|
|
17
|
+
// By value, so a new array each render doesn't put the trail up again.
|
|
18
|
+
const key = crumbs ? JSON.stringify(crumbs.map((crumb) => [crumb.label, crumb.href ?? null])) : null;
|
|
19
|
+
useLayoutEffect(() => {
|
|
20
|
+
if (!slot || key === null)
|
|
21
|
+
return;
|
|
22
|
+
const owner = {};
|
|
23
|
+
const parsed = JSON.parse(key);
|
|
24
|
+
slot.set(owner, parsed.map(([label, href]) => (href === null ? { label } : { label, href })));
|
|
25
|
+
return () => slot.clear(owner);
|
|
26
|
+
}, [slot, key]);
|
|
27
|
+
return slot !== null;
|
|
28
|
+
}
|
|
@@ -9,7 +9,7 @@ export type PageTrailProps = {
|
|
|
9
9
|
* From the top (the record, or the list it's in) down to the page
|
|
10
10
|
* itself, last. Any number: one is just the page.
|
|
11
11
|
*/
|
|
12
|
-
crumbs: PageCrumb[];
|
|
12
|
+
crumbs: readonly PageCrumb[];
|
|
13
13
|
/** The trail's name for screen readers. Defaults to "Breadcrumb". */
|
|
14
14
|
'aria-label'?: string;
|
|
15
15
|
className?: string;
|
|
@@ -63,7 +63,7 @@ export function PageTrail({ crumbs, 'aria-label': ariaLabel, className }) {
|
|
|
63
63
|
return;
|
|
64
64
|
}
|
|
65
65
|
if (index === last)
|
|
66
|
-
items.push(_jsx(BreadcrumbItem, { className: "min-w-0", children: _jsx(BreadcrumbPage, { className: "block truncate", title: crumb.label, children: crumb.label }) }, "page"));
|
|
66
|
+
items.push(_jsx(BreadcrumbItem, { className: "min-w-0", children: _jsx(BreadcrumbPage, { className: "block truncate", title: crumb.label, role: undefined, "aria-disabled": undefined, children: crumb.label }) }, "page"));
|
|
67
67
|
else
|
|
68
68
|
items.push(_jsx(BreadcrumbItem, { className: index === 0 ? 'shrink-0' : 'min-w-0', children: _jsx(CrumbLink, { crumb: crumb, inRouter: inRouter }) }, `crumb-${index}`));
|
|
69
69
|
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** A record found by search: its id and its name. */
|
|
2
|
+
export type ReferenceRecord<Id> = {
|
|
3
|
+
id: Id;
|
|
4
|
+
label: string;
|
|
5
|
+
};
|
|
6
|
+
export type ReferenceComboboxProps<Id extends string | number> = {
|
|
7
|
+
/** The box's id, for the field's `<FieldLabel htmlFor>`. */
|
|
8
|
+
id?: string;
|
|
9
|
+
/** The picked record's id, or `null`. */
|
|
10
|
+
value: Id | null;
|
|
11
|
+
onValueChange: (value: Id | null) => void;
|
|
12
|
+
/** Finds records by what's typed. Rejects to show nothing found. */
|
|
13
|
+
search: (text: string) => Promise<readonly ReferenceRecord<Id>[]>;
|
|
14
|
+
/** The picked record's name (the app's lookup by id), shown in the box. */
|
|
15
|
+
label?: string;
|
|
16
|
+
/** No "Not set" item: a record must be picked. */
|
|
17
|
+
required?: boolean;
|
|
18
|
+
/** The "Not set" item's words, for an optional reference. */
|
|
19
|
+
emptyLabel?: string;
|
|
20
|
+
placeholder?: string;
|
|
21
|
+
/** What the list says when nothing matches. Defaults to "No matches." */
|
|
22
|
+
emptyText?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Offers Create "<typed>" when what's typed matches no record's name.
|
|
25
|
+
* Opens the app's create form with the name; resolves with the saved
|
|
26
|
+
* record (then picked), or `null` when the form was cancelled.
|
|
27
|
+
*/
|
|
28
|
+
onCreate?: (name: string) => Promise<ReferenceRecord<Id> | null>;
|
|
29
|
+
readOnly?: boolean;
|
|
30
|
+
/** Open the list on mount (editing in place, where it opens with the field). */
|
|
31
|
+
defaultOpen?: boolean;
|
|
32
|
+
invalid?: boolean;
|
|
33
|
+
'aria-label'?: string;
|
|
34
|
+
'aria-labelledby'?: string;
|
|
35
|
+
'aria-describedby'?: string;
|
|
36
|
+
};
|
|
37
|
+
export declare function ReferenceCombobox<Id extends string | number>({ id, value, onValueChange, search, label, required, emptyLabel, placeholder, emptyText, onCreate, readOnly, defaultOpen, invalid, ...aria }: ReferenceComboboxProps<Id>): import("react").JSX.Element;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// Another record, picked from a list searched as you type: the one control
|
|
3
|
+
// for a single reference, on a form and edited in place (`inPlace.reference`
|
|
4
|
+
// draws it). The package knows no endpoint: `search` is the app's (its
|
|
5
|
+
// gateway's search by name), called with what's typed (`''` when the
|
|
6
|
+
// list opens). Names come from the searches and from the saved record's
|
|
7
|
+
// `label`, so a pick the current search no longer returns still shows its
|
|
8
|
+
// name.
|
|
9
|
+
//
|
|
10
|
+
// `onCreate` (4.88, the form standard's F14): when what's typed matches no
|
|
11
|
+
// record's name, Create "<typed>" is the list's last item. Picking it
|
|
12
|
+
// calls the app's `onCreate(name)`, which opens its small create form (a
|
|
13
|
+
// FormDialog) with the name filled in and resolves with the new record
|
|
14
|
+
// once it's saved: that record is then picked. While that form is open,
|
|
15
|
+
// an edit in place holding this control stays open (`data-edit-hold`).
|
|
16
|
+
import { useEffect, useRef, useState } from 'react';
|
|
17
|
+
import { namesRecord } from '../../lib/names-record.js';
|
|
18
|
+
import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList } from '../ui/combobox.js';
|
|
19
|
+
/** A select's and a combobox's items are strings: a record's id, or this for "Not set". */
|
|
20
|
+
const NONE = '__none__';
|
|
21
|
+
/** The Create "<typed>" item. */
|
|
22
|
+
const CREATE = '__create__';
|
|
23
|
+
// How long typing pauses before the search runs.
|
|
24
|
+
const SEARCH_DELAY_MS = 200;
|
|
25
|
+
export function ReferenceCombobox({ id, value, onValueChange, search, label, required = false, emptyLabel = 'Not set', placeholder = 'Search', emptyText = 'No matches.', onCreate, readOnly = false, defaultOpen = false, invalid = false, ...aria }) {
|
|
26
|
+
const [text, setText] = useState('');
|
|
27
|
+
const [found, setFound] = useState([]);
|
|
28
|
+
const [creating, setCreating] = useState(false);
|
|
29
|
+
const [names] = useState(() => new Map());
|
|
30
|
+
if (value != null && label !== undefined && !names.has(String(value)))
|
|
31
|
+
names.set(String(value), { id: value, label });
|
|
32
|
+
for (const record of found)
|
|
33
|
+
names.set(String(record.id), record);
|
|
34
|
+
// The search is the app's, often a new function each render: the
|
|
35
|
+
// latest is called, and only what's typed starts a search.
|
|
36
|
+
const latestSearch = useRef(search);
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
latestSearch.current = search;
|
|
39
|
+
});
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
let live = true;
|
|
42
|
+
const timer = setTimeout(() => {
|
|
43
|
+
latestSearch.current(text).then((records) => {
|
|
44
|
+
if (live)
|
|
45
|
+
setFound(records);
|
|
46
|
+
}, () => {
|
|
47
|
+
if (live)
|
|
48
|
+
setFound([]);
|
|
49
|
+
});
|
|
50
|
+
}, text === '' ? 0 : SEARCH_DELAY_MS);
|
|
51
|
+
return () => {
|
|
52
|
+
live = false;
|
|
53
|
+
clearTimeout(timer);
|
|
54
|
+
};
|
|
55
|
+
}, [text]);
|
|
56
|
+
const mounted = useRef(true);
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
mounted.current = true;
|
|
59
|
+
return () => {
|
|
60
|
+
mounted.current = false;
|
|
61
|
+
};
|
|
62
|
+
}, []);
|
|
63
|
+
const typed = text.trim();
|
|
64
|
+
const offerCreate = Boolean(onCreate) && typed !== '' && !namesRecord(found, typed);
|
|
65
|
+
const picked = value == null ? null : String(value);
|
|
66
|
+
const items = [...(required ? [] : [NONE]), ...found.map((record) => String(record.id)), ...(offerCreate ? [CREATE] : [])];
|
|
67
|
+
const nameOf = (key) => (key === NONE || key === CREATE ? '' : (names.get(key)?.label ?? ''));
|
|
68
|
+
const create = (name) => {
|
|
69
|
+
if (!onCreate)
|
|
70
|
+
return;
|
|
71
|
+
setCreating(true);
|
|
72
|
+
onCreate(name).then((record) => {
|
|
73
|
+
if (mounted.current)
|
|
74
|
+
setCreating(false);
|
|
75
|
+
if (!record)
|
|
76
|
+
return;
|
|
77
|
+
names.set(String(record.id), record);
|
|
78
|
+
onValueChange(record.id);
|
|
79
|
+
}, () => {
|
|
80
|
+
if (mounted.current)
|
|
81
|
+
setCreating(false);
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
return (_jsxs(Combobox, { items: items, value: picked, onValueChange: (key) => {
|
|
85
|
+
if (key == null)
|
|
86
|
+
return;
|
|
87
|
+
if (key === CREATE) {
|
|
88
|
+
create(typed);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
onValueChange(key === NONE ? null : (names.get(key)?.id ?? null));
|
|
92
|
+
},
|
|
93
|
+
// The box showing the pick's own name is no search: the list then
|
|
94
|
+
// offers everything, not only the one already picked.
|
|
95
|
+
onInputValueChange: (next) => setText(picked !== null && next === nameOf(picked) ? '' : next), itemToStringLabel: nameOf, filter: null, readOnly: readOnly || creating, defaultOpen: defaultOpen, children: [_jsx(ComboboxInput, { id: id, placeholder: placeholder, "aria-invalid": invalid || undefined, "aria-busy": creating || undefined, "data-edit-hold": creating ? '' : undefined, ...aria }), _jsxs(ComboboxContent, { children: [_jsx(ComboboxEmpty, { children: emptyText }), _jsx(ComboboxList, { children: items.map((key) => (_jsx(ComboboxItem, { value: key, "data-create": key === CREATE ? '' : undefined, children: key === NONE ? emptyLabel : key === CREATE ? `Create "${typed}"` : nameOf(key) }, key))) })] })] }));
|
|
96
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { NavEntry } from './app-shell-nav.js';
|
|
2
|
+
import { type SearchRecord } from '../../lib/search-palette.js';
|
|
3
|
+
export type { RecentRecord, SearchRecord } from '../../lib/search-palette.js';
|
|
4
|
+
/** AppShell's `search`: what the box says and how it finds records. */
|
|
5
|
+
export type AppShellSearch = {
|
|
6
|
+
/** The box's words and the palette's field's name. Defaults to "Search". */
|
|
7
|
+
placeholder?: string;
|
|
8
|
+
/**
|
|
9
|
+
* Finds the app's records whose title has the text in it, from the
|
|
10
|
+
* app's own search endpoint through its gateway (TanStack Query's
|
|
11
|
+
* `queryClient.fetchQuery`, say). Called once typing pauses, never for
|
|
12
|
+
* text of only spaces; `signal` aborts when the text changes again.
|
|
13
|
+
* The results are listed under their `kind`, in the order given.
|
|
14
|
+
* Without it the palette finds pages only.
|
|
15
|
+
*/
|
|
16
|
+
findRecords?: (query: string, options: {
|
|
17
|
+
signal: AbortSignal;
|
|
18
|
+
}) => Promise<readonly SearchRecord[]>;
|
|
19
|
+
/**
|
|
20
|
+
* How many of the records opened lately (useRecentRecord) to list
|
|
21
|
+
* before you type: 5 by default, `false` for none.
|
|
22
|
+
*/
|
|
23
|
+
recent?: number | false;
|
|
24
|
+
};
|
|
25
|
+
/** The box (md and up) and the button (a phone) that open the palette, and the palette. */
|
|
26
|
+
export declare function SearchPalette({ search, nav }: {
|
|
27
|
+
search: AppShellSearch;
|
|
28
|
+
nav: readonly NavEntry[];
|
|
29
|
+
}): import("react").JSX.Element;
|