vultus-core 0.5.12 → 0.5.14
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 +61 -1
- package/dist/Components.d.ts +10 -0
- package/dist/Components.d.ts.map +1 -1
- package/dist/Components.js.map +1 -1
- package/dist/field.d.ts +14 -4
- package/dist/field.d.ts.map +1 -1
- package/dist/field.js +3 -2
- package/dist/field.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/kinds.d.ts +7 -0
- package/dist/kinds.d.ts.map +1 -1
- package/dist/kinds.js.map +1 -1
- package/dist/useFieldGroup.js +2 -2
- package/dist/useFieldGroup.js.map +1 -1
- package/dist/useListDetail.d.ts +92 -0
- package/dist/useListDetail.d.ts.map +1 -0
- package/dist/useListDetail.js +138 -0
- package/dist/useListDetail.js.map +1 -0
- package/dist/useManyOfField.d.ts.map +1 -1
- package/dist/useManyOfField.js +2 -2
- package/dist/useManyOfField.js.map +1 -1
- package/dist/useManyOfTreeField.d.ts.map +1 -1
- package/dist/useManyOfTreeField.js +1 -1
- package/dist/useManyOfTreeField.js.map +1 -1
- package/dist/useNumericField.d.ts.map +1 -1
- package/dist/useNumericField.js +2 -2
- package/dist/useNumericField.js.map +1 -1
- package/dist/useOneOfField.d.ts.map +1 -1
- package/dist/useOneOfField.js +2 -2
- package/dist/useOneOfField.js.map +1 -1
- package/dist/useOneOfTreeField.d.ts +2 -1
- package/dist/useOneOfTreeField.d.ts.map +1 -1
- package/dist/useOneOfTreeField.js +4 -4
- package/dist/useOneOfTreeField.js.map +1 -1
- package/dist/useRangeField.d.ts.map +1 -1
- package/dist/useRangeField.js +2 -2
- package/dist/useRangeField.js.map +1 -1
- package/dist/useStoredField.d.ts +6 -3
- package/dist/useStoredField.d.ts.map +1 -1
- package/dist/useStoredField.js +13 -3
- package/dist/useStoredField.js.map +1 -1
- package/package.json +1 -1
- package/src/Components.tsx +7 -0
- package/src/field.ts +17 -6
- package/src/index.ts +1 -0
- package/src/kinds.ts +8 -1
- package/src/useFieldGroup.ts +3 -3
- package/src/useListDetail.ts +209 -0
- package/src/useManyOfField.ts +3 -2
- package/src/useManyOfTreeField.ts +2 -1
- package/src/useNumericField.ts +3 -2
- package/src/useOneOfField.ts +3 -2
- package/src/useOneOfTreeField.ts +6 -5
- package/src/useRangeField.ts +3 -2
- package/src/useStoredField.ts +13 -4
package/src/field.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { useCallback, useState } from 'react';
|
|
2
|
+
import type { UpdateMethod } from './argStore.js';
|
|
2
3
|
import { getReason, getVerdict, invertDecision } from './decision.js';
|
|
3
4
|
import { useFieldContext } from './FieldContext.js';
|
|
4
5
|
import { resolveIn } from './resolve.js';
|
|
@@ -89,9 +90,12 @@ export interface FieldBaseOptions<TValue, Ctx = any> {
|
|
|
89
90
|
export type FieldStorage<TValue, TRequest = TValue, RouteId extends string = string, Ctx = any> =
|
|
90
91
|
/** Inside the field: its own state, starting at `initialValue` (resolved once, when the field mounts). */
|
|
91
92
|
| { initialValue?: ValueOrFn<TValue, Ctx>; value?: never; onChange?: never; commit?: never; hrefFor?: never }
|
|
92
|
-
/**
|
|
93
|
+
/**
|
|
94
|
+
* Outside, changed locally: the caller owns the value and applies `onChange`. `method` is the
|
|
95
|
+
* one `setValue` was given (see FieldControls), for a caller whose store has a history.
|
|
96
|
+
*/
|
|
93
97
|
| {
|
|
94
|
-
value: TValue; onChange: (value: TRequest) => void; initialValue?: never; commit?: never;
|
|
98
|
+
value: TValue; onChange: (value: TRequest, method?: UpdateMethod) => void; initialValue?: never; commit?: never;
|
|
95
99
|
/** The link that requesting `value` amounts to, where the caller has one (e.g. a URL store). */
|
|
96
100
|
hrefFor?: (value: TRequest) => string | undefined;
|
|
97
101
|
}
|
|
@@ -117,8 +121,14 @@ export interface FieldControls<TValue, TRequest = TValue> {
|
|
|
117
121
|
enabled: boolean;
|
|
118
122
|
whyDisabled: string | undefined;
|
|
119
123
|
value: TValue;
|
|
120
|
-
/**
|
|
121
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Request a new value. Ignored (with a warning) when disabled or while the field is pending.
|
|
126
|
+
* `method` is for a value kept where there is a history (a URL store), as the argument writes
|
|
127
|
+
* take it: `'push'` when the request is a step the person can go back from (choosing an item
|
|
128
|
+
* to open), `'replace'` otherwise; left out, the store's own rule holds (a stored field
|
|
129
|
+
* replaces). A field inside itself, or one that commits, ignores it.
|
|
130
|
+
*/
|
|
131
|
+
setValue(next: TRequest, method?: UpdateMethod): void;
|
|
122
132
|
/**
|
|
123
133
|
* A commit is running, or the field's `pending` option says the app is busy with it. Widgets
|
|
124
134
|
* lock the field meanwhile: focusable, said to be unavailable, changes ignored.
|
|
@@ -228,7 +238,7 @@ export function useFieldCore<TValue, TRequest extends TValue = TValue, RouteId e
|
|
|
228
238
|
const pending = busy || (mode === 'commit' && setter.pending);
|
|
229
239
|
const valueDescription = (opts.valueDescriptions as Record<string, string | undefined> | undefined)?.[String(value)];
|
|
230
240
|
|
|
231
|
-
const setValue = (next: TRequest) => {
|
|
241
|
+
const setValue = (next: TRequest, method?: UpdateMethod) => {
|
|
232
242
|
if (!enabled) {
|
|
233
243
|
console.warn(`Field '${opts.id}': disabled — ${whyDisabled ?? '(no reason)'}`);
|
|
234
244
|
return;
|
|
@@ -240,7 +250,8 @@ export function useFieldCore<TValue, TRequest extends TValue = TValue, RouteId e
|
|
|
240
250
|
return;
|
|
241
251
|
}
|
|
242
252
|
if (mode === 'inside') setInner(next);
|
|
243
|
-
|
|
253
|
+
// Without a method, `onChange` is called with the value alone, as it always was.
|
|
254
|
+
else if (mode === 'local') method === undefined ? opts.onChange!(next) : opts.onChange!(next, method);
|
|
244
255
|
else (setter.fire as (args: TRequest) => void)(next);
|
|
245
256
|
};
|
|
246
257
|
|
package/src/index.ts
CHANGED
|
@@ -73,6 +73,7 @@ export {
|
|
|
73
73
|
argOf, useStoredField, useStoredFields, type StoredField, type StoredFields,
|
|
74
74
|
} from './useStoredField.js';
|
|
75
75
|
export { useFieldGroup, type FieldGroup } from './useFieldGroup.js';
|
|
76
|
+
export { useListDetail, type ListDetailControls, type ListDetailOptions } from './useListDetail.js';
|
|
76
77
|
export { InternalLinkContext, useInternalLink, type InternalLinkComponent } from './LinkContext.js';
|
|
77
78
|
export { MessageSinkContext, useMessageSink, type MessageSink } from './MessageSink.js';
|
|
78
79
|
export {
|
package/src/kinds.ts
CHANGED
|
@@ -30,6 +30,13 @@ type Definition<K extends FieldKind, V, Own, Ctx> =
|
|
|
30
30
|
* `permanent`). Default false. No kind's hook reads it.
|
|
31
31
|
*/
|
|
32
32
|
permanent?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Whether a stored value that does not fit the definition (a choice or node that is not
|
|
35
|
+
* there, a number out of bounds) is worth a console warning when it is replaced. Default
|
|
36
|
+
* true: it usually means a stale link. False where it is expected (a link to an item this
|
|
37
|
+
* person does not have). No kind's hook reads it; the store layer does (useStoredField).
|
|
38
|
+
*/
|
|
39
|
+
warnStored?: boolean;
|
|
33
40
|
} & Own;
|
|
34
41
|
|
|
35
42
|
/**
|
|
@@ -55,7 +62,7 @@ type HookOptions<H extends (...args: any[]) => unknown> = Parameters<H>[0];
|
|
|
55
62
|
/**
|
|
56
63
|
* A compile-time check, the gate for "a definition is its kind's hook options as they are": if a
|
|
57
64
|
* hook's options and the definition drift apart, `tsc` fails here. It checks assignability, so a
|
|
58
|
-
* key no hook takes passes: `permanent`
|
|
65
|
+
* key no hook takes passes: `permanent` and `warnStored`, the store layer's, are the ones there are.
|
|
59
66
|
*/
|
|
60
67
|
type DefinitionsFitTheirHooks = [
|
|
61
68
|
Fits<DefinitionOf<'boolean'>, HookOptions<typeof useBoolField>>,
|
package/src/useFieldGroup.ts
CHANGED
|
@@ -84,7 +84,7 @@ function defaultUnder(
|
|
|
84
84
|
|
|
85
85
|
/** A field's writer and link, as its kind's hook takes them (the outside form's `onChange` and `hrefFor`). */
|
|
86
86
|
interface Outside {
|
|
87
|
-
onChange(value: unknown): void;
|
|
87
|
+
onChange(value: unknown, method?: UpdateMethod): void;
|
|
88
88
|
hrefFor?(value: unknown): string | undefined;
|
|
89
89
|
}
|
|
90
90
|
|
|
@@ -214,10 +214,10 @@ function useStoredValues(definitions: readonly FieldDefinition[], appCtx: FieldC
|
|
|
214
214
|
const outside = (definition: FieldDefinition, group: GroupValues): Outside => {
|
|
215
215
|
const { id } = definition;
|
|
216
216
|
const valueType = valueTypeFor(definition);
|
|
217
|
-
const onChange = (value: unknown) => {
|
|
217
|
+
const onChange = (value: unknown, method: UpdateMethod = 'replace') => {
|
|
218
218
|
const raw = rawFor(value, defaultNow(id), valueType);
|
|
219
219
|
written.set(id, raw);
|
|
220
|
-
store.write([{ key: id, raw }],
|
|
220
|
+
store.write([{ key: id, raw }], method);
|
|
221
221
|
};
|
|
222
222
|
if (store.hrefFor === undefined) return { onChange };
|
|
223
223
|
const defaultValue = group.defaultOf(id);
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import type { UpdateMethod } from './argStore.js';
|
|
3
|
+
import type { ActionOptions, ActionStatus, ErrorRoutesRegistry } from './types.js';
|
|
4
|
+
import { useAction } from './useAction.js';
|
|
5
|
+
import { useActionList } from './useActionList.js';
|
|
6
|
+
import {
|
|
7
|
+
useOneOfTreeField, type OneOfTreeFieldControls, type OneOfTreeFieldOptions, type OneOfTreeNodeControls,
|
|
8
|
+
} from './useOneOfTreeField.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A list-and-detail page's definition: a list of items, and the one of them that is open. For
|
|
12
|
+
* pages where only the detail has content (a chat's conversations, a mailbox).
|
|
13
|
+
*/
|
|
14
|
+
export interface ListDetailOptions<T extends string = string, RouteId extends string = string, Ctx = any> {
|
|
15
|
+
id: string;
|
|
16
|
+
/**
|
|
17
|
+
* The open item: a one-of tree field's options (useOneOfTreeField), its nodes the list's rows.
|
|
18
|
+
* `optional`, so `''` means none is open. Where its value is kept (inside, a URL store) is the
|
|
19
|
+
* caller's choice, as for any field.
|
|
20
|
+
*/
|
|
21
|
+
item: OneOfTreeFieldOptions<T, RouteId, Ctx>;
|
|
22
|
+
/** The list's heading. */
|
|
23
|
+
title?: string;
|
|
24
|
+
/** The actions offered with the list (a "new item"), as action definitions; a view draws one control for each. */
|
|
25
|
+
actions?: ActionOptions<void, any, RouteId>[];
|
|
26
|
+
/** Said while nothing is open. */
|
|
27
|
+
emptyMessage?: string;
|
|
28
|
+
/** An action offered while nothing is open. */
|
|
29
|
+
emptyAction?: ActionOptions<void, any, RouteId>;
|
|
30
|
+
/** Said in the list's place while it has no item. */
|
|
31
|
+
emptyListMessage?: string;
|
|
32
|
+
/** How many items' details stay kept, the most recently shown first; default 10. `false`: only the open one. */
|
|
33
|
+
keep?: number | false;
|
|
34
|
+
/**
|
|
35
|
+
* Opens an item when none is open: `'first'`, the first node that can be chosen, or a function
|
|
36
|
+
* picking among the nodes (`undefined`: none). See useListDetail for when.
|
|
37
|
+
*/
|
|
38
|
+
autoOpen?: 'first' | ((nodes: OneOfTreeNodeControls<T>[]) => T | undefined);
|
|
39
|
+
/** `'auto'` (the default) goes by the width the view reports; `'wide'` and `'narrow'` fix the layout. */
|
|
40
|
+
layout?: 'auto' | 'wide' | 'narrow';
|
|
41
|
+
/** Under this width (px) of the view's own, the layout is narrow; default 720. */
|
|
42
|
+
narrowBelow?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** What a list-and-detail view receives: everything it draws, decided here, and the setters it reports through. */
|
|
46
|
+
export interface ListDetailControls<T extends string = string> {
|
|
47
|
+
id: string;
|
|
48
|
+
title: string | undefined;
|
|
49
|
+
/** The item field: its nodes are the rows, its value the open item. A request without a method is a push. */
|
|
50
|
+
field: OneOfTreeFieldControls<T>;
|
|
51
|
+
actions: ActionStatus[];
|
|
52
|
+
/** The open item; `''` for none (also while the field's value names no node). */
|
|
53
|
+
open: T | '';
|
|
54
|
+
/** The items whose details are kept, the open one first, then the most recently shown. */
|
|
55
|
+
kept: T[];
|
|
56
|
+
emptyMessage: string | undefined;
|
|
57
|
+
emptyAction: ActionStatus | undefined;
|
|
58
|
+
emptyListMessage: string | undefined;
|
|
59
|
+
/** Side by side, or one pane at a time. */
|
|
60
|
+
layout: 'wide' | 'narrow';
|
|
61
|
+
/** Narrow: the pane that shows. Undefined wide, where both do. */
|
|
62
|
+
pane: 'list' | 'detail' | undefined;
|
|
63
|
+
/** From the open item back to the list. Invisible unless narrow with the detail showing. Labelled "Back": a view with texts of its own draws its own label. */
|
|
64
|
+
back: ActionStatus;
|
|
65
|
+
/** The pane the view should move the keyboard focus to, once; `focused()` says it did. */
|
|
66
|
+
focusTarget: 'list' | 'detail' | undefined;
|
|
67
|
+
/** The view reports its own width (px; null while unknown): the layout follows it. */
|
|
68
|
+
setWidth(width: number | null): void;
|
|
69
|
+
/** The view reports that the person chose a row (also the one already open). */
|
|
70
|
+
chosen(value: T | ''): void;
|
|
71
|
+
/** The view reports that it moved the focus to `focusTarget`. */
|
|
72
|
+
focused(): void;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const DEFAULT_KEEP = 10;
|
|
76
|
+
const DEFAULT_NARROW_BELOW = 720;
|
|
77
|
+
const NO_ACTIONS: ActionOptions<void, any, any>[] = [];
|
|
78
|
+
|
|
79
|
+
/** The first node, in pre-order, the field may choose. */
|
|
80
|
+
function firstChoosable<T extends string>(nodes: OneOfTreeNodeControls<T>[]): T | undefined {
|
|
81
|
+
for (const node of nodes) {
|
|
82
|
+
if (node.choosable) return node.value;
|
|
83
|
+
const below = firstChoosable(node.children ?? []);
|
|
84
|
+
if (below !== undefined) return below;
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The controller of a list-and-detail page (vultus-antd's ListDetailLayout draws it): every
|
|
91
|
+
* decision is made here, and the view only reports what it measures and what the person did.
|
|
92
|
+
*
|
|
93
|
+
* The open item is the `item` field's value while it names a node. A request on `field` without
|
|
94
|
+
* a method is a push (the person chose a row: Back returns to the one before); the automatic
|
|
95
|
+
* pick is a replace.
|
|
96
|
+
*
|
|
97
|
+
* Kept details: the open item and the `keep - 1` shown before it (10 in all by default), most
|
|
98
|
+
* recent first, for a view that keeps their content mounted. An item no longer among the nodes
|
|
99
|
+
* is dropped at once.
|
|
100
|
+
*
|
|
101
|
+
* Layout: narrow while the width the view reports is under `narrowBelow` (720), wide otherwise
|
|
102
|
+
* and until a width is known; `layout` fixes it. Narrow, one pane shows: the list while nothing
|
|
103
|
+
* is open, else the detail, with `back` offered, which empties the field (a push). A field that
|
|
104
|
+
* is not optional keeps its value on back and shows the list until a row is chosen.
|
|
105
|
+
*
|
|
106
|
+
* Focus: when the person chooses a row narrow, `focusTarget` is `'detail'`; after back, `'list'`.
|
|
107
|
+
* A value that changes otherwise (the URL, the automatic pick) asks for none.
|
|
108
|
+
*
|
|
109
|
+
* `autoOpen` opens an item when none is open, with a replace (no history step, so Back never
|
|
110
|
+
* lands on a state that picks again): wide, whenever none is open; narrow, on arrival (once the
|
|
111
|
+
* list has an item) and when the open item goes away (no longer among the nodes), not after the
|
|
112
|
+
* person went back. Nothing is opened while the field is disabled or pending.
|
|
113
|
+
*/
|
|
114
|
+
export function useListDetail<T extends string = string, RouteId extends string = string, Ctx = any>(
|
|
115
|
+
opts: ListDetailOptions<T, RouteId, Ctx>,
|
|
116
|
+
registry?: ErrorRoutesRegistry<RouteId>,
|
|
117
|
+
): ListDetailControls<T> {
|
|
118
|
+
const item = useOneOfTreeField<T, RouteId, Ctx>(opts.item, registry);
|
|
119
|
+
const actions = useActionList<void, any, RouteId>(opts.actions ?? NO_ACTIONS, registry);
|
|
120
|
+
const emptyActions = useActionList<void, any, RouteId>(opts.emptyAction ? [opts.emptyAction] : NO_ACTIONS, registry);
|
|
121
|
+
|
|
122
|
+
const [width, setWidth] = useState<number | null>(null);
|
|
123
|
+
const fixed = opts.layout ?? 'auto';
|
|
124
|
+
const narrowBelow = opts.narrowBelow ?? DEFAULT_NARROW_BELOW;
|
|
125
|
+
const layout: 'wide' | 'narrow' = fixed === 'auto' ? (width !== null && width < narrowBelow ? 'narrow' : 'wide') : fixed;
|
|
126
|
+
const narrow = layout === 'narrow';
|
|
127
|
+
|
|
128
|
+
const open: T | '' = item.current ? (item.value as T) : '';
|
|
129
|
+
|
|
130
|
+
/** The person went back to the list of a field that is not optional (its value stays). */
|
|
131
|
+
const [listShown, setListShown] = useState(false);
|
|
132
|
+
const [seenOpen, setSeenOpen] = useState(open);
|
|
133
|
+
if (seenOpen !== open) {
|
|
134
|
+
setSeenOpen(open);
|
|
135
|
+
if (listShown) setListShown(false);
|
|
136
|
+
}
|
|
137
|
+
const pane: 'list' | 'detail' | undefined = !narrow ? undefined : open === '' || listShown ? 'list' : 'detail';
|
|
138
|
+
|
|
139
|
+
// The kept items: the open one first, then the ones shown before, none that left the nodes.
|
|
140
|
+
const limit = opts.keep === false ? 1 : Math.max(1, opts.keep ?? DEFAULT_KEEP);
|
|
141
|
+
const [kept, setKept] = useState<T[]>(open === '' ? [] : [open]);
|
|
142
|
+
// `keep: false` keeps only the open one: with none open, none.
|
|
143
|
+
const present = opts.keep === false ? [] : kept.filter((value) => item.byValue.has(value));
|
|
144
|
+
const next = (open === '' ? present : [open, ...present.filter((value) => value !== open)]).slice(0, limit);
|
|
145
|
+
if (next.length !== kept.length || next.some((value, index) => value !== kept[index])) setKept(next);
|
|
146
|
+
|
|
147
|
+
const [focusTarget, setFocusTarget] = useState<'list' | 'detail' | undefined>(undefined);
|
|
148
|
+
const focused = useCallback(() => setFocusTarget(undefined), []);
|
|
149
|
+
const chosen = (_value: T | '') => {
|
|
150
|
+
setListShown(false);
|
|
151
|
+
if (narrow) setFocusTarget('detail');
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const back = useAction<void, void, RouteId>({
|
|
155
|
+
id: `${opts.id}.back`,
|
|
156
|
+
label: 'Back',
|
|
157
|
+
invisible: pane !== 'detail',
|
|
158
|
+
fire: () => {
|
|
159
|
+
setFocusTarget('list');
|
|
160
|
+
if (item.optional) item.clear('push');
|
|
161
|
+
else setListShown(true);
|
|
162
|
+
},
|
|
163
|
+
}, registry);
|
|
164
|
+
|
|
165
|
+
// The automatic pick (see above). `arrived`: the list has had an item; `shown`: what was open.
|
|
166
|
+
const arrived = useRef(false);
|
|
167
|
+
const shown = useRef<T | ''>('');
|
|
168
|
+
const nodesKey = Array.from(item.byValue.keys()).join('\n');
|
|
169
|
+
const canWrite = item.enabled && !item.pending;
|
|
170
|
+
const { autoOpen } = opts;
|
|
171
|
+
useEffect(() => {
|
|
172
|
+
const before = shown.current;
|
|
173
|
+
shown.current = open;
|
|
174
|
+
if (!autoOpen || item.byValue.size === 0) return;
|
|
175
|
+
const arriving = !arrived.current;
|
|
176
|
+
arrived.current = true;
|
|
177
|
+
if (open !== '' || !canWrite) return;
|
|
178
|
+
const wentAway = before !== '' && !item.byValue.has(before);
|
|
179
|
+
if (narrow && !arriving && !wentAway) return;
|
|
180
|
+
const picked = autoOpen === 'first' ? firstChoosable(item.nodes) : autoOpen(item.nodes);
|
|
181
|
+
if (picked !== undefined && item.byValue.has(picked)) item.setValue(picked, 'replace');
|
|
182
|
+
// Only when what is open, the nodes, the layout or the field's readiness changes: `item` is new on every render.
|
|
183
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
184
|
+
}, [open, nodesKey, narrow, canWrite, autoOpen]);
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
id: opts.id,
|
|
188
|
+
title: opts.title,
|
|
189
|
+
// A request without a method is the person's: a push.
|
|
190
|
+
field: {
|
|
191
|
+
...item,
|
|
192
|
+
setValue: (value: T | '', method: UpdateMethod = 'push') => item.setValue(value, method),
|
|
193
|
+
clear: (method: UpdateMethod = 'push') => item.clear(method),
|
|
194
|
+
},
|
|
195
|
+
actions,
|
|
196
|
+
open,
|
|
197
|
+
kept: next,
|
|
198
|
+
emptyMessage: opts.emptyMessage,
|
|
199
|
+
emptyAction: emptyActions[0],
|
|
200
|
+
emptyListMessage: opts.emptyListMessage,
|
|
201
|
+
layout,
|
|
202
|
+
pane,
|
|
203
|
+
back,
|
|
204
|
+
focusTarget: narrow ? focusTarget : undefined,
|
|
205
|
+
setWidth,
|
|
206
|
+
chosen,
|
|
207
|
+
focused,
|
|
208
|
+
};
|
|
209
|
+
}
|
package/src/useManyOfField.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { UpdateMethod } from './argStore.js';
|
|
1
2
|
import { useFieldCore, type FieldControls, type FieldOptions } from './field.js';
|
|
2
3
|
import { resolveChoice, type OneOfChoice, type OneOfChoiceControls } from './choices.js';
|
|
3
4
|
import { useFieldContext, type FieldCtx } from './FieldContext.js';
|
|
@@ -79,10 +80,10 @@ export function useManyOfField<T extends string = string, RouteId extends string
|
|
|
79
80
|
return [...choiceOrder.filter((v) => requestSet.has(v)), ...heldUnknown.filter((v) => requestSet.has(v))];
|
|
80
81
|
};
|
|
81
82
|
|
|
82
|
-
const setValue = (next: T[]) => {
|
|
83
|
+
const setValue = (next: T[], method?: UpdateMethod) => {
|
|
83
84
|
const normalized = normalize(next, field.value);
|
|
84
85
|
if (normalized === undefined) return;
|
|
85
|
-
field.setValue(normalized);
|
|
86
|
+
field.setValue(normalized, method);
|
|
86
87
|
};
|
|
87
88
|
|
|
88
89
|
const isSelected = (value: T): boolean => field.value.includes(value);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { UpdateMethod } from './argStore.js';
|
|
1
2
|
import { useFieldCore, type FieldControls, type FieldOptions } from './field.js';
|
|
2
3
|
import { resolveChoice } from './choices.js';
|
|
3
4
|
import { useFieldContext, type FieldCtx } from './FieldContext.js';
|
|
@@ -81,7 +82,7 @@ export function useManyOfTreeField<T extends string = string, RouteId extends st
|
|
|
81
82
|
};
|
|
82
83
|
const nodeControls = nodes.map(control);
|
|
83
84
|
|
|
84
|
-
const setValue = (next: T[]) => field.setValue(normalizeTreeValue(nodes, next, selection));
|
|
85
|
+
const setValue = (next: T[], method?: UpdateMethod) => field.setValue(normalizeTreeValue(nodes, next, selection), method);
|
|
85
86
|
|
|
86
87
|
const toggle = (v: T) => {
|
|
87
88
|
const node = byValue.get(v);
|
package/src/useNumericField.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { UpdateMethod } from './argStore.js';
|
|
1
2
|
import { useFieldCore, type FieldControls, type FieldOptions } from './field.js';
|
|
2
3
|
import { useFieldContext, type FieldCtx } from './FieldContext.js';
|
|
3
4
|
import { resolveIn } from './resolve.js';
|
|
@@ -48,12 +49,12 @@ export function useNumericField<RouteId extends string = string, Ctx = any>(
|
|
|
48
49
|
const fit = (n: number) => fitNumber(n, resolved.min, resolved.max, resolved.integer);
|
|
49
50
|
const value = fit(field.value);
|
|
50
51
|
|
|
51
|
-
const setValue = (next: number) => {
|
|
52
|
+
const setValue = (next: number, method?: UpdateMethod) => {
|
|
52
53
|
if (!Number.isFinite(next)) {
|
|
53
54
|
console.warn(`Field '${opts.id}': '${next}' is not a finite number`);
|
|
54
55
|
return;
|
|
55
56
|
}
|
|
56
|
-
field.setValue(fit(next));
|
|
57
|
+
field.setValue(fit(next), method);
|
|
57
58
|
};
|
|
58
59
|
|
|
59
60
|
return {
|
package/src/useOneOfField.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { UpdateMethod } from './argStore.js';
|
|
1
2
|
import { useFieldCore, type FieldControls, type FieldOptions } from './field.js';
|
|
2
3
|
import { resolveChoice, type OneOfChoice, type OneOfChoiceControls } from './choices.js';
|
|
3
4
|
import { useFieldContext, type FieldCtx } from './FieldContext.js';
|
|
@@ -60,7 +61,7 @@ export function useOneOfField<T extends string = string, RouteId extends string
|
|
|
60
61
|
const field = useFieldCore<T, T, RouteId, Ctx>('oneOf', inside ? { ...opts, initialValue } : opts, registry);
|
|
61
62
|
const current = choices.find((c) => c.value === field.value);
|
|
62
63
|
|
|
63
|
-
const setValue = (next: T) => {
|
|
64
|
+
const setValue = (next: T, method?: UpdateMethod) => {
|
|
64
65
|
const choice = choices.find((c) => c.value === next);
|
|
65
66
|
if (!choice) {
|
|
66
67
|
console.warn(`Field '${opts.id}': '${next}' is not one of its choices`);
|
|
@@ -70,7 +71,7 @@ export function useOneOfField<T extends string = string, RouteId extends string
|
|
|
70
71
|
console.warn(`Field '${opts.id}': choice '${next}' is disabled — ${choice.whyDisabled ?? '(no reason)'}`);
|
|
71
72
|
return;
|
|
72
73
|
}
|
|
73
|
-
field.setValue(next);
|
|
74
|
+
field.setValue(next, method);
|
|
74
75
|
};
|
|
75
76
|
|
|
76
77
|
return {
|
package/src/useOneOfTreeField.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { UpdateMethod } from './argStore.js';
|
|
1
2
|
import { useFieldCore, type FieldControls, type FieldOptions } from './field.js';
|
|
2
3
|
import { resolveChoice, type OneOfChoiceControls } from './choices.js';
|
|
3
4
|
import { useFieldContext, type FieldCtx } from './FieldContext.js';
|
|
@@ -56,7 +57,7 @@ export interface OneOfTreeFieldControls<T extends string = string> extends Field
|
|
|
56
57
|
/** The nodes a widget opens at first (from `expand`). */
|
|
57
58
|
expandedKeys: T[];
|
|
58
59
|
/** Requests ''; refused (with a warning) unless `optional`, as `setValue('')` would be. */
|
|
59
|
-
clear(): void;
|
|
60
|
+
clear(method?: UpdateMethod): void;
|
|
60
61
|
}
|
|
61
62
|
|
|
62
63
|
/**
|
|
@@ -139,13 +140,13 @@ export function useOneOfTreeField<T extends string = string, RouteId extends str
|
|
|
139
140
|
const previous = nearestChoosable(index - 1, -1);
|
|
140
141
|
const next = nearestChoosable(index + 1, 1);
|
|
141
142
|
|
|
142
|
-
const setValue = (requested: T | '') => {
|
|
143
|
+
const setValue = (requested: T | '', method?: UpdateMethod) => {
|
|
143
144
|
if (requested === '') {
|
|
144
145
|
if (!optional) {
|
|
145
146
|
console.warn(`Field '${opts.id}': '' is refused — the field is not optional`);
|
|
146
147
|
return;
|
|
147
148
|
}
|
|
148
|
-
field.setValue('');
|
|
149
|
+
field.setValue('', method);
|
|
149
150
|
return;
|
|
150
151
|
}
|
|
151
152
|
const node = byValue.get(requested);
|
|
@@ -161,7 +162,7 @@ export function useOneOfTreeField<T extends string = string, RouteId extends str
|
|
|
161
162
|
}
|
|
162
163
|
return;
|
|
163
164
|
}
|
|
164
|
-
field.setValue(requested);
|
|
165
|
+
field.setValue(requested, method);
|
|
165
166
|
};
|
|
166
167
|
|
|
167
168
|
return {
|
|
@@ -177,6 +178,6 @@ export function useOneOfTreeField<T extends string = string, RouteId extends str
|
|
|
177
178
|
optional,
|
|
178
179
|
choose,
|
|
179
180
|
expandedKeys: expandedTreeKeys(nodes, field.value ? [field.value] : [], opts.expand ?? 'selected'),
|
|
180
|
-
clear: () => setValue(''),
|
|
181
|
+
clear: (method?: UpdateMethod) => setValue('', method),
|
|
181
182
|
};
|
|
182
183
|
}
|
package/src/useRangeField.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { UpdateMethod } from './argStore.js';
|
|
1
2
|
import { useFieldCore, type FieldControls, type FieldOptions } from './field.js';
|
|
2
3
|
import { useFieldContext, type FieldCtx } from './FieldContext.js';
|
|
3
4
|
import { resolveIn } from './resolve.js';
|
|
@@ -62,12 +63,12 @@ export function useRangeField<RouteId extends string = string, Ctx = any>(
|
|
|
62
63
|
const fit = (n: number) => fitNumber(n, min, max, resolved.integer);
|
|
63
64
|
const value = order([fit(field.value[0]), fit(field.value[1])]);
|
|
64
65
|
|
|
65
|
-
const setValue = (next: NumberRange) => {
|
|
66
|
+
const setValue = (next: NumberRange, method?: UpdateMethod) => {
|
|
66
67
|
if (!Number.isFinite(next[0]) || !Number.isFinite(next[1])) {
|
|
67
68
|
console.warn(`Field '${opts.id}': '${next}' is not a finite number range`);
|
|
68
69
|
return;
|
|
69
70
|
}
|
|
70
|
-
field.setValue(order([fit(next[0]), fit(next[1])]));
|
|
71
|
+
field.setValue(order([fit(next[0]), fit(next[1])]), method);
|
|
71
72
|
};
|
|
72
73
|
|
|
73
74
|
return {
|
package/src/useStoredField.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { ArgStore, UpdateMethod } from './argStore.js';
|
|
|
3
3
|
import { useArgStore } from './ArgStoreContext.js';
|
|
4
4
|
import { useFieldContext, type FieldCtx } from './FieldContext.js';
|
|
5
5
|
import { initialValueOf, sanitizeStored, valueTypeOf, type FieldDefinition } from './kinds.js';
|
|
6
|
+
import { resolveIn } from './resolve.js';
|
|
6
7
|
import { useArg, type ArgDefinition } from './useArg.js';
|
|
7
8
|
import type { ValueType } from './valueTypes.js';
|
|
8
9
|
|
|
@@ -70,7 +71,7 @@ function showsDefault(arg: ArgDefinition<any>, stored: unknown, shown: () => unk
|
|
|
70
71
|
export interface StoredField<D extends FieldDefinition = FieldDefinition> {
|
|
71
72
|
/** The definition in its outside form, for the kind's hook (`useManyOfField(stored.options)`). */
|
|
72
73
|
options: D extends any
|
|
73
|
-
? Omit<D, 'initialValue'> & { value: any; onChange(value: any): void; hrefFor(value: any): string | undefined }
|
|
74
|
+
? Omit<D, 'initialValue'> & { value: any; onChange(value: any, method?: UpdateMethod): void; hrefFor(value: any): string | undefined }
|
|
74
75
|
: never;
|
|
75
76
|
isDefault: boolean;
|
|
76
77
|
/** Removes the field's key from the store, so it shows its default again. */
|
|
@@ -86,12 +87,15 @@ function optionsNarrowByKind(stored: StoredField<FieldDefinition>): unknown {
|
|
|
86
87
|
* A field whose value lives in the nearest ArgStore (the URL, localStorage) under its `id`,
|
|
87
88
|
* with its initial value as the default (see argOf). `options.value` is the stored value made
|
|
88
89
|
* fit for the definition as it is now (sanitizeStored: choices, nodes and bounds follow the
|
|
89
|
-
* data); `onChange` writes with `'replace'
|
|
90
|
+
* data); `onChange` writes with the method the field's `setValue` was given, `'replace'` when
|
|
91
|
+
* none was.
|
|
90
92
|
*
|
|
91
93
|
* A stored value that does not fit warns once per field and raw value: not again when only the
|
|
92
94
|
* definition or the field context is a new object (a provider given `value={{...}}` makes one
|
|
93
95
|
* on every render). With the key absent the default is the definer's, not stored input, and is
|
|
94
|
-
* sanitized silently.
|
|
96
|
+
* sanitized silently. So is any value while the field is pending (its choices or nodes may not
|
|
97
|
+
* be in yet; a value that still does not fit once it is ready warns then), and every value of a
|
|
98
|
+
* definition that says `warnStored: false` (a value that does not fit is expected there).
|
|
95
99
|
*
|
|
96
100
|
* `options.value` keeps its identity while it stays the same element by element, so a list
|
|
97
101
|
* does not become a new array on every render (effects keyed on it would re-run), even when
|
|
@@ -119,6 +123,11 @@ export function useStoredField<D extends FieldDefinition>(definition: D): Stored
|
|
|
119
123
|
|
|
120
124
|
const warnedRef = useRef<string | undefined>(undefined);
|
|
121
125
|
const sanitized = useMemo(() => {
|
|
126
|
+
// While the field is pending, what the value is judged against (choices, nodes fetched from
|
|
127
|
+
// a server) may not be in yet: nothing is said, and nothing is counted as said, so a value
|
|
128
|
+
// that still does not fit once the field is ready warns then.
|
|
129
|
+
const pending = resolveIn<boolean>(definition.pending, false, ctx, `Field '${definition.id}': option 'pending'`);
|
|
130
|
+
if (pending || definition.warnStored === false) return sanitizeStored(definition, stored, ctx, silently);
|
|
122
131
|
const input = raw === undefined ? undefined : JSON.stringify([definition.id, raw]);
|
|
123
132
|
const warn = input !== undefined && input !== warnedRef.current ? warnOnConsole : silently;
|
|
124
133
|
warnedRef.current = input;
|
|
@@ -131,7 +140,7 @@ export function useStoredField<D extends FieldDefinition>(definition: D): Stored
|
|
|
131
140
|
: sanitized;
|
|
132
141
|
previousRef.current = { value };
|
|
133
142
|
|
|
134
|
-
const onChange = useCallback((next: unknown) => set(next,
|
|
143
|
+
const onChange = useCallback((next: unknown, method: UpdateMethod = 'replace') => set(next, method), [set]);
|
|
135
144
|
const options = useMemo(() => {
|
|
136
145
|
const { initialValue: _inside, ...rest } = definition;
|
|
137
146
|
return { ...rest, value, onChange, hrefFor } as StoredField<D>['options'];
|