react-f0rm 0.1.0 → 0.2.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 +174 -0
- package/dist/index.cjs.js +667 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.esm.js +482 -340
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +529 -344
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +2 -2
- package/dist/index.umd.min.js.map +1 -1
- package/dist/resolvers/yup.cjs.js +15 -0
- package/dist/resolvers/yup.cjs.js.map +1 -0
- package/dist/resolvers/yup.esm.js +13 -0
- package/dist/resolvers/yup.esm.js.map +1 -0
- package/dist/resolvers/zod.cjs.js +12 -0
- package/dist/resolvers/zod.cjs.js.map +1 -0
- package/dist/resolvers/zod.esm.js +10 -0
- package/dist/resolvers/zod.esm.js.map +1 -0
- package/package.json +82 -45
- package/index.d.ts +0 -22
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var React = require('react');
|
|
4
|
+
|
|
5
|
+
function _interopNamespaceDefault(e) {
|
|
6
|
+
var n = Object.create(null);
|
|
7
|
+
if (e) {
|
|
8
|
+
Object.keys(e).forEach(function (k) {
|
|
9
|
+
if (k !== 'default') {
|
|
10
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
11
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return e[k]; }
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
n.default = e;
|
|
19
|
+
return Object.freeze(n);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
23
|
+
|
|
24
|
+
function getSet(ee, key) {
|
|
25
|
+
var set = ee.get(key);
|
|
26
|
+
if (set) return set;
|
|
27
|
+
var newSet = new Set();
|
|
28
|
+
ee.set(key, newSet);
|
|
29
|
+
return newSet;
|
|
30
|
+
}
|
|
31
|
+
function create$2() {
|
|
32
|
+
return new Map();
|
|
33
|
+
}
|
|
34
|
+
function emit$1(ee, key) {
|
|
35
|
+
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
36
|
+
args[_key - 2] = arguments[_key];
|
|
37
|
+
}
|
|
38
|
+
(ee.get(key) || []).forEach(function (h) {
|
|
39
|
+
return h.apply(void 0, args);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function on(ee, key, handler) {
|
|
43
|
+
var set = getSet(ee, key);
|
|
44
|
+
set.add(handler);
|
|
45
|
+
return function () {
|
|
46
|
+
return set.delete(handler);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function normalizePath(path) {
|
|
51
|
+
if (Array.isArray(path)) return path;
|
|
52
|
+
return path.split(/\.|\[/).map((prop) => prop.endsWith("]") ? Number.parseInt(prop, 10) : prop);
|
|
53
|
+
}
|
|
54
|
+
function get(values, path) {
|
|
55
|
+
return path.reduce((current, p) => {
|
|
56
|
+
if (current == null) return void 0;
|
|
57
|
+
return current[p];
|
|
58
|
+
}, values);
|
|
59
|
+
}
|
|
60
|
+
function set(values, path, value) {
|
|
61
|
+
if (!path.length) return value;
|
|
62
|
+
const [prop, ...props] = path;
|
|
63
|
+
if (typeof prop === "number") {
|
|
64
|
+
const arr = Array.isArray(values) ? values.slice() : [];
|
|
65
|
+
arr[prop] = set(arr[prop], props, value);
|
|
66
|
+
return arr;
|
|
67
|
+
}
|
|
68
|
+
return { ...values, [prop]: set(values && values[prop], props, value) };
|
|
69
|
+
}
|
|
70
|
+
function isPromise(value) {
|
|
71
|
+
return value && typeof value.then === "function";
|
|
72
|
+
}
|
|
73
|
+
function waitUntil(emitter, event, isResolve, isReject) {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
if (isReject()) return void reject();
|
|
76
|
+
if (isResolve()) return void resolve();
|
|
77
|
+
const off = on(emitter, event, () => {
|
|
78
|
+
if (isReject()) {
|
|
79
|
+
off();
|
|
80
|
+
reject();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (isResolve()) return;
|
|
84
|
+
off();
|
|
85
|
+
resolve();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function create$1(name) {
|
|
91
|
+
const value = normalizePath(name);
|
|
92
|
+
return { value, key: JSON.stringify(value) };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const emit = emit$1;
|
|
96
|
+
function create(options) {
|
|
97
|
+
const emitter = create$2();
|
|
98
|
+
return {
|
|
99
|
+
emitter,
|
|
100
|
+
revalidateOnChange: true,
|
|
101
|
+
...options,
|
|
102
|
+
initialValues: options?.initialValues ?? {},
|
|
103
|
+
values: /* @__PURE__ */ new Map(),
|
|
104
|
+
errors: /* @__PURE__ */ new Map(),
|
|
105
|
+
touched: /* @__PURE__ */ new Set(),
|
|
106
|
+
validators: /* @__PURE__ */ new Map(),
|
|
107
|
+
validating: /* @__PURE__ */ new Set(),
|
|
108
|
+
isSubmitting: false,
|
|
109
|
+
submitCount: 0,
|
|
110
|
+
isSubmitSuccessful: void 0
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function getValues(form) {
|
|
114
|
+
return Array.from(form.values.keys()).reduce(
|
|
115
|
+
(v, k) => set(v, JSON.parse(k), form.values.get(k)),
|
|
116
|
+
form.initialValues
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
function getValue(form, name) {
|
|
120
|
+
return getValueByPath(form, create$1(name));
|
|
121
|
+
}
|
|
122
|
+
function getValueByPath({ initialValues, values }, path) {
|
|
123
|
+
if (values.has(path.key)) return values.get(path.key);
|
|
124
|
+
return get(initialValues, path.value);
|
|
125
|
+
}
|
|
126
|
+
function setValue(form, name, value) {
|
|
127
|
+
setValueByPath(form, create$1(name), value);
|
|
128
|
+
}
|
|
129
|
+
function setValueByPath({ emitter, values }, path, value) {
|
|
130
|
+
values.set(path.key, value);
|
|
131
|
+
emit(emitter, "change", path);
|
|
132
|
+
}
|
|
133
|
+
function getError(form, name) {
|
|
134
|
+
return getErrorByPath(form, create$1(name));
|
|
135
|
+
}
|
|
136
|
+
function getErrorByPath({ errors }, path) {
|
|
137
|
+
return errors.get(path.key);
|
|
138
|
+
}
|
|
139
|
+
function getErrors({ errors }) {
|
|
140
|
+
return Array.from(errors.values());
|
|
141
|
+
}
|
|
142
|
+
function getFirstError({ errors }) {
|
|
143
|
+
return errors.values().next().value;
|
|
144
|
+
}
|
|
145
|
+
function unsetValidatingByPath({ emitter, validating }, { key }) {
|
|
146
|
+
validating.delete(key);
|
|
147
|
+
emit(emitter, "validating");
|
|
148
|
+
}
|
|
149
|
+
function setValidatingByPath({ emitter, validating }, { key }) {
|
|
150
|
+
validating.add(key);
|
|
151
|
+
emit(emitter, "validating");
|
|
152
|
+
}
|
|
153
|
+
function setError(form, name, error) {
|
|
154
|
+
setErrorByPath(form, create$1(name), error);
|
|
155
|
+
}
|
|
156
|
+
function setErrorByPath({ emitter, errors }, path, error) {
|
|
157
|
+
if (error) {
|
|
158
|
+
errors.set(path.key, error);
|
|
159
|
+
} else {
|
|
160
|
+
errors.delete(path.key);
|
|
161
|
+
}
|
|
162
|
+
emit(emitter, "errors");
|
|
163
|
+
}
|
|
164
|
+
function clearErrors({ emitter, errors }) {
|
|
165
|
+
errors.clear();
|
|
166
|
+
emit(emitter, "errors");
|
|
167
|
+
}
|
|
168
|
+
function setTouched(form, name) {
|
|
169
|
+
setTouchedByPath(form, create$1(name));
|
|
170
|
+
}
|
|
171
|
+
function setTouchedByPath({ emitter, touched }, { key }) {
|
|
172
|
+
if (touched.has(key)) return;
|
|
173
|
+
touched.add(key);
|
|
174
|
+
emit(emitter, "touched");
|
|
175
|
+
}
|
|
176
|
+
function hasTouched(form, name) {
|
|
177
|
+
return hasTouchedByPath(form, create$1(name));
|
|
178
|
+
}
|
|
179
|
+
function hasTouchedByPath({ touched }, path) {
|
|
180
|
+
return touched.has(path.key);
|
|
181
|
+
}
|
|
182
|
+
function isDirty({ initialValues, values }) {
|
|
183
|
+
for (const [key, value] of values) {
|
|
184
|
+
const path = JSON.parse(key);
|
|
185
|
+
if (get(initialValues, path) !== value) return true;
|
|
186
|
+
}
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
function isTouched({ touched }) {
|
|
190
|
+
return touched.size > 0;
|
|
191
|
+
}
|
|
192
|
+
function removeField(form, name) {
|
|
193
|
+
removeFieldByPath(form, create$1(name));
|
|
194
|
+
}
|
|
195
|
+
function removeFieldByPath(form, { key }) {
|
|
196
|
+
const { emitter, values, touched, errors, validating } = form;
|
|
197
|
+
values.delete(key);
|
|
198
|
+
touched.delete(key);
|
|
199
|
+
errors.delete(key);
|
|
200
|
+
validating.delete(key);
|
|
201
|
+
emit(emitter, "change");
|
|
202
|
+
emit(emitter, "touched");
|
|
203
|
+
emit(emitter, "errors");
|
|
204
|
+
emit(emitter, "validating");
|
|
205
|
+
}
|
|
206
|
+
function setInitialValues(form, initialValues) {
|
|
207
|
+
if (form.initialValues === initialValues) return;
|
|
208
|
+
form.initialValues = initialValues;
|
|
209
|
+
form.values.clear();
|
|
210
|
+
emit(form.emitter, "change");
|
|
211
|
+
}
|
|
212
|
+
function reset(form, initialValues) {
|
|
213
|
+
form.initialValues = initialValues;
|
|
214
|
+
clearErrors(form);
|
|
215
|
+
const { emitter, touched, values } = form;
|
|
216
|
+
values.clear();
|
|
217
|
+
touched.clear();
|
|
218
|
+
emit(emitter, "change");
|
|
219
|
+
emit(emitter, "touched");
|
|
220
|
+
emit(emitter, "reset");
|
|
221
|
+
}
|
|
222
|
+
function hasErrors({ errors }) {
|
|
223
|
+
return errors.size > 0;
|
|
224
|
+
}
|
|
225
|
+
function trigger(form) {
|
|
226
|
+
form.validators.forEach((validator) => validator());
|
|
227
|
+
}
|
|
228
|
+
async function ensureValidate(form) {
|
|
229
|
+
form.validators.forEach((validator) => validator());
|
|
230
|
+
await waitUntil(
|
|
231
|
+
form.emitter,
|
|
232
|
+
"validating",
|
|
233
|
+
() => !form.validating.size,
|
|
234
|
+
() => hasErrors(form)
|
|
235
|
+
).catch(() => {
|
|
236
|
+
throw new Error(getFirstError(form));
|
|
237
|
+
});
|
|
238
|
+
if (form.validate) {
|
|
239
|
+
const result = await form.validate(getValues(form));
|
|
240
|
+
const entries = result ? Object.entries(result) : [];
|
|
241
|
+
if (entries.length) {
|
|
242
|
+
entries.forEach(([field, error]) => {
|
|
243
|
+
setError(form, field, error);
|
|
244
|
+
});
|
|
245
|
+
throw new Error(getFirstError(form));
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
async function validate(form) {
|
|
250
|
+
return ensureValidate(form).catch((e) => e.message);
|
|
251
|
+
}
|
|
252
|
+
function setIsSubmitting(form, value) {
|
|
253
|
+
form.isSubmitting = value;
|
|
254
|
+
emit(form.emitter, "submitting");
|
|
255
|
+
}
|
|
256
|
+
function incrementSubmitCount(form) {
|
|
257
|
+
form.submitCount++;
|
|
258
|
+
emit(form.emitter, "submitCount");
|
|
259
|
+
}
|
|
260
|
+
function setSubmitSuccessful(form, value) {
|
|
261
|
+
form.isSubmitSuccessful = value;
|
|
262
|
+
emit(form.emitter, "submitSuccessful");
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const FormContext = React.createContext(null);
|
|
266
|
+
const FormProvider = FormContext.Provider;
|
|
267
|
+
function useFormContext() {
|
|
268
|
+
const form = React.useContext(FormContext);
|
|
269
|
+
if (!form) throw new Error("no form provided");
|
|
270
|
+
return form;
|
|
271
|
+
}
|
|
272
|
+
const CheckboxGroupContext = React.createContext(null);
|
|
273
|
+
const CheckboxGroupProvider = CheckboxGroupContext.Provider;
|
|
274
|
+
function useCheckboxGroupContext() {
|
|
275
|
+
const group = React.useContext(CheckboxGroupContext);
|
|
276
|
+
if (!group) throw new Error("no group provided");
|
|
277
|
+
return group;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function useForm(options) {
|
|
281
|
+
const ref = React.useRef(null);
|
|
282
|
+
const form = ref.current = ref.current || create(options);
|
|
283
|
+
const initialValues = options && options.initialValues;
|
|
284
|
+
React.useEffect(() => {
|
|
285
|
+
setInitialValues(form, initialValues);
|
|
286
|
+
}, [initialValues]);
|
|
287
|
+
return form;
|
|
288
|
+
}
|
|
289
|
+
function useWatch(emitter, event, getter) {
|
|
290
|
+
const [value, syncValue] = React.useReducer(getter, void 0, getter);
|
|
291
|
+
React.useEffect(() => on(emitter, event, syncValue), [emitter, event]);
|
|
292
|
+
return value;
|
|
293
|
+
}
|
|
294
|
+
function useValue(form, name) {
|
|
295
|
+
return useValueByPath(form, create$1(name));
|
|
296
|
+
}
|
|
297
|
+
function useValueByPath(form, path) {
|
|
298
|
+
return useWatch(
|
|
299
|
+
form.emitter,
|
|
300
|
+
"change",
|
|
301
|
+
getValueByPath.bind(null, form, path)
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
function useTouched(form, name) {
|
|
305
|
+
return useTouchedByPath(form, create$1(name));
|
|
306
|
+
}
|
|
307
|
+
function useTouchedByPath(form, path) {
|
|
308
|
+
return useWatch(
|
|
309
|
+
form.emitter,
|
|
310
|
+
"touched",
|
|
311
|
+
hasTouchedByPath.bind(null, form, path)
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
function useError(form, name) {
|
|
315
|
+
return useErrorByPath(form, create$1(name));
|
|
316
|
+
}
|
|
317
|
+
function useErrorByPath(form, path) {
|
|
318
|
+
return useWatch(
|
|
319
|
+
form.emitter,
|
|
320
|
+
"errors",
|
|
321
|
+
getErrorByPath.bind(null, form, path)
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
function useIsDirty(form) {
|
|
325
|
+
return useWatch(form.emitter, "touched", isDirty.bind(null, form));
|
|
326
|
+
}
|
|
327
|
+
function useHasErrors(form) {
|
|
328
|
+
return useWatch(form.emitter, "errors", hasErrors.bind(null, form));
|
|
329
|
+
}
|
|
330
|
+
function useIsSubmitting(form) {
|
|
331
|
+
return useWatch(form.emitter, "submitting", () => form.isSubmitting);
|
|
332
|
+
}
|
|
333
|
+
function useSubmitCount(form) {
|
|
334
|
+
return useWatch(form.emitter, "submitCount", () => form.submitCount);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function usePath(name) {
|
|
338
|
+
const path = React.useMemo(() => create$1(normalizePath(name)), [name]);
|
|
339
|
+
return React.useMemo(() => path, [path.key]);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function useStage(value) {
|
|
343
|
+
const ref = React.useRef(value);
|
|
344
|
+
ref.current = value;
|
|
345
|
+
return ref;
|
|
346
|
+
}
|
|
347
|
+
function useStageFn(fn) {
|
|
348
|
+
const ref = useStage(fn);
|
|
349
|
+
return React.useCallback(
|
|
350
|
+
(...params) => ref.current(...params),
|
|
351
|
+
[]
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function useValidate(validate, path) {
|
|
356
|
+
const form = useFormContext();
|
|
357
|
+
const lockRef = React.useRef(null);
|
|
358
|
+
const validateRef = React.useRef(validate);
|
|
359
|
+
validateRef.current = validate;
|
|
360
|
+
React.useEffect(() => {
|
|
361
|
+
const validator = () => {
|
|
362
|
+
const fn = validateRef.current;
|
|
363
|
+
if (!fn) return;
|
|
364
|
+
const result = fn(getValueByPath(form, path), { form, path });
|
|
365
|
+
if (!isPromise(result)) {
|
|
366
|
+
setErrorByPath(form, path, result);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
const lock = lockRef.current = {};
|
|
370
|
+
setValidatingByPath(form, path);
|
|
371
|
+
result.then((error) => {
|
|
372
|
+
if (lock === lockRef.current) {
|
|
373
|
+
setErrorByPath(form, path, error);
|
|
374
|
+
}
|
|
375
|
+
}).finally(() => {
|
|
376
|
+
if (lock === lockRef.current) {
|
|
377
|
+
unsetValidatingByPath(form, path);
|
|
378
|
+
lockRef.current = null;
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
};
|
|
382
|
+
form.validators.set(path.key, validator);
|
|
383
|
+
return () => {
|
|
384
|
+
form.validators.delete(path.key);
|
|
385
|
+
};
|
|
386
|
+
}, [form, path.key]);
|
|
387
|
+
return useStageFn(() => form.validators.get(path.key)?.());
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function useField({
|
|
391
|
+
form: f1,
|
|
392
|
+
name,
|
|
393
|
+
initialValue,
|
|
394
|
+
shouldUnregister,
|
|
395
|
+
validate,
|
|
396
|
+
...rest
|
|
397
|
+
}) {
|
|
398
|
+
const f2 = useFormContext();
|
|
399
|
+
const form = f1 || f2;
|
|
400
|
+
const path = usePath(name);
|
|
401
|
+
const validator = useValidate(validate, path);
|
|
402
|
+
React.useMemo(() => {
|
|
403
|
+
if (initialValue !== void 0) setValueByPath(form, path, initialValue);
|
|
404
|
+
}, [form, path]);
|
|
405
|
+
const error = useErrorByPath(form, path);
|
|
406
|
+
const value = useValueByPath(form, path);
|
|
407
|
+
const onChange = useStageFn((v) => {
|
|
408
|
+
setValueByPath(form, path, v);
|
|
409
|
+
if (form.validateOnChange || form.revalidateOnChange && error && error !== void 0)
|
|
410
|
+
validator();
|
|
411
|
+
});
|
|
412
|
+
const onBlur = useStageFn(() => {
|
|
413
|
+
setTouchedByPath(form, path);
|
|
414
|
+
if (form.validateOnBlur || form.revalidateOnBlur && error !== void 0)
|
|
415
|
+
validator();
|
|
416
|
+
});
|
|
417
|
+
React.useEffect(
|
|
418
|
+
() => () => {
|
|
419
|
+
if (shouldUnregister !== false) {
|
|
420
|
+
removeFieldByPath(form, path);
|
|
421
|
+
}
|
|
422
|
+
},
|
|
423
|
+
[path, form, shouldUnregister]
|
|
424
|
+
);
|
|
425
|
+
return { ...rest, value, error, onChange, onBlur, name: path.key };
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
let idCounter = 0;
|
|
429
|
+
function generateId() {
|
|
430
|
+
return `_${++idCounter}`;
|
|
431
|
+
}
|
|
432
|
+
function useFieldArray(options) {
|
|
433
|
+
const f2 = useFormContext();
|
|
434
|
+
const form = options.form || f2;
|
|
435
|
+
const path = usePath(options.name);
|
|
436
|
+
const idsRef = React.useRef([]);
|
|
437
|
+
const getArray = React.useCallback(
|
|
438
|
+
() => getValueByPath(form, path) || [],
|
|
439
|
+
[form, path]
|
|
440
|
+
);
|
|
441
|
+
const setArray = React.useCallback(
|
|
442
|
+
(arr) => {
|
|
443
|
+
setValueByPath(form, path, arr);
|
|
444
|
+
},
|
|
445
|
+
[form, path]
|
|
446
|
+
);
|
|
447
|
+
const computeFields = React.useCallback(() => {
|
|
448
|
+
const arr = getArray();
|
|
449
|
+
while (idsRef.current.length < arr.length) {
|
|
450
|
+
idsRef.current.push(generateId());
|
|
451
|
+
}
|
|
452
|
+
while (idsRef.current.length > arr.length) {
|
|
453
|
+
idsRef.current.pop();
|
|
454
|
+
}
|
|
455
|
+
return idsRef.current.map((id, index) => ({ id, index }));
|
|
456
|
+
}, [getArray]);
|
|
457
|
+
const [fields, syncFields] = React.useReducer(
|
|
458
|
+
computeFields,
|
|
459
|
+
void 0,
|
|
460
|
+
computeFields
|
|
461
|
+
);
|
|
462
|
+
React.useEffect(() => on(form.emitter, "change", syncFields), [form.emitter]);
|
|
463
|
+
const append = useStageFn((value) => {
|
|
464
|
+
const arr = getArray();
|
|
465
|
+
idsRef.current.push(generateId());
|
|
466
|
+
setArray([...arr, value]);
|
|
467
|
+
});
|
|
468
|
+
const prepend = useStageFn((value) => {
|
|
469
|
+
const arr = getArray();
|
|
470
|
+
idsRef.current.unshift(generateId());
|
|
471
|
+
setArray([value, ...arr]);
|
|
472
|
+
});
|
|
473
|
+
const insert = useStageFn((index, value) => {
|
|
474
|
+
const arr = getArray();
|
|
475
|
+
idsRef.current.splice(index, 0, generateId());
|
|
476
|
+
const newArr = [...arr.slice(0, index), value, ...arr.slice(index)];
|
|
477
|
+
setArray(newArr);
|
|
478
|
+
});
|
|
479
|
+
const remove = useStageFn((index) => {
|
|
480
|
+
const arr = getArray();
|
|
481
|
+
idsRef.current.splice(index, 1);
|
|
482
|
+
const newArr = arr.filter((_, i) => i !== index);
|
|
483
|
+
setArray(newArr);
|
|
484
|
+
});
|
|
485
|
+
const swap = useStageFn((from, to) => {
|
|
486
|
+
const arr = getArray();
|
|
487
|
+
[idsRef.current[from], idsRef.current[to]] = [
|
|
488
|
+
idsRef.current[to],
|
|
489
|
+
idsRef.current[from]
|
|
490
|
+
];
|
|
491
|
+
const newArr = [...arr];
|
|
492
|
+
[newArr[from], newArr[to]] = [newArr[to], newArr[from]];
|
|
493
|
+
setArray(newArr);
|
|
494
|
+
});
|
|
495
|
+
const move = useStageFn((from, to) => {
|
|
496
|
+
const arr = getArray();
|
|
497
|
+
const [id] = idsRef.current.splice(from, 1);
|
|
498
|
+
idsRef.current.splice(to, 0, id);
|
|
499
|
+
const newArr = [...arr];
|
|
500
|
+
const [item] = newArr.splice(from, 1);
|
|
501
|
+
newArr.splice(to, 0, item);
|
|
502
|
+
setArray(newArr);
|
|
503
|
+
});
|
|
504
|
+
return { fields, append, prepend, insert, remove, swap, move };
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function Form({
|
|
508
|
+
form: f1,
|
|
509
|
+
initialValues,
|
|
510
|
+
onSubmit,
|
|
511
|
+
onValidSubmit,
|
|
512
|
+
onInvalidSubmit,
|
|
513
|
+
...props
|
|
514
|
+
}) {
|
|
515
|
+
const f2 = useForm({ initialValues });
|
|
516
|
+
const form = f1 || f2;
|
|
517
|
+
async function handleSubmit(e) {
|
|
518
|
+
e.preventDefault();
|
|
519
|
+
setIsSubmitting(form, true);
|
|
520
|
+
incrementSubmitCount(form);
|
|
521
|
+
const error = await validate(form);
|
|
522
|
+
const values = getValues(form);
|
|
523
|
+
if (error) {
|
|
524
|
+
setIsSubmitting(form, false);
|
|
525
|
+
setSubmitSuccessful(form, false);
|
|
526
|
+
if (onInvalidSubmit) onInvalidSubmit(getErrors(form), values);
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
try {
|
|
530
|
+
if (onSubmit) await onSubmit(values, e);
|
|
531
|
+
if (onValidSubmit) onValidSubmit(values, e);
|
|
532
|
+
setSubmitSuccessful(form, true);
|
|
533
|
+
} catch {
|
|
534
|
+
setSubmitSuccessful(form, false);
|
|
535
|
+
} finally {
|
|
536
|
+
setIsSubmitting(form, false);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return /* @__PURE__ */ React__namespace.createElement(FormProvider, { value: form }, /* @__PURE__ */ React__namespace.createElement("form", { ...props, noValidate: true, onSubmit: handleSubmit }));
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const buildInError = /* @__PURE__ */ Symbol("buildInError");
|
|
543
|
+
function setRef(ref, value) {
|
|
544
|
+
if (typeof ref === "function") {
|
|
545
|
+
ref(value);
|
|
546
|
+
} else if (ref) {
|
|
547
|
+
ref.current = value;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
const Field = React__namespace.forwardRef(
|
|
551
|
+
({ validate, eventToValue, initialValue, name, asProps, ...props }, ref) => {
|
|
552
|
+
const innerRef = React__namespace.useRef(null);
|
|
553
|
+
const mergedRef = React__namespace.useCallback(
|
|
554
|
+
(node) => {
|
|
555
|
+
innerRef.current = node;
|
|
556
|
+
setRef(ref, node);
|
|
557
|
+
},
|
|
558
|
+
[ref]
|
|
559
|
+
);
|
|
560
|
+
const { as, value, valueToProps, onChange, error, ...rest } = useField({
|
|
561
|
+
...props,
|
|
562
|
+
name,
|
|
563
|
+
initialValue,
|
|
564
|
+
validate: (...params) => {
|
|
565
|
+
if (innerRef.current?.checkValidity() === false)
|
|
566
|
+
return buildInError;
|
|
567
|
+
if (validate) return validate(...params);
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
const Component = as || "input";
|
|
571
|
+
React__namespace.useEffect(() => {
|
|
572
|
+
if (!innerRef.current) return;
|
|
573
|
+
if (error === buildInError) {
|
|
574
|
+
innerRef.current.setCustomValidity("");
|
|
575
|
+
innerRef.current.reportValidity();
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
if (typeof error === "string") {
|
|
579
|
+
innerRef.current.setCustomValidity(error);
|
|
580
|
+
innerRef.current.reportValidity();
|
|
581
|
+
}
|
|
582
|
+
}, [error]);
|
|
583
|
+
const toValue = eventToValue ?? ((e) => e.target.value);
|
|
584
|
+
return /* @__PURE__ */ React__namespace.createElement(
|
|
585
|
+
Component,
|
|
586
|
+
{
|
|
587
|
+
...rest,
|
|
588
|
+
...asProps,
|
|
589
|
+
...valueToProps ? valueToProps(value) : { value },
|
|
590
|
+
onChange: (e) => onChange(toValue(e)),
|
|
591
|
+
ref: mergedRef
|
|
592
|
+
}
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
);
|
|
596
|
+
const Checkbox = React__namespace.forwardRef(
|
|
597
|
+
({ name, ...props }, ref) => {
|
|
598
|
+
const { value, onChange, error, ...rest } = useField({ ...props, name });
|
|
599
|
+
return /* @__PURE__ */ React__namespace.createElement(
|
|
600
|
+
"input",
|
|
601
|
+
{
|
|
602
|
+
...rest,
|
|
603
|
+
type: "checkbox",
|
|
604
|
+
checked: !!value,
|
|
605
|
+
onChange: (e) => onChange(e.target.checked),
|
|
606
|
+
ref
|
|
607
|
+
}
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
);
|
|
611
|
+
|
|
612
|
+
exports.Checkbox = Checkbox;
|
|
613
|
+
exports.CheckboxGroupContext = CheckboxGroupContext;
|
|
614
|
+
exports.CheckboxGroupProvider = CheckboxGroupProvider;
|
|
615
|
+
exports.Field = Field;
|
|
616
|
+
exports.Form = Form;
|
|
617
|
+
exports.FormContext = FormContext;
|
|
618
|
+
exports.FormProvider = FormProvider;
|
|
619
|
+
exports.clearErrors = clearErrors;
|
|
620
|
+
exports.createForm = create;
|
|
621
|
+
exports.ensureValidate = ensureValidate;
|
|
622
|
+
exports.getError = getError;
|
|
623
|
+
exports.getErrorByPath = getErrorByPath;
|
|
624
|
+
exports.getErrors = getErrors;
|
|
625
|
+
exports.getFirstError = getFirstError;
|
|
626
|
+
exports.getValue = getValue;
|
|
627
|
+
exports.getValueByPath = getValueByPath;
|
|
628
|
+
exports.getValues = getValues;
|
|
629
|
+
exports.hasErrors = hasErrors;
|
|
630
|
+
exports.hasTouched = hasTouched;
|
|
631
|
+
exports.hasTouchedByPath = hasTouchedByPath;
|
|
632
|
+
exports.incrementSubmitCount = incrementSubmitCount;
|
|
633
|
+
exports.isDirty = isDirty;
|
|
634
|
+
exports.isTouched = isTouched;
|
|
635
|
+
exports.removeField = removeField;
|
|
636
|
+
exports.removeFieldByPath = removeFieldByPath;
|
|
637
|
+
exports.reset = reset;
|
|
638
|
+
exports.setError = setError;
|
|
639
|
+
exports.setErrorByPath = setErrorByPath;
|
|
640
|
+
exports.setInitialValues = setInitialValues;
|
|
641
|
+
exports.setIsSubmitting = setIsSubmitting;
|
|
642
|
+
exports.setSubmitSuccessful = setSubmitSuccessful;
|
|
643
|
+
exports.setTouched = setTouched;
|
|
644
|
+
exports.setTouchedByPath = setTouchedByPath;
|
|
645
|
+
exports.setValidatingByPath = setValidatingByPath;
|
|
646
|
+
exports.setValue = setValue;
|
|
647
|
+
exports.setValueByPath = setValueByPath;
|
|
648
|
+
exports.trigger = trigger;
|
|
649
|
+
exports.unsetValidatingByPath = unsetValidatingByPath;
|
|
650
|
+
exports.useCheckboxGroupContext = useCheckboxGroupContext;
|
|
651
|
+
exports.useError = useError;
|
|
652
|
+
exports.useErrorByPath = useErrorByPath;
|
|
653
|
+
exports.useField = useField;
|
|
654
|
+
exports.useFieldArray = useFieldArray;
|
|
655
|
+
exports.useForm = useForm;
|
|
656
|
+
exports.useFormContext = useFormContext;
|
|
657
|
+
exports.useHasErrors = useHasErrors;
|
|
658
|
+
exports.useIsDirty = useIsDirty;
|
|
659
|
+
exports.useIsSubmitting = useIsSubmitting;
|
|
660
|
+
exports.useSubmitCount = useSubmitCount;
|
|
661
|
+
exports.useTouched = useTouched;
|
|
662
|
+
exports.useTouchedByPath = useTouchedByPath;
|
|
663
|
+
exports.useValue = useValue;
|
|
664
|
+
exports.useValueByPath = useValueByPath;
|
|
665
|
+
exports.useWatch = useWatch;
|
|
666
|
+
exports.validate = validate;
|
|
667
|
+
//# sourceMappingURL=index.cjs.js.map
|