@marktiderman/genesis-ui-native 3.4.0 → 3.5.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.
@@ -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
 
@@ -1574,6 +1574,13 @@ function DateField({
1574
1574
  clearable ? /* @__PURE__ */ jsx(NativeButton, { variant: "ghost", size: "sm", testID: `${testID}-clear`, onPress: () => onChange(null), children: fill(messages.form.clear, { label: label.toLowerCase() }) }) : null
1575
1575
  ] });
1576
1576
  }
1577
+ var recordKey = (view, row) => {
1578
+ try {
1579
+ return String(encodeRecordKey(view.contract, row));
1580
+ } catch {
1581
+ return String(row.id);
1582
+ }
1583
+ };
1577
1584
  var SEARCH_DEBOUNCE_MS2 = 300;
1578
1585
  function filterParams(filter) {
1579
1586
  return filterParams$1(filter);
@@ -1711,6 +1718,23 @@ function useNativeResourceView(view, options = {}) {
1711
1718
  [view, messages, softDelete, editable, can, viewer]
1712
1719
  );
1713
1720
  const sections = useMemo(() => sectionsFor(view, pages.rows, messages), [view, pages.rows, messages]);
1721
+ const loaded = useRef({ keys: [], offset: 0 });
1722
+ const hasPrevious = pages.hasPreviousPage;
1723
+ const snapshot = useMemo(() => {
1724
+ const keys = pages.rows.map((row) => recordKey(view, row));
1725
+ const last = loaded.current;
1726
+ let offset = 0;
1727
+ if (hasPrevious && keys.length > 0 && last.keys.length > 0) {
1728
+ const back = keys.indexOf(last.keys[0]);
1729
+ const dropped = last.keys.indexOf(keys[0]);
1730
+ offset = back >= 0 ? last.offset - back : dropped >= 0 ? last.offset + dropped : last.offset;
1731
+ }
1732
+ return { keys, offset: Math.max(0, offset) };
1733
+ }, [view, pages.rows, hasPrevious]);
1734
+ useLayoutEffect(() => {
1735
+ loaded.current = snapshot;
1736
+ }, [snapshot]);
1737
+ const rowOffset = snapshot.offset;
1714
1738
  const { query } = pages;
1715
1739
  const [refreshing, setRefreshing] = useState(false);
1716
1740
  const refresh = useCallback(async () => {
@@ -1735,7 +1759,8 @@ function useNativeResourceView(view, options = {}) {
1735
1759
  hasMore: pages.hasNextPage,
1736
1760
  loadPrevious: pages.fetchPreviousPage,
1737
1761
  loadingPrevious: query.isFetchingPreviousPage,
1738
- hasPrevious: pages.hasPreviousPage,
1762
+ hasPrevious,
1763
+ rowOffset,
1739
1764
  search,
1740
1765
  setSearch,
1741
1766
  refusal,
@@ -1752,13 +1777,6 @@ function useNativeResourceView(view, options = {}) {
1752
1777
  var IOS_GROUP_MARGIN = 16;
1753
1778
  var GROUP_RADIUS = Number.parseFloat(String(Platform.Version)) >= 26 ? 26 : 10;
1754
1779
  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
1780
  function NativeResourceView(props) {
1763
1781
  const scope = useOptionalViewerScope();
1764
1782
  const viewer = scope?.timeZone;
@@ -1793,6 +1811,34 @@ function RowLeading({ picture, testID }) {
1793
1811
  /* @__PURE__ */ jsx(View, { accessibilityElementsHidden: true, importantForAccessibility: "no-hide-descendants", children: /* @__PURE__ */ jsx(NativeAvatar, { testID: `${testID}-initials`, fallback: initials, size: PICTURE_SIZE }) })
1794
1812
  );
1795
1813
  }
1814
+ function cardFor(row, { item: itemFor, actions: actionsFor, handlers, onPress, cancelLabel, testID, first, last }) {
1815
+ const item = itemFor(row);
1816
+ const actions = actionsFor(row, handlers);
1817
+ const chip = item.chips[0];
1818
+ const picture = item.picture;
1819
+ const open = () => onPress(row);
1820
+ const showActions = () => showInboxActions(item.title, actions, cancelLabel);
1821
+ return {
1822
+ item,
1823
+ actions,
1824
+ open,
1825
+ showActions,
1826
+ first,
1827
+ last,
1828
+ pressableProps: {
1829
+ testID: `${testID}-row-${item.id}`,
1830
+ accessibilityRole: "button",
1831
+ accessibilityLabel: [picture?.kind === "image" ? picture.alt : null, item.title, item.subtitle, chip?.label].filter(Boolean).join(", "),
1832
+ accessibilityActions: actions.map((a) => ({ name: a.id, label: a.reason ? `${a.label}: ${a.reason}` : a.label })),
1833
+ onAccessibilityAction: (event) => actions.find((a) => a.id === event.nativeEvent.actionName)?.onPress(),
1834
+ onPress: open,
1835
+ onLongPress: actions.length ? showActions : void 0
1836
+ }
1837
+ };
1838
+ }
1839
+ function withSwipe(content, card, Swipeable) {
1840
+ return card.actions.length && Swipeable ? /* @__PURE__ */ jsx(Swipeable, { actions: card.actions, testID: `${card.pressableProps.testID}-swipe`, children: content }) : content;
1841
+ }
1796
1842
  var ResourceRow = memo(function ResourceRow2({
1797
1843
  row,
1798
1844
  first,
@@ -1808,11 +1854,11 @@ var ResourceRow = memo(function ResourceRow2({
1808
1854
  separator,
1809
1855
  testID
1810
1856
  }) {
1811
- const item = itemFor(row);
1812
- const actions = actionsFor(row, handlers);
1857
+ const card = cardFor(row, { item: itemFor, actions: actionsFor, handlers, onPress, cancelLabel, testID, first, last });
1858
+ const { item, pressableProps } = card;
1813
1859
  const chip = item.chips[0];
1814
1860
  const picture = item.picture;
1815
- const rowTestID = `${testID}-row-${item.id}`;
1861
+ const rowTestID = pressableProps.testID;
1816
1862
  const content = /* @__PURE__ */ jsxs(
1817
1863
  View,
1818
1864
  {
@@ -1829,36 +1875,84 @@ var ResourceRow = memo(function ResourceRow2({
1829
1875
  } : null
1830
1876
  ],
1831
1877
  children: [
1832
- /* @__PURE__ */ jsx(
1833
- Pressable,
1878
+ /* @__PURE__ */ jsx(Pressable, { ...pressableProps, children: /* @__PURE__ */ jsx(
1879
+ NativeListItem,
1834
1880
  {
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
- )
1881
+ testID: `${rowTestID}-item`,
1882
+ title: item.title,
1883
+ subtitle: item.subtitle,
1884
+ leading: picture ? /* @__PURE__ */ jsx(RowLeading, { picture, testID: rowTestID }) : void 0,
1885
+ className: "bg-transparent",
1886
+ trailing: chip ? /* @__PURE__ */ jsx(NativeBadge, { testID: `${rowTestID}-chip`, variant: badgeVariant(chip.tone), children: chip.label }) : void 0,
1887
+ showChevron: chevron
1854
1888
  }
1855
- ),
1889
+ ) }),
1856
1890
  !last ? /* @__PURE__ */ jsx(View, { style: { height: 0.5, marginLeft: 16, backgroundColor: separator } }) : null
1857
1891
  ]
1858
1892
  }
1859
1893
  );
1860
- return actions.length && Swipeable ? /* @__PURE__ */ jsx(Swipeable, { actions, testID: `${rowTestID}-swipe`, children: content }) : content;
1894
+ return withSwipe(content, card, Swipeable);
1861
1895
  });
1896
+ var CustomRow = memo(function CustomRow2(props) {
1897
+ const card = cardFor(props.row, props);
1898
+ return withSwipe(props.renderCard(props.row, props.index, card), card, props.Swipeable);
1899
+ });
1900
+ var GRID_GAP = 12;
1901
+ function GridCard(props) {
1902
+ const card = cardFor(props.row, props);
1903
+ const { item, pressableProps } = card;
1904
+ const chip = item.chips[0];
1905
+ const content = /* @__PURE__ */ jsxs(
1906
+ Pressable,
1907
+ {
1908
+ ...pressableProps,
1909
+ style: { backgroundColor: props.surface, borderRadius: GROUP_RADIUS, padding: 12, gap: 8, minHeight: 44, flexGrow: 1 },
1910
+ children: [
1911
+ item.picture ? /* @__PURE__ */ jsx(RowLeading, { picture: item.picture, testID: pressableProps.testID }) : null,
1912
+ /* @__PURE__ */ jsx(NativeText, { preset: "label", numberOfLines: 2, children: item.title }),
1913
+ item.subtitle ? /* @__PURE__ */ jsx(NativeText, { preset: "body-sm", className: "text-muted-foreground", numberOfLines: 2, children: item.subtitle }) : null,
1914
+ chip ? /* @__PURE__ */ jsx(View, { style: { flexDirection: "row" }, children: /* @__PURE__ */ jsx(NativeBadge, { testID: `${pressableProps.testID}-chip`, variant: badgeVariant(chip.tone), children: chip.label }) }) : null
1915
+ ]
1916
+ }
1917
+ );
1918
+ return withSwipe(content, card, props.Swipeable);
1919
+ }
1920
+ var GridRow = memo(function GridRow2({ line, numColumns, renderCard, ...shared }) {
1921
+ const cells = [];
1922
+ for (let i = 0; i < line.lead; i += 1) cells.push(/* @__PURE__ */ jsx(View, { style: { flex: 1 } }, `lead-${i}`));
1923
+ line.rows.forEach((row, offset) => {
1924
+ const index = line.index + offset;
1925
+ const props = { ...shared, row, first: index === 0, last: index === line.lastIndex };
1926
+ cells.push(
1927
+ /* @__PURE__ */ jsx(View, { style: { flex: 1 }, children: renderCard ? /* @__PURE__ */ jsx(CustomRow, { ...props, index, renderCard }) : /* @__PURE__ */ jsx(GridCard, { ...props }) }, recordKey(shared.view, row))
1928
+ );
1929
+ });
1930
+ for (let i = line.lead + line.rows.length; i < numColumns; i += 1) cells.push(/* @__PURE__ */ jsx(View, { style: { flex: 1 } }, `spacer-${i}`));
1931
+ return /* @__PURE__ */ jsx(View, { testID: `${shared.testID}-grid-line-${line.key}`, style: { flexDirection: "row", gap: GRID_GAP, paddingHorizontal: 16, paddingBottom: GRID_GAP }, children: cells });
1932
+ });
1933
+ function gridLines(sectionKey, rows, above, numColumns, previous = /* @__PURE__ */ new Map()) {
1934
+ const lines = [];
1935
+ const lastIndex = above + rows.length - 1;
1936
+ let j = 0;
1937
+ while (j < rows.length) {
1938
+ const index = above + j;
1939
+ const n = Math.floor(index / numColumns);
1940
+ const lead = index - n * numColumns;
1941
+ const take = numColumns - lead;
1942
+ const next = {
1943
+ key: `${sectionKey || "none"}-${n}`,
1944
+ index,
1945
+ lead,
1946
+ rows: rows.slice(j, j + take),
1947
+ lastIndex: index + take > lastIndex ? lastIndex : -1
1948
+ };
1949
+ const old = previous.get(next.key);
1950
+ 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]);
1951
+ lines.push(same ? old : next);
1952
+ j += take;
1953
+ }
1954
+ return lines;
1955
+ }
1862
1956
  function Screen(props) {
1863
1957
  const {
1864
1958
  view,
@@ -1869,6 +1963,9 @@ function Screen(props) {
1869
1963
  header,
1870
1964
  emptyState,
1871
1965
  searchable,
1966
+ mode = "list",
1967
+ numColumns: columnsProp,
1968
+ renderCard,
1872
1969
  scopeTimeZone: _zone,
1873
1970
  testID = "native-resource-view",
1874
1971
  ...options
@@ -1909,26 +2006,60 @@ function Screen(props) {
1909
2006
  rowPress.current = onRowPress;
1910
2007
  const press = useCallback((row) => rowPress.current ? rowPress.current(row) : setPeekId({ row }), []);
1911
2008
  const chevron = !!onRowPress;
2009
+ const shared = useMemo(
2010
+ () => ({
2011
+ view,
2012
+ item: itemFor,
2013
+ actions: actionsFor,
2014
+ handlers,
2015
+ onPress: press,
2016
+ chevron,
2017
+ Swipeable,
2018
+ cancelLabel: messages.common.cancel,
2019
+ surface: colors.surface,
2020
+ separator: colors.separator,
2021
+ testID
2022
+ }),
2023
+ [view, itemFor, actionsFor, handlers, press, chevron, Swipeable, messages, colors.surface, colors.separator, testID]
2024
+ );
1912
2025
  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]
2026
+ ({ item: row, index, section }) => {
2027
+ const props2 = { ...shared, row, first: index === 0, last: index === section.data.length - 1 };
2028
+ return renderCard ? /* @__PURE__ */ jsx(CustomRow, { ...props2, index, renderCard }) : /* @__PURE__ */ jsx(ResourceRow, { ...props2 });
2029
+ },
2030
+ [shared, renderCard]
2031
+ );
2032
+ const numColumns = Math.max(1, Math.floor(columnsProp ?? 2));
2033
+ const grid = mode === "grid";
2034
+ const sectionStarts = useRef(/* @__PURE__ */ new Map());
2035
+ const lastLines = useRef(/* @__PURE__ */ new Map());
2036
+ const { rowOffset, hasPrevious } = state;
2037
+ const gridState = useMemo(() => {
2038
+ const starts = /* @__PURE__ */ new Map();
2039
+ const lines = /* @__PURE__ */ new Map();
2040
+ let at = rowOffset;
2041
+ if (!grid) return { sections: [], starts, lines };
2042
+ const out = state.sections.map((section, i) => {
2043
+ const seen = sectionStarts.current.get(section.key);
2044
+ const start = i === 0 && hasPrevious && seen !== void 0 ? Math.min(seen, at) : at;
2045
+ starts.set(section.key, start);
2046
+ const data = gridLines(section.key, section.data, at - start, numColumns, lastLines.current);
2047
+ for (const line of data) lines.set(line.key, line);
2048
+ at += section.data.length;
2049
+ return { key: section.key, title: section.title, data };
2050
+ });
2051
+ return { sections: out, starts, lines };
2052
+ }, [grid, state.sections, numColumns, rowOffset, hasPrevious]);
2053
+ useLayoutEffect(() => {
2054
+ if (!grid) return;
2055
+ sectionStarts.current = gridState.starts;
2056
+ lastLines.current = gridState.lines;
2057
+ }, [grid, gridState]);
2058
+ const gridSections = gridState.sections;
2059
+ const gridKey = useCallback((line) => line.key, []);
2060
+ const renderGridLine = useCallback(
2061
+ ({ item: line }) => /* @__PURE__ */ jsx(GridRow, { ...shared, line, numColumns, renderCard }),
2062
+ [shared, numColumns, renderCard]
1932
2063
  );
1933
2064
  const renderSectionHeader = useCallback(
1934
2065
  ({ section }) => section.title ? /* @__PURE__ */ jsx(
@@ -2021,9 +2152,9 @@ function Screen(props) {
2021
2152
  SectionList,
2022
2153
  {
2023
2154
  testID: `${testID}-list`,
2024
- sections: state.sections,
2025
- keyExtractor,
2026
- renderItem,
2155
+ sections: grid ? gridSections : state.sections,
2156
+ keyExtractor: grid ? gridKey : keyExtractor,
2157
+ renderItem: grid ? renderGridLine : renderItem,
2027
2158
  renderSectionHeader,
2028
2159
  extraData: actionsFor,
2029
2160
  stickySectionHeadersEnabled: false,
@@ -2363,18 +2494,25 @@ function SettingsGroup({
2363
2494
  }, [recordView, view, sectionId]);
2364
2495
  const viewer = useViewer$1();
2365
2496
  const locked = recordView.settingsLocked(row, viewer);
2366
- const [draft, setDraft] = useState(() => Object.fromEntries(fields.map((f) => [f.key, row[f.key]])));
2497
+ const [edits, setEdits] = useState({});
2498
+ const draft = useMemo(() => ({ ...Object.fromEntries(fields.map((f) => [f.key, row[f.key]])), ...edits }), [fields, row, edits]);
2499
+ useEffect(() => {
2500
+ setEdits((current) => {
2501
+ const left = Object.fromEntries(Object.entries(current).filter(([k, v]) => !sameFormValue(v, row[k])));
2502
+ return Object.keys(left).length === Object.keys(current).length ? current : left;
2503
+ });
2504
+ }, [row]);
2367
2505
  const [invalid, setInvalid] = useState({});
2368
2506
  const [saving, setSaving] = useState(false);
2369
2507
  const [refusal, setRefusal] = useState(null);
2370
2508
  const shown = fields.filter((f) => !f.visible || f.visible(draft));
2371
2509
  const changed = {};
2372
- for (const f of shown) if (draft[f.key] !== row[f.key]) changed[f.key] = draft[f.key];
2510
+ for (const f of shown) if (f.key in edits && !sameFormValue(draft[f.key], row[f.key])) changed[f.key] = draft[f.key];
2373
2511
  const dirty = Object.keys(changed).length > 0 && !shown.some((f) => invalid[f.key]);
2374
2512
  const set = (key, value, error) => {
2375
2513
  setRefusal(null);
2376
2514
  setInvalid(({ [key]: _old, ...rest }) => error ? { ...rest, [key]: error } : rest);
2377
- setDraft((d) => ({ ...d, [key]: value }));
2515
+ setEdits((d) => ({ ...d, [key]: value }));
2378
2516
  };
2379
2517
  const save = async () => {
2380
2518
  setSaving(true);