@marktiderman/genesis-ui-native 2.0.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/dist/chunk-BAU4YQZV.js +760 -0
- package/dist/chunk-BAU4YQZV.js.map +1 -0
- package/dist/chunk-DGUM43GV.js +10 -0
- package/dist/chunk-DGUM43GV.js.map +1 -0
- package/dist/chunk-HNIEFTEL.js +43 -0
- package/dist/chunk-HNIEFTEL.js.map +1 -0
- package/dist/chunk-N4WHTQIO.js +20 -0
- package/dist/chunk-N4WHTQIO.js.map +1 -0
- package/dist/chunk-OHZD7KRO.js +11 -0
- package/dist/chunk-OHZD7KRO.js.map +1 -0
- package/dist/chunk-PTJ2KCLM.js +160 -0
- package/dist/chunk-PTJ2KCLM.js.map +1 -0
- package/dist/chunk-TXJT55JC.js +2213 -0
- package/dist/chunk-TXJT55JC.js.map +1 -0
- package/dist/chunk-ULS7I6ZS.js +183 -0
- package/dist/chunk-ULS7I6ZS.js.map +1 -0
- package/dist/chunk-ZVF6PONU.js +522 -0
- package/dist/chunk-ZVF6PONU.js.map +1 -0
- package/dist/components/index.d.ts +908 -0
- package/dist/components/index.js +8 -0
- package/dist/components/index.js.map +1 -0
- package/dist/data/index.d.ts +117 -0
- package/dist/data/index.js +770 -0
- package/dist/data/index.js.map +1 -0
- package/dist/hooks/index.d.ts +31 -0
- package/dist/hooks/index.js +5 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/layouts/index.d.ts +364 -0
- package/dist/layouts/index.js +8 -0
- package/dist/layouts/index.js.map +1 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +4 -0
- package/dist/utils.js.map +1 -0
- package/package.json +127 -0
- package/src/components/__tests__/accordion.test.tsx +122 -0
- package/src/components/__tests__/chip-group.test.tsx +45 -0
- package/src/components/__tests__/combobox.test.tsx +97 -0
- package/src/components/__tests__/drawer.test.tsx +98 -0
- package/src/components/__tests__/empty-state.test.tsx +66 -0
- package/src/components/__tests__/header-card.test.tsx +31 -0
- package/src/components/__tests__/layout.test.tsx +109 -0
- package/src/components/__tests__/list-item.test.tsx +31 -0
- package/src/components/__tests__/number-input.test.tsx +46 -0
- package/src/components/__tests__/pagination.test.tsx +43 -0
- package/src/components/__tests__/search-bar.test.tsx +41 -0
- package/src/components/__tests__/segmented-empty-alert.test.tsx +79 -0
- package/src/components/__tests__/slider.test.tsx +134 -0
- package/src/components/__tests__/stepper.test.tsx +25 -0
- package/src/components/__tests__/toast.test.tsx +63 -0
- package/src/components/accordion.tsx +223 -0
- package/src/components/alert.tsx +131 -0
- package/src/components/avatar.tsx +68 -0
- package/src/components/badge.tsx +80 -0
- package/src/components/button.tsx +118 -0
- package/src/components/card.tsx +120 -0
- package/src/components/checkbox.tsx +60 -0
- package/src/components/chip-group.tsx +127 -0
- package/src/components/combobox.tsx +137 -0
- package/src/components/date-time-picker.tsx +127 -0
- package/src/components/dialog.tsx +131 -0
- package/src/components/drawer.tsx +131 -0
- package/src/components/dropdown-menu.tsx +133 -0
- package/src/components/empty-state.tsx +161 -0
- package/src/components/header-card.tsx +116 -0
- package/src/components/index.ts +259 -0
- package/src/components/inline-alert.tsx +150 -0
- package/src/components/input.tsx +51 -0
- package/src/components/label.tsx +35 -0
- package/src/components/layout/box.tsx +115 -0
- package/src/components/layout/flex.ts +24 -0
- package/src/components/layout/grid.tsx +73 -0
- package/src/components/layout/index.ts +27 -0
- package/src/components/layout/inline.tsx +63 -0
- package/src/components/layout/spacing.ts +116 -0
- package/src/components/layout/stack.tsx +58 -0
- package/src/components/list-item.tsx +108 -0
- package/src/components/number-input.tsx +119 -0
- package/src/components/pagination.tsx +132 -0
- package/src/components/popover.tsx +198 -0
- package/src/components/progress.tsx +50 -0
- package/src/components/radio-group.tsx +92 -0
- package/src/components/refresh-control.tsx +73 -0
- package/src/components/search-bar.tsx +128 -0
- package/src/components/section-list.tsx +54 -0
- package/src/components/segmented-control.tsx +79 -0
- package/src/components/select.tsx +178 -0
- package/src/components/separator.tsx +31 -0
- package/src/components/sheet.tsx +192 -0
- package/src/components/skeleton.tsx +104 -0
- package/src/components/slider.tsx +223 -0
- package/src/components/stepper.tsx +96 -0
- package/src/components/switch.tsx +35 -0
- package/src/components/tabs.tsx +147 -0
- package/src/components/text.tsx +62 -0
- package/src/components/textarea.tsx +52 -0
- package/src/components/toast.tsx +228 -0
- package/src/components/toggle.tsx +85 -0
- package/src/components/tooltip.tsx +104 -0
- package/src/data/NativeDataFilters.tsx +100 -0
- package/src/data/NativeDataPageShell.tsx +111 -0
- package/src/data/NativeDetailPanel.tsx +130 -0
- package/src/data/NativeEmptyState.tsx +46 -0
- package/src/data/NativeResourceForm.tsx +157 -0
- package/src/data/NativeResourceFormField.tsx +280 -0
- package/src/data/NativeResourcePage.tsx +505 -0
- package/src/data/index.ts +9 -0
- package/src/hooks/index.ts +16 -0
- package/src/hooks/use-genesis-extension.ts +81 -0
- package/src/hooks/use-reduced-motion.ts +32 -0
- package/src/hooks/use-theme-mode.ts +31 -0
- package/src/index.ts +5 -0
- package/src/layouts/__tests__/auth.test.tsx +150 -0
- package/src/layouts/__tests__/nav-bar.test.tsx +86 -0
- package/src/layouts/__tests__/notifications-page.test.tsx +94 -0
- package/src/layouts/__tests__/screen-container.test.tsx +79 -0
- package/src/layouts/__tests__/settings-page.test.tsx +107 -0
- package/src/layouts/__tests__/settings-sub-page.test.tsx +53 -0
- package/src/layouts/auth.tsx +352 -0
- package/src/layouts/index.ts +61 -0
- package/src/layouts/nav-bar.tsx +200 -0
- package/src/layouts/notifications-page.tsx +274 -0
- package/src/layouts/screen-container.tsx +213 -0
- package/src/layouts/settings-page.tsx +195 -0
- package/src/layouts/settings-sub-page.tsx +183 -0
- package/src/nativewind.d.ts +40 -0
- package/src/utils.ts +6 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { titleCase } from "@marktiderman/genesis-core";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { ScrollView, View } from "react-native";
|
|
5
|
+
import { SafeAreaView } from "react-native-safe-area-context";
|
|
6
|
+
import { NativeButton } from "../components/button";
|
|
7
|
+
import { NativeCard, NativeCardContent } from "../components/card";
|
|
8
|
+
import { NativeSeparator } from "../components/separator";
|
|
9
|
+
import { NativeText } from "../components/text";
|
|
10
|
+
|
|
11
|
+
export interface NativeDetailField<T = Record<string, unknown>> {
|
|
12
|
+
key: Extract<keyof T, string>;
|
|
13
|
+
label: string;
|
|
14
|
+
render?: (value: unknown, item: T) => ReactNode;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface NativeDetailPanelProps<T = Record<string, unknown>> {
|
|
18
|
+
item: T;
|
|
19
|
+
titleField?: Extract<keyof T, string>;
|
|
20
|
+
fields?: NativeDetailField<T>[];
|
|
21
|
+
onEdit?: (item: T) => void;
|
|
22
|
+
onDelete?: (item: T) => void;
|
|
23
|
+
children?: ReactNode;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const SKIP_KEYS = new Set(["id", "created_at", "updated_at"]);
|
|
27
|
+
|
|
28
|
+
function NativeDetailPanel<T extends Record<string, unknown>>({
|
|
29
|
+
item,
|
|
30
|
+
titleField,
|
|
31
|
+
fields,
|
|
32
|
+
onEdit,
|
|
33
|
+
onDelete,
|
|
34
|
+
children,
|
|
35
|
+
}: NativeDetailPanelProps<T>) {
|
|
36
|
+
const title = titleField ? String(item[titleField] ?? "") : undefined;
|
|
37
|
+
const hasActions = onEdit != null || onDelete != null;
|
|
38
|
+
|
|
39
|
+
// Escape hatch — caller provides full content
|
|
40
|
+
if (children) {
|
|
41
|
+
return (
|
|
42
|
+
<SafeAreaView edges={["bottom"]} style={{ flex: 1 }}>
|
|
43
|
+
<ScrollView
|
|
44
|
+
contentContainerStyle={{ padding: 16, gap: 16 }}
|
|
45
|
+
showsVerticalScrollIndicator={false}
|
|
46
|
+
>
|
|
47
|
+
{title ? (
|
|
48
|
+
<NativeText preset="h3">{title}</NativeText>
|
|
49
|
+
) : null}
|
|
50
|
+
{children}
|
|
51
|
+
</ScrollView>
|
|
52
|
+
</SafeAreaView>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Resolve field list
|
|
57
|
+
const resolvedFields: NativeDetailField<T>[] =
|
|
58
|
+
fields ??
|
|
59
|
+
Object.keys(item)
|
|
60
|
+
.filter((k) => !SKIP_KEYS.has(k))
|
|
61
|
+
.map((k) => ({ key: k as Extract<keyof T, string>, label: titleCase(k) }));
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<SafeAreaView edges={["bottom"]} style={{ flex: 1 }}>
|
|
65
|
+
<ScrollView
|
|
66
|
+
contentContainerStyle={{ padding: 16, gap: 16 }}
|
|
67
|
+
showsVerticalScrollIndicator={false}
|
|
68
|
+
>
|
|
69
|
+
{title ? (
|
|
70
|
+
<NativeText preset="h3">{title}</NativeText>
|
|
71
|
+
) : null}
|
|
72
|
+
|
|
73
|
+
<NativeCard>
|
|
74
|
+
<NativeCardContent className="pt-6 gap-4">
|
|
75
|
+
{resolvedFields.map((field, index) => {
|
|
76
|
+
const raw = item[field.key];
|
|
77
|
+
const rendered = field.render
|
|
78
|
+
? field.render(raw, item)
|
|
79
|
+
: raw == null
|
|
80
|
+
? null
|
|
81
|
+
: String(raw);
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<View key={field.key}>
|
|
85
|
+
{index > 0 ? (
|
|
86
|
+
<NativeSeparator style={{ marginBottom: 16 }} />
|
|
87
|
+
) : null}
|
|
88
|
+
<NativeText
|
|
89
|
+
preset="caption"
|
|
90
|
+
className="text-muted-foreground uppercase tracking-wider mb-1"
|
|
91
|
+
>
|
|
92
|
+
{field.label}
|
|
93
|
+
</NativeText>
|
|
94
|
+
{typeof rendered === "string" || rendered == null ? (
|
|
95
|
+
<NativeText preset="body">
|
|
96
|
+
{rendered ?? "—"}
|
|
97
|
+
</NativeText>
|
|
98
|
+
) : (
|
|
99
|
+
rendered
|
|
100
|
+
)}
|
|
101
|
+
</View>
|
|
102
|
+
);
|
|
103
|
+
})}
|
|
104
|
+
</NativeCardContent>
|
|
105
|
+
</NativeCard>
|
|
106
|
+
|
|
107
|
+
{hasActions ? (
|
|
108
|
+
<View style={{ gap: 12 }}>
|
|
109
|
+
<NativeSeparator />
|
|
110
|
+
{onEdit ? (
|
|
111
|
+
<NativeButton variant="outline" onPress={() => onEdit(item)}>
|
|
112
|
+
Edit
|
|
113
|
+
</NativeButton>
|
|
114
|
+
) : null}
|
|
115
|
+
{onDelete ? (
|
|
116
|
+
<NativeButton
|
|
117
|
+
variant="destructive"
|
|
118
|
+
onPress={() => onDelete(item)}
|
|
119
|
+
>
|
|
120
|
+
Delete
|
|
121
|
+
</NativeButton>
|
|
122
|
+
) : null}
|
|
123
|
+
</View>
|
|
124
|
+
) : null}
|
|
125
|
+
</ScrollView>
|
|
126
|
+
</SafeAreaView>
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export { NativeDetailPanel };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { View } from "react-native";
|
|
3
|
+
import { NativeButton } from "../components/button";
|
|
4
|
+
import { NativeText } from "../components/text";
|
|
5
|
+
|
|
6
|
+
export interface NativeEmptyStateProps {
|
|
7
|
+
message?: string;
|
|
8
|
+
action?: React.ReactNode;
|
|
9
|
+
hasFilters?: boolean;
|
|
10
|
+
onClearFilters?: () => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function NativeEmptyState({
|
|
14
|
+
message = "No items found",
|
|
15
|
+
action,
|
|
16
|
+
hasFilters = false,
|
|
17
|
+
onClearFilters,
|
|
18
|
+
}: NativeEmptyStateProps) {
|
|
19
|
+
if (hasFilters) {
|
|
20
|
+
return (
|
|
21
|
+
<View className="flex-1 items-center justify-center px-4 py-12">
|
|
22
|
+
<NativeText preset="body" className="text-muted-foreground text-center">
|
|
23
|
+
No results match your filters
|
|
24
|
+
</NativeText>
|
|
25
|
+
{onClearFilters ? (
|
|
26
|
+
<View className="mt-4">
|
|
27
|
+
<NativeButton variant="outline" size="sm" onPress={onClearFilters}>
|
|
28
|
+
Clear Filters
|
|
29
|
+
</NativeButton>
|
|
30
|
+
</View>
|
|
31
|
+
) : null}
|
|
32
|
+
</View>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<View className="flex-1 items-center justify-center px-4 py-12">
|
|
38
|
+
<NativeText preset="body" className="text-muted-foreground text-center">
|
|
39
|
+
{message}
|
|
40
|
+
</NativeText>
|
|
41
|
+
{action ? <View className="mt-4">{action}</View> : null}
|
|
42
|
+
</View>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { NativeEmptyState };
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { useResourceForm, titleCase } from "@marktiderman/genesis-core";
|
|
2
|
+
import type { UseResourceFormOptions, ResourceFormFieldDef } from "@marktiderman/genesis-core";
|
|
3
|
+
import type { UseFormReturn } from "react-hook-form";
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
import type { ReactNode } from "react";
|
|
6
|
+
import { ScrollView, View } from "react-native";
|
|
7
|
+
import { SafeAreaView } from "react-native-safe-area-context";
|
|
8
|
+
import { NativeAlert, NativeAlertDescription, NativeAlertTitle } from "../components/alert";
|
|
9
|
+
import { NativeButton } from "../components/button";
|
|
10
|
+
import { NativeSeparator } from "../components/separator";
|
|
11
|
+
import { NativeText } from "../components/text";
|
|
12
|
+
import { NativeResourceFormFieldRenderer } from "./NativeResourceFormField";
|
|
13
|
+
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// Types
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
export interface NativeResourceFormProps {
|
|
19
|
+
resource: string;
|
|
20
|
+
action: "create" | "edit";
|
|
21
|
+
fields?: ResourceFormFieldDef[];
|
|
22
|
+
item?: Record<string, unknown>;
|
|
23
|
+
listData?: Record<string, unknown>[];
|
|
24
|
+
title?: string;
|
|
25
|
+
layout?: "inline" | "screen";
|
|
26
|
+
onSuccess?: (data: Record<string, unknown>) => void;
|
|
27
|
+
onError?: (error: Error) => void;
|
|
28
|
+
renderForm?: (
|
|
29
|
+
form: UseFormReturn<Record<string, unknown>>,
|
|
30
|
+
fields: ResourceFormFieldDef[],
|
|
31
|
+
) => ReactNode;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// Component
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
function NativeResourceForm({
|
|
39
|
+
resource,
|
|
40
|
+
action,
|
|
41
|
+
fields,
|
|
42
|
+
item,
|
|
43
|
+
listData,
|
|
44
|
+
title,
|
|
45
|
+
layout = "screen",
|
|
46
|
+
onSuccess,
|
|
47
|
+
onError,
|
|
48
|
+
renderForm,
|
|
49
|
+
}: NativeResourceFormProps) {
|
|
50
|
+
const options: UseResourceFormOptions = {
|
|
51
|
+
resource,
|
|
52
|
+
action,
|
|
53
|
+
initialData: item,
|
|
54
|
+
fields,
|
|
55
|
+
listData,
|
|
56
|
+
onSuccess,
|
|
57
|
+
onError,
|
|
58
|
+
prefillFromUrl: false,
|
|
59
|
+
validationMode: "onSubmit",
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const { form, resolvedFields, onSubmit, isSaving } = useResourceForm(options);
|
|
63
|
+
|
|
64
|
+
const resolvedTitle =
|
|
65
|
+
title ?? (action === "create" ? `Create ${titleCase(resource)}` : `Edit ${titleCase(resource)}`);
|
|
66
|
+
|
|
67
|
+
const submitLabel = action === "create" ? "Create" : "Save";
|
|
68
|
+
|
|
69
|
+
const values = form.watch();
|
|
70
|
+
const rootError = form.formState.errors.root?.message;
|
|
71
|
+
|
|
72
|
+
// Filter and group fields — memoized together to avoid cascading re-creation
|
|
73
|
+
const { groups, firstFieldKey } = React.useMemo(() => {
|
|
74
|
+
const visible = resolvedFields.filter(
|
|
75
|
+
(field) => field.visible == null || field.visible(values) !== false,
|
|
76
|
+
);
|
|
77
|
+
const result: Array<{ group: string | undefined; fields: ResourceFormFieldDef[] }> = [];
|
|
78
|
+
for (const field of visible) {
|
|
79
|
+
const lastGroup = result[result.length - 1];
|
|
80
|
+
if (lastGroup && lastGroup.group === field.group) {
|
|
81
|
+
lastGroup.fields.push(field);
|
|
82
|
+
} else {
|
|
83
|
+
result.push({ group: field.group, fields: [field] });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const first = visible.find((f) => !f.hidden && f.type !== "hidden");
|
|
87
|
+
return { groups: result, firstFieldKey: first?.key ?? null };
|
|
88
|
+
}, [resolvedFields, values]);
|
|
89
|
+
|
|
90
|
+
const formContent = renderForm ? (
|
|
91
|
+
renderForm(form, resolvedFields)
|
|
92
|
+
) : (
|
|
93
|
+
<View style={{ gap: 16 }}>
|
|
94
|
+
{rootError ? (
|
|
95
|
+
<NativeAlert variant="destructive">
|
|
96
|
+
<NativeAlertTitle variant="destructive">Error</NativeAlertTitle>
|
|
97
|
+
<NativeAlertDescription variant="destructive">
|
|
98
|
+
{rootError}
|
|
99
|
+
</NativeAlertDescription>
|
|
100
|
+
</NativeAlert>
|
|
101
|
+
) : null}
|
|
102
|
+
|
|
103
|
+
{groups.map((group, groupIndex) => (
|
|
104
|
+
<View key={group.group ?? `__ungrouped_${groupIndex}`} style={{ gap: 12 }}>
|
|
105
|
+
{group.group ? (
|
|
106
|
+
<View style={{ gap: 8 }}>
|
|
107
|
+
{groupIndex > 0 ? <NativeSeparator /> : null}
|
|
108
|
+
<NativeText preset="body-sm" className="text-muted-foreground">
|
|
109
|
+
{group.group}
|
|
110
|
+
</NativeText>
|
|
111
|
+
</View>
|
|
112
|
+
) : groupIndex > 0 ? (
|
|
113
|
+
<NativeSeparator />
|
|
114
|
+
) : null}
|
|
115
|
+
|
|
116
|
+
{group.fields.map((field) => (
|
|
117
|
+
<NativeResourceFormFieldRenderer
|
|
118
|
+
key={field.key}
|
|
119
|
+
field={field}
|
|
120
|
+
control={form.control}
|
|
121
|
+
action={action}
|
|
122
|
+
autoFocus={field.key === firstFieldKey}
|
|
123
|
+
watchedValues={values}
|
|
124
|
+
/>
|
|
125
|
+
))}
|
|
126
|
+
</View>
|
|
127
|
+
))}
|
|
128
|
+
|
|
129
|
+
<NativeButton onPress={onSubmit} disabled={isSaving} loading={isSaving}>
|
|
130
|
+
{isSaving ? "Saving..." : submitLabel}
|
|
131
|
+
</NativeButton>
|
|
132
|
+
</View>
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
if (layout === "inline") {
|
|
136
|
+
return (
|
|
137
|
+
<View style={{ gap: 16 }}>
|
|
138
|
+
<NativeText preset="h3">{resolvedTitle}</NativeText>
|
|
139
|
+
{formContent}
|
|
140
|
+
</View>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<SafeAreaView edges={["bottom"]} style={{ flex: 1 }}>
|
|
146
|
+
<ScrollView
|
|
147
|
+
contentContainerStyle={{ padding: 16, gap: 16 }}
|
|
148
|
+
showsVerticalScrollIndicator={false}
|
|
149
|
+
>
|
|
150
|
+
<NativeText preset="h3">{resolvedTitle}</NativeText>
|
|
151
|
+
{formContent}
|
|
152
|
+
</ScrollView>
|
|
153
|
+
</SafeAreaView>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export { NativeResourceForm };
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { titleCase } from "@marktiderman/genesis-core";
|
|
2
|
+
import type { ResourceFormFieldDef } from "@marktiderman/genesis-core";
|
|
3
|
+
import { Controller, type Control } from "react-hook-form";
|
|
4
|
+
import { Pressable, View } from "react-native";
|
|
5
|
+
import { NativeCheckbox } from "../components/checkbox";
|
|
6
|
+
import { NativeInput } from "../components/input";
|
|
7
|
+
import { NativeLabel } from "../components/label";
|
|
8
|
+
import {
|
|
9
|
+
NativeSelect,
|
|
10
|
+
NativeSelectContent,
|
|
11
|
+
NativeSelectItem,
|
|
12
|
+
NativeSelectTrigger,
|
|
13
|
+
} from "../components/select";
|
|
14
|
+
import { NativeSwitch } from "../components/switch";
|
|
15
|
+
import { NativeText } from "../components/text";
|
|
16
|
+
import { NativeTextarea } from "../components/textarea";
|
|
17
|
+
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// Types
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
export interface NativeResourceFormFieldRendererProps {
|
|
23
|
+
field: ResourceFormFieldDef;
|
|
24
|
+
control: Control<Record<string, unknown>>;
|
|
25
|
+
action: "create" | "edit";
|
|
26
|
+
autoFocus?: boolean;
|
|
27
|
+
watchedValues?: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
// Static lookup maps (module-scoped to avoid re-creation on each render)
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
|
|
34
|
+
const KEYBOARD_TYPE_MAP: Record<string, "default" | "email-address" | "url"> = {
|
|
35
|
+
email: "email-address",
|
|
36
|
+
url: "url",
|
|
37
|
+
text: "default",
|
|
38
|
+
date: "default",
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const AUTO_CAPITALIZE_MAP: Record<string, "none" | "sentences" | "words" | "characters"> = {
|
|
42
|
+
email: "none",
|
|
43
|
+
url: "none",
|
|
44
|
+
text: "sentences",
|
|
45
|
+
date: "none",
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
// Renderer
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
function NativeResourceFormFieldRenderer({
|
|
53
|
+
field,
|
|
54
|
+
control,
|
|
55
|
+
action,
|
|
56
|
+
autoFocus,
|
|
57
|
+
watchedValues,
|
|
58
|
+
}: NativeResourceFormFieldRendererProps) {
|
|
59
|
+
const label = field.label ?? titleCase(field.key);
|
|
60
|
+
const fieldType = field.type ?? "text";
|
|
61
|
+
const isReadOnly = action === "edit" && !!field.readOnlyOnEdit;
|
|
62
|
+
const isDisabled =
|
|
63
|
+
isReadOnly ||
|
|
64
|
+
(watchedValues ? field.disabled?.(watchedValues) === true : false);
|
|
65
|
+
|
|
66
|
+
// Hidden fields — return null (native has no hidden input concept)
|
|
67
|
+
if (field.hidden || fieldType === "hidden") {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<Controller
|
|
73
|
+
name={field.key}
|
|
74
|
+
control={control}
|
|
75
|
+
render={({ field: f, fieldState }) => {
|
|
76
|
+
const errorMessage = fieldState.error?.message;
|
|
77
|
+
|
|
78
|
+
// ------------------------------------------------------------------
|
|
79
|
+
// Switch
|
|
80
|
+
// ------------------------------------------------------------------
|
|
81
|
+
if (fieldType === "switch") {
|
|
82
|
+
return (
|
|
83
|
+
<View className="gap-1.5">
|
|
84
|
+
<Pressable
|
|
85
|
+
className="flex-row items-center justify-between rounded-lg border border-border p-4"
|
|
86
|
+
onPress={() => !isDisabled && f.onChange(!f.value)}
|
|
87
|
+
accessibilityRole="switch"
|
|
88
|
+
accessibilityState={{ checked: !!f.value, disabled: isDisabled }}
|
|
89
|
+
>
|
|
90
|
+
<NativeLabel disabled={isDisabled}>{label}</NativeLabel>
|
|
91
|
+
<NativeSwitch
|
|
92
|
+
checked={!!f.value}
|
|
93
|
+
onCheckedChange={f.onChange}
|
|
94
|
+
disabled={isDisabled}
|
|
95
|
+
/>
|
|
96
|
+
</Pressable>
|
|
97
|
+
{field.description ? (
|
|
98
|
+
<NativeText preset="caption" className="text-muted-foreground">
|
|
99
|
+
{field.description}
|
|
100
|
+
</NativeText>
|
|
101
|
+
) : null}
|
|
102
|
+
{errorMessage ? (
|
|
103
|
+
<NativeText preset="caption" className="text-destructive">
|
|
104
|
+
{errorMessage}
|
|
105
|
+
</NativeText>
|
|
106
|
+
) : null}
|
|
107
|
+
</View>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ------------------------------------------------------------------
|
|
112
|
+
// Checkbox
|
|
113
|
+
// ------------------------------------------------------------------
|
|
114
|
+
if (fieldType === "checkbox") {
|
|
115
|
+
return (
|
|
116
|
+
<View className="gap-1.5">
|
|
117
|
+
<Pressable
|
|
118
|
+
className="flex-row items-center gap-3"
|
|
119
|
+
onPress={() => !isDisabled && f.onChange(!f.value)}
|
|
120
|
+
accessibilityRole="checkbox"
|
|
121
|
+
accessibilityState={{ checked: !!f.value, disabled: isDisabled }}
|
|
122
|
+
>
|
|
123
|
+
<NativeCheckbox
|
|
124
|
+
checked={!!f.value}
|
|
125
|
+
onCheckedChange={f.onChange}
|
|
126
|
+
disabled={isDisabled}
|
|
127
|
+
/>
|
|
128
|
+
<NativeLabel disabled={isDisabled}>{label}</NativeLabel>
|
|
129
|
+
</Pressable>
|
|
130
|
+
{field.description ? (
|
|
131
|
+
<NativeText preset="caption" className="text-muted-foreground">
|
|
132
|
+
{field.description}
|
|
133
|
+
</NativeText>
|
|
134
|
+
) : null}
|
|
135
|
+
{errorMessage ? (
|
|
136
|
+
<NativeText preset="caption" className="text-destructive">
|
|
137
|
+
{errorMessage}
|
|
138
|
+
</NativeText>
|
|
139
|
+
) : null}
|
|
140
|
+
</View>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ------------------------------------------------------------------
|
|
145
|
+
// Select
|
|
146
|
+
// ------------------------------------------------------------------
|
|
147
|
+
if (fieldType === "select" && field.options) {
|
|
148
|
+
return (
|
|
149
|
+
<View className="gap-1.5">
|
|
150
|
+
<NativeLabel disabled={isDisabled}>{label}</NativeLabel>
|
|
151
|
+
<NativeSelect
|
|
152
|
+
value={f.value != null ? String(f.value) : ""}
|
|
153
|
+
onValueChange={f.onChange}
|
|
154
|
+
defaultValue=""
|
|
155
|
+
>
|
|
156
|
+
<NativeSelectTrigger
|
|
157
|
+
placeholder={
|
|
158
|
+
field.placeholder ?? `Select ${label.toLowerCase()}...`
|
|
159
|
+
}
|
|
160
|
+
disabled={isDisabled}
|
|
161
|
+
/>
|
|
162
|
+
<NativeSelectContent>
|
|
163
|
+
{field.options.map((opt) => (
|
|
164
|
+
<NativeSelectItem key={opt.value} value={opt.value}>
|
|
165
|
+
{opt.label}
|
|
166
|
+
</NativeSelectItem>
|
|
167
|
+
))}
|
|
168
|
+
</NativeSelectContent>
|
|
169
|
+
</NativeSelect>
|
|
170
|
+
{field.description ? (
|
|
171
|
+
<NativeText preset="caption" className="text-muted-foreground">
|
|
172
|
+
{field.description}
|
|
173
|
+
</NativeText>
|
|
174
|
+
) : null}
|
|
175
|
+
{errorMessage ? (
|
|
176
|
+
<NativeText preset="caption" className="text-destructive">
|
|
177
|
+
{errorMessage}
|
|
178
|
+
</NativeText>
|
|
179
|
+
) : null}
|
|
180
|
+
</View>
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// ------------------------------------------------------------------
|
|
185
|
+
// Textarea
|
|
186
|
+
// ------------------------------------------------------------------
|
|
187
|
+
if (fieldType === "textarea") {
|
|
188
|
+
return (
|
|
189
|
+
<View className="gap-1.5">
|
|
190
|
+
<NativeTextarea
|
|
191
|
+
label={label}
|
|
192
|
+
placeholder={field.placeholder}
|
|
193
|
+
rows={field.rows ?? 3}
|
|
194
|
+
value={f.value != null ? String(f.value) : ""}
|
|
195
|
+
onChangeText={f.onChange}
|
|
196
|
+
editable={!isDisabled}
|
|
197
|
+
/>
|
|
198
|
+
{field.description ? (
|
|
199
|
+
<NativeText preset="caption" className="text-muted-foreground">
|
|
200
|
+
{field.description}
|
|
201
|
+
</NativeText>
|
|
202
|
+
) : null}
|
|
203
|
+
{errorMessage ? (
|
|
204
|
+
<NativeText preset="caption" className="text-destructive">
|
|
205
|
+
{errorMessage}
|
|
206
|
+
</NativeText>
|
|
207
|
+
) : null}
|
|
208
|
+
</View>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ------------------------------------------------------------------
|
|
213
|
+
// Number
|
|
214
|
+
// ------------------------------------------------------------------
|
|
215
|
+
if (fieldType === "number") {
|
|
216
|
+
return (
|
|
217
|
+
<View className="gap-1.5">
|
|
218
|
+
<NativeInput
|
|
219
|
+
label={label}
|
|
220
|
+
placeholder={field.placeholder}
|
|
221
|
+
keyboardType="numeric"
|
|
222
|
+
value={f.value != null ? String(f.value) : ""}
|
|
223
|
+
onChangeText={(text) => {
|
|
224
|
+
if (text === "") {
|
|
225
|
+
f.onChange(undefined);
|
|
226
|
+
} else {
|
|
227
|
+
const num = Number(text);
|
|
228
|
+
f.onChange(Number.isNaN(num) ? text : num);
|
|
229
|
+
}
|
|
230
|
+
}}
|
|
231
|
+
editable={!isDisabled}
|
|
232
|
+
autoFocus={autoFocus}
|
|
233
|
+
/>
|
|
234
|
+
{field.description ? (
|
|
235
|
+
<NativeText preset="caption" className="text-muted-foreground">
|
|
236
|
+
{field.description}
|
|
237
|
+
</NativeText>
|
|
238
|
+
) : null}
|
|
239
|
+
{errorMessage ? (
|
|
240
|
+
<NativeText preset="caption" className="text-destructive">
|
|
241
|
+
{errorMessage}
|
|
242
|
+
</NativeText>
|
|
243
|
+
) : null}
|
|
244
|
+
</View>
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// ------------------------------------------------------------------
|
|
249
|
+
// Text, email, url, date (and any other text-like types)
|
|
250
|
+
// ------------------------------------------------------------------
|
|
251
|
+
return (
|
|
252
|
+
<View className="gap-1.5">
|
|
253
|
+
<NativeInput
|
|
254
|
+
label={label}
|
|
255
|
+
placeholder={field.placeholder}
|
|
256
|
+
keyboardType={KEYBOARD_TYPE_MAP[fieldType] ?? "default"}
|
|
257
|
+
autoCapitalize={AUTO_CAPITALIZE_MAP[fieldType] ?? "sentences"}
|
|
258
|
+
value={f.value != null ? String(f.value) : ""}
|
|
259
|
+
onChangeText={f.onChange}
|
|
260
|
+
editable={!isDisabled}
|
|
261
|
+
autoFocus={autoFocus}
|
|
262
|
+
/>
|
|
263
|
+
{field.description ? (
|
|
264
|
+
<NativeText preset="caption" className="text-muted-foreground">
|
|
265
|
+
{field.description}
|
|
266
|
+
</NativeText>
|
|
267
|
+
) : null}
|
|
268
|
+
{errorMessage ? (
|
|
269
|
+
<NativeText preset="caption" className="text-destructive">
|
|
270
|
+
{errorMessage}
|
|
271
|
+
</NativeText>
|
|
272
|
+
) : null}
|
|
273
|
+
</View>
|
|
274
|
+
);
|
|
275
|
+
}}
|
|
276
|
+
/>
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export { NativeResourceFormFieldRenderer };
|