@marktiderman/genesis-ui-native 3.4.0 → 3.5.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.
@@ -1,6 +1,6 @@
1
1
  import { NativeImage, NativeAvatar, NativeCard, NativeCardContent, NativeSeparator, NativeLabel, NativeSwitch, NativeCheckbox, NativeSelect, NativeSelectTrigger, NativeSelectContent, NativeSelectItem, NativeTextarea, NativeAlert, NativeAlertTitle, NativeAlertDescription, NativeSheet, NativeSheetScrollView, NativeInlineAlert, NativeSegmentedControl, NativeDatePicker, NativeTimePicker, NativeSearchBar, NativeSkeleton, NativeCardHeader, NativeCardTitle } from '../chunk-GHYUNNNU.js';
2
2
  import '../chunk-7PCWEY6R.js';
3
- import { showInboxActions, NavBarSaveButton, composeHeaderOptions } from '../chunk-ZHVQTMNV.js';
3
+ import { showInboxActions, NavBarSaveButton, composeHeaderOptions } from '../chunk-ONH2PCGH.js';
4
4
  import { NativeListItem, NativeBadge, NativeText, NativeButton, NativeInput, useGenesisModules, NativeLoadingState, NativeSectionListHeader, NativeEmptyState, NativeRefreshControl, NativeIcon } from '../chunk-6KWEAGRD.js';
5
5
  import { useThemeRoles } from '../chunk-Z3AQYVUA.js';
6
6
  import '../chunk-OHZD7KRO.js';
@@ -8,14 +8,14 @@ import '../chunk-DGUM43GV.js';
8
8
  import { Platform, View, Pressable, Text, Linking, ScrollView, Alert, SectionList, ActionSheetIOS, FlatList } from 'react-native';
9
9
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
10
10
  import * as React from 'react';
11
- import { createContext, memo, useState, useCallback, useMemo, useEffect, useContext, useRef } from 'react';
11
+ import { createContext, memo, useState, useCallback, useMemo, useEffect, useContext, useRef, useLayoutEffect } from 'react';
12
12
  import { SafeAreaView } from 'react-native-safe-area-context';
13
- import { titleCase, useResourceForm, resourceKeys, useDataProvider, useViewer as useViewer$1, encodeRecordKey, useCapabilities as useCapabilities$1, DataWriteError } from '@marktiderman/genesis-core';
13
+ import { encodeRecordKey, titleCase, useResourceForm, resourceKeys, useDataProvider, useViewer as useViewer$1, useCapabilities as useCapabilities$1, DataWriteError } from '@marktiderman/genesis-core';
14
14
  import { Controller, useForm } from 'react-hook-form';
15
15
  import { useRelationLabel, useResource, useViewer, useInfiniteResource, useCapabilities, useOptionalViewerScope, scopeClock, useOne, useLinkOptions } from '@marktiderman/genesis-core/hooks';
16
16
  import { personTimeZone, isTimeZone, wallClock } from '@marktiderman/genesis-core/dates';
17
17
  import { isSafeUrl } from '@marktiderman/genesis-core/urls';
18
- import { valueText, rowPicture, filterParams as filterParams$1 } from '@marktiderman/genesis-core/resource-view';
18
+ import { valueText, sameFormValue, rowPicture, filterParams as filterParams$1 } from '@marktiderman/genesis-core/resource-view';
19
19
  import { useQueryClient, useQuery } from '@tanstack/react-query';
20
20
  import { readinessChip } from '@marktiderman/genesis-core/resource';
21
21
 
@@ -1135,6 +1135,9 @@ function heldTo(link, keys) {
1135
1135
  get labelField() {
1136
1136
  return link.labelField;
1137
1137
  },
1138
+ get orderBy() {
1139
+ return link.orderBy;
1140
+ },
1138
1141
  ...link.searchFields ? { searchFields: link.searchFields } : {},
1139
1142
  filters: (scope, values) => [...link.filters(scope, values), held]
1140
1143
  });
