@marktiderman/genesis-ui-native 2.8.1 → 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.1",
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.1",
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) });
@@ -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";
@@ -168,6 +168,22 @@ const text = (hint: DisplayHint): DisplayRenderer => (value, { messages, column,
168
168
  <NativeText preset="body">{displayText(hint, value, messages, resolveZone(column.timeZone, zones))}</NativeText>
169
169
  );
170
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
+
171
187
  /** Genesis's native renderers. Every one shows text it can't draw, never a broken row. */
172
188
  export const nativeDisplayRenderers: DisplayRenderers = {
173
189
  text: text("text"),
@@ -185,8 +201,8 @@ export const nativeDisplayRenderers: DisplayRenderers = {
185
201
  ),
186
202
  date: text("date"),
187
203
  datetime: text("datetime"),
188
- // The linked row's id until relation labels arrive (batched per page, never per row).
189
- 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} />,
190
206
  url: (value, { messages }) => {
191
207
  const url = openableUrl(value);
192
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",