@marktiderman/genesis-ui-native 2.8.1 → 3.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marktiderman/genesis-ui-native",
3
- "version": "2.8.1",
3
+ "version": "3.0.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.16.1",
72
+ "@marktiderman/genesis-core": "^2.0.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",
@@ -8,6 +8,7 @@ import { useEffect, useMemo, useRef, useState } from "react";
8
8
  import { Pressable, Text, View } from "react-native";
9
9
  import { Controller, useForm } from "react-hook-form";
10
10
  import type { ResourceFormFieldDef } from "@marktiderman/genesis-core";
11
+ import { useRelationLabel } from "@marktiderman/genesis-core/hooks";
11
12
  import { NativeButton } from "../../components/button";
12
13
  import { NativeDatePicker, NativeTimePicker } from "../../components/date-time-picker";
13
14
  import { NativeInlineAlert } from "../../components/inline-alert";
@@ -18,8 +19,8 @@ import { NativeSheet, NativeSheetScrollView } from "../../components/sheet";
18
19
  import { NativeText } from "../../components/text";
19
20
  import { NavBarSaveButton } from "../../layouts/nav-bar";
20
21
  import { NativeResourceFormFieldRenderer } from "../NativeResourceFormField";
21
- import { dateToDay, dayToDate, displayText, resolveZone, useDisplayTimeZones, useNativeResourceViewMessages } from "./display";
22
- import { fill } from "./messages";
22
+ import { dateToDay, dayToDate, displayText, isEmptyValue, resolveZone, useDisplayTimeZones, useNativeResourceViewMessages } from "./display";
23
+ import { defaultNativeResourceViewMessages, fill } from "./messages";
23
24
  import { shownFields, type NativeFormField } from "./mapping";
24
25
  import type { SaveResult } from "./use-native-resource-view";
25
26
 