@@ -1233,7 +1236,7 @@ function NativeLinkPicker({ field, value, onChange, values, error, testID }) {
1233
1236
  const filters = useMemo(() => JSON.parse(filtersKey), [filtersKey]);
1234
1237
  const labelField = field.labelField ?? link.labelField;
1235
1238
  const list = useLinkOptions(
1236
- { resource: link.resource, field: link.field, labelField, searchFields: link.searchFields },
1239
+ { resource: link.resource, field: link.field, labelField, searchFields: link.searchFields, orderBy: link.orderBy },
1237
1240
  { filters, search, enabled: open, untitled: words.untitled }
1238
1241
  );
1239
1242
  const loaded = isEmptyValue(value) ? void 0 : list.options.find((o) => String(o.value) === String(value));
@@ -1574,6 +1577,13 @@ function DateField({
1574
1577
  clearable ? /* @__PURE__ */ jsx(NativeButton, { variant: "ghost", size: "sm", testID: `${testID}-clear`, onPress: () => onChange(null), children: fill(messages.form.clear, { label: label.toLowerCase() }) }) : null
1575
1578
  ] });
1576
1579
  }
1580
+ var recordKey = (view, row) => {
1581
+ try {
1582
+ return String(encodeRecordKey(view.contract, row));
1583
+ } catch {
1584
+ return String(row.id);
1585
+ }
1586
+ };
1577
1587
  var SEARCH_DEBOUNCE_MS2 = 300;
