@passui/tanstack 0.2.6 → 0.3.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/{types/src/auth-shell → auth-shell}/auth-shell.d.ts +3 -3
- package/dist/auth-shell/auth-shell.js +17 -0
- package/dist/auth-shell.d.ts +2 -0
- package/dist/auth-shell.js +1 -0
- package/dist/core.d.ts +3 -0
- package/dist/core.js +3 -0
- package/dist/{types/src/document → document}/compose-viewport.d.ts +11 -11
- package/dist/document/compose-viewport.js +43 -0
- package/dist/document.d.ts +2 -0
- package/dist/document.js +1 -0
- package/dist/form-fields/fields.d.ts +30 -0
- package/dist/form-fields/fields.js +160 -0
- package/dist/form-fields/selection.d.ts +21 -0
- package/dist/form-fields/selection.js +73 -0
- package/dist/form-fields/submit.d.ts +3 -0
- package/dist/form-fields/submit.js +26 -0
- package/dist/form-fields.d.ts +7 -0
- package/dist/form-fields.js +7 -0
- package/dist/forms/contexts.js +4 -0
- package/dist/{types/src/forms → forms}/elements/form-alert.d.ts +4 -4
- package/dist/forms/elements/form-alert.js +19 -0
- package/dist/forms/fields/avatar-picker.d.ts +10 -0
- package/dist/forms/fields/avatar-picker.js +228 -0
- package/dist/forms/fields/avatar.d.ts +30 -0
- package/dist/forms/fields/avatar.js +188 -0
- package/dist/forms/fields/checkbox.d.ts +2 -0
- package/dist/forms/fields/index.d.ts +21 -0
- package/dist/forms/fields/index.js +15 -0
- package/dist/forms/fields/input.d.ts +2 -0
- package/dist/forms/fields/multiselect.d.ts +2 -0
- package/dist/forms/fields/otp.d.ts +2 -0
- package/dist/forms/fields/radio-card.d.ts +2 -0
- package/dist/forms/fields/switch.d.ts +2 -0
- package/dist/forms/fields/textarea.d.ts +2 -0
- package/dist/forms.d.ts +45 -0
- package/dist/forms.js +16 -0
- package/dist/mutation/use-app-mutation.d.ts +23 -0
- package/dist/mutation/use-app-mutation.js +41 -0
- package/dist/mutation.d.ts +3 -0
- package/dist/mutation.js +3 -0
- package/dist/route-boundary/default-boundary.d.ts +2 -0
- package/dist/route-boundary/default-boundary.js +40 -0
- package/dist/route-boundary/default-loader.d.ts +6 -0
- package/dist/route-boundary/default-loader.js +17 -0
- package/dist/{types/src/route-boundary → route-boundary}/not-founder.d.ts +2 -2
- package/dist/route-boundary/not-founder.js +44 -0
- package/dist/route-boundary.d.ts +4 -0
- package/dist/route-boundary.js +4 -0
- package/dist/settings/setting-list.d.ts +51 -0
- package/dist/settings/setting-list.js +122 -0
- package/dist/settings.d.ts +2 -0
- package/dist/settings.js +1 -0
- package/dist/{types/src/shared → shared}/optimistic.d.ts +2 -2
- package/dist/shared/optimistic.js +16 -0
- package/dist/{types/src/shared → shared}/table.d.ts +7 -7
- package/dist/shared/table.js +12 -0
- package/dist/tables/columns.d.ts +3 -0
- package/dist/tables/columns.js +21 -0
- package/dist/tables/data-table.d.ts +76 -0
- package/dist/tables/data-table.js +307 -0
- package/dist/tables/filter-row.js +137 -0
- package/dist/tables/pagination.d.ts +8 -0
- package/dist/tables/pagination.js +86 -0
- package/dist/tables.d.ts +4 -0
- package/dist/tables.js +4 -0
- package/package.json +129 -128
- package/src/styles.css +1 -0
- package/dist/types/src/auth-shell/index.d.ts +0 -1
- package/dist/types/src/document/index.d.ts +0 -1
- package/dist/types/src/form-fields/fields.d.ts +0 -24
- package/dist/types/src/form-fields/index.d.ts +0 -2
- package/dist/types/src/form-fields/selection.d.ts +0 -21
- package/dist/types/src/form-fields/submit.d.ts +0 -3
- package/dist/types/src/forms/contexts.d.ts +0 -1
- package/dist/types/src/forms/elements/submit.d.ts +0 -1
- package/dist/types/src/forms/fields/avatar-picker.d.ts +0 -30
- package/dist/types/src/forms/fields/avatar.d.ts +0 -30
- package/dist/types/src/forms/fields/checkbox.d.ts +0 -1
- package/dist/types/src/forms/fields/index.d.ts +0 -21
- package/dist/types/src/forms/fields/input.d.ts +0 -1
- package/dist/types/src/forms/fields/multiselect.d.ts +0 -1
- package/dist/types/src/forms/fields/otp.d.ts +0 -1
- package/dist/types/src/forms/fields/radio-card.d.ts +0 -1
- package/dist/types/src/forms/fields/switch.d.ts +0 -1
- package/dist/types/src/forms/fields/textarea.d.ts +0 -1
- package/dist/types/src/forms/index.d.ts +0 -41
- package/dist/types/src/mutation/index.d.ts +0 -1
- package/dist/types/src/mutation/use-app-mutation.d.ts +0 -22
- package/dist/types/src/route-boundary/default-boundary.d.ts +0 -2
- package/dist/types/src/route-boundary/default-loader.d.ts +0 -6
- package/dist/types/src/route-boundary/index.d.ts +0 -3
- package/dist/types/src/settings/index.d.ts +0 -1
- package/dist/types/src/settings/setting-list.d.ts +0 -51
- package/dist/types/src/shared/index.d.ts +0 -2
- package/dist/types/src/tables/columns.d.ts +0 -3
- package/dist/types/src/tables/data-table.d.ts +0 -76
- package/dist/types/src/tables/filter-row.d.ts +0 -9
- package/dist/types/src/tables/index.d.ts +0 -3
- package/dist/types/src/tables/pagination.d.ts +0 -8
- package/dist/types/src/tables/utils.d.ts +0 -1
- package/src/auth-shell/auth-shell.tsx +0 -16
- package/src/auth-shell/index.ts +0 -1
- package/src/document/compose-viewport.tsx +0 -80
- package/src/document/index.ts +0 -1
- package/src/form-fields/fields.tsx +0 -147
- package/src/form-fields/index.ts +0 -2
- package/src/form-fields/selection.tsx +0 -109
- package/src/form-fields/submit.tsx +0 -35
- package/src/forms/contexts.ts +0 -4
- package/src/forms/elements/form-alert.tsx +0 -21
- package/src/forms/elements/submit.tsx +0 -1
- package/src/forms/fields/avatar-picker.tsx +0 -319
- package/src/forms/fields/avatar.tsx +0 -269
- package/src/forms/fields/checkbox.tsx +0 -1
- package/src/forms/fields/index.ts +0 -29
- package/src/forms/fields/input.tsx +0 -1
- package/src/forms/fields/multiselect.tsx +0 -1
- package/src/forms/fields/otp.tsx +0 -1
- package/src/forms/fields/radio-card.tsx +0 -1
- package/src/forms/fields/switch.tsx +0 -1
- package/src/forms/fields/textarea.tsx +0 -1
- package/src/forms/index.ts +0 -26
- package/src/mutation/index.ts +0 -1
- package/src/mutation/use-app-mutation.ts +0 -100
- package/src/route-boundary/default-boundary.tsx +0 -46
- package/src/route-boundary/default-loader.tsx +0 -23
- package/src/route-boundary/index.ts +0 -3
- package/src/route-boundary/not-founder.tsx +0 -39
- package/src/settings/index.ts +0 -1
- package/src/settings/setting-list.tsx +0 -221
- package/src/shared/index.ts +0 -2
- package/src/shared/optimistic.ts +0 -19
- package/src/shared/table.ts +0 -12
- package/src/tables/columns.tsx +0 -30
- package/src/tables/data-table.tsx +0 -522
- package/src/tables/filter-row.tsx +0 -196
- package/src/tables/index.ts +0 -3
- package/src/tables/pagination.tsx +0 -85
- package/src/tables/utils.ts +0 -5
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { Button } from "@passui/react/button";
|
|
2
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useRef, useState } from "react";
|
|
4
|
+
import { cn } from "@passui/react/cn";
|
|
5
|
+
import { TrashIcon } from "@passui/icons/trash";
|
|
6
|
+
import { Ambient } from "@passui/react/ambient";
|
|
7
|
+
import { Avatar, AvatarFallback, AvatarImage } from "@passui/react/avatar";
|
|
8
|
+
import { CameraIcon } from "@passui/icons/camera";
|
|
9
|
+
import { PhotoIcon } from "@passui/icons/photo";
|
|
10
|
+
import { TypesetIcon } from "@passui/icons/typeset";
|
|
11
|
+
import { Dialog, DialogContent, DialogTitle } from "@passui/react/dialog";
|
|
12
|
+
// src/forms/fields/avatar-picker.tsx
|
|
13
|
+
const MONOGRAM_GRADIENTS = [
|
|
14
|
+
["#A1A1A6", "#6E6E73"],
|
|
15
|
+
["#FF9F0A", "#FF6A00"],
|
|
16
|
+
["#FF6482", "#FF2D55"],
|
|
17
|
+
["#BF5AF2", "#8944E3"],
|
|
18
|
+
["#64D2FF", "#0A84FF"],
|
|
19
|
+
["#34C759", "#0FA958"]
|
|
20
|
+
];
|
|
21
|
+
function initialsOf(label) {
|
|
22
|
+
const parts = label.trim().split(/\s+/).filter(Boolean);
|
|
23
|
+
return (parts.length > 1 ? [parts[0], parts[parts.length - 1]] : parts).map((part) => part[0]?.toUpperCase() ?? "").join("") || "?";
|
|
24
|
+
}
|
|
25
|
+
async function toFile(canvas, name, type) {
|
|
26
|
+
const blob = await new Promise((resolve) => canvas.toBlob(resolve, type, .92));
|
|
27
|
+
if (!blob) throw new Error("Could not export the image");
|
|
28
|
+
return new File([blob], name, { type });
|
|
29
|
+
}
|
|
30
|
+
async function renderMonogram(initials, [from, to]) {
|
|
31
|
+
const size = 512;
|
|
32
|
+
const canvas = document.createElement("canvas");
|
|
33
|
+
canvas.width = size;
|
|
34
|
+
canvas.height = size;
|
|
35
|
+
const context = canvas.getContext("2d");
|
|
36
|
+
if (!context) throw new Error("Canvas is not available");
|
|
37
|
+
const gradient = context.createLinearGradient(0, 0, 0, size);
|
|
38
|
+
gradient.addColorStop(0, from);
|
|
39
|
+
gradient.addColorStop(1, to);
|
|
40
|
+
context.fillStyle = gradient;
|
|
41
|
+
context.fillRect(0, 0, size, size);
|
|
42
|
+
context.fillStyle = "#fff";
|
|
43
|
+
context.textAlign = "center";
|
|
44
|
+
context.textBaseline = "middle";
|
|
45
|
+
context.font = `600 ${initials.length > 1 ? 196 : 232}px -apple-system, "SF Pro Rounded", "SF Pro Display", system-ui, sans-serif`;
|
|
46
|
+
context.fillText(initials, size / 2, 268);
|
|
47
|
+
return toFile(canvas, "monogram.png", "image/png");
|
|
48
|
+
}
|
|
49
|
+
function Option({ icon: Icon, tone = "default", onClick, children }) {
|
|
50
|
+
return /* @__PURE__ */ jsxs("button", {
|
|
51
|
+
type: "button",
|
|
52
|
+
onClick,
|
|
53
|
+
className: cn("text-subheadline flex w-full items-center gap-3 px-4 py-3 text-left transition-colors", "hover:bg-foreground/[0.04] focus-visible:bg-foreground/[0.06] focus-visible:outline-none", tone === "destructive" && "text-destructive"),
|
|
54
|
+
children: [/* @__PURE__ */ jsx(Icon, {
|
|
55
|
+
size: 18,
|
|
56
|
+
className: "shrink-0 opacity-80"
|
|
57
|
+
}), children]
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/** The live selfie view: the frame under a circle, an iOS shutter below. */
|
|
61
|
+
function CameraView({ labels, onCapture, onCancel }) {
|
|
62
|
+
const videoRef = useRef(null);
|
|
63
|
+
const [failed, setFailed] = useState(false);
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
let stream = null;
|
|
66
|
+
navigator.mediaDevices.getUserMedia({ video: {
|
|
67
|
+
facingMode: "user",
|
|
68
|
+
width: 1280,
|
|
69
|
+
height: 1280
|
|
70
|
+
} }).then((media) => {
|
|
71
|
+
stream = media;
|
|
72
|
+
if (videoRef.current) videoRef.current.srcObject = media;
|
|
73
|
+
}).catch(() => setFailed(true));
|
|
74
|
+
return () => {
|
|
75
|
+
for (const track of stream?.getTracks() ?? []) track.stop();
|
|
76
|
+
};
|
|
77
|
+
}, []);
|
|
78
|
+
async function capture() {
|
|
79
|
+
const video = videoRef.current;
|
|
80
|
+
if (!video?.videoWidth) return;
|
|
81
|
+
const canvas = document.createElement("canvas");
|
|
82
|
+
canvas.width = video.videoWidth;
|
|
83
|
+
canvas.height = video.videoHeight;
|
|
84
|
+
const context = canvas.getContext("2d");
|
|
85
|
+
if (!context) return;
|
|
86
|
+
context.translate(canvas.width, 0);
|
|
87
|
+
context.scale(-1, 1);
|
|
88
|
+
context.drawImage(video, 0, 0);
|
|
89
|
+
onCapture(await toFile(canvas, "photo.jpg", "image/jpeg"));
|
|
90
|
+
}
|
|
91
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
92
|
+
className: "flex flex-col items-center gap-5 bg-neutral-950 px-5 pt-8 pb-6",
|
|
93
|
+
children: [/* @__PURE__ */ jsx("div", {
|
|
94
|
+
className: "relative size-64 overflow-hidden rounded-full ring-1 ring-white/20",
|
|
95
|
+
children: failed ? /* @__PURE__ */ jsx("div", {
|
|
96
|
+
className: "text-caption flex size-full items-center justify-center p-8 text-center text-white/70",
|
|
97
|
+
children: labels.takePhoto
|
|
98
|
+
}) : /* @__PURE__ */ jsx("video", {
|
|
99
|
+
ref: videoRef,
|
|
100
|
+
autoPlay: true,
|
|
101
|
+
playsInline: true,
|
|
102
|
+
muted: true,
|
|
103
|
+
className: "size-full -scale-x-100 object-cover"
|
|
104
|
+
})
|
|
105
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
106
|
+
className: "grid w-full grid-cols-3 items-center",
|
|
107
|
+
children: [/* @__PURE__ */ jsx(Button, {
|
|
108
|
+
type: "button",
|
|
109
|
+
variant: "ghost",
|
|
110
|
+
size: "sm",
|
|
111
|
+
className: "justify-self-start text-white hover:bg-white/10 hover:text-white",
|
|
112
|
+
onClick: onCancel,
|
|
113
|
+
children: labels.cancel
|
|
114
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
115
|
+
type: "button",
|
|
116
|
+
"aria-label": labels.capture,
|
|
117
|
+
disabled: failed,
|
|
118
|
+
onClick: () => void capture(),
|
|
119
|
+
className: "size-16 justify-self-center rounded-full border-4 border-white p-1 transition-transform active:scale-95 disabled:opacity-40",
|
|
120
|
+
children: /* @__PURE__ */ jsx("span", { className: "block size-full rounded-full bg-white" })
|
|
121
|
+
})]
|
|
122
|
+
})]
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The photo sheet Apple shows for a contact photo: the current photo lit by
|
|
127
|
+
* its own colors, then the ways to change it.
|
|
128
|
+
*/
|
|
129
|
+
function AvatarPicker({ open, onOpenChange, label, image, clearable, labels, onChooseFile, onCapture, onChoose, onRemove }) {
|
|
130
|
+
const [mode, setMode] = useState("menu");
|
|
131
|
+
const [monogramOpen, setMonogramOpen] = useState(false);
|
|
132
|
+
const [canUseCamera, setCanUseCamera] = useState(false);
|
|
133
|
+
const initials = initialsOf(label);
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
setCanUseCamera(Boolean(navigator.mediaDevices?.getUserMedia));
|
|
136
|
+
}, []);
|
|
137
|
+
useEffect(() => {
|
|
138
|
+
if (!open) {
|
|
139
|
+
setMode("menu");
|
|
140
|
+
setMonogramOpen(false);
|
|
141
|
+
}
|
|
142
|
+
}, [open]);
|
|
143
|
+
return /* @__PURE__ */ jsx(Dialog, {
|
|
144
|
+
open,
|
|
145
|
+
onOpenChange: ({ open: next }) => onOpenChange(next),
|
|
146
|
+
children: /* @__PURE__ */ jsxs(DialogContent, {
|
|
147
|
+
className: "gap-0 overflow-hidden rounded-3xl",
|
|
148
|
+
style: {
|
|
149
|
+
maxWidth: "22rem",
|
|
150
|
+
padding: 0
|
|
151
|
+
},
|
|
152
|
+
children: [/* @__PURE__ */ jsx(DialogTitle, {
|
|
153
|
+
className: "sr-only",
|
|
154
|
+
children: mode === "camera" ? labels.takePhoto : labels.photo
|
|
155
|
+
}), mode === "camera" ? /* @__PURE__ */ jsx(CameraView, {
|
|
156
|
+
labels,
|
|
157
|
+
onCapture,
|
|
158
|
+
onCancel: () => setMode("menu")
|
|
159
|
+
}) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
160
|
+
/* @__PURE__ */ jsxs(Ambient, {
|
|
161
|
+
src: image,
|
|
162
|
+
className: "flex flex-col items-center gap-3 px-5 pt-8 pb-6 text-center",
|
|
163
|
+
children: [/* @__PURE__ */ jsxs(Avatar, {
|
|
164
|
+
className: "size-28 shadow-[0_12px_32px_-12px_rgb(0_0_0/0.45)] ring-1 ring-white/50",
|
|
165
|
+
children: [/* @__PURE__ */ jsx(AvatarImage, {
|
|
166
|
+
src: image,
|
|
167
|
+
alt: label
|
|
168
|
+
}), /* @__PURE__ */ jsx(AvatarFallback, {
|
|
169
|
+
className: "text-title",
|
|
170
|
+
children: initials
|
|
171
|
+
})]
|
|
172
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
173
|
+
className: "flex flex-col gap-0.5",
|
|
174
|
+
children: [/* @__PURE__ */ jsx("h2", { children: labels.photo }), /* @__PURE__ */ jsx("p", { children: labels.photoDescription })]
|
|
175
|
+
})]
|
|
176
|
+
}),
|
|
177
|
+
/* @__PURE__ */ jsxs("div", {
|
|
178
|
+
className: "divide-border/40 border-border/35 bg-card m-3 flex flex-col divide-y overflow-hidden rounded-2xl border shadow-xs",
|
|
179
|
+
children: [
|
|
180
|
+
/* @__PURE__ */ jsx(Option, {
|
|
181
|
+
icon: PhotoIcon,
|
|
182
|
+
onClick: onChooseFile,
|
|
183
|
+
children: labels.choosePhoto
|
|
184
|
+
}),
|
|
185
|
+
canUseCamera && /* @__PURE__ */ jsx(Option, {
|
|
186
|
+
icon: CameraIcon,
|
|
187
|
+
onClick: () => setMode("camera"),
|
|
188
|
+
children: labels.takePhoto
|
|
189
|
+
}),
|
|
190
|
+
/* @__PURE__ */ jsx(Option, {
|
|
191
|
+
icon: TypesetIcon,
|
|
192
|
+
onClick: () => setMonogramOpen((current) => !current),
|
|
193
|
+
children: labels.monogram
|
|
194
|
+
}),
|
|
195
|
+
monogramOpen && /* @__PURE__ */ jsx("div", {
|
|
196
|
+
className: "flex items-center justify-between gap-2 px-4 py-3",
|
|
197
|
+
children: MONOGRAM_GRADIENTS.map((gradient) => /* @__PURE__ */ jsx("button", {
|
|
198
|
+
type: "button",
|
|
199
|
+
"aria-label": `${labels.monogram} ${gradient[0]}`,
|
|
200
|
+
onClick: () => void renderMonogram(initials, gradient).then(onChoose),
|
|
201
|
+
className: "text-subheadline flex size-10 items-center justify-center rounded-full font-semibold text-white shadow-xs ring-1 ring-black/5 transition-transform hover:scale-105 active:scale-95",
|
|
202
|
+
style: { backgroundImage: `linear-gradient(${gradient[0]}, ${gradient[1]})` },
|
|
203
|
+
children: initials
|
|
204
|
+
}, gradient[0]))
|
|
205
|
+
}),
|
|
206
|
+
clearable && image && /* @__PURE__ */ jsx(Option, {
|
|
207
|
+
icon: TrashIcon,
|
|
208
|
+
tone: "destructive",
|
|
209
|
+
onClick: onRemove,
|
|
210
|
+
children: labels.removePhoto
|
|
211
|
+
})
|
|
212
|
+
]
|
|
213
|
+
}),
|
|
214
|
+
/* @__PURE__ */ jsx("div", {
|
|
215
|
+
className: "px-3 pb-3",
|
|
216
|
+
children: /* @__PURE__ */ jsx(Button, {
|
|
217
|
+
type: "button",
|
|
218
|
+
variant: "secondary",
|
|
219
|
+
className: "w-full rounded-2xl",
|
|
220
|
+
onClick: () => onOpenChange(false),
|
|
221
|
+
children: labels.cancel
|
|
222
|
+
})
|
|
223
|
+
})
|
|
224
|
+
] })]
|
|
225
|
+
})
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
export { AvatarPicker };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AvatarPickerLabels } from "./avatar-picker.js";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
export type FieldAvatarValue = File | null | false;
|
|
4
|
+
export type FieldAvatarLabels = AvatarPickerLabels & {
|
|
5
|
+
edit: string;
|
|
6
|
+
remove: string;
|
|
7
|
+
/** Shown under a picked photo until it's kept or dropped. */
|
|
8
|
+
confirmHint: string;
|
|
9
|
+
save: string;
|
|
10
|
+
};
|
|
11
|
+
type FieldAvatarProps = {
|
|
12
|
+
/** Compact identity row for account settings. */
|
|
13
|
+
compact?: boolean;
|
|
14
|
+
existingImage?: string | null;
|
|
15
|
+
label: string;
|
|
16
|
+
accept?: string;
|
|
17
|
+
/** Offer removing the image once there is one. Off by default: a
|
|
18
|
+
* personal avatar usually just gets replaced, not cleared to nothing. */
|
|
19
|
+
clearable?: boolean;
|
|
20
|
+
/** Sit the photo in a surface lit by its own colors, with this under it. */
|
|
21
|
+
caption?: ReactNode;
|
|
22
|
+
/** For a translated UI; English otherwise. */
|
|
23
|
+
labels?: Partial<FieldAvatarLabels>;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* A photo you click to replace: a small menu (choose, take, monogram), then
|
|
27
|
+
* the new image shows in place and Enter keeps it (Esc drops it); no cropping dialog.
|
|
28
|
+
*/
|
|
29
|
+
export declare function FieldAvatar({ compact, existingImage, label, accept, clearable, caption, labels }: FieldAvatarProps): import("react").JSX.Element;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { useFieldContext } from "../contexts.js";
|
|
2
|
+
import { AvatarPicker } from "./avatar-picker.js";
|
|
3
|
+
import { Button } from "@passui/react/button";
|
|
4
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
+
import { useEffect, useRef, useState } from "react";
|
|
6
|
+
import { cn } from "@passui/react/cn";
|
|
7
|
+
import { TrashIcon } from "@passui/icons/trash";
|
|
8
|
+
import { Ambient } from "@passui/react/ambient";
|
|
9
|
+
import { Avatar, AvatarFallback, AvatarImage } from "@passui/react/avatar";
|
|
10
|
+
// src/forms/fields/avatar.tsx
|
|
11
|
+
const DEFAULT_LABELS = {
|
|
12
|
+
edit: "Edit",
|
|
13
|
+
remove: "Remove",
|
|
14
|
+
confirmHint: "Press Enter to use this photo, Esc to cancel",
|
|
15
|
+
save: "Use photo",
|
|
16
|
+
cancel: "Cancel",
|
|
17
|
+
photo: "Photo",
|
|
18
|
+
photoDescription: "Choose how you appear.",
|
|
19
|
+
choosePhoto: "Choose Photo…",
|
|
20
|
+
takePhoto: "Take Photo",
|
|
21
|
+
capture: "Take photo",
|
|
22
|
+
monogram: "Monogram",
|
|
23
|
+
removePhoto: "Remove Photo"
|
|
24
|
+
};
|
|
25
|
+
const imageFrom = (files) => [...files ?? []].find((file) => file.type.startsWith("image/")) ?? null;
|
|
26
|
+
/**
|
|
27
|
+
* A photo you click to replace: a small menu (choose, take, monogram), then
|
|
28
|
+
* the new image shows in place and Enter keeps it (Esc drops it); no cropping dialog.
|
|
29
|
+
*/
|
|
30
|
+
function FieldAvatar({ compact = false, existingImage, label, accept = "image/png,image/webp,image/jpeg,image/svg+xml", clearable = false, caption, labels }) {
|
|
31
|
+
const text = {
|
|
32
|
+
...DEFAULT_LABELS,
|
|
33
|
+
...labels
|
|
34
|
+
};
|
|
35
|
+
const field = useFieldContext();
|
|
36
|
+
const fileInputRef = useRef(null);
|
|
37
|
+
const [pending, setPending] = useState(null);
|
|
38
|
+
const [sheetOpen, setSheetOpen] = useState(false);
|
|
39
|
+
const [dragOver, setDragOver] = useState(false);
|
|
40
|
+
const [preview, setPreview] = useState(null);
|
|
41
|
+
const isCleared = field.state.value === false;
|
|
42
|
+
const shown = pending ?? field.state.value;
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (!(shown instanceof File)) {
|
|
45
|
+
setPreview(null);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const url = URL.createObjectURL(shown);
|
|
49
|
+
setPreview(url);
|
|
50
|
+
return () => URL.revokeObjectURL(url);
|
|
51
|
+
}, [shown]);
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (!pending) return;
|
|
54
|
+
const onKey = (event) => {
|
|
55
|
+
if (event.key === "Enter") {
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
keep();
|
|
58
|
+
}
|
|
59
|
+
if (event.key === "Escape") setPending(null);
|
|
60
|
+
};
|
|
61
|
+
window.addEventListener("keydown", onKey);
|
|
62
|
+
return () => window.removeEventListener("keydown", onKey);
|
|
63
|
+
});
|
|
64
|
+
function keep() {
|
|
65
|
+
if (!pending) return;
|
|
66
|
+
field.handleChange(pending);
|
|
67
|
+
setPending(null);
|
|
68
|
+
}
|
|
69
|
+
const currentImage = isCleared ? void 0 : preview ?? existingImage ?? void 0;
|
|
70
|
+
const canClear = clearable && !pending && !isCleared && Boolean(currentImage);
|
|
71
|
+
function onDrop(event) {
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
setDragOver(false);
|
|
74
|
+
const file = imageFrom(event.dataTransfer.files);
|
|
75
|
+
if (file) setPending(file);
|
|
76
|
+
}
|
|
77
|
+
const photo = /* @__PURE__ */ jsxs("div", {
|
|
78
|
+
className: "flex w-fit flex-col items-center gap-3",
|
|
79
|
+
children: [
|
|
80
|
+
/* @__PURE__ */ jsxs("button", {
|
|
81
|
+
type: "button",
|
|
82
|
+
onClick: () => setSheetOpen(true),
|
|
83
|
+
onDragOver: (event) => {
|
|
84
|
+
event.preventDefault();
|
|
85
|
+
setDragOver(true);
|
|
86
|
+
},
|
|
87
|
+
onDragLeave: () => setDragOver(false),
|
|
88
|
+
onDrop,
|
|
89
|
+
onPaste: (event) => {
|
|
90
|
+
const file = imageFrom(event.clipboardData.files);
|
|
91
|
+
if (file) setPending(file);
|
|
92
|
+
},
|
|
93
|
+
className: cn("group relative rounded-full outline-none", "focus-visible:ring-ring focus-visible:ring-offset-background focus-visible:ring-2 focus-visible:ring-offset-2"),
|
|
94
|
+
"aria-label": `${text.edit} ${label}`,
|
|
95
|
+
children: [/* @__PURE__ */ jsxs(Avatar, {
|
|
96
|
+
className: cn("relative ring-1 ring-white/40", compact ? "size-16!" : "size-43!", caption && !compact && "shadow-[0_16px_40px_-16px_rgb(0_0_0/0.5)]", "transition-transform duration-300 ease-out group-hover:scale-[1.02] group-active:scale-[0.98]", (dragOver || pending) && "scale-[1.04]"),
|
|
97
|
+
children: [/* @__PURE__ */ jsx(AvatarImage, {
|
|
98
|
+
src: currentImage,
|
|
99
|
+
alt: label,
|
|
100
|
+
className: "object-cover transition-[filter] duration-300 group-hover:brightness-90"
|
|
101
|
+
}), /* @__PURE__ */ jsx(AvatarFallback, {
|
|
102
|
+
className: compact ? "text-title3" : "text-large-title",
|
|
103
|
+
children: (label.charAt(0) || "?").toUpperCase()
|
|
104
|
+
})]
|
|
105
|
+
}), !pending && /* @__PURE__ */ jsx("span", {
|
|
106
|
+
className: cn("absolute bottom-3 left-1/2 -translate-x-1/2 rounded-full px-3 py-1", "text-caption bg-black/35 font-medium text-white backdrop-blur-md", "opacity-0 transition-opacity duration-200 group-hover:opacity-100 group-focus-visible:opacity-100", dragOver && "opacity-100"),
|
|
107
|
+
children: text.edit
|
|
108
|
+
})]
|
|
109
|
+
}),
|
|
110
|
+
pending && /* @__PURE__ */ jsxs("div", {
|
|
111
|
+
className: "animate-in fade-in-0 slide-in-from-top-1 flex flex-col items-center gap-2 duration-300",
|
|
112
|
+
children: [/* @__PURE__ */ jsx("p", {
|
|
113
|
+
className: "text-muted-foreground",
|
|
114
|
+
children: text.confirmHint
|
|
115
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
116
|
+
className: "flex items-center gap-1",
|
|
117
|
+
children: [/* @__PURE__ */ jsx(Button, {
|
|
118
|
+
type: "button",
|
|
119
|
+
variant: "ghost",
|
|
120
|
+
size: "sm",
|
|
121
|
+
onClick: () => setPending(null),
|
|
122
|
+
children: text.cancel
|
|
123
|
+
}), /* @__PURE__ */ jsx(Button, {
|
|
124
|
+
variant: "default",
|
|
125
|
+
type: "button",
|
|
126
|
+
size: "sm",
|
|
127
|
+
onClick: keep,
|
|
128
|
+
children: text.save
|
|
129
|
+
})]
|
|
130
|
+
})]
|
|
131
|
+
}),
|
|
132
|
+
canClear && !caption && /* @__PURE__ */ jsxs(Button, {
|
|
133
|
+
type: "button",
|
|
134
|
+
variant: "ghost",
|
|
135
|
+
size: "sm",
|
|
136
|
+
className: "text-muted-foreground",
|
|
137
|
+
onClick: () => field.handleChange(false),
|
|
138
|
+
children: [/* @__PURE__ */ jsx(TrashIcon, { size: 16 }), text.remove]
|
|
139
|
+
})
|
|
140
|
+
]
|
|
141
|
+
});
|
|
142
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
143
|
+
compact ? /* @__PURE__ */ jsxs("div", {
|
|
144
|
+
className: "flex min-w-0 flex-wrap items-center gap-4",
|
|
145
|
+
children: [photo, !pending && caption]
|
|
146
|
+
}) : caption ? /* @__PURE__ */ jsxs(Ambient, {
|
|
147
|
+
src: currentImage,
|
|
148
|
+
className: "flex w-full flex-col items-center gap-4 pt-6 pb-2 text-center",
|
|
149
|
+
children: [photo, !pending && caption]
|
|
150
|
+
}) : photo,
|
|
151
|
+
/* @__PURE__ */ jsx("input", {
|
|
152
|
+
ref: fileInputRef,
|
|
153
|
+
type: "file",
|
|
154
|
+
accept,
|
|
155
|
+
className: "hidden",
|
|
156
|
+
onChange: (e) => {
|
|
157
|
+
const file = e.target.files?.[0];
|
|
158
|
+
e.target.value = "";
|
|
159
|
+
if (file) setPending(file);
|
|
160
|
+
}
|
|
161
|
+
}),
|
|
162
|
+
/* @__PURE__ */ jsx(AvatarPicker, {
|
|
163
|
+
open: sheetOpen,
|
|
164
|
+
onOpenChange: setSheetOpen,
|
|
165
|
+
label,
|
|
166
|
+
image: currentImage,
|
|
167
|
+
clearable,
|
|
168
|
+
labels: text,
|
|
169
|
+
onChooseFile: () => {
|
|
170
|
+
setSheetOpen(false);
|
|
171
|
+
fileInputRef.current?.click();
|
|
172
|
+
},
|
|
173
|
+
onCapture: (file) => {
|
|
174
|
+
setSheetOpen(false);
|
|
175
|
+
setPending(file);
|
|
176
|
+
},
|
|
177
|
+
onChoose: (file) => {
|
|
178
|
+
setSheetOpen(false);
|
|
179
|
+
field.handleChange(file);
|
|
180
|
+
},
|
|
181
|
+
onRemove: () => {
|
|
182
|
+
setSheetOpen(false);
|
|
183
|
+
field.handleChange(false);
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
] });
|
|
187
|
+
}
|
|
188
|
+
export { FieldAvatar };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FieldCheckbox, FieldInput, FieldOtp, FieldSwitch, FieldTextarea } from "../../form-fields/fields.js";
|
|
2
|
+
import { FieldMultiselect, FieldRadioCard } from "../../form-fields/selection.js";
|
|
3
|
+
import { FieldAvatar, FieldAvatarLabels, FieldAvatarValue } from "./avatar.js";
|
|
4
|
+
import "./checkbox.js";
|
|
5
|
+
import "./input.js";
|
|
6
|
+
import "./multiselect.js";
|
|
7
|
+
import "./otp.js";
|
|
8
|
+
import "./radio-card.js";
|
|
9
|
+
import "./switch.js";
|
|
10
|
+
import "./textarea.js";
|
|
11
|
+
export declare const fields: {
|
|
12
|
+
input: typeof FieldInput;
|
|
13
|
+
checkbox: typeof FieldCheckbox;
|
|
14
|
+
textarea: typeof FieldTextarea;
|
|
15
|
+
switch: typeof FieldSwitch;
|
|
16
|
+
radioCard: typeof FieldRadioCard;
|
|
17
|
+
multiselect: typeof FieldMultiselect;
|
|
18
|
+
avatar: typeof FieldAvatar;
|
|
19
|
+
otp: typeof FieldOtp;
|
|
20
|
+
};
|
|
21
|
+
export { FieldAvatar, FieldAvatarLabels, FieldAvatarValue, FieldCheckbox, FieldInput, FieldMultiselect, FieldOtp, FieldRadioCard, FieldSwitch, FieldTextarea };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { FieldCheckbox, FieldInput, FieldOtp, FieldSwitch, FieldTextarea } from "../../form-fields/fields.js";
|
|
2
|
+
import { FieldMultiselect, FieldRadioCard } from "../../form-fields/selection.js";
|
|
3
|
+
import { FieldAvatar } from "./avatar.js";
|
|
4
|
+
// src/forms/fields/index.ts
|
|
5
|
+
const fields = {
|
|
6
|
+
input: FieldInput,
|
|
7
|
+
checkbox: FieldCheckbox,
|
|
8
|
+
textarea: FieldTextarea,
|
|
9
|
+
switch: FieldSwitch,
|
|
10
|
+
radioCard: FieldRadioCard,
|
|
11
|
+
multiselect: FieldMultiselect,
|
|
12
|
+
avatar: FieldAvatar,
|
|
13
|
+
otp: FieldOtp
|
|
14
|
+
};
|
|
15
|
+
export { FieldAvatar, FieldCheckbox, FieldInput, FieldMultiselect, FieldOtp, FieldRadioCard, FieldSwitch, FieldTextarea, fields };
|
package/dist/forms.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Submit } from "./form-fields/submit.js";
|
|
2
|
+
import { FormAlert } from "./forms/elements/form-alert.js";
|
|
3
|
+
import { FieldCheckbox, FieldInput, FieldOtp, FieldSwitch, FieldTextarea } from "./form-fields/fields.js";
|
|
4
|
+
import { FieldMultiselect, FieldRadioCard } from "./form-fields/selection.js";
|
|
5
|
+
import { FieldAvatar, FieldAvatarLabels, FieldAvatarValue } from "./forms/fields/avatar.js";
|
|
6
|
+
import { fields } from "./forms/fields/index.js";
|
|
7
|
+
import "./forms.js";
|
|
8
|
+
import { useSelector, useStore } from "@tanstack/react-form";
|
|
9
|
+
export declare const useAppForm: <TFormData, TOnMount extends undefined | import("@tanstack/react-form").FormValidateOrFn<TFormData>, TOnChange extends undefined | import("@tanstack/react-form").FormValidateOrFn<TFormData>, TOnChangeAsync extends undefined | import("@tanstack/react-form").FormAsyncValidateOrFn<TFormData>, TOnBlur extends undefined | import("@tanstack/react-form").FormValidateOrFn<TFormData>, TOnBlurAsync extends undefined | import("@tanstack/react-form").FormAsyncValidateOrFn<TFormData>, TOnSubmit extends undefined | import("@tanstack/react-form").FormValidateOrFn<TFormData>, TOnSubmitAsync extends undefined | import("@tanstack/react-form").FormAsyncValidateOrFn<TFormData>, TOnDynamic extends undefined | import("@tanstack/react-form").FormValidateOrFn<TFormData>, TOnDynamicAsync extends undefined | import("@tanstack/react-form").FormAsyncValidateOrFn<TFormData>, TOnServer extends undefined | import("@tanstack/react-form").FormAsyncValidateOrFn<TFormData>, TSubmitMeta>(props: import("@tanstack/react-form").FormOptions<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta>) => import("@tanstack/react-form").AppFieldExtendedReactFormApi<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta, {
|
|
10
|
+
readonly input: typeof FieldInput;
|
|
11
|
+
readonly checkbox: typeof FieldCheckbox;
|
|
12
|
+
readonly textarea: typeof FieldTextarea;
|
|
13
|
+
readonly switch: typeof FieldSwitch;
|
|
14
|
+
readonly radioCard: typeof FieldRadioCard;
|
|
15
|
+
readonly multiselect: typeof FieldMultiselect;
|
|
16
|
+
readonly avatar: typeof FieldAvatar;
|
|
17
|
+
readonly otp: typeof FieldOtp;
|
|
18
|
+
}, {
|
|
19
|
+
readonly submit: typeof Submit;
|
|
20
|
+
}>, withForm: <TFormData, TOnMount extends undefined | import("@tanstack/react-form").FormValidateOrFn<TFormData>, TOnChange extends undefined | import("@tanstack/react-form").FormValidateOrFn<TFormData>, TOnChangeAsync extends undefined | import("@tanstack/react-form").FormAsyncValidateOrFn<TFormData>, TOnBlur extends undefined | import("@tanstack/react-form").FormValidateOrFn<TFormData>, TOnBlurAsync extends undefined | import("@tanstack/react-form").FormAsyncValidateOrFn<TFormData>, TOnSubmit extends undefined | import("@tanstack/react-form").FormValidateOrFn<TFormData>, TOnSubmitAsync extends undefined | import("@tanstack/react-form").FormAsyncValidateOrFn<TFormData>, TOnDynamic extends undefined | import("@tanstack/react-form").FormValidateOrFn<TFormData>, TOnDynamicAsync extends undefined | import("@tanstack/react-form").FormAsyncValidateOrFn<TFormData>, TOnServer extends undefined | import("@tanstack/react-form").FormAsyncValidateOrFn<TFormData>, TSubmitMeta, TRenderProps extends object = {}>({ render, props }: import("@tanstack/react-form").WithFormProps<TFormData, TOnMount, TOnChange, TOnChangeAsync, TOnBlur, TOnBlurAsync, TOnSubmit, TOnSubmitAsync, TOnDynamic, TOnDynamicAsync, TOnServer, TSubmitMeta, {
|
|
21
|
+
readonly input: typeof FieldInput;
|
|
22
|
+
readonly checkbox: typeof FieldCheckbox;
|
|
23
|
+
readonly textarea: typeof FieldTextarea;
|
|
24
|
+
readonly switch: typeof FieldSwitch;
|
|
25
|
+
readonly radioCard: typeof FieldRadioCard;
|
|
26
|
+
readonly multiselect: typeof FieldMultiselect;
|
|
27
|
+
readonly avatar: typeof FieldAvatar;
|
|
28
|
+
readonly otp: typeof FieldOtp;
|
|
29
|
+
}, {
|
|
30
|
+
readonly submit: typeof Submit;
|
|
31
|
+
}, TRenderProps>) => import("react").FunctionComponent<import("react").PropsWithChildren<NoInfer<[unknown] extends [TRenderProps] ? any : TRenderProps> & {
|
|
32
|
+
form: import("@tanstack/react-form").AppFieldExtendedReactFormApi<[unknown] extends [TFormData] ? any : TFormData, [import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined] extends [TOnMount] ? [TOnMount] extends [TOnMount & (import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined)] ? any : TOnMount : TOnMount, [import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined] extends [TOnChange] ? [TOnChange] extends [TOnChange & (import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined)] ? any : TOnChange : TOnChange, [import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined] extends [TOnChangeAsync] ? [TOnChangeAsync] extends [TOnChangeAsync & (import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined)] ? any : TOnChangeAsync : TOnChangeAsync, [import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined] extends [TOnBlur] ? [TOnBlur] extends [TOnBlur & (import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined)] ? any : TOnBlur : TOnBlur, [import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined] extends [TOnBlurAsync] ? [TOnBlurAsync] extends [TOnBlurAsync & (import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined)] ? any : TOnBlurAsync : TOnBlurAsync, [import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined] extends [TOnSubmit] ? [TOnSubmit] extends [TOnSubmit & (import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined)] ? any : TOnSubmit : TOnSubmit, [import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined] extends [TOnSubmitAsync] ? [TOnSubmitAsync] extends [TOnSubmitAsync & (import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined)] ? any : TOnSubmitAsync : TOnSubmitAsync, [import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined] extends [TOnDynamic] ? [TOnDynamic] extends [TOnDynamic & (import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined)] ? any : TOnDynamic : TOnDynamic, [import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined] extends [TOnDynamicAsync] ? [TOnDynamicAsync] extends [TOnDynamicAsync & (import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined)] ? any : TOnDynamicAsync : TOnDynamicAsync, [import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined] extends [TOnServer] ? [TOnServer] extends [TOnServer & (import("@tanstack/react-form").FormValidateOrFn<TFormData> | undefined)] ? any : TOnServer : TOnServer, [unknown] extends [TSubmitMeta] ? any : TSubmitMeta, {
|
|
33
|
+
readonly input: typeof FieldInput;
|
|
34
|
+
readonly checkbox: typeof FieldCheckbox;
|
|
35
|
+
readonly textarea: typeof FieldTextarea;
|
|
36
|
+
readonly switch: typeof FieldSwitch;
|
|
37
|
+
readonly radioCard: typeof FieldRadioCard;
|
|
38
|
+
readonly multiselect: typeof FieldMultiselect;
|
|
39
|
+
readonly avatar: typeof FieldAvatar;
|
|
40
|
+
readonly otp: typeof FieldOtp;
|
|
41
|
+
}, {
|
|
42
|
+
readonly submit: typeof Submit;
|
|
43
|
+
}>;
|
|
44
|
+
}>>;
|
|
45
|
+
export { FieldAvatar, FieldAvatarLabels, FieldAvatarValue, FieldCheckbox, FieldInput, FieldMultiselect, FieldOtp, FieldRadioCard, FieldSwitch, FieldTextarea, FormAlert, fields, useSelector, useStore };
|
package/dist/forms.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { fieldContext, formContext } from "./forms/contexts.js";
|
|
2
|
+
import { Submit } from "./form-fields/submit.js";
|
|
3
|
+
import { FieldCheckbox, FieldInput, FieldOtp, FieldSwitch, FieldTextarea } from "./form-fields/fields.js";
|
|
4
|
+
import { FieldMultiselect, FieldRadioCard } from "./form-fields/selection.js";
|
|
5
|
+
import { FieldAvatar } from "./forms/fields/avatar.js";
|
|
6
|
+
import { fields } from "./forms/fields/index.js";
|
|
7
|
+
import { FormAlert } from "./forms/elements/form-alert.js";
|
|
8
|
+
import { createFormHook, useSelector, useStore } from "@tanstack/react-form";
|
|
9
|
+
// src/forms/index.ts
|
|
10
|
+
const { useAppForm, withForm } = createFormHook({
|
|
11
|
+
fieldComponents: { ...fields },
|
|
12
|
+
formComponents: { submit: Submit },
|
|
13
|
+
fieldContext,
|
|
14
|
+
formContext
|
|
15
|
+
});
|
|
16
|
+
export { FieldAvatar, FieldCheckbox, FieldInput, FieldMultiselect, FieldOtp, FieldRadioCard, FieldSwitch, FieldTextarea, FormAlert, fields, useAppForm, useSelector, useStore, withForm };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { restoreOptimisticSnapshot, shouldRestoreOptimisticSnapshot } from "../shared/optimistic.js";
|
|
2
|
+
import { QueryKey, UseMutationOptions } from "@tanstack/react-query";
|
|
3
|
+
type OptimisticUpdate<TOptimisticData, TVariables> = {
|
|
4
|
+
queryKey: QueryKey;
|
|
5
|
+
/** Applied to the cache immediately, before the mutation resolves. Rolled back if it errors. */
|
|
6
|
+
updater: (old: TOptimisticData | undefined, variables: TVariables) => TOptimisticData;
|
|
7
|
+
};
|
|
8
|
+
type MutationContext<TOptimisticData> = {
|
|
9
|
+
previous: TOptimisticData | undefined;
|
|
10
|
+
next: TOptimisticData;
|
|
11
|
+
} | undefined;
|
|
12
|
+
type AppMutationOptions<TData, TVariables, TOptimisticData> = Omit<UseMutationOptions<TData, Error, TVariables, MutationContext<TOptimisticData>>, "onSuccess" | "onError" | "onMutate" | "onSettled"> & {
|
|
13
|
+
/** Query keys to invalidate once the mutation settles, win or lose. */
|
|
14
|
+
invalidates?: QueryKey[];
|
|
15
|
+
optimisticUpdate?: OptimisticUpdate<TOptimisticData, TVariables>;
|
|
16
|
+
successMessage?: string | ((data: TData, variables: TVariables) => string);
|
|
17
|
+
successDescription?: string | ((data: TData, variables: TVariables) => string | undefined);
|
|
18
|
+
errorMessage?: string;
|
|
19
|
+
onSuccess?: (data: TData, variables: TVariables) => void;
|
|
20
|
+
onError?: (error: Error, variables: TVariables) => void;
|
|
21
|
+
};
|
|
22
|
+
export declare function useAppMutation<TData, TVariables = void, TOptimisticData = unknown>({ invalidates, optimisticUpdate, successMessage, successDescription, errorMessage, onSuccess, onError, ...options }: AppMutationOptions<TData, TVariables, TOptimisticData>): import("@tanstack/react-query").UseMutationResult<TData, Error, TVariables, MutationContext<TOptimisticData>>;
|
|
23
|
+
export { restoreOptimisticSnapshot, shouldRestoreOptimisticSnapshot };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { restoreOptimisticSnapshot, shouldRestoreOptimisticSnapshot } from "../shared/optimistic.js";
|
|
2
|
+
import { toast } from "@passui/react/toast";
|
|
3
|
+
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
|
4
|
+
// src/mutation/use-app-mutation.ts
|
|
5
|
+
function useAppMutation({ invalidates = [], optimisticUpdate, successMessage, successDescription, errorMessage, onSuccess, onError, ...options }) {
|
|
6
|
+
const q = useQueryClient();
|
|
7
|
+
return useMutation({
|
|
8
|
+
...options,
|
|
9
|
+
onMutate: async (variables) => {
|
|
10
|
+
if (!optimisticUpdate) return void 0;
|
|
11
|
+
const { queryKey, updater } = optimisticUpdate;
|
|
12
|
+
await q.cancelQueries({ queryKey });
|
|
13
|
+
const previous = q.getQueryData(queryKey);
|
|
14
|
+
const next = updater(previous, variables);
|
|
15
|
+
q.setQueryData(queryKey, next);
|
|
16
|
+
return {
|
|
17
|
+
previous,
|
|
18
|
+
next
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
onSuccess: (data, variables) => {
|
|
22
|
+
if (successMessage) toast.success({
|
|
23
|
+
title: typeof successMessage === "function" ? successMessage(data, variables) : successMessage,
|
|
24
|
+
description: typeof successDescription === "function" ? successDescription(data, variables) : successDescription
|
|
25
|
+
});
|
|
26
|
+
onSuccess?.(data, variables);
|
|
27
|
+
},
|
|
28
|
+
onError: (error, variables, context) => {
|
|
29
|
+
if (optimisticUpdate && context) restoreOptimisticSnapshot(q, optimisticUpdate.queryKey, context.previous, context.next);
|
|
30
|
+
toast.error({
|
|
31
|
+
title: errorMessage ?? "Something went wrong",
|
|
32
|
+
description: error.message
|
|
33
|
+
});
|
|
34
|
+
onError?.(error, variables);
|
|
35
|
+
},
|
|
36
|
+
onSettled: () => {
|
|
37
|
+
for (const queryKey of invalidates) q.invalidateQueries({ queryKey });
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export { restoreOptimisticSnapshot, shouldRestoreOptimisticSnapshot, useAppMutation };
|
package/dist/mutation.js
ADDED