@marktiderman/genesis-ui-native 3.11.0 → 3.12.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/README.md +69 -0
- package/dist/{chunk-ZUR4PK77.js → chunk-47CDRP43.js} +45 -166
- package/dist/chunk-47CDRP43.js.map +1 -0
- package/dist/{chunk-SG7LYX7Z.js → chunk-BQYT6TH2.js} +14 -6
- package/dist/chunk-BQYT6TH2.js.map +1 -0
- package/dist/{chunk-EAHK7D7N.js → chunk-C2ARUGT7.js} +146 -42
- package/dist/chunk-C2ARUGT7.js.map +1 -0
- package/dist/{chunk-VEWSY4TZ.js → chunk-D4UU6KIF.js} +236 -21
- package/dist/chunk-D4UU6KIF.js.map +1 -0
- package/dist/{chunk-GGH3ZQNO.js → chunk-TUDJDIGC.js} +4 -4
- package/dist/{chunk-GGH3ZQNO.js.map → chunk-TUDJDIGC.js.map} +1 -1
- package/dist/{chunk-PMS2V46D.js → chunk-VHUUOH46.js} +2 -2
- package/dist/{chunk-PMS2V46D.js.map → chunk-VHUUOH46.js.map} +1 -1
- package/dist/{chunk-IWWXERKQ.js → chunk-X5CPLUPZ.js} +1236 -136
- package/dist/chunk-X5CPLUPZ.js.map +1 -0
- package/dist/components/index.d.ts +510 -25
- package/dist/components/index.js +5 -5
- package/dist/data/index.d.ts +167 -33
- package/dist/data/index.js +1082 -760
- package/dist/data/index.js.map +1 -1
- package/dist/hooks/index.d.ts +2 -2
- package/dist/hooks/index.js +1 -1
- package/dist/icons/lucide.d.ts +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.js +7 -7
- package/dist/layouts/index.d.ts +79 -6
- package/dist/layouts/index.js +5 -5
- package/dist/{native-modules-Bkm1jHO-.d.ts → native-modules-6JnyeGR-.d.ts} +55 -1
- package/package.json +70 -4
- package/src/components/__tests__/combobox.test.tsx +20 -0
- package/src/components/__tests__/number-input.test.tsx +112 -1
- package/src/components/__tests__/parity-controls.test.tsx +366 -0
- package/src/components/__tests__/parity-display.test.tsx +295 -0
- package/src/components/__tests__/twin-props.test.tsx +114 -0
- package/src/components/badge.tsx +3 -0
- package/src/components/button-group.tsx +50 -0
- package/src/components/calendar.tsx +183 -0
- package/src/components/carousel.tsx +175 -0
- package/src/components/chart.tsx +268 -0
- package/src/components/chip-group.tsx +34 -14
- package/src/components/choice-card.tsx +56 -19
- package/src/components/combobox.tsx +6 -5
- package/src/components/empty-state.tsx +40 -0
- package/src/components/flatten-children.ts +13 -0
- package/src/components/index.ts +49 -1
- package/src/components/inline-alert.tsx +3 -1
- package/src/components/input-group.tsx +99 -0
- package/src/components/input-otp.tsx +124 -0
- package/src/components/input.tsx +40 -12
- package/src/components/list-item.tsx +20 -4
- package/src/components/number-input.tsx +45 -15
- package/src/components/radio-group.tsx +5 -0
- package/src/components/resource.tsx +227 -0
- package/src/components/settings-row.tsx +194 -0
- package/src/components/slider.tsx +3 -9
- package/src/components/sortable-list.tsx +287 -0
- package/src/components/stat-card.tsx +106 -0
- package/src/components/stepper.tsx +4 -2
- package/src/components/widget-tile.tsx +66 -0
- package/src/components/workspace-switcher.tsx +217 -0
- package/src/data/NativeCustomView.tsx +117 -0
- package/src/data/NativeFormFields.tsx +312 -0
- package/src/data/NativeRecordPeek.tsx +130 -0
- package/src/data/NativeResourceFormField.tsx +32 -0
- package/src/data/__tests__/parity-data.test.tsx +350 -0
- package/src/data/index.ts +13 -0
- package/src/layouts/__tests__/keyboard-scroll.test.tsx +58 -2
- package/src/layouts/__tests__/notifications-page.test.tsx +12 -0
- package/src/layouts/index.ts +13 -0
- package/src/layouts/keyboard.tsx +22 -2
- package/src/layouts/notifications-page.tsx +13 -35
- package/src/layouts/pages.tsx +262 -0
- package/src/native-modules.tsx +37 -0
- package/dist/chunk-EAHK7D7N.js.map +0 -1
- package/dist/chunk-IWWXERKQ.js.map +0 -1
- package/dist/chunk-SG7LYX7Z.js.map +0 -1
- package/dist/chunk-VEWSY4TZ.js.map +0 -1
- package/dist/chunk-ZUR4PK77.js.map +0 -1
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WorkspaceSwitcher — where the viewer is working (one organization, the roll-up, or personal),
|
|
3
|
+
* and a sheet to switch: the twin of genesis-ui's WorkspaceSwitcher, on the same workspaces.
|
|
4
|
+
*
|
|
5
|
+
* It reads the nearest WorkspaceScope (genesis-core) when `workspaces` and `current` aren't
|
|
6
|
+
* passed. The grouping, order, search, initials and badge words are genesis-core's
|
|
7
|
+
* (`groupWorkspaces`, `filterWorkspaces`, `workspaceInitials`, `workspaceBadgeLabel`), so the
|
|
8
|
+
* phone lists the same workspaces, in the same order, as the web.
|
|
9
|
+
*
|
|
10
|
+
* The phone navigates with its own router: `onSelect` gets the workspace and the href to go to
|
|
11
|
+
* (the scope's `hrefFor`, keeping the viewer's place when it can). Put it in a screen's header
|
|
12
|
+
* (`headerTitle`) or at the top of a tab.
|
|
13
|
+
*
|
|
14
|
+
* @stability Beta
|
|
15
|
+
*/
|
|
16
|
+
import { useState, type ReactNode } from "react";
|
|
17
|
+
import { Pressable, Text, View } from "react-native";
|
|
18
|
+
import { useWorkspace } from "@marktiderman/genesis-core/hooks";
|
|
19
|
+
import {
|
|
20
|
+
DEFAULT_WORKSPACE_KIND_LABELS,
|
|
21
|
+
WORKSPACE_KIND_ORDER,
|
|
22
|
+
WORKSPACE_SWITCHER_SEARCH_THRESHOLD,
|
|
23
|
+
filterWorkspaces,
|
|
24
|
+
groupWorkspaces,
|
|
25
|
+
hasWorkspaceBadge,
|
|
26
|
+
workspaceBadgeLabel,
|
|
27
|
+
workspaceHref,
|
|
28
|
+
workspaceInitials,
|
|
29
|
+
type Workspace,
|
|
30
|
+
type WorkspaceKind,
|
|
31
|
+
} from "@marktiderman/genesis-core/workspaces";
|
|
32
|
+
import { NativeAvatar } from "./avatar";
|
|
33
|
+
import { NativeBadge } from "./badge";
|
|
34
|
+
import { NativeListItem } from "./list-item";
|
|
35
|
+
import { NativeSearchBar } from "./search-bar";
|
|
36
|
+
import { NativeSectionListHeader } from "./section-list";
|
|
37
|
+
import { NativeSheet, NativeSheetScrollView } from "./sheet";
|
|
38
|
+
import { cn } from "../utils";
|
|
39
|
+
|
|
40
|
+
export interface NativeWorkspaceSwitcherProps<W extends Workspace = Workspace> {
|
|
41
|
+
/** Every workspace; defaults to the nearest WorkspaceScope's. */
|
|
42
|
+
workspaces?: readonly W[];
|
|
43
|
+
/** The current workspace; defaults to the nearest WorkspaceScope's. */
|
|
44
|
+
current?: W;
|
|
45
|
+
/** The viewer picked a workspace: navigate to `href`. */
|
|
46
|
+
onSelect: (workspace: W, href: string) => void;
|
|
47
|
+
/** Where each choice goes; defaults to the WorkspaceScope's, else the workspace's landing page. */
|
|
48
|
+
hrefFor?: (workspace: W) => string;
|
|
49
|
+
/** The order of the kinds (default personal, roll-up, tenants). */
|
|
50
|
+
order?: readonly WorkspaceKind[];
|
|
51
|
+
/** Headings over each kind's group; `null` hides one. */
|
|
52
|
+
kindLabels?: Partial<Record<WorkspaceKind, string | null>>;
|
|
53
|
+
/** Replaces the picture (a brand mark). */
|
|
54
|
+
renderImage?: (workspace: W, size: number) => ReactNode;
|
|
55
|
+
/** What a screen reader hears for a badge. Default "3 new" for a number. */
|
|
56
|
+
badgeLabel?: (workspace: W, value: string | number) => string;
|
|
57
|
+
/** A search box over the list. `"auto"` past seven workspaces. Default `"auto"`. */
|
|
58
|
+
search?: boolean | "auto";
|
|
59
|
+
searchPlaceholder?: string;
|
|
60
|
+
emptyText?: string;
|
|
61
|
+
/** Shown when the viewer is in no workspace. Default "Choose a workspace". */
|
|
62
|
+
placeholder?: string;
|
|
63
|
+
/** The sheet's title and the trigger's hint. Default "Switch workspace". */
|
|
64
|
+
label?: string;
|
|
65
|
+
/** Rows at the bottom of the sheet ("Create an organization"). */
|
|
66
|
+
actions?: readonly { id: string; label: string; onPress: () => void }[];
|
|
67
|
+
/** Controlled open state. */
|
|
68
|
+
open?: boolean;
|
|
69
|
+
onOpenChange?: (open: boolean) => void;
|
|
70
|
+
/** `row` (default): picture, name and description. `compact`: the name and a chevron, for a header. */
|
|
71
|
+
variant?: "row" | "compact";
|
|
72
|
+
className?: string;
|
|
73
|
+
testID?: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const NO_WORKSPACES: readonly Workspace[] = [];
|
|
77
|
+
|
|
78
|
+
export function NativeWorkspaceSwitcher<W extends Workspace = Workspace>({
|
|
79
|
+
workspaces: workspacesProp,
|
|
80
|
+
current: currentProp,
|
|
81
|
+
onSelect,
|
|
82
|
+
hrefFor: hrefForProp,
|
|
83
|
+
order = WORKSPACE_KIND_ORDER,
|
|
84
|
+
kindLabels,
|
|
85
|
+
renderImage,
|
|
86
|
+
badgeLabel = workspaceBadgeLabel,
|
|
87
|
+
search = "auto",
|
|
88
|
+
searchPlaceholder = "Find a workspace",
|
|
89
|
+
emptyText = "No workspaces found",
|
|
90
|
+
placeholder = "Choose a workspace",
|
|
91
|
+
label = "Switch workspace",
|
|
92
|
+
actions,
|
|
93
|
+
open: openProp,
|
|
94
|
+
onOpenChange,
|
|
95
|
+
variant = "row",
|
|
96
|
+
className,
|
|
97
|
+
testID = "workspace-switcher",
|
|
98
|
+
}: NativeWorkspaceSwitcherProps<W>) {
|
|
99
|
+
const scope = useWorkspace<W>();
|
|
100
|
+
const workspaces = workspacesProp ?? scope?.workspaces ?? (NO_WORKSPACES as readonly W[]);
|
|
101
|
+
const current = currentProp ?? scope?.current;
|
|
102
|
+
const hrefFor = hrefForProp ?? scope?.hrefFor ?? ((to: W) => workspaceHref(workspaces, to, current?.path));
|
|
103
|
+
const labels = { ...DEFAULT_WORKSPACE_KIND_LABELS, ...kindLabels };
|
|
104
|
+
const [openState, setOpenState] = useState(false);
|
|
105
|
+
const open = openProp ?? openState;
|
|
106
|
+
const [query, setQuery] = useState("");
|
|
107
|
+
const setOpen = (next: boolean) => {
|
|
108
|
+
if (openProp === undefined) setOpenState(next);
|
|
109
|
+
onOpenChange?.(next);
|
|
110
|
+
if (!next) setQuery("");
|
|
111
|
+
};
|
|
112
|
+
const showSearch = search === true || (search === "auto" && workspaces.length > WORKSPACE_SWITCHER_SEARCH_THRESHOLD);
|
|
113
|
+
const groups = groupWorkspaces(showSearch ? filterWorkspaces(workspaces, query) : workspaces, order);
|
|
114
|
+
|
|
115
|
+
const picture = (w: W | undefined, size: number) =>
|
|
116
|
+
w && renderImage ? renderImage(w, size) : <NativeAvatar size={size} src={w?.image ?? null} fallback={w ? workspaceInitials(w.label) : "?"} alt="" />;
|
|
117
|
+
|
|
118
|
+
const pick = (w: W) => {
|
|
119
|
+
setOpen(false);
|
|
120
|
+
if (w.id !== current?.id) onSelect(w, hrefFor(w));
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
return (
|
|
124
|
+
<>
|
|
125
|
+
<Pressable
|
|
126
|
+
testID={`${testID}-trigger`}
|
|
127
|
+
accessibilityRole="button"
|
|
128
|
+
accessibilityLabel={current ? `${current.label}${current.description ? `, ${current.description}` : ""}` : placeholder}
|
|
129
|
+
accessibilityHint={label}
|
|
130
|
+
accessibilityState={{ expanded: open }}
|
|
131
|
+
onPress={() => setOpen(true)}
|
|
132
|
+
className={cn("min-h-[44px] flex-row items-center gap-3", variant === "row" ? "rounded-lg px-3 py-2" : "px-1", className)}
|
|
133
|
+
>
|
|
134
|
+
{variant === "row" ? <View accessibilityElementsHidden>{picture(current, 32)}</View> : null}
|
|
135
|
+
<View className="min-w-0 flex-1">
|
|
136
|
+
<Text numberOfLines={1} className={cn("text-foreground", variant === "row" ? "text-base font-semibold" : "text-base font-semibold")}>
|
|
137
|
+
{current?.label ?? placeholder}
|
|
138
|
+
</Text>
|
|
139
|
+
{variant === "row" && current?.description ? (
|
|
140
|
+
<Text numberOfLines={1} className="text-xs text-muted-foreground">
|
|
141
|
+
{current.description}
|
|
142
|
+
</Text>
|
|
143
|
+
) : null}
|
|
144
|
+
</View>
|
|
145
|
+
<Text className="text-muted-foreground" accessibilityElementsHidden>
|
|
146
|
+
⌄
|
|
147
|
+
</Text>
|
|
148
|
+
</Pressable>
|
|
149
|
+
<NativeSheet open={open} onOpenChange={setOpen} testID={`${testID}-sheet`} snapPoints={["60%", "90%"]}>
|
|
150
|
+
<NativeSheetScrollView contentContainerStyle={{ paddingBottom: 24 }}>
|
|
151
|
+
<Text accessibilityRole="header" className="px-4 pb-2 pt-1 text-lg font-semibold text-foreground">
|
|
152
|
+
{label}
|
|
153
|
+
</Text>
|
|
154
|
+
{showSearch ? (
|
|
155
|
+
<View className="px-4 pb-2">
|
|
156
|
+
<NativeSearchBar testID={`${testID}-search`} value={query} onChange={setQuery} placeholder={searchPlaceholder} showCancel={false} />
|
|
157
|
+
</View>
|
|
158
|
+
) : null}
|
|
159
|
+
{groups.length === 0 ? (
|
|
160
|
+
<Text testID={`${testID}-empty`} className="px-4 py-6 text-center text-sm text-muted-foreground">
|
|
161
|
+
{emptyText}
|
|
162
|
+
</Text>
|
|
163
|
+
) : (
|
|
164
|
+
groups.map((group) => (
|
|
165
|
+
<View key={group.kind} accessibilityRole="radiogroup">
|
|
166
|
+
{labels[group.kind] ? <NativeSectionListHeader title={labels[group.kind]!} /> : null}
|
|
167
|
+
{group.workspaces.map((w) => {
|
|
168
|
+
const selected = w.id === current?.id;
|
|
169
|
+
const badge = hasWorkspaceBadge(w.badge) ? w.badge : null;
|
|
170
|
+
return (
|
|
171
|
+
<NativeListItem
|
|
172
|
+
key={w.id}
|
|
173
|
+
testID={`${testID}-option-${w.id}`}
|
|
174
|
+
title={w.label}
|
|
175
|
+
subtitle={w.description}
|
|
176
|
+
leading={picture(w, 32)}
|
|
177
|
+
accessibilityRole="radio"
|
|
178
|
+
accessibilityState={{ checked: selected }}
|
|
179
|
+
// One element: its badge's words go in its name, or they're never read.
|
|
180
|
+
accessibilityLabel={[w.label, w.description, badge !== null ? badgeLabel(w, badge) : null].filter(Boolean).join(", ")}
|
|
181
|
+
trailing={
|
|
182
|
+
<View className="flex-row items-center gap-2">
|
|
183
|
+
{badge !== null ? (
|
|
184
|
+
<NativeBadge variant="secondary" accessibilityLabel={badgeLabel(w, badge)}>
|
|
185
|
+
{String(badge)}
|
|
186
|
+
</NativeBadge>
|
|
187
|
+
) : null}
|
|
188
|
+
{selected ? <Text className="text-base text-primary-text">✓</Text> : null}
|
|
189
|
+
</View>
|
|
190
|
+
}
|
|
191
|
+
onPress={() => pick(w)}
|
|
192
|
+
/>
|
|
193
|
+
);
|
|
194
|
+
})}
|
|
195
|
+
</View>
|
|
196
|
+
))
|
|
197
|
+
)}
|
|
198
|
+
{actions?.length ? (
|
|
199
|
+
<View className="mt-2 border-t border-border">
|
|
200
|
+
{actions.map((a) => (
|
|
201
|
+
<NativeListItem
|
|
202
|
+
key={a.id}
|
|
203
|
+
testID={`${testID}-action-${a.id}`}
|
|
204
|
+
title={a.label}
|
|
205
|
+
onPress={() => {
|
|
206
|
+
setOpen(false);
|
|
207
|
+
a.onPress();
|
|
208
|
+
}}
|
|
209
|
+
/>
|
|
210
|
+
))}
|
|
211
|
+
</View>
|
|
212
|
+
) : null}
|
|
213
|
+
</NativeSheetScrollView>
|
|
214
|
+
</NativeSheet>
|
|
215
|
+
</>
|
|
216
|
+
);
|
|
217
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// NativeCustomView: a screen an app draws itself over a view (ADR 0019), on the phone. The twin of
|
|
2
|
+
// genesis-ui's CustomView: Genesis loads the view's rows (scope, filters, preset, search, paging,
|
|
3
|
+
// live updates and permissions, through the same hook NativeResourceView uses) and draws loading,
|
|
4
|
+
// empty and error with a retry; the app draws the rest. The status rule is genesis-core's
|
|
5
|
+
// `customViewStatus`, and `groups` is core's `groupRows`, as on the web.
|
|
6
|
+
//
|
|
7
|
+
// The list type is built; a board and a calendar type follow the native board and calendar modes
|
|
8
|
+
// (resource-view-board's native platform).
|
|
9
|
+
|
|
10
|
+
import type { ReactNode } from "react";
|
|
11
|
+
import { View } from "react-native";
|
|
12
|
+
import type { DataRow } from "@marktiderman/genesis-core";
|
|
13
|
+
import { customViewStatus, type CustomViewStatus } from "@marktiderman/genesis-core/headless";
|
|
14
|
+
import { groupRows, type GroupRowsOptions, type RowGroup } from "@marktiderman/genesis-core/resource-view";
|
|
15
|
+
import { NativeButton } from "../components/button";
|
|
16
|
+
import { NativeEmptyState } from "../components/empty-state";
|
|
17
|
+
import { NativeSkeleton } from "../components/skeleton";
|
|
18
|
+
import { useReducedMotion } from "../hooks/use-reduced-motion";
|
|
19
|
+
import { fill } from "./resource-view/messages";
|
|
20
|
+
import { useNativeResourceViewMessages } from "./resource-view/display";
|
|
21
|
+
import type { AnyResourceView } from "./resource-view/mapping";
|
|
22
|
+
import { useNativeResourceView, type NativeResourceViewOptions, type UseNativeResourceViewReturn } from "./resource-view/use-native-resource-view";
|
|
23
|
+
|
|
24
|
+
type Row = Record<string, unknown>;
|
|
25
|
+
|
|
26
|
+
/** What a native custom view draws from: the status, the rows, the list's state and its helpers. */
|
|
27
|
+
export interface NativeCustomViewState<T extends Row = Row> {
|
|
28
|
+
view: AnyResourceView<T>;
|
|
29
|
+
type: "list";
|
|
30
|
+
status: CustomViewStatus;
|
|
31
|
+
error: string | null;
|
|
32
|
+
retry: () => void;
|
|
33
|
+
rows: T[];
|
|
34
|
+
/** The rows under headers: the view's `groupBy`, or `options.field`. */
|
|
35
|
+
groups: (rows?: readonly T[], options?: GroupRowsOptions) => RowGroup<T & DataRow>[];
|
|
36
|
+
/** Paging, search, refresh, the row's actions and the writes, as NativeResourceView has them. */
|
|
37
|
+
list: UseNativeResourceViewReturn<T>;
|
|
38
|
+
/** The person asked for less motion: skip or shorten animations. */
|
|
39
|
+
reducedMotion: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface NativeCustomViewProps<T extends Row = Row> extends NativeResourceViewOptions<T> {
|
|
43
|
+
view: AnyResourceView<T>;
|
|
44
|
+
/** Draws the view once there's something to show. */
|
|
45
|
+
children: (state: NativeCustomViewState<T>) => ReactNode;
|
|
46
|
+
loading?: ReactNode;
|
|
47
|
+
/** Replaces the empty state; `false` hands an empty view to `children` too. */
|
|
48
|
+
empty?: ReactNode | false;
|
|
49
|
+
/** Replaces the error state; a function gets the error and a retry. */
|
|
50
|
+
error?: ReactNode | ((error: string, retry: () => void) => ReactNode);
|
|
51
|
+
testID?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A native custom view's data and states, headless. `<NativeCustomView>` calls it and draws the states. */
|
|
55
|
+
export function useNativeCustomView<T extends Row = Row>(view: AnyResourceView<T>, options: NativeResourceViewOptions<T> = {}): NativeCustomViewState<T> {
|
|
56
|
+
const list = useNativeResourceView<T>(view, options);
|
|
57
|
+
const reducedMotion = useReducedMotion();
|
|
58
|
+
const status = customViewStatus({ error: list.error, loading: list.loading, empty: list.rows.length === 0 });
|
|
59
|
+
return {
|
|
60
|
+
view,
|
|
61
|
+
type: "list",
|
|
62
|
+
status,
|
|
63
|
+
error: list.error,
|
|
64
|
+
retry: () => void list.refresh(),
|
|
65
|
+
rows: list.rows,
|
|
66
|
+
groups: (rows, o) => groupRows(view, (rows ?? list.rows) as readonly (T & DataRow)[], o),
|
|
67
|
+
list,
|
|
68
|
+
reducedMotion,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function NativeCustomView<T extends Row = Row>({ view, children, loading, empty, error, testID = "custom-view", ...options }: NativeCustomViewProps<T>) {
|
|
73
|
+
const state = useNativeCustomView<T>(view, options);
|
|
74
|
+
const messages = useNativeResourceViewMessages();
|
|
75
|
+
const copy = view.definition.copy?.empty;
|
|
76
|
+
const showChildren = state.status === "ready" || (state.status === "empty" && empty === false);
|
|
77
|
+
return (
|
|
78
|
+
<View testID={testID} accessibilityState={{ busy: state.status === "loading" || state.list.refreshing }} className="flex-1">
|
|
79
|
+
{showChildren ? (
|
|
80
|
+
children(state)
|
|
81
|
+
) : state.status === "loading" ? (
|
|
82
|
+
(loading ?? (
|
|
83
|
+
<View testID={`${testID}-loading`} accessibilityRole="progressbar" accessibilityLabel={messages.form.loading} className="gap-2 p-4">
|
|
84
|
+
<NativeSkeleton height={40} className="w-full" />
|
|
85
|
+
<NativeSkeleton height={40} className="w-full" />
|
|
86
|
+
<NativeSkeleton height={40} className="w-2/3" />
|
|
87
|
+
</View>
|
|
88
|
+
))
|
|
89
|
+
) : state.status === "error" ? (
|
|
90
|
+
typeof error === "function" ? (
|
|
91
|
+
error(state.error ?? "", state.retry)
|
|
92
|
+
) : (
|
|
93
|
+
(error ?? (
|
|
94
|
+
<NativeEmptyState
|
|
95
|
+
testID={`${testID}-error`}
|
|
96
|
+
variant="error"
|
|
97
|
+
title={state.error ?? undefined}
|
|
98
|
+
primaryAction={
|
|
99
|
+
<NativeButton variant="outline" size="sm" onPress={state.retry} testID={`${testID}-retry`}>
|
|
100
|
+
{messages.list.retry}
|
|
101
|
+
</NativeButton>
|
|
102
|
+
}
|
|
103
|
+
/>
|
|
104
|
+
))
|
|
105
|
+
)
|
|
106
|
+
) : (
|
|
107
|
+
(empty ?? (
|
|
108
|
+
<NativeEmptyState
|
|
109
|
+
testID={`${testID}-empty`}
|
|
110
|
+
title={copy?.title ?? fill(messages.list.emptyTitle, { nounPlural: view.nounPlural })}
|
|
111
|
+
body={copy?.body ?? messages.list.emptyBody}
|
|
112
|
+
/>
|
|
113
|
+
))
|
|
114
|
+
)}
|
|
115
|
+
</View>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
// The phone's twins of genesis-ui's form pieces beyond one input: NativeFieldArrayRenderer
|
|
2
|
+
// (repeating groups), NativeFileUploadField (pick and list files), NativeRelationField (a link's
|
|
3
|
+
// picker) and NativeWizardForm (a form in steps). Each follows the same ResourceFormFieldDef as
|
|
4
|
+
// the web and takes its rules from genesis-core (`@marktiderman/genesis-core/headless`:
|
|
5
|
+
// fieldArrayLimits, fileProblem, wizardStepFields; `useWizard`, `buildArrayItemDefaults`), so a
|
|
6
|
+
// form definition behaves the same on both platforms.
|
|
7
|
+
|
|
8
|
+
import { useRef, useState, type ReactNode } from "react";
|
|
9
|
+
import { Pressable, Text, View } from "react-native";
|
|
10
|
+
import { useFieldArray, useWatch, type Control, type FieldErrors, type UseFormReturn } from "react-hook-form";
|
|
11
|
+
import { titleCase, type ResourceFormFieldDef } from "@marktiderman/genesis-core";
|
|
12
|
+
import { buildArrayItemDefaults, useWizard, type WizardStep } from "@marktiderman/genesis-core/hooks";
|
|
13
|
+
import {
|
|
14
|
+
fieldAccept,
|
|
15
|
+
fieldArrayAddLabel,
|
|
16
|
+
fieldArrayLimits,
|
|
17
|
+
fileProblem,
|
|
18
|
+
formatFileSize,
|
|
19
|
+
wizardProgressLabel,
|
|
20
|
+
wizardStepFields,
|
|
21
|
+
} from "@marktiderman/genesis-core/headless";
|
|
22
|
+
import { NativeButton } from "../components/button";
|
|
23
|
+
import { NativeCard } from "../components/card";
|
|
24
|
+
import { NativeImage } from "../components/image";
|
|
25
|
+
import { NativeLabel } from "../components/label";
|
|
26
|
+
import { NativeProgress } from "../components/progress";
|
|
27
|
+
import { NativeText } from "../components/text";
|
|
28
|
+
import { useGenesisModules, type GenesisFilePicker, type NativePickedFile } from "../native-modules";
|
|
29
|
+
import { NativeResourceFormFieldRenderer } from "./NativeResourceFormField";
|
|
30
|
+
import { NativeLinkPicker } from "./resource-view/NativeLinkPicker";
|
|
31
|
+
import type { NativeFormField } from "./resource-view/mapping";
|
|
32
|
+
|
|
33
|
+
// ── Repeating groups ─────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
export interface NativeFieldArrayRendererProps {
|
|
36
|
+
field: ResourceFormFieldDef;
|
|
37
|
+
control: Control<Record<string, unknown>>;
|
|
38
|
+
action: "create" | "edit";
|
|
39
|
+
watchedValues?: Record<string, unknown>;
|
|
40
|
+
/** The words. `{n}`: the item's number. `remove` is what a screen reader hears, `removeButton` the button's visible word. */
|
|
41
|
+
labels?: { item?: string; remove?: string; removeButton?: string; add?: string };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A field of `type: "array"`: one card per item, each with the field's `arrayFields`, and Add / Remove within `arrayMin` and `arrayMax`. */
|
|
45
|
+
export function NativeFieldArrayRenderer({ field, control, action, watchedValues, labels = {} }: NativeFieldArrayRendererProps) {
|
|
46
|
+
const { fields: items, append, remove } = useFieldArray({ control: control as unknown as Control, name: field.key });
|
|
47
|
+
const subFields = field.arrayFields ?? [];
|
|
48
|
+
const label = field.label ?? titleCase(field.key);
|
|
49
|
+
const { canAdd, canRemove } = fieldArrayLimits(items.length, field);
|
|
50
|
+
const fill = (template: string, n: number) => template.replace("{n}", String(n));
|
|
51
|
+
return (
|
|
52
|
+
<View testID={`field-array-${field.key}`} className="gap-2">
|
|
53
|
+
<NativeLabel>{field.required ? `${label} *` : label}</NativeLabel>
|
|
54
|
+
{field.description ? (
|
|
55
|
+
<NativeText preset="caption" className="text-muted-foreground">
|
|
56
|
+
{field.description}
|
|
57
|
+
</NativeText>
|
|
58
|
+
) : null}
|
|
59
|
+
{items.map((item, index) => (
|
|
60
|
+
<NativeCard key={item.id} className="gap-3 p-4" testID={`field-array-${field.key}-${index}`}>
|
|
61
|
+
<View className="flex-row items-center justify-between">
|
|
62
|
+
<Text className="text-xs font-medium text-muted-foreground">{fill(labels.item ?? "Item {n}", index + 1)}</Text>
|
|
63
|
+
<NativeButton
|
|
64
|
+
variant="ghost"
|
|
65
|
+
size="sm"
|
|
66
|
+
disabled={!canRemove}
|
|
67
|
+
onPress={() => remove(index)}
|
|
68
|
+
accessibilityLabel={fill(labels.remove ?? "Remove item {n}", index + 1)}
|
|
69
|
+
testID={`field-array-${field.key}-${index}-remove`}
|
|
70
|
+
textClassName="text-destructive"
|
|
71
|
+
>
|
|
72
|
+
{labels.removeButton ?? "Remove"}
|
|
73
|
+
</NativeButton>
|
|
74
|
+
</View>
|
|
75
|
+
{subFields.map((sub) => (
|
|
76
|
+
<NativeResourceFormFieldRenderer
|
|
77
|
+
key={sub.key}
|
|
78
|
+
field={{ ...sub, key: `${field.key}.${index}.${sub.key}` }}
|
|
79
|
+
control={control}
|
|
80
|
+
action={action}
|
|
81
|
+
watchedValues={watchedValues}
|
|
82
|
+
/>
|
|
83
|
+
))}
|
|
84
|
+
</NativeCard>
|
|
85
|
+
))}
|
|
86
|
+
<NativeButton variant="outline" size="sm" disabled={!canAdd} onPress={() => append(buildArrayItemDefaults(subFields))} testID={`field-array-${field.key}-add`} className="self-start">
|
|
87
|
+
{labels.add ?? fieldArrayAddLabel(label, field.arrayItemLabel)}
|
|
88
|
+
</NativeButton>
|
|
89
|
+
</View>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ── Files ────────────────────────────────────────────────────────────────
|
|
94
|
+
|
|
95
|
+
export interface NativeFileUploadFieldProps {
|
|
96
|
+
field: ResourceFormFieldDef;
|
|
97
|
+
value: NativePickedFile | NativePickedFile[] | null;
|
|
98
|
+
onChange: (value: NativePickedFile | NativePickedFile[] | null) => void;
|
|
99
|
+
disabled?: boolean;
|
|
100
|
+
/** Opens the picker. Default `GenesisModules.filePicker`. */
|
|
101
|
+
pickFiles?: GenesisFilePicker;
|
|
102
|
+
/** The words. `pickFailed`: the picker rejected (a permission denied, a download that failed). */
|
|
103
|
+
labels?: { choose?: string; chooseMore?: string; remove?: string; noPicker?: string; pickFailed?: string };
|
|
104
|
+
error?: string;
|
|
105
|
+
testID?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A field of `type: "file"` or `"image"`: a button that opens the system picker, then the picked files with their size and a Remove each. */
|
|
109
|
+
export function NativeFileUploadField({ field, value, onChange, disabled, pickFiles, labels = {}, error, testID = `file-upload-${field.key}` }: NativeFileUploadFieldProps) {
|
|
110
|
+
const { filePicker } = useGenesisModules();
|
|
111
|
+
const pick = pickFiles ?? filePicker;
|
|
112
|
+
const multiple = field.multiple ?? false;
|
|
113
|
+
const accept = fieldAccept(field);
|
|
114
|
+
const files = value ? (Array.isArray(value) ? value : [value]) : [];
|
|
115
|
+
const [problems, setProblems] = useState<string[]>([]);
|
|
116
|
+
// One picker at a time: a second tap while one is open does nothing (iOS throws on a second one).
|
|
117
|
+
const picking = useRef(false);
|
|
118
|
+
const [busy, setBusy] = useState(false);
|
|
119
|
+
const label = field.label ?? titleCase(field.key);
|
|
120
|
+
|
|
121
|
+
const choose = async () => {
|
|
122
|
+
if (!pick || picking.current) return;
|
|
123
|
+
picking.current = true;
|
|
124
|
+
setBusy(true);
|
|
125
|
+
let picked: NativePickedFile[];
|
|
126
|
+
try {
|
|
127
|
+
picked = await pick({ multiple, accept });
|
|
128
|
+
} catch {
|
|
129
|
+
// The picker's own error is for developers (E_NO_PERMISSIONS); the person gets a sentence.
|
|
130
|
+
setProblems([labels.pickFailed ?? "Couldn't open the picker. Check that the app may use your photos and files, then try again."]);
|
|
131
|
+
return;
|
|
132
|
+
} finally {
|
|
133
|
+
picking.current = false;
|
|
134
|
+
setBusy(false);
|
|
135
|
+
}
|
|
136
|
+
if (!picked.length) return;
|
|
137
|
+
const bad = picked.map((f) => fileProblem(f, { accept, maxSize: field.maxSize })).filter((p): p is string => p !== null);
|
|
138
|
+
const good = picked.filter((f) => fileProblem(f, { accept, maxSize: field.maxSize }) === null);
|
|
139
|
+
setProblems(bad);
|
|
140
|
+
if (!good.length) return;
|
|
141
|
+
onChange(multiple ? [...files, ...good] : good[0]!);
|
|
142
|
+
};
|
|
143
|
+
const removeAt = (index: number) => {
|
|
144
|
+
const next = files.filter((_, i) => i !== index);
|
|
145
|
+
onChange(next.length === 0 ? null : multiple ? next : next[0]!);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
return (
|
|
149
|
+
<View testID={testID} className="gap-2">
|
|
150
|
+
<NativeLabel>{field.required ? `${label} *` : label}</NativeLabel>
|
|
151
|
+
{files.map((file, i) => (
|
|
152
|
+
<View key={`${file.uri}-${i}`} testID={`${testID}-file-${i}`} className="flex-row items-center gap-3 rounded-md border border-border p-2">
|
|
153
|
+
{file.type?.startsWith("image/") ? <NativeImage src={file.uri} width={40} height={40} className="rounded" /> : null}
|
|
154
|
+
<View className="min-w-0 flex-1">
|
|
155
|
+
<Text numberOfLines={1} className="text-sm text-foreground">
|
|
156
|
+
{file.name}
|
|
157
|
+
</Text>
|
|
158
|
+
{file.size != null ? <Text className="text-xs text-muted-foreground">{formatFileSize(file.size)}</Text> : null}
|
|
159
|
+
</View>
|
|
160
|
+
<Pressable
|
|
161
|
+
testID={`${testID}-remove-${i}`}
|
|
162
|
+
accessibilityRole="button"
|
|
163
|
+
accessibilityLabel={`${labels.remove ?? "Remove"} ${file.name}`}
|
|
164
|
+
disabled={disabled}
|
|
165
|
+
onPress={() => removeAt(i)}
|
|
166
|
+
hitSlop={8}
|
|
167
|
+
className="min-h-[44px] min-w-[44px] items-center justify-center"
|
|
168
|
+
>
|
|
169
|
+
<Text className="text-base text-muted-foreground">✕</Text>
|
|
170
|
+
</Pressable>
|
|
171
|
+
</View>
|
|
172
|
+
))}
|
|
173
|
+
{multiple || files.length === 0 ? (
|
|
174
|
+
<NativeButton
|
|
175
|
+
variant="outline"
|
|
176
|
+
disabled={disabled || !pick || busy}
|
|
177
|
+
onPress={() => void choose()}
|
|
178
|
+
testID={`${testID}-choose`}
|
|
179
|
+
accessibilityHint={field.description}
|
|
180
|
+
className="self-start"
|
|
181
|
+
>
|
|
182
|
+
{files.length && multiple ? (labels.chooseMore ?? "Add more") : (labels.choose ?? (field.type === "image" ? "Choose a photo" : "Choose a file"))}
|
|
183
|
+
</NativeButton>
|
|
184
|
+
) : null}
|
|
185
|
+
{!pick ? (
|
|
186
|
+
<NativeText preset="caption" className="text-muted-foreground">
|
|
187
|
+
{labels.noPicker ?? "No file picker is set up (GenesisModules.filePicker)."}
|
|
188
|
+
</NativeText>
|
|
189
|
+
) : null}
|
|
190
|
+
{[...problems, ...(error ? [error] : [])].map((p) => (
|
|
191
|
+
<NativeText key={p} preset="caption" className="text-destructive" accessibilityRole="alert">
|
|
192
|
+
{p}
|
|
193
|
+
</NativeText>
|
|
194
|
+
))}
|
|
195
|
+
</View>
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ── A link's picker ──────────────────────────────────────────────────────
|
|
200
|
+
|
|
201
|
+
export interface NativeRelationFieldProps {
|
|
202
|
+
field: ResourceFormFieldDef;
|
|
203
|
+
value: unknown;
|
|
204
|
+
/** The key as the field's `relation.keys` says: a string (`""` for none), or the row's own (`null` for none). */
|
|
205
|
+
onChange: (value: string | number | null) => void;
|
|
206
|
+
values?: Readonly<Record<string, unknown>>;
|
|
207
|
+
error?: string;
|
|
208
|
+
testID?: string;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* A field of `type: "relation"`: the linked resource's rows through the data provider, searched
|
|
213
|
+
* and paged on the server. The same picker NativeResourceView's form uses (NativeLinkPicker),
|
|
214
|
+
* fed from the field's `relation`.
|
|
215
|
+
*/
|
|
216
|
+
export function NativeRelationField({ field, value, onChange, values = {}, error, testID = `relation-${field.key}` }: NativeRelationFieldProps) {
|
|
217
|
+
const relation = field.relation!;
|
|
218
|
+
const label = field.label ?? titleCase(field.key);
|
|
219
|
+
const native: NativeFormField = {
|
|
220
|
+
key: field.key,
|
|
221
|
+
label,
|
|
222
|
+
kind: "link" as NativeFormField["kind"],
|
|
223
|
+
display: "text" as NativeFormField["display"],
|
|
224
|
+
required: field.required ?? false,
|
|
225
|
+
readOnly: false,
|
|
226
|
+
section: null,
|
|
227
|
+
sectionTitle: null,
|
|
228
|
+
...(relation.missingLabel ? { missingLabel: relation.missingLabel } : {}),
|
|
229
|
+
link: {
|
|
230
|
+
resource: relation.resource,
|
|
231
|
+
field: relation.valueField ?? "id",
|
|
232
|
+
labelField: relation.labelField,
|
|
233
|
+
...(relation.searchFields ? { searchFields: relation.searchFields } : {}),
|
|
234
|
+
...(relation.orderBy ? { orderBy: { field: relation.orderBy.field, direction: relation.orderBy.direction ?? "asc", nulls: relation.orderBy.nulls } } : {}),
|
|
235
|
+
filters: (_scope, formValues) => {
|
|
236
|
+
const f = relation.filters;
|
|
237
|
+
return typeof f === "function" ? f(formValues ?? {}) : (f ?? []);
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
return (
|
|
242
|
+
<NativeLinkPicker
|
|
243
|
+
field={native}
|
|
244
|
+
value={value}
|
|
245
|
+
values={values}
|
|
246
|
+
error={error}
|
|
247
|
+
testID={testID}
|
|
248
|
+
onChange={(next) => onChange(relation.keys === "row" ? next : next === null ? "" : String(next))}
|
|
249
|
+
/>
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// ── Steps ────────────────────────────────────────────────────────────────
|
|
254
|
+
|
|
255
|
+
export interface NativeWizardFormProps {
|
|
256
|
+
steps: WizardStep[];
|
|
257
|
+
form: UseFormReturn<Record<string, unknown>>;
|
|
258
|
+
fields: ResourceFormFieldDef[];
|
|
259
|
+
onSubmit: (data: Record<string, unknown>) => Promise<void>;
|
|
260
|
+
isSaving: boolean;
|
|
261
|
+
action: "create" | "edit";
|
|
262
|
+
onInvalid?: (errors: FieldErrors) => void;
|
|
263
|
+
renderField?: (field: ResourceFormFieldDef, control: UseFormReturn<Record<string, unknown>>["control"], action: "create" | "edit", watchedValues: Record<string, unknown>) => ReactNode;
|
|
264
|
+
labels?: { back?: string; next?: string; create?: string; save?: string };
|
|
265
|
+
testID?: string;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** A form in steps (genesis-core's `useWizard`): the step's fields, its progress, Back and Next, and Create or Save on the last step. */
|
|
269
|
+
export function NativeWizardForm({ steps, form, fields, onSubmit, isSaving, action, onInvalid, renderField, labels = {}, testID = "wizard-form" }: NativeWizardFormProps) {
|
|
270
|
+
const wizard = useWizard(steps, form);
|
|
271
|
+
const watched = useWatch({ control: form.control }) as Record<string, unknown>;
|
|
272
|
+
const stepFields = wizardStepFields(fields, wizard.currentStepDef.fields, watched);
|
|
273
|
+
const progress = wizardProgressLabel(wizard);
|
|
274
|
+
const next = async () => {
|
|
275
|
+
if (wizard.isLast) await form.handleSubmit(onSubmit, onInvalid)();
|
|
276
|
+
else await wizard.next();
|
|
277
|
+
};
|
|
278
|
+
return (
|
|
279
|
+
<View testID={testID} className="gap-6">
|
|
280
|
+
<View className="gap-2">
|
|
281
|
+
<View className="flex-row items-center justify-between">
|
|
282
|
+
<Text className="text-sm text-muted-foreground">{progress.step}</Text>
|
|
283
|
+
<Text className="text-sm text-muted-foreground">{progress.percent}</Text>
|
|
284
|
+
</View>
|
|
285
|
+
<NativeProgress value={wizard.progress * 100} testID={`${testID}-progress`} />
|
|
286
|
+
</View>
|
|
287
|
+
<View className="gap-1">
|
|
288
|
+
<Text accessibilityRole="header" className="text-lg font-semibold text-foreground">
|
|
289
|
+
{wizard.currentStepDef.title}
|
|
290
|
+
</Text>
|
|
291
|
+
{wizard.currentStepDef.description ? <Text className="text-sm text-muted-foreground">{wizard.currentStepDef.description}</Text> : null}
|
|
292
|
+
</View>
|
|
293
|
+
<View className="gap-4" key={wizard.currentStep}>
|
|
294
|
+
{stepFields.map((f) =>
|
|
295
|
+
renderField ? (
|
|
296
|
+
<View key={f.key}>{renderField(f, form.control, action, watched)}</View>
|
|
297
|
+
) : (
|
|
298
|
+
<NativeResourceFormFieldRenderer key={f.key} field={f} control={form.control} action={action} watchedValues={watched} />
|
|
299
|
+
),
|
|
300
|
+
)}
|
|
301
|
+
</View>
|
|
302
|
+
<View className="flex-row justify-between pt-2">
|
|
303
|
+
<NativeButton variant="outline" onPress={wizard.back} disabled={wizard.isFirst || isSaving} testID={`${testID}-back`}>
|
|
304
|
+
{labels.back ?? "Back"}
|
|
305
|
+
</NativeButton>
|
|
306
|
+
<NativeButton onPress={() => void next()} disabled={isSaving} loading={isSaving} testID={`${testID}-next`}>
|
|
307
|
+
{wizard.isLast ? (action === "create" ? (labels.create ?? "Create") : (labels.save ?? "Save")) : (labels.next ?? "Next")}
|
|
308
|
+
</NativeButton>
|
|
309
|
+
</View>
|
|
310
|
+
</View>
|
|
311
|
+
);
|
|
312
|
+
}
|