@marktiderman/genesis-ui-native 2.8.0 → 2.9.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.0",
3
+ "version": "2.9.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.0",
72
+ "@marktiderman/genesis-core": "^1.17.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>
@@ -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) });
@@ -697,6 +733,9 @@ describe("calendar days", () => {
697
733
  // Only a whole day: a timestamp or trailing text isn't one.
698
734
  expect(dayToDate("2026-01-02T00:00:00Z")).toBeNull();
699
735
  expect(formatDay("2026-01-02junk")).toBe("2026-01-02junk");
736
+ // A timestamp is the day on the clock asked for, not its UTC day: 8pm in New York on Jan 2.
737
+ expect(formatDay("2026-01-03T01:00:00Z", "America/New_York")).toBe(new Intl.DateTimeFormat(undefined, { dateStyle: "medium" }).format(new Date(2026, 0, 2)));
738
+ expect(formatDay("2026-01-03T01:00:00Z", "UTC")).toBe(new Intl.DateTimeFormat(undefined, { dateStyle: "medium" }).format(new Date(2026, 0, 3)));
700
739
  // What `new Date(day)` would have shown here: the day before.
701
740
  expect(new Date("2026-01-02").getDate()).toBe(1);
702
741
  });
@@ -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";
@@ -62,9 +62,18 @@ export function dateToDay(date: Date): string {
62
62
  }
63
63
 
64
64
  /** A calendar day, the same day on every clock. */
65
- export function formatDay(value: unknown): string {
65
+ /**
66
+ * A day. A calendar day (`YYYY-MM-DD`) from its parts; a timestamp as the day it is on `zone`'s
67
+ * clock (core's `wallClock`), so an 8pm edit in New York isn't tomorrow's (its UTC day).
68
+ */
69
+ export function formatDay(value: unknown, zone: string = personTimeZone()): string {
66
70
  const date = dayToDate(value);
67
- return date ? DAY.format(date) : String(value);
71
+ if (date) return DAY.format(date);
72
+ const text = String(value);
73
+ const at = /^\d{4}-\d{2}-\d{2}[T ]/.test(text) ? new Date(text) : null;
74
+ if (!at || Number.isNaN(at.getTime()) || !isTimeZone(zone)) return text;
75
+ const wall = wallClock(zone, at);
76
+ return DAY.format(new Date(wall.year, wall.month - 1, wall.day));
68
77
  }
69
78
 
70
79
  /** A column's clock: "viewer" (the default) and "practice" from `zones`, else a valid IANA name. */
@@ -116,7 +125,7 @@ export function displayText(
116
125
  case "badge":
117
126
  return titleCase(String(value));
118
127
  case "date":
119
- return formatDay(value);
128
+ return formatDay(value, timeZone);
120
129
  case "datetime":
121
130
  return formatInstant(value, timeZone);
122
131
  case "url": {
@@ -159,6 +168,22 @@ const text = (hint: DisplayHint): DisplayRenderer => (value, { messages, column,
159
168
  <NativeText preset="body">{displayText(hint, value, messages, resolveZone(column.timeZone, zones))}</NativeText>
160
169
  );
161
170
 
171
+ /**
172
+ * A link as the linked row's words (batched per page). A plain relation field with no `references`
173
+ * shows its value; a linked row that's hidden or gone shows words, never its raw id.
174
+ */
175
+ function RelationText({ value, context }: { value: unknown; context: DisplayContext }) {
176
+ const { column, messages } = context;
177
+ const reference = column.field?.references;
178
+ const label = useRelationLabel(reference, value, column.labelField ? { labelField: column.labelField } : {});
179
+ const shown = !reference
180
+ ? String(value)
181
+ : label === undefined
182
+ ? messages.common.empty
183
+ : (label ?? column.missingLabel ?? messages.common.hiddenLink ?? defaultNativeResourceViewMessages.common.hiddenLink!);
184
+ return <NativeText preset="body">{shown}</NativeText>;
185
+ }
186
+
162
187
  /** Genesis's native renderers. Every one shows text it can't draw, never a broken row. */
163
188
  export const nativeDisplayRenderers: DisplayRenderers = {
164
189
  text: text("text"),
@@ -176,8 +201,8 @@ export const nativeDisplayRenderers: DisplayRenderers = {
176
201
  ),
177
202
  date: text("date"),
178
203
  datetime: text("datetime"),
179
- // The linked row's id until relation labels arrive (batched per page, never per row).
180
- relation: text("relation"),
204
+ // The linked row's words, batched across the list (one query per linked resource), else its id.
205
+ relation: (value, context) => <RelationText value={value} context={context} />,
181
206
  url: (value, { messages }) => {
182
207
  const url = openableUrl(value);
183
208
  return url && /^https?:/i.test(url) ? (
@@ -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",