@marktiderman/genesis-ui-native 2.7.1 → 2.8.1

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.7.1",
3
+ "version": "2.8.1",
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.15.0",
72
+ "@marktiderman/genesis-core": "^1.16.1",
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",
@@ -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,
@@ -696,6 +697,9 @@ describe("calendar days", () => {
696
697
  // Only a whole day: a timestamp or trailing text isn't one.
697
698
  expect(dayToDate("2026-01-02T00:00:00Z")).toBeNull();
698
699
  expect(formatDay("2026-01-02junk")).toBe("2026-01-02junk");
700
+ // A timestamp is the day on the clock asked for, not its UTC day: 8pm in New York on Jan 2.
701
+ expect(formatDay("2026-01-03T01:00:00Z", "America/New_York")).toBe(new Intl.DateTimeFormat(undefined, { dateStyle: "medium" }).format(new Date(2026, 0, 2)));
702
+ expect(formatDay("2026-01-03T01:00:00Z", "UTC")).toBe(new Intl.DateTimeFormat(undefined, { dateStyle: "medium" }).format(new Date(2026, 0, 3)));
699
703
  // What `new Date(day)` would have shown here: the day before.
700
704
  expect(new Date("2026-01-02").getDate()).toBe(1);
701
705
  });
@@ -773,7 +777,7 @@ describe("paging and search", () => {
773
777
  it("keeps at most 10 pages loaded by default, and takes the screen's number", () => {
774
778
  hooks.infiniteOptions.length = 0;
775
779
  renderHook(() => useNativeResourceView(plainView), { wrapper: wrap(provider()) });
776
- expect(hooks.infiniteOptions.at(-1)).toMatchObject({ maxPages: 10, defaultSort: { field: "status", order: "asc" } });
780
+ expect(hooks.infiniteOptions.at(-1)).toMatchObject({ maxPages: 10, defaultSorts: [{ field: "status", order: "asc" }, { field: "name", order: "asc" }] });
777
781
  renderHook(() => useNativeResourceView(summaryView, { maxPages: 3 }), { wrapper: wrap(provider()) });
778
782
  expect(hooks.infiniteOptions.at(-1)).toMatchObject({ maxPages: 3 });
779
783
  });
@@ -788,6 +792,22 @@ describe("paging and search", () => {
788
792
  expect(serverSort(summaryView)).toBeUndefined();
789
793
  });
790
794
 
795
+ it("sorts within each group by orderBy: the group first, then the order", () => {
796
+ const grouped = defineView(brands, { groupBy: "status", orderBy: { field: "name", direction: "desc" } });
797
+ expect(serverSorts(grouped)).toEqual([
798
+ { field: "status", order: "asc" },
799
+ { field: "name", order: "desc" },
800
+ ]);
801
+ // A provider that sorts by one field keeps the sections whole.
802
+ expect(serverSort(grouped)).toEqual({ field: "status", order: "asc" });
803
+ expect(serverSorts(defineView(brands, { groupBy: "status" }))).toEqual([{ field: "status", order: "asc" }]);
804
+ expect(serverSorts(summaryView)).toEqual([]);
805
+ hooks.infiniteOptions.length = 0;
806
+ renderHook(() => useNativeResourceView(grouped), { wrapper: wrap(provider()) });
807
+ expect(hooks.infiniteOptions.at(-1)).toMatchObject({ defaultSorts: serverSorts(grouped) });
808
+ expect(hooks.infiniteOptions.at(-1)).not.toHaveProperty("defaultSort");
809
+ });
810
+
791
811
  it("doesn't redraw rows while the person types", async () => {
792
812
  const summary = vi.fn((r: Brand) => ({ title: r.name }));
793
813
  const counted = defineView(brands, { summary });
@@ -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": {
@@ -5,6 +5,7 @@ export {
5
5
  SEARCH_DEBOUNCE_MS,
6
6
  screenValues,
7
7
  serverSort,
8
+ serverSorts,
8
9
  DEFAULT_MAX_PAGES,
9
10
  type NativeResourceViewOptions,
10
11
  type SaveResult,
@@ -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 sort: the `groupBy` field first, so every page arrives in section order and a later
129
- * page never lands in the middle of an earlier section; else the view's `orderBy`. (A second sort
130
- * key within a group needs `useInfiniteResource` to take several sorts.)
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 serverSort<T extends Row>(view: AnyResourceView<T>): { field: string; order: "asc" | "desc" } | undefined {
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
- // TODO: within a group the view's orderBy is dropped (one sort only). Pass [groupBy, orderBy] once
136
- // useInfiniteResource takes several sorts (the coming multi-sort, owned by core).
137
- if (groupBy) return { field: groupBy, order: orderBy?.field === groupBy ? (orderBy.direction ?? "asc") : "asc" };
138
- return orderBy ? { field: orderBy.field as string, order: orderBy.direction ?? "asc" } : undefined;
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 sort = useMemo(() => serverSort(view), [view]);
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
- ...(sort ? { defaultSort: sort } : {}),
170
+ ...(sorts.length ? { defaultSorts: sorts } : {}),
164
171
  ...(perPage ? { perPage } : {}),
165
172
  maxPages,
166
173
  ...(searchFields ? { searchFields } : {}),