@marktiderman/genesis-ui-native 3.0.0 → 3.1.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 +7 -2
- package/dist/data/index.js +19 -4
- package/dist/data/index.js.map +1 -1
- package/package.json +51 -3
- package/src/data/resource-view/__tests__/native-resource-view.test.tsx +45 -0
- package/src/data/resource-view/mapping.ts +18 -1
- package/src/data/resource-view/use-native-resource-view.ts +18 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marktiderman/genesis-ui-native",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -69,8 +69,8 @@
|
|
|
69
69
|
"@expo/ui": ">=58.0.0",
|
|
70
70
|
"@gorhom/bottom-sheet": ">=5.0.0",
|
|
71
71
|
"@hookform/resolvers": "^3.0.0 || ^4.0.0 || ^5.0.0",
|
|
72
|
-
"@marktiderman/genesis-core": "^2.
|
|
73
|
-
"@marktiderman/genesis-design-system": "^1.5.
|
|
72
|
+
"@marktiderman/genesis-core": "^2.1.0",
|
|
73
|
+
"@marktiderman/genesis-design-system": "^1.5.1",
|
|
74
74
|
"@react-native-community/datetimepicker": ">=8.0.0",
|
|
75
75
|
"@shopify/flash-list": ">=2.0.0",
|
|
76
76
|
"@tanstack/react-query": "^5.0.0",
|
|
@@ -146,6 +146,54 @@
|
|
|
146
146
|
"typescript": "~6.0.3",
|
|
147
147
|
"vitest": "^4.1.12"
|
|
148
148
|
},
|
|
149
|
+
"genesis": {
|
|
150
|
+
"doctor": {
|
|
151
|
+
"prefer": [
|
|
152
|
+
{
|
|
153
|
+
"id": "lists-use-native-resource-view",
|
|
154
|
+
"need": "a list screen on the phone",
|
|
155
|
+
"level": "warn",
|
|
156
|
+
"platform": "native",
|
|
157
|
+
"newOnly": true,
|
|
158
|
+
"files": [
|
|
159
|
+
"**/*.tsx",
|
|
160
|
+
"**/*.jsx"
|
|
161
|
+
],
|
|
162
|
+
"forbid": {
|
|
163
|
+
"jsx": [
|
|
164
|
+
"FlatList",
|
|
165
|
+
"SectionList",
|
|
166
|
+
"NativeResourcePage"
|
|
167
|
+
]
|
|
168
|
+
},
|
|
169
|
+
"unless": {
|
|
170
|
+
"renders": [
|
|
171
|
+
"NativeResourceView",
|
|
172
|
+
"NativeResourceWorkspace"
|
|
173
|
+
]
|
|
174
|
+
},
|
|
175
|
+
"use": "the web screen's view (defineView) rendered by <NativeResourceView>",
|
|
176
|
+
"import": "@marktiderman/genesis-ui-native/data",
|
|
177
|
+
"docs": "https://github.com/marktiderman/genesis/blob/main/docs/RESOURCE-VIEW.md#native",
|
|
178
|
+
"why": "The phone list comes from the same view as the web list: one description, two surfaces, no second set of helpers."
|
|
179
|
+
}
|
|
180
|
+
],
|
|
181
|
+
"primitives": [
|
|
182
|
+
{
|
|
183
|
+
"need": "a phone screen",
|
|
184
|
+
"use": "ScreenContainer, or a …Page / …Screen layout",
|
|
185
|
+
"import": "@marktiderman/genesis-ui-native/layouts",
|
|
186
|
+
"docs": "https://github.com/marktiderman/genesis/blob/main/packages/ui-native/README.md"
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
"need": "a record page on the phone",
|
|
190
|
+
"use": "NativeResourceWorkspace",
|
|
191
|
+
"import": "@marktiderman/genesis-ui-native/data",
|
|
192
|
+
"docs": "https://github.com/marktiderman/genesis/blob/main/docs/RESOURCE-VIEW.md#resourceworkspace"
|
|
193
|
+
}
|
|
194
|
+
]
|
|
195
|
+
}
|
|
196
|
+
},
|
|
149
197
|
"scripts": {
|
|
150
198
|
"build": "tsup",
|
|
151
199
|
"dev": "tsup --watch",
|
|
@@ -529,6 +529,51 @@ describe("form sheet", () => {
|
|
|
529
529
|
});
|
|
530
530
|
});
|
|
531
531
|
|
|
532
|
+
it("treats an empty input as the web form does, from the contract's facts", () => {
|
|
533
|
+
type Known = { id: string; name: string; code: string; made_by: string; notes: string | null; due_on: string | null };
|
|
534
|
+
const known = defineResource<Known>()({
|
|
535
|
+
name: "known",
|
|
536
|
+
key: "id",
|
|
537
|
+
labelField: "name",
|
|
538
|
+
fields: [
|
|
539
|
+
{ name: "id", type: "string", hasDefault: true },
|
|
540
|
+
{ name: "name", type: "string", nullable: false },
|
|
541
|
+
{ name: "code", type: "string", nullable: false, optional: true },
|
|
542
|
+
{ name: "made_by", type: "string", nullable: false, hasDefault: true },
|
|
543
|
+
{ name: "notes", type: "string", nullable: true },
|
|
544
|
+
{ name: "due_on", type: "datetime", dbType: "date", nullable: true },
|
|
545
|
+
],
|
|
546
|
+
});
|
|
547
|
+
const knownView = defineView(known, { form: ["name", "code", "made_by", "notes", "due_on"] });
|
|
548
|
+
const blank = { name: "a", code: "", made_by: "", notes: "", due_on: "" };
|
|
549
|
+
const create = formFieldsFor(knownView, "create");
|
|
550
|
+
expect(create.find((f) => f.key === "notes")).toMatchObject({ nullable: true });
|
|
551
|
+
expect(create.find((f) => f.key === "made_by")).toMatchObject({ nullable: false, hasDefault: true });
|
|
552
|
+
// A create leaves blanks out (defaults apply); a not-null text without a default keeps "".
|
|
553
|
+
expect(writableValues(knownView, "create", create, blank)).toEqual({ name: "a", code: "" });
|
|
554
|
+
expect(writableValues(knownView, "edit", formFieldsFor(knownView, "edit"), blank)).toEqual({
|
|
555
|
+
name: "a", code: "", made_by: "", notes: null, due_on: null,
|
|
556
|
+
});
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
it("sends a list filter as `in`, and a create never takes it", async () => {
|
|
560
|
+
const p = provider();
|
|
561
|
+
const getList = vi.spyOn(p, "getList");
|
|
562
|
+
const { result } = renderHook(() => useNativeResourceView(plainView, { filter: { status: ["active", "archived"], practice_id: "p1" } }), {
|
|
563
|
+
wrapper: wrap(p),
|
|
564
|
+
});
|
|
565
|
+
await waitFor(() => expect(getList).toHaveBeenCalled());
|
|
566
|
+
expect(getList.mock.calls.at(-1)![1].filters).toEqual(
|
|
567
|
+
expect.arrayContaining([
|
|
568
|
+
{ field: "status", operator: "in", value: ["active", "archived"] },
|
|
569
|
+
{ field: "practice_id", operator: "eq", value: "p1" },
|
|
570
|
+
]),
|
|
571
|
+
);
|
|
572
|
+
expect(result.current.formFields("create").some((f) => f.key === "practice_id")).toBe(false);
|
|
573
|
+
expect(result.current.formFields("create").some((f) => f.key === "status")).toBe(true);
|
|
574
|
+
expect(screenValues({ status: ["a"], practice_id: "p1" })).toEqual({ practice_id: "p1" });
|
|
575
|
+
});
|
|
576
|
+
|
|
532
577
|
it("shows an empty date as a button to add one, and a set date can be cleared", async () => {
|
|
533
578
|
render(<NativeResourceView view={plainView} />, { wrapper: wrap(provider()) });
|
|
534
579
|
fireEvent.click(await screen.findByTestId("native-resource-view-create"));
|
|
@@ -196,6 +196,10 @@ export interface NativeFormField {
|
|
|
196
196
|
required: boolean;
|
|
197
197
|
/** Shown as text, never as an input, and never sent. */
|
|
198
198
|
readOnly: boolean;
|
|
199
|
+
/** From the contract: the column takes null. Unset when the contract doesn't say. */
|
|
200
|
+
nullable?: boolean;
|
|
201
|
+
/** From the contract: the column has a default (or is generated). */
|
|
202
|
+
hasDefault?: boolean;
|
|
199
203
|
max?: number;
|
|
200
204
|
options?: { value: string; label: string }[];
|
|
201
205
|
/** For a link: what it points at and the linked row's naming field, so it shows the row's words. */
|
|
@@ -259,6 +263,8 @@ export function formFieldsFor<T extends Row>(view: AnyResourceView<T>, mode: "cr
|
|
|
259
263
|
display: f.display,
|
|
260
264
|
required: !readOnly && f.required,
|
|
261
265
|
readOnly,
|
|
266
|
+
...(typeof f.field.nullable === "boolean" ? { nullable: f.field.nullable } : {}),
|
|
267
|
+
...(f.field.hasDefault || f.field.generated ? { hasDefault: true } : {}),
|
|
262
268
|
...(f.max !== undefined ? { max: f.max } : {}),
|
|
263
269
|
...(options ? { options } : {}),
|
|
264
270
|
...(f.field.references ? { references: f.field.references } : {}),
|
|
@@ -295,11 +301,22 @@ export function writableValues<T extends Row>(
|
|
|
295
301
|
if (field.readOnly || !writable.has(field.key)) continue;
|
|
296
302
|
if (dirty && !dirty.has(field.key)) continue;
|
|
297
303
|
const value = values[field.key];
|
|
298
|
-
|
|
304
|
+
if ((value !== "" && value !== undefined) || field.required) {
|
|
305
|
+
out[field.key] = value;
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
// Empty, as the web form treats it: a text column the contract says can't be null keeps ""
|
|
309
|
+
// (on a create, only without a default); otherwise a create leaves it out, so the column's
|
|
310
|
+
// default applies, and an edit clears it. Without the contract's word, null, as before.
|
|
311
|
+
if (field.nullable === undefined) out[field.key] = null;
|
|
312
|
+
else if (TEXT_KINDS.has(field.kind) && !field.nullable && !(mode === "create" && field.hasDefault)) out[field.key] = "";
|
|
313
|
+
else if (mode === "edit") out[field.key] = null;
|
|
299
314
|
}
|
|
300
315
|
return out;
|
|
301
316
|
}
|
|
302
317
|
|
|
318
|
+
const TEXT_KINDS: ReadonlySet<NativeFormFieldKind> = new Set(["text", "email", "url", "textarea"]);
|
|
319
|
+
|
|
303
320
|
/** Field errors for a save: required fields with no value, and text over `max`. */
|
|
304
321
|
export function validateValues(
|
|
305
322
|
fields: readonly NativeFormField[],
|
|
@@ -28,7 +28,7 @@ import {
|
|
|
28
28
|
import { fill } from "./messages";
|
|
29
29
|
|
|
30
30
|
type Row = Record<string, unknown>;
|
|
31
|
-
type Filter = Readonly<Record<string, string | number | boolean | null>>;
|
|
31
|
+
type Filter = Readonly<Record<string, string | number | boolean | null | readonly (string | number)[]>>;
|
|
32
32
|
|
|
33
33
|
/** How long typing waits before the search goes to the server. */
|
|
34
34
|
export const SEARCH_DEBOUNCE_MS = 300;
|
|
@@ -37,7 +37,8 @@ export interface NativeResourceViewOptions<T extends Row = Row> {
|
|
|
37
37
|
/** A tab the view defines: its server filter. */
|
|
38
38
|
preset?: string;
|
|
39
39
|
/**
|
|
40
|
-
*
|
|
40
|
+
* Filters sent to the server (`{ client_id: id }`); `null` means "is empty" and a list means
|
|
41
|
+
* "any of" (`{ status: ["open", "blocked"] }`, sent as `in`; a create never takes a list). A create
|
|
41
42
|
* also saves every non-null value here (a tab's filter too), even for read-only fields such as
|
|
42
43
|
* a required link: they come from the screen, never from an input. An edit never sends them.
|
|
43
44
|
*/
|
|
@@ -107,8 +108,12 @@ export interface UseNativeResourceViewReturn<T extends Row = Row> {
|
|
|
107
108
|
}
|
|
108
109
|
|
|
109
110
|
function filterParams(filter: Filter | undefined): FilterParam[] {
|
|
110
|
-
return Object.entries(filter ?? {}).map(([field, value]) =>
|
|
111
|
-
value === null
|
|
111
|
+
return Object.entries(filter ?? {}).map(([field, value]): FilterParam =>
|
|
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 },
|
|
112
117
|
);
|
|
113
118
|
}
|
|
114
119
|
|
|
@@ -116,7 +121,9 @@ function filterParams(filter: Filter | undefined): FilterParam[] {
|
|
|
116
121
|
export function screenValues(...filters: (Filter | undefined)[]): Record<string, string | number | boolean> {
|
|
117
122
|
const out: Record<string, string | number | boolean> = {};
|
|
118
123
|
for (const filter of filters) {
|
|
119
|
-
for (const [field, value] of Object.entries(filter ?? {}))
|
|
124
|
+
for (const [field, value] of Object.entries(filter ?? {})) {
|
|
125
|
+
if (value !== null && !Array.isArray(value)) out[field] = value as string | number | boolean;
|
|
126
|
+
}
|
|
120
127
|
}
|
|
121
128
|
return out;
|
|
122
129
|
}
|
|
@@ -156,7 +163,12 @@ export function useNativeResourceView<T extends Row = Row>(
|
|
|
156
163
|
|
|
157
164
|
// ── Reads: cursor pages, server filters, server search ──────────────────
|
|
158
165
|
const tabFilter = tab?.filter as Filter | undefined;
|
|
159
|
-
|
|
166
|
+
// By value: an inline `filter={{ status: ["open", "done"] }}` is one list, not a new one per render.
|
|
167
|
+
const filterKey = JSON.stringify(filter ?? {});
|
|
168
|
+
const defaultFilters = useMemo(
|
|
169
|
+
() => [...filterParams(tabFilter), ...filterParams(JSON.parse(filterKey) as Filter)],
|
|
170
|
+
[tabFilter, filterKey],
|
|
171
|
+
);
|
|
160
172
|
// By value, so an inline `filter={{ client_id }}` doesn't rebuild the form every render.
|
|
161
173
|
const createKey = JSON.stringify(screenValues(tabFilter, filter));
|
|
162
174
|
const createDefaults = useMemo(() => JSON.parse(createKey) as Record<string, string | number | boolean>, [createKey]);
|