@marktiderman/genesis-ui-native 3.1.0 → 3.2.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 -21
- package/dist/data/index.js +46 -33
- package/dist/data/index.js.map +1 -1
- package/package.json +20 -6
- package/src/data/resource-view/{NativeResourceWorkspace.tsx → NativeRecordView.tsx} +91 -56
- package/src/data/resource-view/__tests__/{native-resource-workspace.test.tsx → native-record-view.test.tsx} +34 -34
- package/src/data/resource-view/index.ts +10 -3
package/package.json
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marktiderman/genesis-ui-native",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"description": "Genesis React Native components (NativeWind v4-5). Part of the Genesis framework.",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"genesis",
|
|
10
|
+
"genesis-framework",
|
|
11
|
+
"react-native",
|
|
12
|
+
"ui",
|
|
13
|
+
"components",
|
|
14
|
+
"nativewind",
|
|
15
|
+
"expo"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://github.com/marktiderman/genesis/tree/main/packages/ui-native#readme",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/marktiderman/genesis/issues"
|
|
20
|
+
},
|
|
8
21
|
"repository": {
|
|
9
22
|
"type": "git",
|
|
10
23
|
"url": "git+https://github.com/marktiderman/genesis.git",
|
|
@@ -69,7 +82,7 @@
|
|
|
69
82
|
"@expo/ui": ">=58.0.0",
|
|
70
83
|
"@gorhom/bottom-sheet": ">=5.0.0",
|
|
71
84
|
"@hookform/resolvers": "^3.0.0 || ^4.0.0 || ^5.0.0",
|
|
72
|
-
"@marktiderman/genesis-core": "^2.
|
|
85
|
+
"@marktiderman/genesis-core": "^2.2.0",
|
|
73
86
|
"@marktiderman/genesis-design-system": "^1.5.1",
|
|
74
87
|
"@react-native-community/datetimepicker": ">=8.0.0",
|
|
75
88
|
"@shopify/flash-list": ">=2.0.0",
|
|
@@ -139,7 +152,7 @@
|
|
|
139
152
|
"@testing-library/react": "^16.3.2",
|
|
140
153
|
"@types/react": "^19.2.14",
|
|
141
154
|
"@vitest/coverage-v8": "^4.1.12",
|
|
142
|
-
"happy-dom": "^
|
|
155
|
+
"happy-dom": "^21.0.4",
|
|
143
156
|
"react": "^19.2.5",
|
|
144
157
|
"react-dom": "^19.2.5",
|
|
145
158
|
"tsup": "^8.3.0",
|
|
@@ -169,6 +182,7 @@
|
|
|
169
182
|
"unless": {
|
|
170
183
|
"renders": [
|
|
171
184
|
"NativeResourceView",
|
|
185
|
+
"NativeRecordView",
|
|
172
186
|
"NativeResourceWorkspace"
|
|
173
187
|
]
|
|
174
188
|
},
|
|
@@ -186,10 +200,10 @@
|
|
|
186
200
|
"docs": "https://github.com/marktiderman/genesis/blob/main/packages/ui-native/README.md"
|
|
187
201
|
},
|
|
188
202
|
{
|
|
189
|
-
"need": "a record
|
|
190
|
-
"use": "
|
|
203
|
+
"need": "a record view on the phone",
|
|
204
|
+
"use": "NativeRecordView",
|
|
191
205
|
"import": "@marktiderman/genesis-ui-native/data",
|
|
192
|
-
"docs": "https://github.com/marktiderman/genesis/blob/main/docs/RESOURCE-VIEW.md#
|
|
206
|
+
"docs": "https://github.com/marktiderman/genesis/blob/main/docs/RESOURCE-VIEW.md#recordview"
|
|
193
207
|
}
|
|
194
208
|
]
|
|
195
209
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
//
|
|
1
|
+
// NativeRecordView: one record's screen from a genesis-core record view (defineRecordView),
|
|
2
2
|
// in native patterns:
|
|
3
3
|
// - the title in the navigation bar and the actions in the header menu, through
|
|
4
|
-
// `
|
|
4
|
+
// `getNativeRecordViewStackOptions` (the host's <Stack.Screen options>), as SettingsPage does;
|
|
5
5
|
// - up to four sections as a segmented control; more as pushed rows (`onOpenSection`);
|
|
6
6
|
// - a list section → an embedded NativeResourceView of the child view, filtered by the row;
|
|
7
7
|
// - a settings section → inset grouped settings rows, saving only its listed keys, a zero-row
|
|
@@ -14,7 +14,7 @@ import { ActionSheetIOS, Alert, Platform, Pressable, ScrollView, View } from "re
|
|
|
14
14
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
15
15
|
import { DataWriteError, resourceKeys, useCapabilities, useDataProvider, useViewer, type DataRow, type ResourceContract } from "@marktiderman/genesis-core";
|
|
16
16
|
import { readinessChip, type ReadinessLabels } from "@marktiderman/genesis-core/resource";
|
|
17
|
-
import type { Allowed,
|
|
17
|
+
import type { Allowed, RecordView as RecordViewDef, RecordViewHeaderAction } from "@marktiderman/genesis-core/resource-view";
|
|
18
18
|
import { NativeBadge } from "../../components/badge";
|
|
19
19
|
import { NativeButton } from "../../components/button";
|
|
20
20
|
import { NativeIcon } from "../../components/icon";
|
|
@@ -37,14 +37,14 @@ import { NativeResourceView, type NativeResourceViewProps } from "./NativeResour
|
|
|
37
37
|
type Row = Record<string, unknown>;
|
|
38
38
|
type Values = Record<string, unknown>;
|
|
39
39
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any contract; the row is what matters here
|
|
40
|
-
type
|
|
40
|
+
type AnyRecordView<T extends Row> = RecordViewDef<ResourceContract<any>, T & DataRow>;
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
|
-
* The words
|
|
43
|
+
* The words NativeRecordView shows. Override any of them with `messages`. The readiness
|
|
44
44
|
* chip's keys (`ready`, `empty`, `pending`, `problems`, `warnings`) are genesis-core/resource's
|
|
45
45
|
* `readinessChip` labels: unset, they use its defaults.
|
|
46
46
|
*/
|
|
47
|
-
export interface
|
|
47
|
+
export interface NativeRecordViewMessages extends ReadinessLabels {
|
|
48
48
|
actions: string;
|
|
49
49
|
cancel: string;
|
|
50
50
|
save: string;
|
|
@@ -61,7 +61,7 @@ export interface NativeResourceWorkspaceMessages extends ReadinessLabels {
|
|
|
61
61
|
notANumber: string;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
export const
|
|
64
|
+
export const defaultNativeRecordViewMessages: NativeRecordViewMessages = {
|
|
65
65
|
actions: "Actions",
|
|
66
66
|
cancel: "Cancel",
|
|
67
67
|
save: "Save",
|
|
@@ -85,22 +85,22 @@ export const SEGMENTED_SECTIONS_MAX = 4;
|
|
|
85
85
|
|
|
86
86
|
// ── Header: title and menu ────────────────────────────────────────────
|
|
87
87
|
|
|
88
|
-
export interface
|
|
89
|
-
|
|
88
|
+
export interface NativeRecordViewHeaderProps<T extends Row = Row> {
|
|
89
|
+
view: AnyRecordView<T>;
|
|
90
90
|
row: T;
|
|
91
91
|
/** Runs an allowed (and confirmed) action. Default: the provider's `runAction`. */
|
|
92
92
|
onAction?: (name: string, row: T) => unknown;
|
|
93
93
|
/** The screen's own rule: `false` hides an action, a sentence refuses it. Only narrows. */
|
|
94
94
|
can?: (action: string, row: T) => Allowed | boolean;
|
|
95
|
-
messages?: Partial<
|
|
95
|
+
messages?: Partial<NativeRecordViewMessages>;
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
/** Runs header actions: guards explain, confirms ask, one at a time, failures in an alert. */
|
|
99
|
-
function useHeaderActions<T extends Row>({
|
|
100
|
-
const words = { ...
|
|
99
|
+
function useHeaderActions<T extends Row>({ view: recordView, row, onAction, can, messages }: NativeRecordViewHeaderProps<T>) {
|
|
100
|
+
const words = { ...defaultNativeRecordViewMessages, ...messages };
|
|
101
101
|
const provider = useDataProvider();
|
|
102
102
|
const queryClient = useQueryClient();
|
|
103
|
-
const view =
|
|
103
|
+
const view = recordView.view;
|
|
104
104
|
// Actions show only where something can run them, and only those the provider serves when it says.
|
|
105
105
|
const capabilities = useCapabilities(view.resource, { enabled: !onAction && !!provider.capabilities });
|
|
106
106
|
const described = useQuery({
|
|
@@ -111,7 +111,7 @@ function useHeaderActions<T extends Row>({ workspace, row, onAction, can, messag
|
|
|
111
111
|
});
|
|
112
112
|
// The view's action guards read the viewer (ViewerScope's id, scope and roles).
|
|
113
113
|
const viewer = useViewer();
|
|
114
|
-
const actions =
|
|
114
|
+
const actions = recordView.headerFor(row as T & DataRow, {
|
|
115
115
|
viewer,
|
|
116
116
|
runnable: Boolean(onAction) || (Boolean(provider.runAction) && capabilities.data?.actions !== false),
|
|
117
117
|
...(!onAction && described.data?.actions ? { served: described.data.actions.map((a) => a.name) } : {}),
|
|
@@ -120,13 +120,13 @@ function useHeaderActions<T extends Row>({ workspace, row, onAction, can, messag
|
|
|
120
120
|
// One action at a time: a second press while one runs does nothing.
|
|
121
121
|
const running = useRef(false);
|
|
122
122
|
const [pending, setPending] = useState(false);
|
|
123
|
-
const run = async (a:
|
|
123
|
+
const run = async (a: RecordViewHeaderAction) => {
|
|
124
124
|
if (running.current) return;
|
|
125
125
|
running.current = true;
|
|
126
126
|
setPending(true);
|
|
127
127
|
try {
|
|
128
128
|
if (onAction) await onAction(a.name, row);
|
|
129
|
-
else if (provider.runAction) await provider.runAction(view.resource, { id:
|
|
129
|
+
else if (provider.runAction) await provider.runAction(view.resource, { id: recordView.recordId(row as T & DataRow), action: a.name });
|
|
130
130
|
else throw new DataWriteError("error", fill(words.noRunner, { name: a.label }));
|
|
131
131
|
await queryClient.invalidateQueries({ queryKey: [...resourceKeys(view.resource).all] });
|
|
132
132
|
} catch (error) {
|
|
@@ -136,7 +136,7 @@ function useHeaderActions<T extends Row>({ workspace, row, onAction, can, messag
|
|
|
136
136
|
setPending(false);
|
|
137
137
|
}
|
|
138
138
|
};
|
|
139
|
-
const press = (a:
|
|
139
|
+
const press = (a: RecordViewHeaderAction) => {
|
|
140
140
|
if (running.current) return;
|
|
141
141
|
if (a.allowed !== true) return Alert.alert(a.label, a.allowed);
|
|
142
142
|
if (!a.confirm) return void run(a);
|
|
@@ -153,22 +153,22 @@ function useHeaderActions<T extends Row>({ workspace, row, onAction, can, messag
|
|
|
153
153
|
const A11Y_PREFIX = "action:";
|
|
154
154
|
|
|
155
155
|
/**
|
|
156
|
-
* The header menu: a button that lists the
|
|
156
|
+
* The header menu: a button that lists the record view's actions. On iOS an action sheet
|
|
157
157
|
* (ActionSheetIOS, destructive ones in red); elsewhere a sheet of rows, since Android's alert
|
|
158
158
|
* shows at most three buttons. A refused action stays listed and explains why when pressed.
|
|
159
159
|
*/
|
|
160
|
-
export function
|
|
160
|
+
export function NativeRecordViewMenu<T extends Row = Row>(props: NativeRecordViewHeaderProps<T> & { testID?: string }) {
|
|
161
161
|
const { testID = "native-resource-workspace-menu" } = props;
|
|
162
162
|
const { actions, press, pending, words } = useHeaderActions(props);
|
|
163
163
|
const [open, setOpen] = useState(false);
|
|
164
164
|
if (actions.length === 0) return null;
|
|
165
|
-
const labelOf = (a:
|
|
165
|
+
const labelOf = (a: RecordViewHeaderAction) => (a.allowed === true ? a.label : `${a.label} (${a.allowed})`);
|
|
166
166
|
const show = () => {
|
|
167
167
|
if (Platform.OS === "ios") {
|
|
168
168
|
const destructive = actions.flatMap((a, i) => (a.destructive ? [i] : []));
|
|
169
169
|
ActionSheetIOS.showActionSheetWithOptions(
|
|
170
170
|
{
|
|
171
|
-
title: props.
|
|
171
|
+
title: props.view.headerFor(props.row as T & DataRow).title,
|
|
172
172
|
options: [...actions.map(labelOf), words.cancel],
|
|
173
173
|
cancelButtonIndex: actions.length,
|
|
174
174
|
...(destructive.length ? { destructiveButtonIndex: destructive } : {}),
|
|
@@ -238,29 +238,29 @@ export function NativeResourceWorkspaceMenu<T extends Row = Row>(props: NativeRe
|
|
|
238
238
|
}
|
|
239
239
|
|
|
240
240
|
/**
|
|
241
|
-
* `<Stack.Screen options>` for a
|
|
241
|
+
* `<Stack.Screen options>` for a record view: the record's title in the navigation bar and the
|
|
242
242
|
* actions in the header menu.
|
|
243
243
|
*
|
|
244
244
|
* ```tsx
|
|
245
|
-
* <Stack.Screen options={
|
|
245
|
+
* <Stack.Screen options={getNativeRecordViewStackOptions({ view: brandRecordView, row: brand })} />
|
|
246
246
|
* ```
|
|
247
247
|
*/
|
|
248
|
-
export function
|
|
249
|
-
props:
|
|
248
|
+
export function getNativeRecordViewStackOptions<T extends Row>(
|
|
249
|
+
props: NativeRecordViewHeaderProps<T> & Omit<ScreenHeaderProps, "headerTitle">,
|
|
250
250
|
): Record<string, unknown> {
|
|
251
|
-
const {
|
|
252
|
-
const menu = {
|
|
251
|
+
const { view: recordView, row, onAction, can, messages, ...header } = props;
|
|
252
|
+
const menu = { view: recordView, row, ...(onAction ? { onAction } : {}), ...(can ? { can } : {}), ...(messages ? { messages } : {}) };
|
|
253
253
|
return composeHeaderOptions({
|
|
254
254
|
...header,
|
|
255
|
-
headerTitle:
|
|
256
|
-
headerRight: header.headerRight ?? (
|
|
255
|
+
headerTitle: recordView.headerFor(row as T & DataRow).title,
|
|
256
|
+
headerRight: header.headerRight ?? (recordView.headerActions.length ? () => <NativeRecordViewMenu {...menu} /> : undefined),
|
|
257
257
|
});
|
|
258
258
|
}
|
|
259
259
|
|
|
260
260
|
// ── Screen ────────────────────────────────────────────────────────────
|
|
261
261
|
|
|
262
|
-
export interface
|
|
263
|
-
|
|
262
|
+
export interface NativeRecordViewProps<T extends Row = Row> {
|
|
263
|
+
view: AnyRecordView<T>;
|
|
264
264
|
/** The record, as the screen loaded it. A save refreshes it through the cache. */
|
|
265
265
|
row: T;
|
|
266
266
|
/**
|
|
@@ -283,31 +283,31 @@ export interface NativeResourceWorkspaceProps<T extends Row = Row> {
|
|
|
283
283
|
slots?: Partial<Record<string, ReactNode>>;
|
|
284
284
|
/** More props for a list section's NativeResourceView, by section id. */
|
|
285
285
|
lists?: Partial<Record<string, Partial<Omit<NativeResourceViewProps, "view" | "filter">>>>;
|
|
286
|
-
messages?: Partial<
|
|
286
|
+
messages?: Partial<NativeRecordViewMessages>;
|
|
287
287
|
testID?: string;
|
|
288
288
|
}
|
|
289
289
|
|
|
290
290
|
/**
|
|
291
|
-
* One record's screen from
|
|
291
|
+
* One record's screen, from its record view (defineRecordView):
|
|
292
292
|
*
|
|
293
293
|
* ```tsx
|
|
294
|
-
* <Stack.Screen options={
|
|
295
|
-
* <
|
|
294
|
+
* <Stack.Screen options={getNativeRecordViewStackOptions({ view: brandRecordView, row: brand })} />
|
|
295
|
+
* <NativeRecordView view={brandRecordView} row={brand} />
|
|
296
296
|
* ```
|
|
297
297
|
*/
|
|
298
|
-
export function
|
|
299
|
-
const {
|
|
300
|
-
const words = { ...
|
|
298
|
+
export function NativeRecordView<T extends Row = Row>(props: NativeRecordViewProps<T>) {
|
|
299
|
+
const { view: recordView, row, section, onSectionChange, onOpenSection, onSaved, switcher, slots, lists, messages, testID = "native-resource-workspace" } = props;
|
|
300
|
+
const words = { ...defaultNativeRecordViewMessages, ...messages };
|
|
301
301
|
const colors = useThemeRoles();
|
|
302
302
|
const queryClient = useQueryClient();
|
|
303
303
|
const record = row as T & DataRow;
|
|
304
|
-
const sections =
|
|
304
|
+
const sections = recordView.sections;
|
|
305
305
|
const many = sections.length > SEGMENTED_SECTIONS_MAX;
|
|
306
306
|
const [local, setLocal] = useState<string | undefined>(undefined);
|
|
307
307
|
const activeId = section ?? local ?? (many ? undefined : sections[0]?.id);
|
|
308
|
-
const active = activeId ?
|
|
309
|
-
const readiness =
|
|
310
|
-
const summary =
|
|
308
|
+
const active = activeId ? recordView.section(activeId) : undefined;
|
|
309
|
+
const readiness = recordView.readinessFor(record);
|
|
310
|
+
const summary = recordView.readinessSummary(record);
|
|
311
311
|
const chip = summary ? readinessChip(summary, words) : null;
|
|
312
312
|
|
|
313
313
|
const choose = (id: string) => {
|
|
@@ -395,7 +395,7 @@ export function NativeResourceWorkspace<T extends Row = Row>(props: NativeResour
|
|
|
395
395
|
testID={`${testID}-list`}
|
|
396
396
|
{...lists?.[active.id]}
|
|
397
397
|
title={false}
|
|
398
|
-
filter={
|
|
398
|
+
filter={recordView.listFilter(active.id, record)}
|
|
399
399
|
/>
|
|
400
400
|
);
|
|
401
401
|
} else if (active?.kind === "settings") {
|
|
@@ -403,13 +403,13 @@ export function NativeResourceWorkspace<T extends Row = Row>(props: NativeResour
|
|
|
403
403
|
<ScrollView contentContainerStyle={{ padding: 16 }} keyboardShouldPersistTaps="handled">
|
|
404
404
|
<SettingsGroup
|
|
405
405
|
// A fresh draft per record and section.
|
|
406
|
-
key={`${String(
|
|
407
|
-
|
|
406
|
+
key={`${String(recordView.recordId(record))}:${active.id}`}
|
|
407
|
+
recordView={recordView}
|
|
408
408
|
sectionId={active.id}
|
|
409
409
|
row={record}
|
|
410
410
|
words={words}
|
|
411
411
|
onSaved={(saved) => {
|
|
412
|
-
void queryClient.invalidateQueries({ queryKey: [...resourceKeys(
|
|
412
|
+
void queryClient.invalidateQueries({ queryKey: [...resourceKeys(recordView.view.resource).all] });
|
|
413
413
|
onSaved?.(saved as T);
|
|
414
414
|
}}
|
|
415
415
|
testID={`${testID}-settings`}
|
|
@@ -432,7 +432,7 @@ export function NativeResourceWorkspace<T extends Row = Row>(props: NativeResour
|
|
|
432
432
|
// ── Settings ──────────────────────────────────────────────────────────
|
|
433
433
|
|
|
434
434
|
function SettingsGroup({
|
|
435
|
-
|
|
435
|
+
recordView,
|
|
436
436
|
sectionId,
|
|
437
437
|
row,
|
|
438
438
|
words,
|
|
@@ -440,23 +440,23 @@ function SettingsGroup({
|
|
|
440
440
|
testID,
|
|
441
441
|
}: {
|
|
442
442
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any contract
|
|
443
|
-
|
|
443
|
+
recordView: RecordViewDef<ResourceContract<any>, any>;
|
|
444
444
|
sectionId: string;
|
|
445
445
|
row: DataRow;
|
|
446
|
-
words:
|
|
446
|
+
words: NativeRecordViewMessages;
|
|
447
447
|
onSaved: (row: DataRow) => void;
|
|
448
448
|
testID: string;
|
|
449
449
|
}) {
|
|
450
450
|
const provider = useDataProvider();
|
|
451
|
-
const view =
|
|
451
|
+
const view = recordView.view;
|
|
452
452
|
// The view's edit-form mapping (the form sheet's), narrowed to this section's keys in their order.
|
|
453
453
|
const fields = useMemo(() => {
|
|
454
454
|
const byKey = new Map(formFieldsFor(view, "edit").map((f) => [f.key, f]));
|
|
455
|
-
return
|
|
456
|
-
}, [
|
|
455
|
+
return recordView.settingsFields(sectionId).map((f) => byKey.get(f.field.name)).filter((f): f is NativeFormField => !!f && !f.readOnly);
|
|
456
|
+
}, [recordView, view, sectionId]);
|
|
457
457
|
// The view's update guard, for this viewer: when it refuses, the settings are read-only with its reason.
|
|
458
458
|
const viewer = useViewer();
|
|
459
|
-
const locked =
|
|
459
|
+
const locked = recordView.settingsLocked(row, viewer);
|
|
460
460
|
const [draft, setDraft] = useState<Values>(() => Object.fromEntries(fields.map((f) => [f.key, row[f.key]])));
|
|
461
461
|
const [invalid, setInvalid] = useState<Record<string, string>>({});
|
|
462
462
|
const [saving, setSaving] = useState(false);
|
|
@@ -476,7 +476,7 @@ function SettingsGroup({
|
|
|
476
476
|
setRefusal(null);
|
|
477
477
|
try {
|
|
478
478
|
// Only the section's listed keys, never anything else the row or draft holds.
|
|
479
|
-
const result = await provider.update(view.resource, { id:
|
|
479
|
+
const result = await provider.update(view.resource, { id: recordView.recordId(row), data: recordView.settingsPatch(sectionId, changed) });
|
|
480
480
|
// A write that matched no rows is a refusal, never a silent success.
|
|
481
481
|
if (!result || result.data === null || result.data === undefined) setRefusal(fill(words.notChanged, { noun: view.noun }));
|
|
482
482
|
else onSaved(result.data as DataRow);
|
|
@@ -491,7 +491,7 @@ function SettingsGroup({
|
|
|
491
491
|
<View style={{ gap: 12 }}>
|
|
492
492
|
{refusal ? <NativeInlineAlert testID={`${testID}-refusal`} variant="destructive" description={refusal} /> : null}
|
|
493
493
|
{locked !== null ? <NativeInlineAlert testID={`${testID}-locked`} variant="info" description={locked} /> : null}
|
|
494
|
-
<Group testID={`${testID}-group`} title={
|
|
494
|
+
<Group testID={`${testID}-group`} title={recordView.section(sectionId)?.label}>
|
|
495
495
|
{shown.map((f, i) => (
|
|
496
496
|
<Cell key={f.key} last={i === shown.length - 1}>
|
|
497
497
|
<SettingRow
|
|
@@ -516,6 +516,41 @@ function SettingsGroup({
|
|
|
516
516
|
);
|
|
517
517
|
}
|
|
518
518
|
|
|
519
|
+
// The names before record views were called record views ("workspace" now means where the viewer
|
|
520
|
+
// is working: docs/adr/0004-workspaces.md). Kept for one minor release; `workspace` is now `view`.
|
|
521
|
+
|
|
522
|
+
type OldPage<P extends { view: unknown }> = Omit<P, "view"> & { workspace: P["view"] };
|
|
523
|
+
|
|
524
|
+
/** @deprecated Renamed `NativeRecordViewProps`; its `workspace` prop is now `view`. */
|
|
525
|
+
export type NativeResourceWorkspaceProps<T extends Row = Row> = OldPage<NativeRecordViewProps<T>>;
|
|
526
|
+
|
|
527
|
+
/** @deprecated Renamed `NativeRecordView`, with `view` in place of `workspace`. */
|
|
528
|
+
export function NativeResourceWorkspace<T extends Row = Row>({ workspace, ...props }: NativeResourceWorkspaceProps<T>) {
|
|
529
|
+
return <NativeRecordView<T> {...props} view={workspace} />;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** @deprecated Renamed `NativeRecordViewMenu`, with `view` in place of `workspace`. */
|
|
533
|
+
export function NativeResourceWorkspaceMenu<T extends Row = Row>({ workspace, ...props }: OldPage<NativeRecordViewHeaderProps<T>> & { testID?: string }) {
|
|
534
|
+
return <NativeRecordViewMenu<T> {...props} view={workspace} />;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/** @deprecated Renamed `getNativeRecordViewStackOptions`, with `view` in place of `workspace`. */
|
|
538
|
+
export function getNativeResourceWorkspaceStackOptions<T extends Row>({
|
|
539
|
+
workspace,
|
|
540
|
+
...props
|
|
541
|
+
}: OldPage<NativeRecordViewHeaderProps<T>> & Omit<ScreenHeaderProps, "headerTitle">): Record<string, unknown> {
|
|
542
|
+
return getNativeRecordViewStackOptions<T>({ ...props, view: workspace });
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/** @deprecated Renamed `NativeRecordViewHeaderProps`; its `workspace` prop is now `view`. */
|
|
546
|
+
export type NativeResourceWorkspaceHeaderProps<T extends Row = Row> = OldPage<NativeRecordViewHeaderProps<T>>;
|
|
547
|
+
|
|
548
|
+
/** @deprecated Renamed `NativeRecordViewMessages`. */
|
|
549
|
+
export type NativeResourceWorkspaceMessages = NativeRecordViewMessages;
|
|
550
|
+
|
|
551
|
+
/** @deprecated Renamed `defaultNativeRecordViewMessages`. */
|
|
552
|
+
export const defaultNativeResourceWorkspaceMessages = defaultNativeRecordViewMessages;
|
|
553
|
+
|
|
519
554
|
/**
|
|
520
555
|
* A number as typed: "1.5" or, with a comma as the decimal mark, "1,5". Anything else (an empty
|
|
521
556
|
* string aside) is an error, never NaN.
|
|
@@ -542,7 +577,7 @@ function SettingRow({
|
|
|
542
577
|
value: unknown;
|
|
543
578
|
error?: string | undefined;
|
|
544
579
|
disabled: boolean;
|
|
545
|
-
words:
|
|
580
|
+
words: NativeRecordViewMessages;
|
|
546
581
|
onChange: (v: unknown, error?: string) => void;
|
|
547
582
|
testID: string;
|
|
548
583
|
}) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @feat:native-components
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
* - the title and the header menu come from
|
|
3
|
+
* NativeRecordView: one record's screen from a genesis-core page, natively.
|
|
4
|
+
* - the title and the header menu come from getNativeRecordViewStackOptions;
|
|
5
5
|
* - up to four sections are a segmented control, more are pushed rows;
|
|
6
6
|
* - a list section is an embedded NativeResourceView filtered by the row;
|
|
7
7
|
* - a settings section saves only its changed listed keys; a zero-row save is a refusal, inline;
|
|
@@ -13,8 +13,8 @@ import { useState, type ReactNode } from "react";
|
|
|
13
13
|
import { ActionSheetIOS, Alert, Platform } from "react-native";
|
|
14
14
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
15
15
|
import { defineResource, GenesisProvider, ViewerScope, type DataProvider, type ListParams, type Viewer } from "@marktiderman/genesis-core";
|
|
16
|
-
import { defineView,
|
|
17
|
-
import {
|
|
16
|
+
import { defineView, defineRecordView } from "@marktiderman/genesis-core/resource-view";
|
|
17
|
+
import { getNativeRecordViewStackOptions, NativeRecordView, NativeRecordViewMenu, parseSettingNumber } from "..";
|
|
18
18
|
|
|
19
19
|
type Brand = { id: string; name: string; website: string | null; public: boolean; is_default: boolean };
|
|
20
20
|
type Product = { id: string; brand_id: string; title: string };
|
|
@@ -56,7 +56,7 @@ const sections = [
|
|
|
56
56
|
{ id: "settings", label: "Settings", settings: ["website", "public"] as const },
|
|
57
57
|
{ id: "activity", label: "Activity" },
|
|
58
58
|
];
|
|
59
|
-
const
|
|
59
|
+
const page = defineRecordView(brandsView, {
|
|
60
60
|
sections,
|
|
61
61
|
readiness: (b) => [
|
|
62
62
|
{ title: "Has a website", ok: !!b.website, reason: "Add the website.", tone: "danger" },
|
|
@@ -95,20 +95,20 @@ beforeEach(() => {
|
|
|
95
95
|
(ActionSheetIOS.showActionSheetWithOptions as unknown as { mockClear: () => void }).mockClear();
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
-
describe("
|
|
98
|
+
describe("NativeRecordView", () => {
|
|
99
99
|
it("puts the title in the navigation bar and the actions in the header menu", () => {
|
|
100
|
-
const options =
|
|
100
|
+
const options = getNativeRecordViewStackOptions({ view: page, row: brand });
|
|
101
101
|
expect(options.title).toBe("Northwind");
|
|
102
102
|
expect(typeof options.headerRight).toBe("function");
|
|
103
|
-
const none =
|
|
104
|
-
expect(
|
|
103
|
+
const none = defineRecordView(brandsView, { header: { actions: [] }, sections: [{ id: "a", label: "A" }] });
|
|
104
|
+
expect(getNativeRecordViewStackOptions({ view: none, row: brand }).headerRight).toBeUndefined();
|
|
105
105
|
});
|
|
106
106
|
|
|
107
107
|
it("iOS: the menu is an action sheet; a refused action explains, a confirm asks, then the provider runs it", async () => {
|
|
108
108
|
const alert = vi.spyOn(Alert, "alert").mockImplementation(() => {});
|
|
109
109
|
const sheet = vi.spyOn(ActionSheetIOS, "showActionSheetWithOptions").mockImplementation(() => {});
|
|
110
110
|
const p = provider();
|
|
111
|
-
render(<
|
|
111
|
+
render(<NativeRecordViewMenu view={page} row={brand} />, { wrapper: wrap(p) });
|
|
112
112
|
fireEvent.click(screen.getByTestId("native-resource-workspace-menu"));
|
|
113
113
|
const [options, pick] = sheet.mock.calls[0]!;
|
|
114
114
|
expect(options.options).toEqual(["Archive (The default brand stays.)", "Publish", "Purge", "Cancel"]);
|
|
@@ -121,7 +121,7 @@ describe("NativeResourceWorkspace", () => {
|
|
|
121
121
|
|
|
122
122
|
alert.mockClear();
|
|
123
123
|
const onAction = vi.fn();
|
|
124
|
-
render(<
|
|
124
|
+
render(<NativeRecordViewMenu view={page} row={{ ...brand, is_default: false }} onAction={onAction} testID="menu2" />, {
|
|
125
125
|
wrapper: wrap(provider()),
|
|
126
126
|
});
|
|
127
127
|
fireEvent.click(screen.getByTestId("menu2"));
|
|
@@ -140,7 +140,7 @@ describe("NativeResourceWorkspace", () => {
|
|
|
140
140
|
const alert = vi.spyOn(Alert, "alert").mockImplementation(() => {});
|
|
141
141
|
try {
|
|
142
142
|
const p = provider();
|
|
143
|
-
render(<
|
|
143
|
+
render(<NativeRecordViewMenu view={page} row={{ ...brand, is_default: false }} />, { wrapper: wrap(p) });
|
|
144
144
|
fireEvent.click(screen.getByTestId("native-resource-workspace-menu"));
|
|
145
145
|
expect(alert).not.toHaveBeenCalled();
|
|
146
146
|
expect((await screen.findByTestId("native-resource-workspace-menu-purge")).textContent).toContain("Purge");
|
|
@@ -155,7 +155,7 @@ describe("NativeResourceWorkspace", () => {
|
|
|
155
155
|
let finish!: () => void;
|
|
156
156
|
const runAction = vi.fn(() => new Promise<{ data: null }>((resolve) => (finish = () => resolve({ data: null }))));
|
|
157
157
|
const sheet = vi.spyOn(ActionSheetIOS, "showActionSheetWithOptions").mockImplementation(() => {});
|
|
158
|
-
render(<
|
|
158
|
+
render(<NativeRecordViewMenu view={page} row={brand} />, { wrapper: wrap(provider({ runAction: runAction as never })) });
|
|
159
159
|
fireEvent.click(screen.getByTestId("native-resource-workspace-menu"));
|
|
160
160
|
const pick = sheet.mock.calls[0]![1];
|
|
161
161
|
pick(1);
|
|
@@ -168,9 +168,9 @@ describe("NativeResourceWorkspace", () => {
|
|
|
168
168
|
|
|
169
169
|
it("names accessibility actions with a prefix, so 'activate' can't take over the button", async () => {
|
|
170
170
|
const view = defineView(brands, { actions: [{ name: "activate", label: "Activate" }] });
|
|
171
|
-
const ws =
|
|
171
|
+
const ws = defineRecordView(view, { sections: [{ id: "a", label: "A" }] });
|
|
172
172
|
const p = provider();
|
|
173
|
-
render(<
|
|
173
|
+
render(<NativeRecordViewMenu view={ws} row={brand} />, { wrapper: wrap(p) });
|
|
174
174
|
const button = screen.getByTestId("native-resource-workspace-menu");
|
|
175
175
|
expect(button.getAttribute("data-a11y-actions")).toBe("action:activate");
|
|
176
176
|
// The platform's own "activate" does nothing here; the prefixed one runs the action.
|
|
@@ -181,16 +181,16 @@ describe("NativeResourceWorkspace", () => {
|
|
|
181
181
|
});
|
|
182
182
|
|
|
183
183
|
it("hides actions when nothing can run them, or the screen turns them off", () => {
|
|
184
|
-
render(<
|
|
184
|
+
render(<NativeRecordViewMenu view={page} row={brand} testID="none" />, { wrapper: wrap(provider({ runAction: undefined })) });
|
|
185
185
|
expect(screen.queryByTestId("none")).toBeNull();
|
|
186
186
|
const sheet = vi.spyOn(ActionSheetIOS, "showActionSheetWithOptions").mockImplementation(() => {});
|
|
187
|
-
render(<
|
|
187
|
+
render(<NativeRecordViewMenu view={page} row={brand} can={(name) => name !== "publish"} />, { wrapper: wrap(provider()) });
|
|
188
188
|
fireEvent.click(screen.getByTestId("native-resource-workspace-menu"));
|
|
189
189
|
expect(sheet.mock.calls.at(-1)![0].options).not.toContain("Publish");
|
|
190
190
|
});
|
|
191
191
|
|
|
192
192
|
it("shows up to four sections as a segmented control, and the readiness checks", () => {
|
|
193
|
-
render(<
|
|
193
|
+
render(<NativeRecordView view={page} row={brand} section="activity" slots={{ activity: <></> }} switcher={<></>} />, {
|
|
194
194
|
wrapper: wrap(provider()),
|
|
195
195
|
});
|
|
196
196
|
const control = screen.getByTestId("native-resource-workspace-sections");
|
|
@@ -202,7 +202,7 @@ describe("NativeResourceWorkspace", () => {
|
|
|
202
202
|
});
|
|
203
203
|
|
|
204
204
|
it("shows the summary chip alone once every check is met", () => {
|
|
205
|
-
render(<
|
|
205
|
+
render(<NativeRecordView view={page} row={{ ...brand, website: "https://x.example" }} section="activity" messages={{ ready: "Ready to go" }} />, {
|
|
206
206
|
wrapper: wrap(provider()),
|
|
207
207
|
});
|
|
208
208
|
expect(screen.getByTestId("native-resource-workspace-readiness").textContent).toBe("Ready to go");
|
|
@@ -210,11 +210,11 @@ describe("NativeResourceWorkspace", () => {
|
|
|
210
210
|
});
|
|
211
211
|
|
|
212
212
|
it("pushes sections when there are more than four", () => {
|
|
213
|
-
const wide =
|
|
213
|
+
const wide = defineRecordView(brandsView, {
|
|
214
214
|
sections: ["a", "b", "c", "d", "e"].map((id) => ({ id, label: id.toUpperCase() })),
|
|
215
215
|
});
|
|
216
216
|
const onOpenSection = vi.fn();
|
|
217
|
-
render(<
|
|
217
|
+
render(<NativeRecordView view={wide} row={brand} onOpenSection={onOpenSection} />, { wrapper: wrap(provider()) });
|
|
218
218
|
expect(screen.queryByTestId("native-resource-workspace-sections-option-a")).toBeNull();
|
|
219
219
|
fireEvent.click(screen.getByTestId("native-resource-workspace-section-e"));
|
|
220
220
|
expect(onOpenSection).toHaveBeenCalledWith("e");
|
|
@@ -222,7 +222,7 @@ describe("NativeResourceWorkspace", () => {
|
|
|
222
222
|
|
|
223
223
|
it("draws a list section as an embedded NativeResourceView filtered by the row", async () => {
|
|
224
224
|
const p = provider();
|
|
225
|
-
render(<
|
|
225
|
+
render(<NativeRecordView view={page} row={brand} />, { wrapper: wrap(p) });
|
|
226
226
|
expect(await screen.findByText("Kettle")).toBeTruthy();
|
|
227
227
|
const params = (p.getList as ReturnType<typeof vi.fn>).mock.calls[0]![1] as ListParams;
|
|
228
228
|
expect(params.filters).toContainEqual({ field: "brand_id", operator: "eq", value: "b1" });
|
|
@@ -231,7 +231,7 @@ describe("NativeResourceWorkspace", () => {
|
|
|
231
231
|
it("switches sections and saves only the changed listed keys", async () => {
|
|
232
232
|
const p = provider();
|
|
233
233
|
const onSaved = vi.fn();
|
|
234
|
-
render(<
|
|
234
|
+
render(<NativeRecordView view={page} row={brand} onSaved={onSaved} />, { wrapper: wrap(p) });
|
|
235
235
|
fireEvent.click(screen.getByTestId("native-resource-workspace-sections-option-settings"));
|
|
236
236
|
const input = screen.getByTestId("native-resource-workspace-settings-website-input");
|
|
237
237
|
fireEvent.change(input, { target: { value: "https://northwind.example" } });
|
|
@@ -242,15 +242,15 @@ describe("NativeResourceWorkspace", () => {
|
|
|
242
242
|
|
|
243
243
|
it("shows a zero-row save as a refusal, inline", async () => {
|
|
244
244
|
const p = provider({ update: vi.fn(async () => ({ data: null })) as unknown as DataProvider["update"] });
|
|
245
|
-
render(<
|
|
245
|
+
render(<NativeRecordView view={page} row={brand} section="settings" />, { wrapper: wrap(p) });
|
|
246
246
|
fireEvent.change(screen.getByTestId("native-resource-workspace-settings-website-input"), { target: { value: "https://x.example" } });
|
|
247
247
|
fireEvent.click(screen.getByTestId("native-resource-workspace-settings-save"));
|
|
248
248
|
expect((await screen.findByTestId("native-resource-workspace-settings-refusal")).textContent).toContain("This brand was not changed.");
|
|
249
249
|
});
|
|
250
250
|
});
|
|
251
251
|
|
|
252
|
-
describe("
|
|
253
|
-
const locking =
|
|
252
|
+
describe("NativeRecordView review fixes", () => {
|
|
253
|
+
const locking = defineRecordView(
|
|
254
254
|
defineView(brands, {
|
|
255
255
|
fields: { website: { visible: (v) => v["public"] === true } },
|
|
256
256
|
can: { update: (b) => (b.is_default ? "Locked." : true) },
|
|
@@ -259,14 +259,14 @@ describe("NativeResourceWorkspace review fixes", () => {
|
|
|
259
259
|
);
|
|
260
260
|
|
|
261
261
|
it("shows settings read-only with the reason when the view refuses update", () => {
|
|
262
|
-
render(<
|
|
262
|
+
render(<NativeRecordView view={locking} row={brand} />, { wrapper: wrap(provider()) });
|
|
263
263
|
expect(screen.getByTestId("native-resource-workspace-settings-locked").textContent).toContain("Locked.");
|
|
264
264
|
expect(screen.queryByTestId("native-resource-workspace-settings-save")).toBeNull();
|
|
265
265
|
});
|
|
266
266
|
|
|
267
267
|
it("leaves a field hidden by `visible` out of the save", async () => {
|
|
268
268
|
const p = provider();
|
|
269
|
-
render(<
|
|
269
|
+
render(<NativeRecordView view={locking} row={{ ...brand, is_default: false, public: true }} />, { wrapper: wrap(p) });
|
|
270
270
|
fireEvent.change(screen.getByTestId("native-resource-workspace-settings-website-input"), { target: { value: "https://x.example" } });
|
|
271
271
|
fireEvent.change(screen.getByTestId("native-resource-workspace-settings-name-input"), { target: { value: "Southwind" } });
|
|
272
272
|
fireEvent.click(screen.getByTestId("native-resource-workspace-settings-public-input"));
|
|
@@ -276,8 +276,8 @@ describe("NativeResourceWorkspace review fixes", () => {
|
|
|
276
276
|
});
|
|
277
277
|
|
|
278
278
|
it("opens a section in place with a way back when there are more than four and no onOpenSection", () => {
|
|
279
|
-
const wide =
|
|
280
|
-
render(<
|
|
279
|
+
const wide = defineRecordView(brandsView, { sections: ["a", "b", "c", "d", "e"].map((id) => ({ id, label: id.toUpperCase() })) });
|
|
280
|
+
render(<NativeRecordView view={wide} row={brand} slots={{ e: <p>Section E</p> }} />, { wrapper: wrap(provider()) });
|
|
281
281
|
fireEvent.click(screen.getByTestId("native-resource-workspace-section-e"));
|
|
282
282
|
expect(screen.getByText("Section E")).toBeTruthy();
|
|
283
283
|
fireEvent.click(screen.getByTestId("native-resource-workspace-all-sections"));
|
|
@@ -294,9 +294,9 @@ describe("NativeResourceWorkspace review fixes", () => {
|
|
|
294
294
|
});
|
|
295
295
|
});
|
|
296
296
|
|
|
297
|
-
describe("
|
|
297
|
+
describe("NativeRecordView when only the viewer's roles change", () => {
|
|
298
298
|
const isEditor = (v?: Viewer) => ((v?.roles as { teamRoles?: readonly string[] } | undefined)?.teamRoles ?? []).includes("editor");
|
|
299
|
-
const guarded =
|
|
299
|
+
const guarded = defineRecordView(
|
|
300
300
|
defineView(brands, {
|
|
301
301
|
can: { update: (_b, v) => (isEditor(v) ? true : "Only editors change brands.") },
|
|
302
302
|
actions: [{ name: "publish", label: "Publish", can: (_b, v) => (isEditor(v) ? true : "Only editors publish.") }],
|
|
@@ -318,8 +318,8 @@ describe("NativeResourceWorkspace when only the viewer's roles change", () => {
|
|
|
318
318
|
<Base>
|
|
319
319
|
<ViewerScope viewer="ann" roles={roles as never}>
|
|
320
320
|
<Mounts />
|
|
321
|
-
<
|
|
322
|
-
<
|
|
321
|
+
<NativeRecordViewMenu view={guarded} row={brand} />
|
|
322
|
+
<NativeRecordView view={guarded} row={brand} />
|
|
323
323
|
</ViewerScope>
|
|
324
324
|
</Base>
|
|
325
325
|
);
|