@marktiderman/genesis-ui-native 2.7.0 → 2.8.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 +11 -8
- package/dist/data/index.js +18 -12
- package/dist/data/index.js.map +1 -1
- package/package.json +2 -2
- package/src/data/resource-view/NativeResourceFormSheet.tsx +4 -1
- package/src/data/resource-view/__tests__/native-resource-view.test.tsx +32 -1
- package/src/data/resource-view/display.tsx +2 -2
- package/src/data/resource-view/index.ts +1 -0
- package/src/data/resource-view/mapping.ts +4 -5
- package/src/data/resource-view/use-native-resource-view.ts +17 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marktiderman/genesis-ui-native",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -69,7 +69,7 @@
|
|
|
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": "^1.
|
|
72
|
+
"@marktiderman/genesis-core": "^1.16.0",
|
|
73
73
|
"@marktiderman/genesis-design-system": "^1.5.0",
|
|
74
74
|
"@react-native-community/datetimepicker": ">=8.0.0",
|
|
75
75
|
"@shopify/flash-list": ">=2.0.0",
|
|
@@ -196,7 +196,10 @@ function FormField({
|
|
|
196
196
|
const error = f.formState.errors[field.key]?.message as string | undefined;
|
|
197
197
|
if (field.readOnly) {
|
|
198
198
|
// A link shows its raw id until batched relation labels arrive.
|
|
199
|
-
const
|
|
199
|
+
const value: unknown = f.getValues(field.key);
|
|
200
|
+
// A choice shows in the view's words (its `labels`).
|
|
201
|
+
const choice = field.options?.find((o) => o.value === String(value));
|
|
202
|
+
const shown = choice ? choice.label : displayText(field.display, value, messages, resolveZone(undefined, zones));
|
|
200
203
|
return (
|
|
201
204
|
<View testID={id} style={{ gap: 4 }} accessibilityLabel={`${field.label}, ${shown}`}>
|
|
202
205
|
<NativeLabel>{field.label}</NativeLabel>
|
|
@@ -56,6 +56,7 @@ import {
|
|
|
56
56
|
screenValues,
|
|
57
57
|
sectionsFor,
|
|
58
58
|
serverSort,
|
|
59
|
+
serverSorts,
|
|
59
60
|
formatInstant,
|
|
60
61
|
resolveZone,
|
|
61
62
|
swipeActionsFor,
|
|
@@ -229,6 +230,20 @@ describe("sections", () => {
|
|
|
229
230
|
]);
|
|
230
231
|
});
|
|
231
232
|
|
|
233
|
+
it("titles sections and form choices in the view's words", () => {
|
|
234
|
+
const labelled = defineView(brands, {
|
|
235
|
+
fields: { status: { labels: { active: "Published", draft: "In progress" } } },
|
|
236
|
+
form: ["name", "status"],
|
|
237
|
+
groupBy: "status",
|
|
238
|
+
});
|
|
239
|
+
expect(sectionsFor(labelled, ROWS, messages).map((s) => s.title)).toEqual(["Published", "Archived"]);
|
|
240
|
+
expect(formFieldsFor(labelled, "create").find((f) => f.key === "status")!.options).toEqual([
|
|
241
|
+
{ value: "active", label: "Published" },
|
|
242
|
+
{ value: "archived", label: "Archived" },
|
|
243
|
+
{ value: "draft", label: "In progress" },
|
|
244
|
+
]);
|
|
245
|
+
});
|
|
246
|
+
|
|
232
247
|
it("is one untitled section without groupBy, and none for no rows", () => {
|
|
233
248
|
expect(sectionsFor(summaryView, ROWS, messages).map((s) => s.title)).toEqual([""]);
|
|
234
249
|
expect(sectionsFor(summaryView, [], messages)).toEqual([]);
|
|
@@ -759,7 +774,7 @@ describe("paging and search", () => {
|
|
|
759
774
|
it("keeps at most 10 pages loaded by default, and takes the screen's number", () => {
|
|
760
775
|
hooks.infiniteOptions.length = 0;
|
|
761
776
|
renderHook(() => useNativeResourceView(plainView), { wrapper: wrap(provider()) });
|
|
762
|
-
expect(hooks.infiniteOptions.at(-1)).toMatchObject({ maxPages: 10,
|
|
777
|
+
expect(hooks.infiniteOptions.at(-1)).toMatchObject({ maxPages: 10, defaultSorts: [{ field: "status", order: "asc" }, { field: "name", order: "asc" }] });
|
|
763
778
|
renderHook(() => useNativeResourceView(summaryView, { maxPages: 3 }), { wrapper: wrap(provider()) });
|
|
764
779
|
expect(hooks.infiniteOptions.at(-1)).toMatchObject({ maxPages: 3 });
|
|
765
780
|
});
|
|
@@ -774,6 +789,22 @@ describe("paging and search", () => {
|
|
|
774
789
|
expect(serverSort(summaryView)).toBeUndefined();
|
|
775
790
|
});
|
|
776
791
|
|
|
792
|
+
it("sorts within each group by orderBy: the group first, then the order", () => {
|
|
793
|
+
const grouped = defineView(brands, { groupBy: "status", orderBy: { field: "name", direction: "desc" } });
|
|
794
|
+
expect(serverSorts(grouped)).toEqual([
|
|
795
|
+
{ field: "status", order: "asc" },
|
|
796
|
+
{ field: "name", order: "desc" },
|
|
797
|
+
]);
|
|
798
|
+
// A provider that sorts by one field keeps the sections whole.
|
|
799
|
+
expect(serverSort(grouped)).toEqual({ field: "status", order: "asc" });
|
|
800
|
+
expect(serverSorts(defineView(brands, { groupBy: "status" }))).toEqual([{ field: "status", order: "asc" }]);
|
|
801
|
+
expect(serverSorts(summaryView)).toEqual([]);
|
|
802
|
+
hooks.infiniteOptions.length = 0;
|
|
803
|
+
renderHook(() => useNativeResourceView(grouped), { wrapper: wrap(provider()) });
|
|
804
|
+
expect(hooks.infiniteOptions.at(-1)).toMatchObject({ defaultSorts: serverSorts(grouped) });
|
|
805
|
+
expect(hooks.infiniteOptions.at(-1)).not.toHaveProperty("defaultSort");
|
|
806
|
+
});
|
|
807
|
+
|
|
777
808
|
it("doesn't redraw rows while the person types", async () => {
|
|
778
809
|
const summary = vi.fn((r: Brand) => ({ title: r.name }));
|
|
779
810
|
const counted = defineView(brands, { summary });
|
|
@@ -11,7 +11,7 @@ import { titleCase } from "@marktiderman/genesis-core";
|
|
|
11
11
|
import { isTimeZone, personTimeZone, wallClock } from "@marktiderman/genesis-core/dates";
|
|
12
12
|
import { useViewer } from "@marktiderman/genesis-core/hooks";
|
|
13
13
|
import { isSafeUrl } from "@marktiderman/genesis-core/urls";
|
|
14
|
-
import type
|
|
14
|
+
import { valueText, type DisplayHint, type ResolvedColumn, type ViewTone } from "@marktiderman/genesis-core/resource-view";
|
|
15
15
|
import { NativeBadge } from "../../components/badge";
|
|
16
16
|
import { NativeImage } from "../../components/image";
|
|
17
17
|
import { NativeText } from "../../components/text";
|
|
@@ -172,7 +172,7 @@ export const nativeDisplayRenderers: DisplayRenderers = {
|
|
|
172
172
|
percent: text("percent"),
|
|
173
173
|
boolean: text("boolean"),
|
|
174
174
|
badge: (value, { column }) => (
|
|
175
|
-
<NativeBadge variant={badgeVariant(column.tones?.[String(value)])}>{
|
|
175
|
+
<NativeBadge variant={badgeVariant(column.tones?.[String(value)])}>{valueText(column.labels, value)}</NativeBadge>
|
|
176
176
|
),
|
|
177
177
|
date: text("date"),
|
|
178
178
|
datetime: text("datetime"),
|
|
@@ -85,7 +85,9 @@ export function sectionsFor<T extends Row>(
|
|
|
85
85
|
if (!group) {
|
|
86
86
|
const title = isEmptyValue(value)
|
|
87
87
|
? fill(messages.list.noGroup, { label: view.label(field).toLowerCase() })
|
|
88
|
-
:
|
|
88
|
+
: display === "badge"
|
|
89
|
+
? view.valueLabel(field, value)
|
|
90
|
+
: displayText(display, value, messages);
|
|
89
91
|
group = { key, title, data: [] };
|
|
90
92
|
groups.set(key, group);
|
|
91
93
|
}
|
|
@@ -244,7 +246,7 @@ export function formFieldsFor<T extends Row>(view: AnyResourceView<T>, mode: "cr
|
|
|
244
246
|
.filter((f) => f.display !== "json" && !f.field.array)
|
|
245
247
|
.filter((f) => mode === "edit" || writable.has(f.field.name))
|
|
246
248
|
.map((f) => {
|
|
247
|
-
const options = f.
|
|
249
|
+
const options = f.options?.map((o) => ({ value: o.value, label: o.label }));
|
|
248
250
|
const readOnly = !writable.has(f.field.name);
|
|
249
251
|
return {
|
|
250
252
|
key: f.field.name,
|
|
@@ -262,9 +264,6 @@ export function formFieldsFor<T extends Row>(view: AnyResourceView<T>, mode: "cr
|
|
|
262
264
|
});
|
|
263
265
|
}
|
|
264
266
|
|
|
265
|
-
// Only `badge` is formatted here, which reads no messages.
|
|
266
|
-
const EMPTY_WORDS = { common: { empty: "", yes: "", no: "" } } as NativeResourceViewMessages;
|
|
267
|
-
|
|
268
267
|
/** Fields shown for these values: a field whose `visible` says no is hidden, and isn't saved. */
|
|
269
268
|
export function shownFields(fields: readonly NativeFormField[], values: Readonly<Record<string, unknown>>): NativeFormField[] {
|
|
270
269
|
return fields.filter((f) => !f.visible || f.visible(values));
|
|
@@ -124,18 +124,25 @@ export function screenValues(...filters: (Filter | undefined)[]): Record<string,
|
|
|
124
124
|
/** Pages kept loaded by default (cursor paging). */
|
|
125
125
|
export const DEFAULT_MAX_PAGES = 10;
|
|
126
126
|
|
|
127
|
+
type ServerSort = { field: string; order: "asc" | "desc" };
|
|
128
|
+
|
|
127
129
|
/**
|
|
128
|
-
* The server
|
|
129
|
-
* page never lands in the middle of an earlier section
|
|
130
|
-
*
|
|
130
|
+
* The server sorts: the `groupBy` field first, so every page arrives in section order and a later
|
|
131
|
+
* page never lands in the middle of an earlier section, then the view's `orderBy` within each
|
|
132
|
+
* group. A provider without `multiSort` applies only the first.
|
|
131
133
|
*/
|
|
132
|
-
export function
|
|
134
|
+
export function serverSorts<T extends Row>(view: AnyResourceView<T>): ServerSort[] {
|
|
133
135
|
const groupBy = view.definition.groupBy as string | undefined;
|
|
134
136
|
const orderBy = view.definition.orderBy;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (
|
|
138
|
-
return
|
|
137
|
+
const order: ServerSort | undefined = orderBy ? { field: orderBy.field as string, order: orderBy.direction ?? "asc" } : undefined;
|
|
138
|
+
if (!groupBy) return order ? [order] : [];
|
|
139
|
+
if (order?.field === groupBy) return [order];
|
|
140
|
+
return [{ field: groupBy, order: "asc" }, ...(order ? [order] : [])];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The first of {@link serverSorts}: what a provider that sorts by one field gets. */
|
|
144
|
+
export function serverSort<T extends Row>(view: AnyResourceView<T>): ServerSort | undefined {
|
|
145
|
+
return serverSorts(view)[0];
|
|
139
146
|
}
|
|
140
147
|
|
|
141
148
|
export function useNativeResourceView<T extends Row = Row>(
|
|
@@ -153,14 +160,14 @@ export function useNativeResourceView<T extends Row = Row>(
|
|
|
153
160
|
// By value, so an inline `filter={{ client_id }}` doesn't rebuild the form every render.
|
|
154
161
|
const createKey = JSON.stringify(screenValues(tabFilter, filter));
|
|
155
162
|
const createDefaults = useMemo(() => JSON.parse(createKey) as Record<string, string | number | boolean>, [createKey]);
|
|
156
|
-
const
|
|
163
|
+
const sorts = useMemo(() => serverSorts(view), [view]);
|
|
157
164
|
const searchFields = useMemo(
|
|
158
165
|
() => searchOption ?? (view.labelField ? [view.labelField] : undefined),
|
|
159
166
|
[searchOption, view.labelField],
|
|
160
167
|
);
|
|
161
168
|
const pages = useInfiniteResource<T & BaseRecord>(view.resource, {
|
|
162
169
|
defaultFilters,
|
|
163
|
-
...(
|
|
170
|
+
...(sorts.length ? { defaultSorts: sorts } : {}),
|
|
164
171
|
...(perPage ? { perPage } : {}),
|
|
165
172
|
maxPages,
|
|
166
173
|
...(searchFields ? { searchFields } : {}),
|