@marktiderman/genesis-ui-native 3.3.0 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data/index.d.ts +42 -5
- package/dist/data/index.js +207 -17
- package/dist/data/index.js.map +1 -1
- package/package.json +3 -3
- package/src/data/NativeResourceFormField.tsx +12 -3
- package/src/data/resource-view/NativeLinkPicker.tsx +191 -0
- package/src/data/resource-view/NativeResourceFormSheet.tsx +25 -1
- package/src/data/resource-view/__tests__/native-link-picker.test.tsx +201 -0
- package/src/data/resource-view/__tests__/native-tel-field.test.tsx +79 -0
- package/src/data/resource-view/mapping.ts +60 -7
- package/src/data/resource-view/messages.ts +25 -0
- package/src/data/resource-view/use-native-resource-view.ts +21 -10
|
@@ -4,8 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
import type { DataRow, FieldReference } from "@marktiderman/genesis-core";
|
|
6
6
|
import type {
|
|
7
|
+
ResolvedFormField,
|
|
7
8
|
Allowed,
|
|
8
9
|
DisplayHint,
|
|
10
|
+
ResolvedLink,
|
|
9
11
|
ResourceView,
|
|
10
12
|
RowPicture,
|
|
11
13
|
RowSummary,
|
|
@@ -179,6 +181,7 @@ export function swipeActionsFor<T extends Row>(
|
|
|
179
181
|
export type NativeFormFieldKind =
|
|
180
182
|
| "text"
|
|
181
183
|
| "email"
|
|
184
|
+
| "tel"
|
|
182
185
|
| "url"
|
|
183
186
|
| "number"
|
|
184
187
|
| "textarea"
|
|
@@ -186,7 +189,9 @@ export type NativeFormFieldKind =
|
|
|
186
189
|
| "segmented"
|
|
187
190
|
| "select"
|
|
188
191
|
| "date"
|
|
189
|
-
| "datetime"
|
|
192
|
+
| "datetime"
|
|
193
|
+
/** A link: a searchable list of the linked rows, through the provider. */
|
|
194
|
+
| "link";
|
|
190
195
|
|
|
191
196
|
export interface NativeFormField {
|
|
192
197
|
key: string;
|
|
@@ -201,9 +206,17 @@ export interface NativeFormField {
|
|
|
201
206
|
/** From the contract: the column has a default (or is generated). */
|
|
202
207
|
hasDefault?: boolean;
|
|
203
208
|
max?: number;
|
|
209
|
+
/** The view field's autofill token (`ViewField.autoComplete`); unset, none is passed. */
|
|
210
|
+
autoComplete?: string;
|
|
211
|
+
/** The keyboard (`ViewField.inputMode`), over the kind's own. */
|
|
212
|
+
inputMode?: ResolvedFormField["inputMode"];
|
|
213
|
+
/** The Return key's label (`ViewField.enterKeyHint`). */
|
|
214
|
+
enterKeyHint?: ResolvedFormField["enterKeyHint"];
|
|
204
215
|
options?: { value: string; label: string }[];
|
|
205
216
|
/** For a link: what it points at and the linked row's naming field, so it shows the row's words. */
|
|
206
217
|
references?: FieldReference;
|
|
218
|
+
/** For a link the person can change: how its picker lists the linked rows (`ResolvedFormField.link`). */
|
|
219
|
+
link?: ResolvedLink;
|
|
207
220
|
labelField?: string;
|
|
208
221
|
missingLabel?: string;
|
|
209
222
|
/** The heading it sits under; null for the form's first, untitled group. */
|
|
@@ -215,8 +228,11 @@ export interface NativeFormField {
|
|
|
215
228
|
/** Choices that fit a segmented control; more go to a list. */
|
|
216
229
|
const SEGMENTED_MAX = 4;
|
|
217
230
|
|
|
218
|
-
function kindFor(display: DisplayHint, options: unknown[] | undefined): NativeFormFieldKind {
|
|
231
|
+
function kindFor(display: DisplayHint, options: unknown[] | undefined, widget?: string): NativeFormFieldKind {
|
|
219
232
|
if (options) return options.length <= SEGMENTED_MAX ? "segmented" : "select";
|
|
233
|
+
// A phone number shows as text, so only the contract's `widget: "tel"` makes it one (as on the
|
|
234
|
+
// web); a column's name is never read as a type.
|
|
235
|
+
if (widget === "tel" && display === "text") return "tel";
|
|
220
236
|
switch (display) {
|
|
221
237
|
case "boolean":
|
|
222
238
|
return "switch";
|
|
@@ -240,14 +256,46 @@ function kindFor(display: DisplayHint, options: unknown[] | undefined): NativeFo
|
|
|
240
256
|
}
|
|
241
257
|
}
|
|
242
258
|
|
|
259
|
+
/** A link's picker narrowed to `keys` (an `in` filter on the linked key), or the link as it is. */
|
|
260
|
+
function heldTo(link: ResolvedLink, keys: readonly (string | number)[] | undefined): ResolvedLink {
|
|
261
|
+
if (!keys) return link;
|
|
262
|
+
const held = { field: link.field, operator: "in" as const, value: [...keys] };
|
|
263
|
+
return Object.freeze({
|
|
264
|
+
resource: link.resource,
|
|
265
|
+
field: link.field,
|
|
266
|
+
get labelField() {
|
|
267
|
+
return link.labelField;
|
|
268
|
+
},
|
|
269
|
+
...(link.searchFields ? { searchFields: link.searchFields } : {}),
|
|
270
|
+
filters: (scope?: Parameters<ResolvedLink["filters"]>[0], values?: Parameters<ResolvedLink["filters"]>[1]) => [...link.filters(scope, values), held],
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
243
274
|
/**
|
|
244
|
-
* The form sheet's fields: `view.form(mode)` (which already leaves out JSON, lists,
|
|
275
|
+
* The form sheet's fields: `view.form(mode)` (which already leaves out JSON, lists, scope,
|
|
245
276
|
* sensitive and unreadable fields and server-filled keys), each marked read-only unless
|
|
246
|
-
* `view.writable(mode)` lists it. Copied, never mutated: core caches and freezes `form()`. A create leaves read-only fields out (there is nothing to show yet); an edit shows
|
|
277
|
+
* `view.writable(mode)` lists it. A link the person can change is a picker (`kind: "link"`). Copied, never mutated: core caches and freezes `form()`. A create leaves read-only fields out (there is nothing to show yet); an edit shows
|
|
247
278
|
* them as text.
|
|
248
279
|
*/
|
|
249
|
-
export function formFieldsFor<T extends Row>(
|
|
250
|
-
|
|
280
|
+
export function formFieldsFor<T extends Row>(
|
|
281
|
+
view: AnyResourceView<T>,
|
|
282
|
+
mode: "create" | "edit",
|
|
283
|
+
options: {
|
|
284
|
+
/**
|
|
285
|
+
* Fields the screen's filter fixes. A link among them stays read-only: the list is that
|
|
286
|
+
* record's (a client's sessions), and a picker would move the row out of it (as on the web).
|
|
287
|
+
*/
|
|
288
|
+
pinned?: Iterable<string>;
|
|
289
|
+
/**
|
|
290
|
+
* Lists the screen's filter holds fields to (`{ owner_id: ["a", "b"] }`). A link among them
|
|
291
|
+
* picks only among those rows, so a save never moves the row out of the list.
|
|
292
|
+
*/
|
|
293
|
+
within?: Readonly<Record<string, readonly (string | number)[]>>;
|
|
294
|
+
} = {},
|
|
295
|
+
): NativeFormField[] {
|
|
296
|
+
const pinned = new Set(options.pinned ?? []);
|
|
297
|
+
const within = options.within ?? {};
|
|
298
|
+
const writable = new Set([...view.writable(mode)].filter((name) => !(pinned.has(name) && view.field(name)?.references)));
|
|
251
299
|
return view
|
|
252
300
|
.form(mode)
|
|
253
301
|
// JSON and lists need editors of their own; a plain input would send them back as text (as the web).
|
|
@@ -256,18 +304,23 @@ export function formFieldsFor<T extends Row>(view: AnyResourceView<T>, mode: "cr
|
|
|
256
304
|
.map((f) => {
|
|
257
305
|
const options = f.options?.map((o) => ({ value: o.value, label: o.label }));
|
|
258
306
|
const readOnly = !writable.has(f.field.name);
|
|
307
|
+
const link = !readOnly && !options && f.link ? heldTo(f.link, Object.hasOwn(within, f.field.name) ? within[f.field.name] : undefined) : undefined;
|
|
259
308
|
return {
|
|
260
309
|
key: f.field.name,
|
|
261
310
|
label: f.label,
|
|
262
|
-
kind: kindFor(f.display, options),
|
|
311
|
+
kind: link ? ("link" as const) : kindFor(f.display, options, f.field.widget),
|
|
263
312
|
display: f.display,
|
|
264
313
|
required: !readOnly && f.required,
|
|
265
314
|
readOnly,
|
|
266
315
|
...(typeof f.field.nullable === "boolean" ? { nullable: f.field.nullable } : {}),
|
|
267
316
|
...(f.field.hasDefault || f.field.generated ? { hasDefault: true } : {}),
|
|
268
317
|
...(f.max !== undefined ? { max: f.max } : {}),
|
|
318
|
+
...(f.autoComplete ? { autoComplete: f.autoComplete } : {}),
|
|
319
|
+
...(f.inputMode ? { inputMode: f.inputMode } : {}),
|
|
320
|
+
...(f.enterKeyHint ? { enterKeyHint: f.enterKeyHint } : {}),
|
|
269
321
|
...(options ? { options } : {}),
|
|
270
322
|
...(f.field.references ? { references: f.field.references } : {}),
|
|
323
|
+
...(link ? { link } : {}),
|
|
271
324
|
...(f.labelField ? { labelField: f.labelField } : {}),
|
|
272
325
|
...(f.missingLabel ? { missingLabel: f.missingLabel } : {}),
|
|
273
326
|
section: f.section,
|
|
@@ -65,6 +65,22 @@ export interface NativeResourceViewMessages {
|
|
|
65
65
|
clear: string;
|
|
66
66
|
loading: string;
|
|
67
67
|
};
|
|
68
|
+
/**
|
|
69
|
+
* A link's picker in the form sheet. Optional, so a catalogue written before it still fits; the
|
|
70
|
+
* English is used for what's missing.
|
|
71
|
+
*/
|
|
72
|
+
link?: {
|
|
73
|
+
/** The picker with nothing chosen. */
|
|
74
|
+
placeholder?: string;
|
|
75
|
+
search?: string;
|
|
76
|
+
/** The choice that empties an optional link. */
|
|
77
|
+
none?: string;
|
|
78
|
+
noResults?: string;
|
|
79
|
+
loadMore?: string;
|
|
80
|
+
loadFailed?: string;
|
|
81
|
+
/** A row whose name is empty (never its raw id). */
|
|
82
|
+
untitled?: string;
|
|
83
|
+
};
|
|
68
84
|
refusal: {
|
|
69
85
|
/** `{noun}`. A delete failed with no message of its own. */
|
|
70
86
|
deleteFailed: string;
|
|
@@ -115,6 +131,15 @@ export const defaultNativeResourceViewMessages: NativeResourceViewMessages = {
|
|
|
115
131
|
clear: "Clear {label}",
|
|
116
132
|
loading: "Loading",
|
|
117
133
|
},
|
|
134
|
+
link: {
|
|
135
|
+
placeholder: "Choose…",
|
|
136
|
+
search: "Search",
|
|
137
|
+
none: "None",
|
|
138
|
+
noResults: "No matches",
|
|
139
|
+
loadMore: "Show more",
|
|
140
|
+
loadFailed: "Couldn't load the choices.",
|
|
141
|
+
untitled: "Untitled",
|
|
142
|
+
},
|
|
118
143
|
refusal: {
|
|
119
144
|
deleteFailed: "This {noun} couldn't be deleted. Try again.",
|
|
120
145
|
dismiss: "Dismiss",
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
7
7
|
import type { BaseRecord, DataRow, FilterParam } from "@marktiderman/genesis-core";
|
|
8
8
|
import { useCapabilities, useInfiniteResource, useResource, useViewer } from "@marktiderman/genesis-core/hooks";
|
|
9
|
-
import type
|
|
9
|
+
import { filterParams as toFilters, type Allowed } from "@marktiderman/genesis-core/resource-view";
|
|
10
10
|
import { useNativeResourceViewMessages } from "./display";
|
|
11
11
|
import {
|
|
12
12
|
allowed,
|
|
@@ -108,13 +108,7 @@ export interface UseNativeResourceViewReturn<T extends Row = Row> {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
function filterParams(filter: Filter | undefined): FilterParam[] {
|
|
111
|
-
return
|
|
112
|
-
value === null
|
|
113
|
-
? { field, operator: "is_null" }
|
|
114
|
-
: Array.isArray(value)
|
|
115
|
-
? { field, operator: "in", value: [...(value as readonly (string | number)[])] }
|
|
116
|
-
: { field, operator: "eq", value: value as string | number | boolean },
|
|
117
|
-
);
|
|
111
|
+
return toFilters(filter);
|
|
118
112
|
}
|
|
119
113
|
|
|
120
114
|
/** The values a create takes from the screen: every non-null equality filter, the screen's over the tab's. */
|
|
@@ -128,6 +122,18 @@ export function screenValues(...filters: (Filter | undefined)[]): Record<string,
|
|
|
128
122
|
return out;
|
|
129
123
|
}
|
|
130
124
|
|
|
125
|
+
/** The list (`in`) filters a screen holds fields to, the screen's over the tab's. */
|
|
126
|
+
export function screenLists(...filters: (Filter | undefined)[]): Record<string, (string | number)[]> {
|
|
127
|
+
const out: Record<string, (string | number)[]> = {};
|
|
128
|
+
for (const filter of filters) {
|
|
129
|
+
for (const [field, value] of Object.entries(filter ?? {})) {
|
|
130
|
+
if (Array.isArray(value)) out[field] = [...(value as readonly (string | number)[])];
|
|
131
|
+
else delete out[field];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return out;
|
|
135
|
+
}
|
|
136
|
+
|
|
131
137
|
/** Pages kept loaded by default (cursor paging). */
|
|
132
138
|
export const DEFAULT_MAX_PAGES = 10;
|
|
133
139
|
|
|
@@ -172,6 +178,9 @@ export function useNativeResourceView<T extends Row = Row>(
|
|
|
172
178
|
// By value, so an inline `filter={{ client_id }}` doesn't rebuild the form every render.
|
|
173
179
|
const createKey = JSON.stringify(screenValues(tabFilter, filter));
|
|
174
180
|
const createDefaults = useMemo(() => JSON.parse(createKey) as Record<string, string | number | boolean>, [createKey]);
|
|
181
|
+
// A link the filter holds to a list (`in`) picks only among the list's rows (as on the web).
|
|
182
|
+
const listKey = JSON.stringify(screenLists(tabFilter, filter));
|
|
183
|
+
const within = useMemo(() => JSON.parse(listKey) as Record<string, (string | number)[]>, [listKey]);
|
|
175
184
|
const sorts = useMemo(() => serverSorts(view), [view]);
|
|
176
185
|
const searchFields = useMemo(
|
|
177
186
|
() => searchOption ?? (view.labelField ? [view.labelField] : undefined),
|
|
@@ -219,8 +228,10 @@ export function useNativeResourceView<T extends Row = Row>(
|
|
|
219
228
|
// saved, so an input for it would be overridden without a word (the web hides them too).
|
|
220
229
|
const formFields = useCallback(
|
|
221
230
|
(mode: "create" | "edit") =>
|
|
222
|
-
mode === "create"
|
|
223
|
-
|
|
231
|
+
mode === "create"
|
|
232
|
+
? formFieldsFor(view, "create", { within }).filter((f) => !(f.key in createDefaults))
|
|
233
|
+
: formFieldsFor(view, "edit", { pinned: Object.keys(createDefaults), within }),
|
|
234
|
+
[view, createDefaults, within],
|
|
224
235
|
);
|
|
225
236
|
const editable = useMemo(() => formFieldsFor(view, "edit").some((f) => !f.readOnly), [view]);
|
|
226
237
|
// Guards read the viewer (ViewerScope's id, scope and roles): a change asks them again.
|