@@ -194,12 +195,23 @@ function FormField({
194
195
  const messages = useNativeResourceViewMessages();
195
196
  const zones = useDisplayTimeZones();
196
197
  const error = f.formState.errors[field.key]?.message as string | undefined;
198
+ // Before any return, as every hook must be. Only a read-only link asks.
199
+ const linkLabel = useRelationLabel(field.references, f.getValues(field.key), {
200
+ ...(field.labelField ? { labelField: field.labelField } : {}),
201
+ enabled: field.readOnly && field.display === "relation",
202
+ });
197
203
  if (field.readOnly) {
198
- // A link shows its raw id until batched relation labels arrive.
199
204
  const value: unknown = f.getValues(field.key);
200
- // A choice shows in the view's words (its `labels`).
205
+ // A choice shows in the view's words (its `labels`), a link as the linked row's words.
201
206
  const choice = field.options?.find((o) => o.value === String(value));
202
- const shown = choice ? choice.label : displayText(field.display, value, messages, resolveZone(undefined, zones));
207
+ const linked = field.references && field.display === "relation" && !isEmptyValue(value);
208
+ const shown = choice
209
+ ? choice.label
210
+ : linked
211
+ ? linkLabel === undefined
212
+ ? messages.common.empty
213
+ : (linkLabel ?? field.missingLabel ?? messages.common.hiddenLink ?? defaultNativeResourceViewMessages.common.hiddenLink!)
214
+ : displayText(field.display, value, messages, resolveZone(undefined, zones));
203
215
  return (
204
216
  <View testID={id} style={{ gap: 4 }} accessibilityLabel={`${field.label}, ${shown}`}>
205
217
  <NativeLabel>{field.label}</NativeLabel>
@@ -17,8 +17,8 @@ import { memo, useCallback, useMemo, useRef, useState, type ComponentType, type
17
17
  import { Alert, Platform, Pressable, SectionList, View } from "react-native";
18
18
  import { SafeAreaView } from "react-native-safe-area-context";
19
19
  import { encodeRecordKey, type BaseRecord, type DataRow } from "@marktiderman/genesis-core";
20
- import { isTimeZone, personTimeZone } from "@marktiderman/genesis-core/dates";
21
- import { useOne, useOptionalViewerScope } from "@marktiderman/genesis-core/hooks";
20
+ import { personTimeZone } from "@marktiderman/genesis-core/dates";
21
+ import { scopeClock, useOne, useOptionalViewerScope } from "@marktiderman/genesis-core/hooks";
22
22
  import type { RowPicture } from "@marktiderman/genesis-core/resource-view";
23
23
  import { NativeAvatar } from "../../components/avatar";
24
24
  import { NativeBadge } from "../../components/badge";
@@ -78,10 +78,11 @@ export interface NativeResourceViewProps<T extends Row = Row> extends NativeReso
78
78
  /** Hide the search box. Shown by default when the view has something to search. */
79
79
  searchable?: boolean;
80
80
  /**
81
- * The practice's IANA zone, for columns with `timeZone: "practice"`. Timestamps otherwise read
82
- * on the viewer's clock (ViewerScope's zone, else the device's).
81
+ * The IANA zone of whatever this screen is scoped to (a location, a league, a team), for columns
82
+ * with `timeZone: "scope"`. Default: ViewerScope's `scopeTimeZone`, else the viewer's clock.
83
+ * Timestamps on other columns read on the viewer's clock (ViewerScope's zone, else the device's).
83
84
  */
84
- practiceTimeZone?: string;
85
+ scopeTimeZone?: string;
85
86
  testID?: string;
86
87
  }
87
88
 
@@ -110,11 +111,12 @@ const recordKey = (view: AnyResourceView<Row>, row: Row) => {
110
111
  export function NativeResourceView<T extends Row = Row>(props: NativeResourceViewProps<T>) {
111
112
  const scope = useOptionalViewerScope();
112
113
  const viewer = scope?.timeZone;
113
- const { practiceTimeZone } = props;
114
+ const scopeZone = props.scopeTimeZone ?? scope?.scopeTimeZone;
114
115
  const zones = useMemo<DisplayTimeZones>(() => {
115
116
  const own = personTimeZone({ timeZone: viewer });
116
- return { viewer: own, practice: practiceTimeZone && isTimeZone(practiceTimeZone) ? practiceTimeZone : own };
117
- }, [viewer, practiceTimeZone]);
117
+ // An unknown zone is loud (core's scopeClock), never a quiet fall back to the viewer's clock.
118
+ return { viewer: own, scope: scopeClock(scopeZone, own) };
119
+ }, [viewer, scopeZone]);
118
120
  return (
119
121
  <DisplayTimeZonesProvider value={zones}>
120
122
  <Screen {...props} />
@@ -256,7 +258,7 @@ function Screen<T extends Row = Row>(props: NativeResourceViewProps<T>) {
256
258
  header,
257
259
  emptyState,
258
260
  searchable,
259
- practiceTimeZone: _zone,
261
+ scopeTimeZone: _zone,
260
262
  testID = "native-resource-view",
261
263
  ...options
262
264
  } = props;
@@ -51,6 +51,7 @@ import {
51
51
  formFieldsFor,
52
52
  formatDay,
53
53
  NativeResourceView,
54
+ NativeResourceViewMessagesProvider,
54
55
  openableUrl,
55
56
  rowItem,
56
57
  screenValues,
@@ -559,7 +560,7 @@ describe("links and the screen's filter", () => {
559
560
  const questionProvider = () =>
560
561
  provider({ getList: vi.fn(async () => ({ data: [QUESTION], total: 1 })) as unknown as DataProvider["getList"] });
561
562
 
562
- it("shows a read-only link as its label and raw id, never an input", async () => {
563
+ it("shows a read-only link as text, never an input, and words when its row is hidden", async () => {
563
564
  render(<NativeResourceView view={questionsView} filter={{ form_id: "f1" }} />, { wrapper: wrap(questionProvider()) });
564
565
  const row = await screen.findByTestId("native-resource-view-row-q1");
565
566
  act(() => {
@@ -568,10 +569,45 @@ describe("links and the screen's filter", () => {
568
569
  const field = await screen.findByTestId("native-resource-view-form-field-form_id");
569
570
  expect(field.querySelector("input")).toBeNull();
570
571
  expect(field.textContent).toContain("Form");
571
- expect(screen.getByTestId("native-resource-view-form-field-form_id-value").textContent).toBe("f1");
572
+ // getList answers every resource with the question, so form f1 isn't found: words, not "f1".
573
+ await waitFor(() => expect(screen.getByTestId("native-resource-view-form-field-form_id-value").textContent).toBe("Not available"));
572
574
  expect(formFieldsFor(questionsView, "create").map((f) => f.key)).toEqual(["prompt"]);
573
575
  });
574
576
 
577
+ it("shows a read-only link in the linked row's words once they arrive", async () => {
578
+ const p = provider({
579
+ getList: vi.fn(async (resource: string) =>
580
+ resource === "forms" ? { data: [{ id: "f1", title: "Exit survey" }], total: -1 } : { data: [QUESTION], total: 1 },
581
+ ) as unknown as DataProvider["getList"],
582
+ });
583
+ render(<NativeResourceView view={questionsView} filter={{ form_id: "f1" }} />, { wrapper: wrap(p) });
584
+ const row = await screen.findByTestId("native-resource-view-row-q1");
585
+ act(() => {
586
+ row.dispatchEvent(new CustomEvent("accessibilityaction", { detail: "edit" }));
587
+ });
588
+ await waitFor(() => expect(screen.getByTestId("native-resource-view-form-field-form_id-value").textContent).toBe("Exit survey"));
589
+ const asks = (p.getList as ReturnType<typeof vi.fn>).mock.calls.filter(([r]) => r === "forms");
590
+ expect(asks).toHaveLength(1);
591
+ expect(asks[0]![1].filters).toEqual([{ field: "id", operator: "in", value: ["f1"] }]);
592
+ });
593
+
594
+ it("falls back to the English for a hidden link when an older catalogue has no hiddenLink", async () => {
595
+ const { common, ...rest } = messages;
596
+ const { hiddenLink: _dropped, ...older } = common;
597
+ void _dropped;
598
+ render(
599
+ <NativeResourceViewMessagesProvider value={{ ...rest, common: older }}>
600
+ <NativeResourceView view={questionsView} filter={{ form_id: "f1" }} />
601
+ </NativeResourceViewMessagesProvider>,
602
+ { wrapper: wrap(questionProvider()) },
603
+ );
604
+ const row = await screen.findByTestId("native-resource-view-row-q1");
605
+ act(() => {
606
+ row.dispatchEvent(new CustomEvent("accessibilityaction", { detail: "edit" }));
607
+ });
608
+ await waitFor(() => expect(screen.getByTestId("native-resource-view-form-field-form_id-value").textContent).toBe("Not available"));
609
+ });
610
+
575
611
  it("sends the filter's link on create although it's read-only, and never on edit", async () => {
576
612
  const p = questionProvider();
577
613
  const { result } = renderHook(() => useNativeResourceView(questionsView, { filter: { form_id: "f1" } }), { wrapper: wrap(p) });
@@ -659,16 +695,32 @@ describe("peek", () => {
659
695
  expect(alert).toHaveBeenLastCalledWith("Move to Bin", "The default brand stays.");
660
696
  });
661
697
 
662
- it("reads a practice-clock column on the practice's zone", async () => {
698
+ it("reads a scope-clock column on the scope's zone: the prop, else ViewerScope's", async () => {
663
699
  const clocked = defineView(brands, {
664
- fields: { updated_at: { timeZone: "practice" } },
700
+ fields: { updated_at: { timeZone: "scope" } },
665
701
  columns: { default: ["name", "updated_at"] },
666
702
  });
667
- render(<NativeResourceView view={clocked} practiceTimeZone="Asia/Tokyo" />, { wrapper: wrap(provider()) });
703
+ const at = "2026-01-02T10:00:00Z";
704
+ expect(formatInstant(at, "Asia/Tokyo")).not.toBe(formatInstant(at, "Europe/Berlin"));
705
+
706
+ const first = render(<NativeResourceView view={clocked} scopeTimeZone="Asia/Tokyo" />, { wrapper: wrap(provider()) });
707
+ fireEvent.click(await screen.findByTestId("native-resource-view-row-b2"));
708
+ expect((await screen.findByTestId("native-resource-view-peek")).textContent).toContain(formatInstant(at, "Asia/Tokyo"));
709
+ first.unmount();
710
+
711
+ // No prop: ViewerScope's scopeTimeZone.
712
+ const Base = wrap(provider());
713
+ render(<NativeResourceView view={clocked} />, {
714
+ wrapper: ({ children }: { children: ReactNode }) => (
715
+ <Base>
716
+ <ViewerScope viewer="ann" timeZone="America/Los_Angeles" scopeTimeZone="Europe/Berlin">
717
+ {children}
718
+ </ViewerScope>
719
+ </Base>
720
+ ),
721
+ });
668
722
  fireEvent.click(await screen.findByTestId("native-resource-view-row-b2"));
669
- const peek = await screen.findByTestId("native-resource-view-peek");
670
- expect(peek.textContent).toContain(formatInstant("2026-01-02T10:00:00Z", "Asia/Tokyo"));
671
- expect(formatInstant("2026-01-02T10:00:00Z", "Asia/Tokyo")).not.toBe(formatInstant("2026-01-02T10:00:00Z", "America/Los_Angeles"));
723
+ expect((await screen.findByTestId("native-resource-view-peek")).textContent).toContain(formatInstant(at, "Europe/Berlin"));
672
724
  });
673
725
 
674
726
  it("shows view.columns() drawn like the cells: a when-false value and a sensitive field never appear", async () => {
@@ -706,13 +758,15 @@ describe("calendar days", () => {
706
758
  });
707
759
 
708
760
  describe("clocks", () => {
709
- it("resolves viewer and practice to the given zones, and an unknown name to the viewer's", () => {
710
- const zones = { viewer: "America/Los_Angeles", practice: "Europe/Berlin" };
761
+ it("resolves viewer and scope to the given zones, and refuses anything else", () => {
762
+ const zones = { viewer: "America/Los_Angeles", scope: "Europe/Berlin" };
711
763
  expect(resolveZone(undefined, zones)).toBe("America/Los_Angeles");
712
764
  expect(resolveZone("viewer", zones)).toBe("America/Los_Angeles");
713
- expect(resolveZone("practice", zones)).toBe("Europe/Berlin");
765
+ expect(resolveZone("scope", zones)).toBe("Europe/Berlin");
714
766
  expect(resolveZone("Asia/Tokyo", zones)).toBe("Asia/Tokyo");
715
- expect(resolveZone("Not/AZone", zones)).toBe("America/Los_Angeles");
767
+ // Never a quiet fall back to the viewer's clock: the removed name and an unknown zone throw.
768
+ expect(() => resolveZone("practice", zones)).toThrow(/"practice" was renamed "scope"/);
769
+ expect(() => resolveZone("Not/AZone", zones)).toThrow(RangeError);
716
770
  });
717
771
  });
718
772
 
@@ -9,7 +9,7 @@ import { createContext, useContext, useMemo, type ReactNode } from "react";
9
9
  import { Linking, Text } from "react-native";
10
10
  import { titleCase } from "@marktiderman/genesis-core";
11
11
  import { isTimeZone, personTimeZone, wallClock } from "@marktiderman/genesis-core/dates";
12
- import { useViewer } from "@marktiderman/genesis-core/hooks";
12
+ import { useRelationLabel, useViewer } from "@marktiderman/genesis-core/hooks";
13
13
  import { isSafeUrl } from "@marktiderman/genesis-core/urls";
14
14
  import { valueText, type DisplayHint, type ResolvedColumn, type ViewTone } from "@marktiderman/genesis-core/resource-view";
15
15
  import { NativeBadge } from "../../components/badge";
@@ -17,10 +17,11 @@ import { NativeImage } from "../../components/image";
17
17
  import { NativeText } from "../../components/text";
18
18
  import { defaultNativeResourceViewMessages, type NativeResourceViewMessages } from "./messages";
19
19
 
20
- /** The clocks a timestamp can be read on: the viewer's and the practice's (IANA names). */
20
+ /** The clocks a timestamp can be read on: the viewer's and the scope's (IANA names). */
21
21
  export interface DisplayTimeZones {
22
22
  viewer: string;
23
- practice: string;
23
+ /** The zone of whatever the screen is scoped to (a location, a league, a team): `timeZone: "scope"`. */
24
+ scope: string;
24
25
  }
25
26
 
26
27
  /** What a renderer gets besides the value (as on the web, with native's messages and clocks). */
@@ -76,11 +77,20 @@ export function formatDay(value: unknown, zone: string = personTimeZone()): stri
76
77
  return DAY.format(new Date(wall.year, wall.month - 1, wall.day));
77
78
  }
78
79
 
79
- /** A column's clock: "viewer" (the default) and "practice" from `zones`, else a valid IANA name. */
80
+ /**
81
+ * A column's clock: "viewer" (the default) and "scope" from `zones`, else a valid IANA name.
82
+ * Anything else throws (`defineView` refuses it first): drawing it on the viewer's clock instead
83
+ * would show the wrong times with nothing to say so.
84
+ */
80
85
  export function resolveZone(zone: string | undefined, zones: DisplayTimeZones): string {
81
86
  if (!zone || zone === "viewer") return zones.viewer;
82
- if (zone === "practice") return zones.practice;
83
- return isTimeZone(zone) ? zone : zones.viewer;
87
+ if (zone === "scope") return zones.scope;
88
+ if (isTimeZone(zone)) return zone;
89
+ throw new RangeError(
90
+ zone === "practice"
91
+ ? `[genesis-ui-native] timeZone "practice" was renamed "scope" in genesis-core 2; run \`genesis codemod genesis-core-2-scope-names\``
92
+ : `[genesis-ui-native] timeZone "${zone}" isn't "viewer", "scope" or a time zone this runtime knows`,
93
+ );
84
94
  }
85
95
 
86
96
  // The wall clock's parts are drawn as UTC, so one formatter serves every zone.
@@ -168,6 +178,22 @@ const text = (hint: DisplayHint): DisplayRenderer => (value, { messages, column,
168
178
  <NativeText preset="body">{displayText(hint, value, messages, resolveZone(column.timeZone, zones))}</NativeText>
169
179
  );
170
180
 
181
+ /**
182
+ * A link as the linked row's words (batched per page). A plain relation field with no `references`
183
+ * shows its value; a linked row that's hidden or gone shows words, never its raw id.
184
+ */
185
+ function RelationText({ value, context }: { value: unknown; context: DisplayContext }) {
186
+ const { column, messages } = context;
187
+ const reference = column.field?.references;
188
+ const label = useRelationLabel(reference, value, column.labelField ? { labelField: column.labelField } : {});
189
+ const shown = !reference
190
+ ? String(value)
191
+ : label === undefined
192
+ ? messages.common.empty
193
+ : (label ?? column.missingLabel ?? messages.common.hiddenLink ?? defaultNativeResourceViewMessages.common.hiddenLink!);
194
+ return <NativeText preset="body">{shown}</NativeText>;
195
+ }
196
+
171
197
  /** Genesis's native renderers. Every one shows text it can't draw, never a broken row. */
172
198
  export const nativeDisplayRenderers: DisplayRenderers = {
173
199
  text: text("text"),
@@ -185,8 +211,8 @@ export const nativeDisplayRenderers: DisplayRenderers = {
185
211
  ),
186
212
  date: text("date"),
187
213
  datetime: text("datetime"),
188
- // The linked row's id until relation labels arrive (batched per page, never per row).
189
- relation: text("relation"),
214
+ // The linked row's words, batched across the list (one query per linked resource), else its id.
215
+ relation: (value, context) => <RelationText value={value} context={context} />,
190
216
  url: (value, { messages }) => {
191
217
  const url = openableUrl(value);
192
218
  return url && /^https?:/i.test(url) ? (
@@ -226,7 +252,7 @@ export const DisplayTimeZonesProvider = TimeZonesContext.Provider;
226
252
  /** The clocks for timestamps: the nearest provider's, else the device's for both. */
227
253
  export function useDisplayTimeZones(): DisplayTimeZones {
228
254
  const zones = useContext(TimeZonesContext);
229
- return useMemo(() => zones ?? { viewer: personTimeZone(), practice: personTimeZone() }, [zones]);
255
+ return useMemo(() => zones ?? { viewer: personTimeZone(), scope: personTimeZone() }, [zones]);
230
256
  }
231
257
 
232
258
  /** Replace some of the renderers for everything below (nested providers build on each other). */
@@ -2,7 +2,7 @@
2
2
  // SectionList, guards as swipe actions, and the form sheet's fields. NativeResourceView and
3
3
  // useNativeResourceView draw from these, and the tests check them directly.
4
4
 
5
- import type { DataRow } from "@marktiderman/genesis-core";
5
+ import type { DataRow, FieldReference } from "@marktiderman/genesis-core";
6
6
  import type {
7
7
  Allowed,
8
8
  DisplayHint,
@@ -198,6 +198,10 @@ export interface NativeFormField {
198
198
  readOnly: boolean;
199
199
  max?: number;
200
200
  options?: { value: string; label: string }[];
201
+ /** For a link: what it points at and the linked row's naming field, so it shows the row's words. */
202
+ references?: FieldReference;
203
+ labelField?: string;
204
+ missingLabel?: string;
201
205
  /** The heading it sits under; null for the form's first, untitled group. */
202
206
  section: string | null;
203
207
  sectionTitle: string | null;
@@ -257,6 +261,9 @@ export function formFieldsFor<T extends Row>(view: AnyResourceView<T>, mode: "cr
257
261
  readOnly,
258
262
  ...(f.max !== undefined ? { max: f.max } : {}),
259
263
  ...(options ? { options } : {}),
264
+ ...(f.field.references ? { references: f.field.references } : {}),
265
+ ...(f.labelField ? { labelField: f.labelField } : {}),
266
+ ...(f.missingLabel ? { missingLabel: f.missingLabel } : {}),
260
267
  section: f.section,
261
268
  sectionTitle: f.sectionTitle,
262
269
  ...(f.visible ? { visible: f.visible } : {}),
@@ -6,6 +6,11 @@ export interface NativeResourceViewMessages {
6
6
  common: {
7
7
  /** Shown for an empty value. */
8
8
  empty: string;
9
+ /**
10
+ * A link whose row this viewer can't see, or that's gone (never its raw id). Optional, so a
11
+ * catalogue written before it still fits; the English is used when it's missing.
12
+ */
13
+ hiddenLink?: string;
9
14
  yes: string;
10
15
  no: string;
11
16
  cancel: string;
@@ -70,6 +75,7 @@ export interface NativeResourceViewMessages {
70
75
  export const defaultNativeResourceViewMessages: NativeResourceViewMessages = {
71
76
  common: {
72
77
  empty: "—",
78
+ hiddenLink: "Not available",
73
79
  yes: "Yes",
74
80
  no: "No",
75
81
  cancel: "Cancel",