@hirely/hooks 0.1.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/README.md +503 -0
- package/dist/context.d.ts +10 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/index.cjs +573 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +535 -0
- package/dist/index.mjs +535 -0
- package/dist/useFormHandler.d.ts +61 -0
- package/dist/useFormHandler.d.ts.map +1 -0
- package/dist/useFormMutation.d.ts +26 -0
- package/dist/useFormMutation.d.ts.map +1 -0
- package/dist/withFormHandler.d.ts +12 -0
- package/dist/withFormHandler.d.ts.map +1 -0
- package/package.json +82 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
8
|
+
var __toCommonJS = (from) => {
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
+
if (entry)
|
|
11
|
+
return entry;
|
|
12
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
__moduleCache.set(from, entry);
|
|
22
|
+
return entry;
|
|
23
|
+
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, {
|
|
32
|
+
get: all[name],
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/index.ts
|
|
40
|
+
var exports_src = {};
|
|
41
|
+
__export(exports_src, {
|
|
42
|
+
FormHandlerProvider: () => FormHandlerProvider,
|
|
43
|
+
createFormHandler: () => createFormHandler,
|
|
44
|
+
useFormHandler: () => useFormHandler,
|
|
45
|
+
useFormHandlerContext: () => useFormHandlerContext,
|
|
46
|
+
useFormHandlerInternal: () => useFormHandlerInternal,
|
|
47
|
+
useFormMutation: () => useFormMutation,
|
|
48
|
+
withFormHandler: () => withFormHandler
|
|
49
|
+
});
|
|
50
|
+
module.exports = __toCommonJS(exports_src);
|
|
51
|
+
|
|
52
|
+
// src/useFormHandler.ts
|
|
53
|
+
var import_react2 = require("react");
|
|
54
|
+
var import_react_hook_form3 = require("react-hook-form");
|
|
55
|
+
|
|
56
|
+
// node_modules/@hookform/resolvers/dist/resolvers.mjs
|
|
57
|
+
var import_react_hook_form = require("react-hook-form");
|
|
58
|
+
var t = (r2, t2, s) => {
|
|
59
|
+
if (r2 && "reportValidity" in r2) {
|
|
60
|
+
const o = import_react_hook_form.get(s, t2);
|
|
61
|
+
r2.setCustomValidity(o && o.message || ""), r2.reportValidity();
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
var s = (e2, r2) => {
|
|
65
|
+
for (const s2 in r2.fields) {
|
|
66
|
+
const o = r2.fields[s2];
|
|
67
|
+
o && o.ref && "reportValidity" in o.ref ? t(o.ref, s2, e2) : o && o.refs && o.refs.forEach((r3) => t(r3, s2, e2));
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var o = (t2, o2) => {
|
|
71
|
+
o2.shouldUseNativeValidation && s(t2, o2);
|
|
72
|
+
const n = {};
|
|
73
|
+
for (const s2 in t2) {
|
|
74
|
+
const f = import_react_hook_form.get(o2.fields, s2), c = Object.assign(t2[s2] || {}, { ref: f && f.refs ? f.refs[0] : f && f.ref });
|
|
75
|
+
if (i(o2.names || Object.keys(t2), s2)) {
|
|
76
|
+
const t3 = Object.assign({}, import_react_hook_form.get(n, s2));
|
|
77
|
+
import_react_hook_form.set(t3, "root", c), import_react_hook_form.set(n, s2, t3);
|
|
78
|
+
} else
|
|
79
|
+
import_react_hook_form.set(n, s2, c);
|
|
80
|
+
}
|
|
81
|
+
return n;
|
|
82
|
+
};
|
|
83
|
+
var i = (e2, r2) => {
|
|
84
|
+
const t2 = n(r2).replace(/[.*+?^${}()|\\]/g, "\\$&");
|
|
85
|
+
return e2.some((e3) => n(e3).match(`^${t2}\\.\\d+`));
|
|
86
|
+
};
|
|
87
|
+
function n(e2) {
|
|
88
|
+
return e2.replace(/\[(\d+)]/g, ".$1").replace(/[[\]]/g, "");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// node_modules/@hookform/resolvers/zod/dist/zod.mjs
|
|
92
|
+
var import_react_hook_form2 = require("react-hook-form");
|
|
93
|
+
function o2() {
|
|
94
|
+
return o2 = Object.assign ? Object.assign.bind() : function(r2) {
|
|
95
|
+
for (var e2 = 1;e2 < arguments.length; e2++) {
|
|
96
|
+
var n3 = arguments[e2];
|
|
97
|
+
for (var o3 in n3)
|
|
98
|
+
({}).hasOwnProperty.call(n3, o3) && (r2[o3] = n3[o3]);
|
|
99
|
+
}
|
|
100
|
+
return r2;
|
|
101
|
+
}, o2.apply(null, arguments);
|
|
102
|
+
}
|
|
103
|
+
function t2(r2, e2) {
|
|
104
|
+
try {
|
|
105
|
+
var n3 = r2();
|
|
106
|
+
} catch (r3) {
|
|
107
|
+
return e2(r3);
|
|
108
|
+
}
|
|
109
|
+
return n3 && n3.then ? n3.then(undefined, e2) : n3;
|
|
110
|
+
}
|
|
111
|
+
function s2(r2, e2) {
|
|
112
|
+
for (var o3 = Object.create(null);r2.length; ) {
|
|
113
|
+
var t3 = r2[0], s3 = t3.code, i2 = t3.message, u = t3.path.join(".");
|
|
114
|
+
if (!o3[u])
|
|
115
|
+
if ("unionErrors" in t3) {
|
|
116
|
+
var a, c, l = t3.unionErrors.reduce(function(r3, e3) {
|
|
117
|
+
return e3.errors.length < r3.errors.length ? e3 : r3;
|
|
118
|
+
}).errors[0];
|
|
119
|
+
o3[u] = { message: (a = l == null ? undefined : l.message) != null ? a : i2, type: (c = l == null ? undefined : l.code) != null ? c : s3 };
|
|
120
|
+
} else
|
|
121
|
+
o3[u] = { message: i2, type: s3 };
|
|
122
|
+
if ("unionErrors" in t3 && t3.unionErrors.forEach(function(e3) {
|
|
123
|
+
return e3.errors.forEach(function(e4) {
|
|
124
|
+
return r2.push(e4);
|
|
125
|
+
});
|
|
126
|
+
}), e2) {
|
|
127
|
+
var f = o3[u].types, d = f && f[t3.code];
|
|
128
|
+
o3[u] = import_react_hook_form2.appendErrors(u, e2, o3, s3, d ? [].concat(d, t3.message) : t3.message);
|
|
129
|
+
}
|
|
130
|
+
r2.shift();
|
|
131
|
+
}
|
|
132
|
+
return o3;
|
|
133
|
+
}
|
|
134
|
+
function i2(r2, e2) {
|
|
135
|
+
for (var t3 = Object.create(null), s3 = function() {
|
|
136
|
+
var s4 = r2[0], i3 = s4.code, u = s4.message, a = s4.path.join(".");
|
|
137
|
+
if (!t3[a])
|
|
138
|
+
if (s4.code === "invalid_union" && s4.errors.length > 0) {
|
|
139
|
+
var c, l, f = s4.errors.reduce(function(r3, e3) {
|
|
140
|
+
return e3.length < r3.length ? e3 : r3;
|
|
141
|
+
})[0];
|
|
142
|
+
t3[a] = { message: (c = f == null ? undefined : f.message) != null ? c : u, type: (l = f == null ? undefined : f.code) != null ? l : i3 };
|
|
143
|
+
} else
|
|
144
|
+
t3[a] = { message: u, type: i3 };
|
|
145
|
+
if (s4.code === "invalid_union" && s4.errors.forEach(function(e3) {
|
|
146
|
+
return e3.forEach(function(e4) {
|
|
147
|
+
return r2.push(o2({}, e4, { path: [].concat(s4.path, e4.path) }));
|
|
148
|
+
});
|
|
149
|
+
}), e2) {
|
|
150
|
+
var d = t3[a].types, h = d && d[s4.code];
|
|
151
|
+
t3[a] = import_react_hook_form2.appendErrors(a, e2, t3, i3, h ? [].concat(h, s4.message) : s4.message);
|
|
152
|
+
}
|
|
153
|
+
r2.shift();
|
|
154
|
+
};r2.length; )
|
|
155
|
+
s3();
|
|
156
|
+
return t3;
|
|
157
|
+
}
|
|
158
|
+
function u(n3, o3, u2) {
|
|
159
|
+
if (u2 === undefined && (u2 = {}), function(r2) {
|
|
160
|
+
return "_zod" in r2 && typeof r2._zod == "object";
|
|
161
|
+
}(n3))
|
|
162
|
+
return function(s3, a, c) {
|
|
163
|
+
try {
|
|
164
|
+
return Promise.resolve(t2(function() {
|
|
165
|
+
function e2(e3) {
|
|
166
|
+
return c.shouldUseNativeValidation && s({}, c), { errors: {}, values: u2.raw ? Object.assign({}, s3) : e3 };
|
|
167
|
+
}
|
|
168
|
+
var t3 = n3;
|
|
169
|
+
return u2.mode === "sync" ? e2(t3.parse(s3, o3)) : Promise.resolve(t3.parseAsync(s3, o3)).then(e2);
|
|
170
|
+
}, function(r2) {
|
|
171
|
+
if (function(r3) {
|
|
172
|
+
var e2;
|
|
173
|
+
return !(r3 == null || (e2 = r3._zod) == null || (e2 = e2.traits) == null || !e2.has("$ZodError"));
|
|
174
|
+
}(r2))
|
|
175
|
+
return { values: {}, errors: o(i2(r2.issues, !c.shouldUseNativeValidation && c.criteriaMode === "all"), c) };
|
|
176
|
+
throw r2;
|
|
177
|
+
}));
|
|
178
|
+
} catch (r2) {
|
|
179
|
+
return Promise.reject(r2);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
if (function(r2) {
|
|
183
|
+
return "_def" in r2 && typeof r2._def == "object";
|
|
184
|
+
}(n3))
|
|
185
|
+
return function(i3, a, c) {
|
|
186
|
+
try {
|
|
187
|
+
return Promise.resolve(t2(function() {
|
|
188
|
+
return Promise.resolve(n3[u2.mode === "sync" ? "parse" : "parseAsync"](i3, o3)).then(function(e2) {
|
|
189
|
+
return c.shouldUseNativeValidation && s({}, c), { errors: {}, values: u2.raw ? Object.assign({}, i3) : e2 };
|
|
190
|
+
});
|
|
191
|
+
}, function(r2) {
|
|
192
|
+
if (function(r3) {
|
|
193
|
+
return Array.isArray(r3 == null ? undefined : r3.issues);
|
|
194
|
+
}(r2))
|
|
195
|
+
return { values: {}, errors: o(s2(r2.errors, !c.shouldUseNativeValidation && c.criteriaMode === "all"), c) };
|
|
196
|
+
throw r2;
|
|
197
|
+
}));
|
|
198
|
+
} catch (r2) {
|
|
199
|
+
return Promise.reject(r2);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
throw new Error("Invalid input: not a Zod schema");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/context.tsx
|
|
206
|
+
var import_react = require("react");
|
|
207
|
+
var jsx_runtime = require("react/jsx-runtime");
|
|
208
|
+
"use client";
|
|
209
|
+
var FormHandlerContext = import_react.createContext({});
|
|
210
|
+
var FormHandlerProvider = ({
|
|
211
|
+
children,
|
|
212
|
+
defaultOptions
|
|
213
|
+
}) => {
|
|
214
|
+
const value = import_react.useMemo(() => defaultOptions, [defaultOptions]);
|
|
215
|
+
return /* @__PURE__ */ jsx_runtime.jsx(FormHandlerContext.Provider, {
|
|
216
|
+
value,
|
|
217
|
+
children
|
|
218
|
+
});
|
|
219
|
+
};
|
|
220
|
+
var useFormHandlerContext = () => import_react.useContext(FormHandlerContext);
|
|
221
|
+
|
|
222
|
+
// src/useFormHandler.ts
|
|
223
|
+
"use client";
|
|
224
|
+
var defaultParseResponse = (res) => ({
|
|
225
|
+
success: res?.success !== undefined ? Boolean(res.success) : true,
|
|
226
|
+
message: res?.message,
|
|
227
|
+
data: res
|
|
228
|
+
});
|
|
229
|
+
var defaultParseError = (err) => {
|
|
230
|
+
let message = "Request failed";
|
|
231
|
+
if (err?.isAxiosError) {
|
|
232
|
+
message = err?.response?.data?.message || err.message || message;
|
|
233
|
+
if (err.code === "ERR_CANCELED") {
|
|
234
|
+
message = "Request was cancelled";
|
|
235
|
+
}
|
|
236
|
+
} else if (err instanceof Error) {
|
|
237
|
+
message = err.message;
|
|
238
|
+
}
|
|
239
|
+
return { message };
|
|
240
|
+
};
|
|
241
|
+
var cachedAxios = null;
|
|
242
|
+
var axiosLoadPromise = null;
|
|
243
|
+
var getAxios = async () => {
|
|
244
|
+
if (cachedAxios)
|
|
245
|
+
return cachedAxios;
|
|
246
|
+
if (!axiosLoadPromise) {
|
|
247
|
+
axiosLoadPromise = import("axios").then((m) => {
|
|
248
|
+
cachedAxios = m.default || m;
|
|
249
|
+
return cachedAxios;
|
|
250
|
+
}).catch(() => {
|
|
251
|
+
axiosLoadPromise = null;
|
|
252
|
+
return null;
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
return axiosLoadPromise;
|
|
256
|
+
};
|
|
257
|
+
var cachedSonnerToast = null;
|
|
258
|
+
var sonnerLoadPromise = null;
|
|
259
|
+
var getSonnerToast = async () => {
|
|
260
|
+
if (cachedSonnerToast)
|
|
261
|
+
return cachedSonnerToast;
|
|
262
|
+
if (!sonnerLoadPromise) {
|
|
263
|
+
sonnerLoadPromise = import("sonner").then((m) => {
|
|
264
|
+
cachedSonnerToast = m.toast || m;
|
|
265
|
+
return cachedSonnerToast;
|
|
266
|
+
}).catch(() => {
|
|
267
|
+
sonnerLoadPromise = null;
|
|
268
|
+
return null;
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
return sonnerLoadPromise;
|
|
272
|
+
};
|
|
273
|
+
function useFormHandlerInternal(options) {
|
|
274
|
+
const optionsRef = import_react2.useRef(options);
|
|
275
|
+
optionsRef.current = options;
|
|
276
|
+
const {
|
|
277
|
+
schema,
|
|
278
|
+
defaultValues,
|
|
279
|
+
values,
|
|
280
|
+
mode = "onSubmit",
|
|
281
|
+
reValidateMode = "onChange"
|
|
282
|
+
} = options;
|
|
283
|
+
const [loading, setLoading] = import_react2.useState(false);
|
|
284
|
+
const [error, setError] = import_react2.useState(null);
|
|
285
|
+
const abortControllerRef = import_react2.useRef(null);
|
|
286
|
+
const form = import_react_hook_form3.useForm({
|
|
287
|
+
resolver: u(schema),
|
|
288
|
+
defaultValues,
|
|
289
|
+
values,
|
|
290
|
+
mode,
|
|
291
|
+
reValidateMode
|
|
292
|
+
});
|
|
293
|
+
const abort = import_react2.useCallback(() => {
|
|
294
|
+
if (abortControllerRef.current) {
|
|
295
|
+
abortControllerRef.current.abort();
|
|
296
|
+
abortControllerRef.current = null;
|
|
297
|
+
}
|
|
298
|
+
}, []);
|
|
299
|
+
const submitHandler = import_react2.useCallback(async (data) => {
|
|
300
|
+
const currentOptions = optionsRef.current;
|
|
301
|
+
const {
|
|
302
|
+
endpoint,
|
|
303
|
+
method = "post",
|
|
304
|
+
service,
|
|
305
|
+
transformData,
|
|
306
|
+
onMutate,
|
|
307
|
+
onSuccess,
|
|
308
|
+
onError,
|
|
309
|
+
onSubmitStart,
|
|
310
|
+
onSubmitEnd,
|
|
311
|
+
notify,
|
|
312
|
+
resetOptions = { resetAfterSuccess: true, keepDefaultValues: true },
|
|
313
|
+
useFormData = false,
|
|
314
|
+
enableAbort = false,
|
|
315
|
+
axiosConfig = {},
|
|
316
|
+
parseResponse = defaultParseResponse,
|
|
317
|
+
parseError = defaultParseError
|
|
318
|
+
} = currentOptions;
|
|
319
|
+
let context;
|
|
320
|
+
const transformedData = transformData ? transformData(data) : data;
|
|
321
|
+
let notifyFn;
|
|
322
|
+
if (notify) {
|
|
323
|
+
notifyFn = notify;
|
|
324
|
+
} else {
|
|
325
|
+
const toast = await getSonnerToast();
|
|
326
|
+
if (toast) {
|
|
327
|
+
notifyFn = (message, type) => {
|
|
328
|
+
if (type === "success") {
|
|
329
|
+
toast.success(message);
|
|
330
|
+
} else {
|
|
331
|
+
toast.error(message);
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
} else {
|
|
335
|
+
notifyFn = () => {};
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (enableAbort) {
|
|
339
|
+
abortControllerRef.current = new AbortController;
|
|
340
|
+
}
|
|
341
|
+
try {
|
|
342
|
+
setLoading(true);
|
|
343
|
+
setError(null);
|
|
344
|
+
onSubmitStart?.();
|
|
345
|
+
if (onMutate) {
|
|
346
|
+
context = await onMutate(data);
|
|
347
|
+
}
|
|
348
|
+
let responseData;
|
|
349
|
+
let payload = transformedData;
|
|
350
|
+
if (useFormData) {
|
|
351
|
+
const formData = new FormData;
|
|
352
|
+
if (transformedData && typeof transformedData === "object") {
|
|
353
|
+
for (const [key, value] of Object.entries(transformedData)) {
|
|
354
|
+
if (value instanceof File || value instanceof Blob) {
|
|
355
|
+
formData.append(key, value);
|
|
356
|
+
} else if (Array.isArray(value)) {
|
|
357
|
+
for (const item of value) {
|
|
358
|
+
if (item instanceof File || item instanceof Blob) {
|
|
359
|
+
formData.append(key, item);
|
|
360
|
+
} else if (item !== undefined && item !== null) {
|
|
361
|
+
formData.append(key, String(item));
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
} else if (value !== undefined && value !== null) {
|
|
365
|
+
formData.append(key, String(value));
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
payload = formData;
|
|
370
|
+
}
|
|
371
|
+
if (service) {
|
|
372
|
+
responseData = await service(payload);
|
|
373
|
+
} else {
|
|
374
|
+
if (!endpoint) {
|
|
375
|
+
throw new Error("No endpoint or service provided");
|
|
376
|
+
}
|
|
377
|
+
const axiosInstance = await getAxios();
|
|
378
|
+
if (!axiosInstance) {
|
|
379
|
+
throw new Error("axios is not installed. Please install axios or provide a custom `service` function.");
|
|
380
|
+
}
|
|
381
|
+
const config = {
|
|
382
|
+
method,
|
|
383
|
+
url: endpoint,
|
|
384
|
+
data: payload,
|
|
385
|
+
...axiosConfig,
|
|
386
|
+
signal: enableAbort ? abortControllerRef.current?.signal : undefined,
|
|
387
|
+
headers: useFormData ? {
|
|
388
|
+
...axiosConfig.headers,
|
|
389
|
+
"Content-Type": "multipart/form-data"
|
|
390
|
+
} : axiosConfig.headers
|
|
391
|
+
};
|
|
392
|
+
const res = await axiosInstance(config);
|
|
393
|
+
responseData = res.data;
|
|
394
|
+
}
|
|
395
|
+
const parsed = parseResponse(responseData);
|
|
396
|
+
const isSuccess = parsed.success;
|
|
397
|
+
const message = parsed.message || (isSuccess ? "Success" : "Something went wrong");
|
|
398
|
+
if (isSuccess) {
|
|
399
|
+
notifyFn(message, "success");
|
|
400
|
+
onSuccess?.(responseData, context);
|
|
401
|
+
if (resetOptions.resetAfterSuccess !== false) {
|
|
402
|
+
form.reset(undefined, {
|
|
403
|
+
keepDefaultValues: resetOptions.keepDefaultValues !== false,
|
|
404
|
+
keepDirty: resetOptions.keepDirty || false,
|
|
405
|
+
keepTouched: false,
|
|
406
|
+
keepErrors: false,
|
|
407
|
+
keepValues: false
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
} else {
|
|
411
|
+
notifyFn(message, "error");
|
|
412
|
+
onError?.(responseData, context);
|
|
413
|
+
setError(new Error(message));
|
|
414
|
+
}
|
|
415
|
+
} catch (err) {
|
|
416
|
+
const parsedError = parseError(err);
|
|
417
|
+
const errorMsg = parsedError.message;
|
|
418
|
+
setError(new Error(errorMsg));
|
|
419
|
+
notifyFn(errorMsg, "error");
|
|
420
|
+
onError?.(err, context);
|
|
421
|
+
} finally {
|
|
422
|
+
setLoading(false);
|
|
423
|
+
onSubmitEnd?.();
|
|
424
|
+
abortControllerRef.current = null;
|
|
425
|
+
}
|
|
426
|
+
}, [form]);
|
|
427
|
+
const onSubmit = import_react2.useMemo(() => form.handleSubmit(submitHandler), [form, submitHandler]);
|
|
428
|
+
return import_react2.useMemo(() => ({
|
|
429
|
+
...form,
|
|
430
|
+
onSubmit,
|
|
431
|
+
loading,
|
|
432
|
+
error,
|
|
433
|
+
setError,
|
|
434
|
+
abort: options.enableAbort ? abort : undefined
|
|
435
|
+
}), [form, onSubmit, loading, error, setError, abort, options.enableAbort]);
|
|
436
|
+
}
|
|
437
|
+
function useFormHandler(options) {
|
|
438
|
+
const contextOptions = useFormHandlerContext();
|
|
439
|
+
const mergedOptions = import_react2.useMemo(() => {
|
|
440
|
+
return {
|
|
441
|
+
...contextOptions,
|
|
442
|
+
...options,
|
|
443
|
+
axiosConfig: {
|
|
444
|
+
...contextOptions.axiosConfig || {},
|
|
445
|
+
...options.axiosConfig || {}
|
|
446
|
+
},
|
|
447
|
+
resetOptions: {
|
|
448
|
+
...contextOptions.resetOptions || {},
|
|
449
|
+
...options.resetOptions || {}
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
}, [contextOptions, options]);
|
|
453
|
+
return useFormHandlerInternal(mergedOptions);
|
|
454
|
+
}
|
|
455
|
+
function createFormHandler(defaultOptions) {
|
|
456
|
+
return function useCreatedFormHandler(options) {
|
|
457
|
+
const mergedOptions = import_react2.useMemo(() => {
|
|
458
|
+
return {
|
|
459
|
+
...defaultOptions,
|
|
460
|
+
...options,
|
|
461
|
+
axiosConfig: {
|
|
462
|
+
...defaultOptions.axiosConfig || {},
|
|
463
|
+
...options.axiosConfig || {}
|
|
464
|
+
},
|
|
465
|
+
resetOptions: {
|
|
466
|
+
...defaultOptions.resetOptions || {},
|
|
467
|
+
...options.resetOptions || {}
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
}, [options]);
|
|
471
|
+
return useFormHandlerInternal(mergedOptions);
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
// src/withFormHandler.tsx
|
|
475
|
+
var jsx_runtime2 = require("react/jsx-runtime");
|
|
476
|
+
"use client";
|
|
477
|
+
function withFormHandler(WrappedComponent, options) {
|
|
478
|
+
const WithFormHandler = (props) => {
|
|
479
|
+
const formHandler = useFormHandler(options);
|
|
480
|
+
return /* @__PURE__ */ jsx_runtime2.jsx(WrappedComponent, {
|
|
481
|
+
...props,
|
|
482
|
+
formHandler
|
|
483
|
+
});
|
|
484
|
+
};
|
|
485
|
+
WithFormHandler.displayName = `WithFormHandler(${WrappedComponent.displayName || WrappedComponent.name || "Component"})`;
|
|
486
|
+
return WithFormHandler;
|
|
487
|
+
}
|
|
488
|
+
// src/useFormMutation.ts
|
|
489
|
+
var import_react_query = require("@tanstack/react-query");
|
|
490
|
+
var import_react3 = require("react");
|
|
491
|
+
"use client";
|
|
492
|
+
var cachedAxios2 = null;
|
|
493
|
+
var axiosLoadPromise2 = null;
|
|
494
|
+
var getAxios2 = async () => {
|
|
495
|
+
if (cachedAxios2)
|
|
496
|
+
return cachedAxios2;
|
|
497
|
+
if (!axiosLoadPromise2) {
|
|
498
|
+
axiosLoadPromise2 = import("axios").then((m) => {
|
|
499
|
+
cachedAxios2 = m.default || m;
|
|
500
|
+
return cachedAxios2;
|
|
501
|
+
}).catch(() => {
|
|
502
|
+
axiosLoadPromise2 = null;
|
|
503
|
+
return null;
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
return axiosLoadPromise2;
|
|
507
|
+
};
|
|
508
|
+
function useFormMutation(options) {
|
|
509
|
+
const { mutationOptions, ...formOptions } = options;
|
|
510
|
+
const optionsRef = import_react3.useRef(options);
|
|
511
|
+
optionsRef.current = options;
|
|
512
|
+
const form = useFormHandler(formOptions);
|
|
513
|
+
const mutationFn = import_react3.useMemo(() => {
|
|
514
|
+
return async (data) => {
|
|
515
|
+
const currentOpts = optionsRef.current;
|
|
516
|
+
if (currentOpts.service) {
|
|
517
|
+
return await currentOpts.service(data);
|
|
518
|
+
}
|
|
519
|
+
if (currentOpts.endpoint) {
|
|
520
|
+
const axiosInstance = await getAxios2();
|
|
521
|
+
if (!axiosInstance) {
|
|
522
|
+
throw new Error("axios is not installed. Please install axios or provide a custom `service` function.");
|
|
523
|
+
}
|
|
524
|
+
const res = await axiosInstance({
|
|
525
|
+
method: currentOpts.method || "post",
|
|
526
|
+
url: currentOpts.endpoint,
|
|
527
|
+
data,
|
|
528
|
+
...currentOpts.axiosConfig
|
|
529
|
+
});
|
|
530
|
+
return res.data;
|
|
531
|
+
}
|
|
532
|
+
throw new Error("No service or endpoint provided");
|
|
533
|
+
};
|
|
534
|
+
}, []);
|
|
535
|
+
const mutation = import_react_query.useMutation({
|
|
536
|
+
mutationFn,
|
|
537
|
+
...mutationOptions,
|
|
538
|
+
onSuccess: (data, variables, context) => {
|
|
539
|
+
optionsRef.current.onSuccess?.(data, context);
|
|
540
|
+
if (mutationOptions?.onSuccess) {
|
|
541
|
+
mutationOptions.onSuccess(data, variables, context);
|
|
542
|
+
}
|
|
543
|
+
},
|
|
544
|
+
onError: (error, variables, context) => {
|
|
545
|
+
optionsRef.current.onError?.(error, context);
|
|
546
|
+
if (mutationOptions?.onError) {
|
|
547
|
+
mutationOptions.onError(error, variables, context);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
const reset = () => {
|
|
552
|
+
mutation.reset();
|
|
553
|
+
form.reset();
|
|
554
|
+
};
|
|
555
|
+
return {
|
|
556
|
+
...form,
|
|
557
|
+
mutate: mutation.mutate,
|
|
558
|
+
mutateAsync: mutation.mutateAsync,
|
|
559
|
+
isPending: mutation.isPending,
|
|
560
|
+
isLoading: mutation.isPending,
|
|
561
|
+
isError: mutation.isError,
|
|
562
|
+
error: mutation.error,
|
|
563
|
+
formError: form.error,
|
|
564
|
+
data: mutation.data,
|
|
565
|
+
reset,
|
|
566
|
+
resetForm: form.reset,
|
|
567
|
+
resetMutation: mutation.reset,
|
|
568
|
+
status: mutation.status
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// src/index.ts
|
|
573
|
+
"use client";
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { useFormHandlerInternal, useFormHandler, createFormHandler, } from "./useFormHandler";
|
|
2
|
+
export type { UseFormHandlerOptions, NotifyFn, ResponseParser, ErrorParser, } from "./useFormHandler";
|
|
3
|
+
export { FormHandlerProvider, useFormHandlerContext } from "./context";
|
|
4
|
+
export { withFormHandler } from "./withFormHandler";
|
|
5
|
+
export { useFormMutation } from "./useFormMutation";
|
|
6
|
+
export type { UseFormMutationReturn } from "./useFormMutation";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,sBAAsB,EACtB,cAAc,EACd,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EACV,qBAAqB,EACrB,QAAQ,EACR,cAAc,EACd,WAAW,GACZ,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAEvE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,YAAY,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC"}
|