1578
1588
  function filterParams(filter) {
1579
1589
  return filterParams$1(filter);
@@ -1711,6 +1721,23 @@ function useNativeResourceView(view, options = {}) {
1711
1721
  [view, messages, softDelete, editable, can, viewer]
1712
1722
  );
1713
1723
  const sections = useMemo(() => sectionsFor(view, pages.rows, messages), [view, pages.rows, messages]);
1724
+ const loaded = useRef({ keys: [], offset: 0 });
1725
+ const hasPrevious = pages.hasPreviousPage;
1726
+ const snapshot = useMemo(() => {
1727
+ const keys = pages.rows.map((row) => recordKey(view, row));
1728
+ const last = loaded.current;
1729
+ let offset = 0;
1730
+ if (hasPrevious && keys.length > 0 && last.keys.length > 0) {
1731
+ const back = keys.indexOf(last.keys[0]);
1732
+ const dropped = last.keys.indexOf(keys[0]);
1733
+ offset = back >= 0 ? last.offset - back : dropped >= 0 ? last.offset + dropped : last.offset;
1734
+ }
1735
+ return { keys, offset: Math.max(0, offset) };
1736
+ }, [view, pages.rows, hasPrevious]);
1737
+ useLayoutEffect(() => {
1738
+ loaded.current = snapshot;
1739
+ }, [snapshot]);
1740
+ const rowOffset = snapshot.offset;
1714
1741
  const { query } = pages;
1715
1742
  const [refreshing, setRefreshing] = useState(false);
1716
1743
  const refresh = useCallback(async () => {
@@ -1735,7 +1762,8 @@ function useNativeResourceView(view, options = {}) {
1735
1762
  hasMore: pages.hasNextPage,
1736
1763
  loadPrevious: pages.fetchPreviousPage,
1737
1764
  loadingPrevious: query.isFetchingPreviousPage,
1738
- hasPrevious: pages.hasPreviousPage,
1765
+ hasPrevious,
1766
+ rowOffset,
1739
1767
  search,
1740
1768
  setSearch,
1741
1769
  refusal,
@@ -1752,13 +1780,6 @@ function useNativeResourceView(view, options = {}) {
1752
1780
  var IOS_GROUP_MARGIN = 16;
1753
1781
  var GROUP_RADIUS = Number.parseFloat(String(Platform.Version)) >= 26 ? 26 : 10;
1754
1782
  var IOS = Platform.OS === "ios";
1755
- var recordKey = (view, row) => {
1756
- try {
1757
- return String(encodeRecordKey(view.contract, row));
1758
- } catch {
1759
- return String(row.id);
1760
- }
1761
- };
1762
1783
  function NativeResourceView(props) {
1763
1784
  const scope = useOptionalViewerScope();
1764
1785
  const viewer = scope?.timeZone;
@@ -1793,6 +1814,34 @@ function RowLeading({ picture, testID }) {
1793
1814
  /* @__PURE__ */ jsx(View, { accessibilityElementsHidden: true, importantForAccessibility: "no-hide-descendants", children: /* @__PURE__ */ jsx(NativeAvatar, { testID: `${testID}-initials`, fallback: initials, size: PICTURE_SIZE }) })
1794
1815
  );
1795
1816
  }
1817
+ function cardFor(row, { item: itemFor, actions: actionsFor, handlers, onPress, cancelLabel, testID, first, last }) {
1818
+ const item = itemFor(row);
1819
+ const actions = actionsFor(row, handlers);
1820
+ const chip = item.chips[0];
1821
+ const picture = item.picture;
1822
+ const open = () => onPress(row);
1823
+ const showActions = () => showInboxActions(item.title, actions, cancelLabel);
1824
+ return {
1825
+ item,
1826
+ actions,
1827
+ open,
1828
+ showActions,
1829
+ first,
1830
+ last,
1831
+ pressableProps: {
1832
+ testID: `${testID}-row-${item.id}`,
1833
+ accessibilityRole: "button",
1834
+ accessibilityLabel: [picture?.kind === "image" ? picture.alt : null, item.title, item.subtitle, chip?.label].filter(Boolean).join(", "),
1835
+ accessibilityActions: actions.map((a) => ({ name: a.id, label: a.reason ? `${a.label}: ${a.reason}` : a.label })),
1836
+ onAccessibilityAction: (event) => actions.find((a) => a.id === event.nativeEvent.actionName)?.onPress(),
1837
+ onPress: open,
1838
+ onLongPress: actions.length ? showActions : void 0
1839
+ }
1840
+ };
1841
+ }
1842
+ function withSwipe(content, card, Swipeable) {
1843
+ return card.actions.length && Swipeable ? /* @__PURE__ */ jsx(Swipeable, { actions: card.actions, testID: `${card.pressableProps.testID}-swipe`, children: content }) : content;
1844
+ }
1796
1845
  var ResourceRow = memo(function ResourceRow2({
1797
1846
  row,
1798
1847
  first,
@@ -1808,11 +1857,11 @@ var ResourceRow = memo(function ResourceRow2({
1808
1857
  separator,
1809
1858
  testID
1810
1859
  }) {
1811
- const item = itemFor(row);
1812
- const actions = actionsFor(row, handlers);
1860
+ const card = cardFor(row, { item: itemFor, actions: actionsFor, handlers, onPress, cancelLabel, testID, first, last });
1861
+ const { item, pressableProps } = card;
1813
1862
  const chip = item.chips[0];
1814
1863
  const picture = item.picture;
1815
- const rowTestID = `${testID}-row-${item.id}`;
1864
+ const rowTestID = pressableProps.testID;
1816
1865
  const content = /* @__PURE__ */ jsxs(
1817
1866
  View,
1818
1867
  {
@@ -1829,36 +1878,84 @@ var ResourceRow = memo(function ResourceRow2({
1829
1878
  } : null
1830
1879
  ],
1831
1880
  children: [
1832
- /* @__PURE__ */ jsx(
1833
- Pressable,
1881
+ /* @__PURE__ */ jsx(Pressable, { ...pressableProps, children: /* @__PURE__ */ jsx(
1882
+ NativeListItem,
1834
1883
  {
1835
- testID: rowTestID,
1836
- accessibilityRole: "button",
1837
- accessibilityLabel: [picture?.kind === "image" ? picture.alt : null, item.title, item.subtitle, chip?.label].filter(Boolean).join(", "),
1838
- accessibilityActions: actions.map((a) => ({ name: a.id, label: a.reason ? `${a.label}: ${a.reason}` : a.label })),
1839
- onAccessibilityAction: (event) => actions.find((a) => a.id === event.nativeEvent.actionName)?.onPress(),
1840
- onPress: () => onPress(row),
1841
- onLongPress: actions.length ? () => showInboxActions(item.title, actions, cancelLabel) : void 0,
1842
- children: /* @__PURE__ */ jsx(
1843
- NativeListItem,
1844
- {
1845
- testID: `${rowTestID}-item`,
1846
- title: item.title,
1847
- subtitle: item.subtitle,
1848
- leading: picture ? /* @__PURE__ */ jsx(RowLeading, { picture, testID: rowTestID }) : void 0,
1849
- className: "bg-transparent",
1850
- trailing: chip ? /* @__PURE__ */ jsx(NativeBadge, { testID: `${rowTestID}-chip`, variant: badgeVariant(chip.tone), children: chip.label }) : void 0,
1851
- showChevron: chevron
1852
- }
1853
- )
1884
+ testID: `${rowTestID}-item`,
1885
+ title: item.title,
1886
+ subtitle: item.subtitle,
1887
+ leading: picture ? /* @__PURE__ */ jsx(RowLeading, { picture, testID: rowTestID }) : void 0,
1888
+ className: "bg-transparent",
1889
+ trailing: chip ? /* @__PURE__ */ jsx(NativeBadge, { testID: `${rowTestID}-chip`, variant: badgeVariant(chip.tone), children: chip.label }) : void 0,
1890
+ showChevron: chevron
1854
1891
  }
1855
- ),
1892
+ ) }),
1856
1893
  !last ? /* @__PURE__ */ jsx(View, { style: { height: 0.5, marginLeft: 16, backgroundColor: separator } }) : null
1857
1894
  ]
1858
1895
  }
1859
1896
  );
1860
- return actions.length && Swipeable ? /* @__PURE__ */ jsx(Swipeable, { actions, testID: `${rowTestID}-swipe`, children: content }) : content;
1897
+ return withSwipe(content, card, Swipeable);
1861
1898
  });
1899
+ var CustomRow = memo(function CustomRow2(props) {
1900
+ const card = cardFor(props.row, props);
1901
+ return withSwipe(props.renderCard(props.row, props.index, card), card, props.Swipeable);
1902
+ });
1903
+ var GRID_GAP = 12;
1904
+ function GridCard(props) {
1905
+ const card = cardFor(props.row, props);
1906
+ const { item, pressableProps } = card;
1907
+ const chip = item.chips[0];
1908
+ const content = /* @__PURE__ */ jsxs(
1909
+ Pressable,
1910
+ {
1911
+ ...pressableProps,
1912
+ style: { backgroundColor: props.surface, borderRadius: GROUP_RADIUS, padding: 12, gap: 8, minHeight: 44, flexGrow: 1 },
1913
+ children: [
1914
+ item.picture ? /* @__PURE__ */ jsx(RowLeading, { picture: item.picture, testID: pressableProps.testID }) : null,
1915
+ /* @__PURE__ */ jsx(NativeText, { preset: "label", numberOfLines: 2, children: item.title }),
1916
+ item.subtitle ? /* @__PURE__ */ jsx(NativeText, { preset: "body-sm", className: "text-muted-foreground", numberOfLines: 2, children: item.subtitle }) : null,
1917
+ chip ? /* @__PURE__ */ jsx(View, { style: { flexDirection: "row" }, children: /* @__PURE__ */ jsx(NativeBadge, { testID: `${pressableProps.testID}-chip`, variant: badgeVariant(chip.tone), children: chip.label }) }) : null
1918
+ ]
1919
+ }
1920
+ );
1921
+ return withSwipe(content, card, props.Swipeable);
1922
+ }
1923
+ var GridRow = memo(function GridRow2({ line, numColumns, renderCard, ...shared }) {
1924
+ const cells = [];
1925
+ for (let i = 0; i < line.lead; i += 1) cells.push(/* @__PURE__ */ jsx(View, { style: { flex: 1 } }, `lead-${i}`));
1926
+ line.rows.forEach((row, offset) => {
1927
+ const index = line.index + offset;
1928
+ const props = { ...shared, row, first: index === 0, last: index === line.lastIndex };
1929
+ cells.push(
1930
+ /* @__PURE__ */ jsx(View, { style: { flex: 1 }, children: renderCard ? /* @__PURE__ */ jsx(CustomRow, { ...props, index, renderCard }) : /* @__PURE__ */ jsx(GridCard, { ...props }) }, recordKey(shared.view, row))
1931
+ );
1932
+ });
1933
+ for (let i = line.lead + line.rows.length; i < numColumns; i += 1) cells.push(/* @__PURE__ */ jsx(View, { style: { flex: 1 } }, `spacer-${i}`));
1934
+ return /* @__PURE__ */ jsx(View, { testID: `${shared.testID}-grid-line-${line.key}`, style: { flexDirection: "row", gap: GRID_GAP, paddingHorizontal: 16, paddingBottom: GRID_GAP }, children: cells });
1935
+ });
1936
+ function gridLines(sectionKey, rows, above, numColumns, previous = /* @__PURE__ */ new Map()) {
1937
+ const lines = [];
1938
+ const lastIndex = above + rows.length - 1;
1939
+ let j = 0;
1940
+ while (j < rows.length) {
1941
+ const index = above + j;
1942
+ const n = Math.floor(index / numColumns);
1943
+ const lead = index - n * numColumns;
1944
+ const take = numColumns - lead;
1945
+ const next = {
1946
+ key: `${sectionKey || "none"}-${n}`,
1947
+ index,
1948
+ lead,
1949
+ rows: rows.slice(j, j + take),
1950
+ lastIndex: index + take > lastIndex ? lastIndex : -1
1951
+ };
1952
+ const old = previous.get(next.key);
1953
+ const same = old !== void 0 && old.index === next.index && old.lastIndex === next.lastIndex && old.rows.length === next.rows.length && old.rows.every((row, i) => row === next.rows[i]);
1954
+ lines.push(same ? old : next);
1955
+ j += take;
1956
+ }
1957
+ return lines;
1958
+ }
1862
1959
  function Screen(props) {
1863
1960
  const {
1864
1961
  view,
@@ -1869,6 +1966,9 @@ function Screen(props) {
1869
1966
  header,
1870
1967
  emptyState,
1871
1968
  searchable,
1969
+ mode = "list",
1970
+ numColumns: columnsProp,
1971
+ renderCard,
1872
1972
  scopeTimeZone: _zone,
1873
1973
  testID = "native-resource-view",
1874
1974
  ...options
@@ -1909,26 +2009,60 @@ function Screen(props) {
1909
2009
  rowPress.current = onRowPress;
1910
2010
  const press = useCallback((row) => rowPress.current ? rowPress.current(row) : setPeekId({ row }), []);
1911
2011
  const chevron = !!onRowPress;
2012
+ const shared = useMemo(
2013
+ () => ({
2014
+ view,
2015
+ item: itemFor,
2016
+ actions: actionsFor,
2017
+ handlers,
2018
+ onPress: press,
2019
+ chevron,
2020
+ Swipeable,
2021
+ cancelLabel: messages.common.cancel,
2022
+ surface: colors.surface,
2023
+ separator: colors.separator,
2024
+ testID
2025
+ }),
2026
+ [view, itemFor, actionsFor, handlers, press, chevron, Swipeable, messages, colors.surface, colors.separator, testID]
2027
+ );
1912
2028
  const renderItem = useCallback(
1913
- ({ item: row, index, section }) => /* @__PURE__ */ jsx(
1914
- ResourceRow,
1915
- {
1916
- row,
1917
- first: index === 0,
1918
- last: index === section.data.length - 1,
1919
- item: itemFor,
1920
- actions: actionsFor,
1921
- handlers,
1922
- onPress: press,
1923
- chevron,
1924
- Swipeable,
1925
- cancelLabel: messages.common.cancel,
1926
- surface: colors.surface,
1927
- separator: colors.separator,
1928
- testID
1929
- }
1930
- ),
1931
- [itemFor, actionsFor, handlers, press, chevron, Swipeable, messages, colors.surface, colors.separator, testID]
2029
+ ({ item: row, index, section }) => {
2030
+ const props2 = { ...shared, row, first: index === 0, last: index === section.data.length - 1 };
2031
+ return renderCard ? /* @__PURE__ */ jsx(CustomRow, { ...props2, index, renderCard }) : /* @__PURE__ */ jsx(ResourceRow, { ...props2 });
2032
+ },
2033
+ [shared, renderCard]
2034
+ );
2035
+ const numColumns = Math.max(1, Math.floor(columnsProp ?? 2));
2036
+ const grid = mode === "grid";
2037
+ const sectionStarts = useRef(/* @__PURE__ */ new Map());
2038
+ const lastLines = useRef(/* @__PURE__ */ new Map());
2039
+ const { rowOffset, hasPrevious } = state;
2040
+ const gridState = useMemo(() => {
2041
+ const starts = /* @__PURE__ */ new Map();
2042
+ const lines = /* @__PURE__ */ new Map();
2043
+ let at = rowOffset;
2044
+ if (!grid) return { sections: [], starts, lines };
2045
+ const out = state.sections.map((section, i) => {
2046
+ const seen = sectionStarts.current.get(section.key);
2047
+ const start = i === 0 && hasPrevious && seen !== void 0 ? Math.min(seen, at) : at;
2048
+ starts.set(section.key, start);
2049
+ const data = gridLines(section.key, section.data, at - start, numColumns, lastLines.current);
2050
+ for (const line of data) lines.set(line.key, line);
2051
+ at += section.data.length;
2052
+ return { key: section.key, title: section.title, data };
2053
+ });
2054
+ return { sections: out, starts, lines };
2055
+ }, [grid, state.sections, numColumns, rowOffset, hasPrevious]);
2056
+ useLayoutEffect(() => {
2057
+ if (!grid) return;
2058
+ sectionStarts.current = gridState.starts;
2059
+ lastLines.current = gridState.lines;
2060
+ }, [grid, gridState]);
2061
+ const gridSections = gridState.sections;
2062
+ const gridKey = useCallback((line) => line.key, []);
2063
+ const renderGridLine = useCallback(
2064
+ ({ item: line }) => /* @__PURE__ */ jsx(GridRow, { ...shared, line, numColumns, renderCard }),
2065
+ [shared, numColumns, renderCard]
1932
2066
  );
1933
2067
  const renderSectionHeader = useCallback(
1934
2068
  ({ section }) => section.title ? /* @__PURE__ */ jsx(
@@ -2021,9 +2155,9 @@ function Screen(props) {
2021
2155
  SectionList,
2022
2156
  {
2023
2157
  testID: `${testID}-list`,
2024
- sections: state.sections,
2025
- keyExtractor,
2026
- renderItem,
2158
+ sections: grid ? gridSections : state.sections,
2159
+ keyExtractor: grid ? gridKey : keyExtractor,
2160
+ renderItem: grid ? renderGridLine : renderItem,
2027
2161
  renderSectionHeader,
2028
2162
  extraData: actionsFor,
2029
2163
  stickySectionHeadersEnabled: false,
@@ -2363,18 +2497,25 @@ function SettingsGroup({
2363
2497
  }, [recordView, view, sectionId]);
2364
2498
  const viewer = useViewer$1();
2365
2499
  const locked = recordView.settingsLocked(row, viewer);
2366
- const [draft, setDraft] = useState(() => Object.fromEntries(fields.map((f) => [f.key, row[f.key]])));
2500
+ const [edits, setEdits] = useState({});
2501
+ const draft = useMemo(() => ({ ...Object.fromEntries(fields.map((f) => [f.key, row[f.key]])), ...edits }), [fields, row, edits]);
2502
+ useEffect(() => {
2503
+ setEdits((current) => {
2504
+ const left = Object.fromEntries(Object.entries(current).filter(([k, v]) => !sameFormValue(v, row[k])));
2505
+ return Object.keys(left).length === Object.keys(current).length ? current : left;
2506
+ });
2507
+ }, [row]);
2367
2508
  const [invalid, setInvalid] = useState({});
2368
2509
  const [saving, setSaving] = useState(false);
2369
2510
  const [refusal, setRefusal] = useState(null);
2370
2511
  const shown = fields.filter((f) => !f.visible || f.visible(draft));
2371
2512
  const changed = {};
2372
- for (const f of shown) if (draft[f.key] !== row[f.key]) changed[f.key] = draft[f.key];
2513
+ for (const f of shown) if (f.key in edits && !sameFormValue(draft[f.key], row[f.key])) changed[f.key] = draft[f.key];
2373
2514
  const dirty = Object.keys(changed).length > 0 && !shown.some((f) => invalid[f.key]);
2374
2515
  const set = (key, value, error) => {
2375
2516
  setRefusal(null);
2376
2517
  setInvalid(({ [key]: _old, ...rest }) => error ? { ...rest, [key]: error } : rest);
2377
- setDraft((d) => ({ ...d, [key]: value }));
2518
+ setEdits((d) => ({ ...d, [key]: value }));
2378
2519
  };
2379
2520
  const save = async () => {
2380
2521
  setSaving(true);