remix-validated-form 4.6.5 → 4.6.7
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 +10 -9
- package/dist/index.cjs.js +1930 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +337 -0
- package/dist/{remix-validated-form.es.js → index.esm.js} +603 -905
- package/dist/index.esm.js.map +1 -0
- package/package.json +14 -10
- package/src/ValidatedForm.tsx +17 -5
- package/src/hooks.ts +1 -1
- package/src/server.ts +1 -1
- package/tsup.config.ts +3 -0
- package/dist/remix-validated-form.cjs.js +0 -27
- package/dist/remix-validated-form.cjs.js.map +0 -1
- package/dist/remix-validated-form.es.js.map +0 -1
- package/dist/remix-validated-form.umd.js +0 -27
- package/dist/remix-validated-form.umd.js.map +0 -1
- package/dist/types/ValidatedForm.d.ts +0 -50
- package/dist/types/hooks.d.ts +0 -67
- package/dist/types/index.d.ts +0 -7
- package/dist/types/internal/MultiValueMap.d.ts +0 -11
- package/dist/types/internal/constants.d.ts +0 -3
- package/dist/types/internal/flatten.d.ts +0 -1
- package/dist/types/internal/formContext.d.ts +0 -12
- package/dist/types/internal/getInputProps.d.ts +0 -29
- package/dist/types/internal/hooks.d.ts +0 -35
- package/dist/types/internal/hydratable.d.ts +0 -14
- package/dist/types/internal/logic/getCheckboxChecked.d.ts +0 -1
- package/dist/types/internal/logic/getRadioChecked.d.ts +0 -1
- package/dist/types/internal/logic/requestSubmit.d.ts +0 -5
- package/dist/types/internal/state/arrayUtil.d.ts +0 -12
- package/dist/types/internal/state/controlledFields.d.ts +0 -7
- package/dist/types/internal/state/createFormStore.d.ts +0 -79
- package/dist/types/internal/state/fieldArray.d.ts +0 -28
- package/dist/types/internal/state/storeHooks.d.ts +0 -3
- package/dist/types/internal/state/types.d.ts +0 -1
- package/dist/types/internal/submissionCallbacks.d.ts +0 -1
- package/dist/types/internal/util.d.ts +0 -5
- package/dist/types/server.d.ts +0 -21
- package/dist/types/unreleased/formStateHooks.d.ts +0 -64
- package/dist/types/userFacingFormContext.d.ts +0 -85
- package/dist/types/validation/createValidator.d.ts +0 -7
- package/dist/types/validation/types.d.ts +0 -58
- package/vite.config.ts +0 -7
@@ -1,177 +1,11 @@
|
|
1
|
-
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
ar[i2] = from2[i2];
|
10
|
-
}
|
11
|
-
}
|
12
|
-
return to.concat(ar || Array.prototype.slice.call(from2));
|
13
|
-
};
|
14
|
-
function purry(fn2, args, lazy) {
|
15
|
-
var diff = fn2.length - args.length;
|
16
|
-
var arrayArgs = Array.from(args);
|
17
|
-
if (diff === 0) {
|
18
|
-
return fn2.apply(void 0, arrayArgs);
|
19
|
-
}
|
20
|
-
if (diff === 1) {
|
21
|
-
var ret = function(data) {
|
22
|
-
return fn2.apply(void 0, __spreadArray([data], arrayArgs, false));
|
23
|
-
};
|
24
|
-
if (lazy || fn2.lazy) {
|
25
|
-
ret.lazy = lazy || fn2.lazy;
|
26
|
-
ret.lazyArgs = args;
|
27
|
-
}
|
28
|
-
return ret;
|
29
|
-
}
|
30
|
-
throw new Error("Wrong number of arguments");
|
31
|
-
}
|
32
|
-
function _reduceLazy(array, lazy, indexed) {
|
33
|
-
return array.reduce(function(acc, item, index) {
|
34
|
-
var result = indexed ? lazy(item, index, array) : lazy(item);
|
35
|
-
if (result.hasMany === true) {
|
36
|
-
acc.push.apply(acc, result.next);
|
37
|
-
} else if (result.hasNext === true) {
|
38
|
-
acc.push(result.next);
|
39
|
-
}
|
40
|
-
return acc;
|
41
|
-
}, []);
|
42
|
-
}
|
43
|
-
var isArray = Array.isArray;
|
44
|
-
var keyList = Object.keys;
|
45
|
-
var hasProp = Object.prototype.hasOwnProperty;
|
46
|
-
function equals() {
|
47
|
-
return purry(_equals, arguments);
|
48
|
-
}
|
49
|
-
function _equals(a2, b2) {
|
50
|
-
if (a2 === b2) {
|
51
|
-
return true;
|
52
|
-
}
|
53
|
-
if (a2 && b2 && typeof a2 === "object" && typeof b2 === "object") {
|
54
|
-
var arrA = isArray(a2);
|
55
|
-
var arrB = isArray(b2);
|
56
|
-
var i2 = void 0;
|
57
|
-
var length = void 0;
|
58
|
-
var key = void 0;
|
59
|
-
if (arrA && arrB) {
|
60
|
-
length = a2.length;
|
61
|
-
if (length !== b2.length) {
|
62
|
-
return false;
|
63
|
-
}
|
64
|
-
for (i2 = length; i2-- !== 0; ) {
|
65
|
-
if (!equals(a2[i2], b2[i2])) {
|
66
|
-
return false;
|
67
|
-
}
|
68
|
-
}
|
69
|
-
return true;
|
70
|
-
}
|
71
|
-
if (arrA !== arrB) {
|
72
|
-
return false;
|
73
|
-
}
|
74
|
-
var dateA = a2 instanceof Date;
|
75
|
-
var dateB = b2 instanceof Date;
|
76
|
-
if (dateA !== dateB) {
|
77
|
-
return false;
|
78
|
-
}
|
79
|
-
if (dateA && dateB) {
|
80
|
-
return a2.getTime() === b2.getTime();
|
81
|
-
}
|
82
|
-
var regexpA = a2 instanceof RegExp;
|
83
|
-
var regexpB = b2 instanceof RegExp;
|
84
|
-
if (regexpA !== regexpB) {
|
85
|
-
return false;
|
86
|
-
}
|
87
|
-
if (regexpA && regexpB) {
|
88
|
-
return a2.toString() === b2.toString();
|
89
|
-
}
|
90
|
-
var keys = keyList(a2);
|
91
|
-
length = keys.length;
|
92
|
-
if (length !== keyList(b2).length) {
|
93
|
-
return false;
|
94
|
-
}
|
95
|
-
for (i2 = length; i2-- !== 0; ) {
|
96
|
-
if (!hasProp.call(b2, keys[i2])) {
|
97
|
-
return false;
|
98
|
-
}
|
99
|
-
}
|
100
|
-
for (i2 = length; i2-- !== 0; ) {
|
101
|
-
key = keys[i2];
|
102
|
-
if (!equals(a2[key], b2[key])) {
|
103
|
-
return false;
|
104
|
-
}
|
105
|
-
}
|
106
|
-
return true;
|
107
|
-
}
|
108
|
-
return a2 !== a2 && b2 !== b2;
|
109
|
-
}
|
110
|
-
function omit() {
|
111
|
-
return purry(_omit, arguments);
|
112
|
-
}
|
113
|
-
function _omit(object, names) {
|
114
|
-
var set = new Set(names);
|
115
|
-
return Object.entries(object).reduce(function(acc, _a) {
|
116
|
-
var name = _a[0], value = _a[1];
|
117
|
-
if (!set.has(name)) {
|
118
|
-
acc[name] = value;
|
119
|
-
}
|
120
|
-
return acc;
|
121
|
-
}, {});
|
122
|
-
}
|
123
|
-
function omitBy() {
|
124
|
-
return purry(_omitBy, arguments);
|
125
|
-
}
|
126
|
-
function _omitBy(object, fn2) {
|
127
|
-
return Object.keys(object).reduce(function(acc, key) {
|
128
|
-
if (!fn2(object[key], key)) {
|
129
|
-
acc[key] = object[key];
|
130
|
-
}
|
131
|
-
return acc;
|
132
|
-
}, {});
|
133
|
-
}
|
134
|
-
function pathOr() {
|
135
|
-
return purry(_pathOr, arguments);
|
136
|
-
}
|
137
|
-
function _pathOr(object, path, defaultValue) {
|
138
|
-
var current = object;
|
139
|
-
for (var _i = 0, path_1 = path; _i < path_1.length; _i++) {
|
140
|
-
var prop = path_1[_i];
|
141
|
-
if (current == null || current[prop] == null) {
|
142
|
-
return defaultValue;
|
143
|
-
}
|
144
|
-
current = current[prop];
|
145
|
-
}
|
146
|
-
return current;
|
147
|
-
}
|
148
|
-
function uniq() {
|
149
|
-
return purry(_uniq, arguments, uniq.lazy);
|
150
|
-
}
|
151
|
-
function _uniq(array) {
|
152
|
-
return _reduceLazy(array, uniq.lazy());
|
153
|
-
}
|
154
|
-
(function(uniq2) {
|
155
|
-
function lazy() {
|
156
|
-
var set = /* @__PURE__ */ new Set();
|
157
|
-
return function(value) {
|
158
|
-
if (set.has(value)) {
|
159
|
-
return {
|
160
|
-
done: false,
|
161
|
-
hasNext: false
|
162
|
-
};
|
163
|
-
}
|
164
|
-
set.add(value);
|
165
|
-
return {
|
166
|
-
done: false,
|
167
|
-
hasNext: true,
|
168
|
-
next: value
|
169
|
-
};
|
170
|
-
};
|
171
|
-
}
|
172
|
-
uniq2.lazy = lazy;
|
173
|
-
})(uniq || (uniq = {}));
|
174
|
-
const getCheckboxChecked = (checkboxValue = "on", newValue) => {
|
1
|
+
// src/hooks.ts
|
2
|
+
import { useEffect as useEffect2, useMemo } from "react";
|
3
|
+
|
4
|
+
// src/internal/getInputProps.ts
|
5
|
+
import * as R from "remeda";
|
6
|
+
|
7
|
+
// src/internal/logic/getCheckboxChecked.ts
|
8
|
+
var getCheckboxChecked = (checkboxValue = "on", newValue) => {
|
175
9
|
if (Array.isArray(newValue))
|
176
10
|
return newValue.some((val) => val === true || val === checkboxValue);
|
177
11
|
if (typeof newValue === "boolean")
|
@@ -180,17 +14,31 @@ const getCheckboxChecked = (checkboxValue = "on", newValue) => {
|
|
180
14
|
return newValue === checkboxValue;
|
181
15
|
return void 0;
|
182
16
|
};
|
183
|
-
|
17
|
+
|
18
|
+
// src/internal/logic/getRadioChecked.ts
|
19
|
+
var getRadioChecked = (radioValue = "on", newValue) => {
|
184
20
|
if (typeof newValue === "string")
|
185
21
|
return newValue === radioValue;
|
186
22
|
return void 0;
|
187
23
|
};
|
188
|
-
|
24
|
+
if (void 0) {
|
25
|
+
const { it, expect } = void 0;
|
26
|
+
it("getRadioChecked", () => {
|
27
|
+
expect(getRadioChecked("on", "on")).toBe(true);
|
28
|
+
expect(getRadioChecked("on", void 0)).toBe(void 0);
|
29
|
+
expect(getRadioChecked("trueValue", void 0)).toBe(void 0);
|
30
|
+
expect(getRadioChecked("trueValue", "bob")).toBe(false);
|
31
|
+
expect(getRadioChecked("trueValue", "trueValue")).toBe(true);
|
32
|
+
});
|
33
|
+
}
|
34
|
+
|
35
|
+
// src/internal/getInputProps.ts
|
36
|
+
var defaultValidationBehavior = {
|
189
37
|
initial: "onBlur",
|
190
38
|
whenTouched: "onChange",
|
191
39
|
whenSubmitted: "onChange"
|
192
40
|
};
|
193
|
-
|
41
|
+
var createGetInputProps = ({
|
194
42
|
clearError,
|
195
43
|
validate,
|
196
44
|
defaultValue,
|
@@ -232,19 +80,27 @@ const createGetInputProps = ({
|
|
232
80
|
} else if (props.value === void 0) {
|
233
81
|
inputProps.defaultValue = defaultValue;
|
234
82
|
}
|
235
|
-
return omitBy(inputProps, (value) => value === void 0);
|
83
|
+
return R.omitBy(inputProps, (value) => value === void 0);
|
236
84
|
};
|
237
85
|
};
|
238
|
-
|
86
|
+
|
87
|
+
// src/internal/hooks.ts
|
88
|
+
import { useActionData, useMatches, useTransition } from "@remix-run/react";
|
89
|
+
import { useCallback, useContext } from "react";
|
90
|
+
|
91
|
+
// ../set-get/src/stringToPathArray.ts
|
92
|
+
var stringToPathArray = (path) => {
|
239
93
|
if (path.length === 0)
|
240
94
|
return [];
|
241
95
|
const match = path.match(/^\[(.+?)\](.*)$/) || path.match(/^\.?([^\.\[\]]+)(.*)$/);
|
242
96
|
if (match) {
|
243
|
-
const [
|
97
|
+
const [_, key, rest] = match;
|
244
98
|
return [/^\d+$/.test(key) ? Number(key) : key, ...stringToPathArray(rest)];
|
245
99
|
}
|
246
100
|
return [path];
|
247
101
|
};
|
102
|
+
|
103
|
+
// ../set-get/src/setPath.ts
|
248
104
|
function setPath(object, path, defaultValue) {
|
249
105
|
return _setPathNormalized(object, stringToPathArray(path), defaultValue);
|
250
106
|
}
|
@@ -253,10 +109,10 @@ function _setPathNormalized(object, path, value) {
|
|
253
109
|
const leadingSegments = path.slice(0, -1);
|
254
110
|
const lastSegment = path[path.length - 1];
|
255
111
|
let obj = object;
|
256
|
-
for (let
|
257
|
-
const segment = leadingSegments[
|
112
|
+
for (let i = 0; i < leadingSegments.length; i++) {
|
113
|
+
const segment = leadingSegments[i];
|
258
114
|
if (obj[segment] === void 0) {
|
259
|
-
const nextSegment = (_a = leadingSegments[
|
115
|
+
const nextSegment = (_a = leadingSegments[i + 1]) != null ? _a : lastSegment;
|
260
116
|
obj[segment] = typeof nextSegment === "number" ? [] : {};
|
261
117
|
}
|
262
118
|
obj = obj[segment];
|
@@ -264,546 +120,48 @@ function _setPathNormalized(object, path, value) {
|
|
264
120
|
obj[lastSegment] = value;
|
265
121
|
return object;
|
266
122
|
}
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
}
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
123
|
+
|
124
|
+
// ../set-get/src/getPath.ts
|
125
|
+
import get from "lodash.get";
|
126
|
+
var getPath = (object, path) => {
|
127
|
+
return get(object, path);
|
128
|
+
};
|
129
|
+
|
130
|
+
// src/internal/hooks.ts
|
131
|
+
import invariant3 from "tiny-invariant";
|
132
|
+
|
133
|
+
// src/internal/constants.ts
|
134
|
+
var FORM_ID_FIELD = "__rvfInternalFormId";
|
135
|
+
var FORM_DEFAULTS_FIELD = "__rvfInternalFormDefaults";
|
136
|
+
var formDefaultValuesKey = (formId) => `${FORM_DEFAULTS_FIELD}_${formId}`;
|
137
|
+
|
138
|
+
// src/internal/formContext.ts
|
139
|
+
import { createContext } from "react";
|
140
|
+
var InternalFormContext = createContext(null);
|
141
|
+
|
142
|
+
// src/internal/hydratable.ts
|
143
|
+
var serverData = (data) => ({
|
286
144
|
hydrateTo: () => data,
|
287
|
-
map: (
|
145
|
+
map: (fn) => serverData(fn(data))
|
288
146
|
});
|
289
|
-
|
147
|
+
var hydratedData = () => ({
|
290
148
|
hydrateTo: (hydratedData2) => hydratedData2,
|
291
149
|
map: () => hydratedData()
|
292
150
|
});
|
293
|
-
|
294
|
-
|
151
|
+
var from = (data, hydrated) => hydrated ? hydratedData() : serverData(data);
|
152
|
+
var hydratable = {
|
295
153
|
serverData,
|
296
154
|
hydratedData,
|
297
155
|
from
|
298
156
|
};
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
listeners.forEach((listener) => listener(state, previousState));
|
308
|
-
}
|
309
|
-
};
|
310
|
-
const getState = () => state;
|
311
|
-
const subscribe = (listener) => {
|
312
|
-
listeners.add(listener);
|
313
|
-
return () => listeners.delete(listener);
|
314
|
-
};
|
315
|
-
const destroy = () => listeners.clear();
|
316
|
-
const api = { setState, getState, subscribe, destroy };
|
317
|
-
state = createState(setState, getState, api);
|
318
|
-
return api;
|
319
|
-
};
|
320
|
-
const createStore = (createState) => createState ? createStoreImpl(createState) : createStoreImpl;
|
321
|
-
var withSelector = { exports: {} };
|
322
|
-
var withSelector_production_min = {};
|
323
|
-
var shim = { exports: {} };
|
324
|
-
var useSyncExternalStoreShim_production_min = {};
|
325
|
-
/**
|
326
|
-
* @license React
|
327
|
-
* use-sync-external-store-shim.production.min.js
|
328
|
-
*
|
329
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
330
|
-
*
|
331
|
-
* This source code is licensed under the MIT license found in the
|
332
|
-
* LICENSE file in the root directory of this source tree.
|
333
|
-
*/
|
334
|
-
var e = React;
|
335
|
-
function h$2(a2, b2) {
|
336
|
-
return a2 === b2 && (0 !== a2 || 1 / a2 === 1 / b2) || a2 !== a2 && b2 !== b2;
|
337
|
-
}
|
338
|
-
var k$1 = "function" === typeof Object.is ? Object.is : h$2, l$1 = e.useState, m = e.useEffect, n$2 = e.useLayoutEffect, p$2 = e.useDebugValue;
|
339
|
-
function q$2(a2, b2) {
|
340
|
-
var d2 = b2(), f2 = l$1({ inst: { value: d2, getSnapshot: b2 } }), c2 = f2[0].inst, g2 = f2[1];
|
341
|
-
n$2(function() {
|
342
|
-
c2.value = d2;
|
343
|
-
c2.getSnapshot = b2;
|
344
|
-
r$2(c2) && g2({ inst: c2 });
|
345
|
-
}, [a2, d2, b2]);
|
346
|
-
m(function() {
|
347
|
-
r$2(c2) && g2({ inst: c2 });
|
348
|
-
return a2(function() {
|
349
|
-
r$2(c2) && g2({ inst: c2 });
|
350
|
-
});
|
351
|
-
}, [a2]);
|
352
|
-
p$2(d2);
|
353
|
-
return d2;
|
354
|
-
}
|
355
|
-
function r$2(a2) {
|
356
|
-
var b2 = a2.getSnapshot;
|
357
|
-
a2 = a2.value;
|
358
|
-
try {
|
359
|
-
var d2 = b2();
|
360
|
-
return !k$1(a2, d2);
|
361
|
-
} catch (f2) {
|
362
|
-
return true;
|
363
|
-
}
|
364
|
-
}
|
365
|
-
function t$2(a2, b2) {
|
366
|
-
return b2();
|
367
|
-
}
|
368
|
-
var u$2 = "undefined" === typeof window || "undefined" === typeof window.document || "undefined" === typeof window.document.createElement ? t$2 : q$2;
|
369
|
-
useSyncExternalStoreShim_production_min.useSyncExternalStore = void 0 !== e.useSyncExternalStore ? e.useSyncExternalStore : u$2;
|
370
|
-
{
|
371
|
-
shim.exports = useSyncExternalStoreShim_production_min;
|
372
|
-
}
|
373
|
-
/**
|
374
|
-
* @license React
|
375
|
-
* use-sync-external-store-shim/with-selector.production.min.js
|
376
|
-
*
|
377
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
378
|
-
*
|
379
|
-
* This source code is licensed under the MIT license found in the
|
380
|
-
* LICENSE file in the root directory of this source tree.
|
381
|
-
*/
|
382
|
-
var h$1 = React, n$1 = shim.exports;
|
383
|
-
function p$1(a2, b2) {
|
384
|
-
return a2 === b2 && (0 !== a2 || 1 / a2 === 1 / b2) || a2 !== a2 && b2 !== b2;
|
385
|
-
}
|
386
|
-
var q$1 = "function" === typeof Object.is ? Object.is : p$1, r$1 = n$1.useSyncExternalStore, t$1 = h$1.useRef, u$1 = h$1.useEffect, v$1 = h$1.useMemo, w$1 = h$1.useDebugValue;
|
387
|
-
withSelector_production_min.useSyncExternalStoreWithSelector = function(a2, b2, e2, l2, g2) {
|
388
|
-
var c2 = t$1(null);
|
389
|
-
if (null === c2.current) {
|
390
|
-
var f2 = { hasValue: false, value: null };
|
391
|
-
c2.current = f2;
|
392
|
-
} else
|
393
|
-
f2 = c2.current;
|
394
|
-
c2 = v$1(function() {
|
395
|
-
function a3(a4) {
|
396
|
-
if (!c3) {
|
397
|
-
c3 = true;
|
398
|
-
d3 = a4;
|
399
|
-
a4 = l2(a4);
|
400
|
-
if (void 0 !== g2 && f2.hasValue) {
|
401
|
-
var b3 = f2.value;
|
402
|
-
if (g2(b3, a4))
|
403
|
-
return k2 = b3;
|
404
|
-
}
|
405
|
-
return k2 = a4;
|
406
|
-
}
|
407
|
-
b3 = k2;
|
408
|
-
if (q$1(d3, a4))
|
409
|
-
return b3;
|
410
|
-
var e3 = l2(a4);
|
411
|
-
if (void 0 !== g2 && g2(b3, e3))
|
412
|
-
return b3;
|
413
|
-
d3 = a4;
|
414
|
-
return k2 = e3;
|
415
|
-
}
|
416
|
-
var c3 = false, d3, k2, m2 = void 0 === e2 ? null : e2;
|
417
|
-
return [function() {
|
418
|
-
return a3(b2());
|
419
|
-
}, null === m2 ? void 0 : function() {
|
420
|
-
return a3(m2());
|
421
|
-
}];
|
422
|
-
}, [b2, e2, l2, g2]);
|
423
|
-
var d2 = r$1(a2, c2[0], c2[1]);
|
424
|
-
u$1(function() {
|
425
|
-
f2.hasValue = true;
|
426
|
-
f2.value = d2;
|
427
|
-
}, [d2]);
|
428
|
-
w$1(d2);
|
429
|
-
return d2;
|
430
|
-
};
|
431
|
-
{
|
432
|
-
withSelector.exports = withSelector_production_min;
|
433
|
-
}
|
434
|
-
var useSyncExternalStoreExports = withSelector.exports;
|
435
|
-
const { useSyncExternalStoreWithSelector } = useSyncExternalStoreExports;
|
436
|
-
function useStore(api, selector = api.getState, equalityFn) {
|
437
|
-
const slice = useSyncExternalStoreWithSelector(
|
438
|
-
api.subscribe,
|
439
|
-
api.getState,
|
440
|
-
api.getServerState || api.getState,
|
441
|
-
selector,
|
442
|
-
equalityFn
|
443
|
-
);
|
444
|
-
useDebugValue(slice);
|
445
|
-
return slice;
|
446
|
-
}
|
447
|
-
const createImpl = (createState) => {
|
448
|
-
const api = typeof createState === "function" ? createStore(createState) : createState;
|
449
|
-
const useBoundStore = (selector, equalityFn) => useStore(api, selector, equalityFn);
|
450
|
-
Object.assign(useBoundStore, api);
|
451
|
-
return useBoundStore;
|
452
|
-
};
|
453
|
-
const create = (createState) => createState ? createImpl(createState) : createImpl;
|
454
|
-
function n(n2) {
|
455
|
-
for (var r2 = arguments.length, t2 = Array(r2 > 1 ? r2 - 1 : 0), e2 = 1; e2 < r2; e2++)
|
456
|
-
t2[e2 - 1] = arguments[e2];
|
457
|
-
throw Error("[Immer] minified error nr: " + n2 + (t2.length ? " " + t2.map(function(n3) {
|
458
|
-
return "'" + n3 + "'";
|
459
|
-
}).join(",") : "") + ". Find the full error at: https://bit.ly/3cXEKWf");
|
460
|
-
}
|
461
|
-
function r(n2) {
|
462
|
-
return !!n2 && !!n2[Q];
|
463
|
-
}
|
464
|
-
function t(n2) {
|
465
|
-
var r2;
|
466
|
-
return !!n2 && (function(n3) {
|
467
|
-
if (!n3 || "object" != typeof n3)
|
468
|
-
return false;
|
469
|
-
var r3 = Object.getPrototypeOf(n3);
|
470
|
-
if (null === r3)
|
471
|
-
return true;
|
472
|
-
var t2 = Object.hasOwnProperty.call(r3, "constructor") && r3.constructor;
|
473
|
-
return t2 === Object || "function" == typeof t2 && Function.toString.call(t2) === Z;
|
474
|
-
}(n2) || Array.isArray(n2) || !!n2[L] || !!(null === (r2 = n2.constructor) || void 0 === r2 ? void 0 : r2[L]) || s(n2) || v(n2));
|
475
|
-
}
|
476
|
-
function i(n2, r2, t2) {
|
477
|
-
void 0 === t2 && (t2 = false), 0 === o(n2) ? (t2 ? Object.keys : nn)(n2).forEach(function(e2) {
|
478
|
-
t2 && "symbol" == typeof e2 || r2(e2, n2[e2], n2);
|
479
|
-
}) : n2.forEach(function(t3, e2) {
|
480
|
-
return r2(e2, t3, n2);
|
481
|
-
});
|
482
|
-
}
|
483
|
-
function o(n2) {
|
484
|
-
var r2 = n2[Q];
|
485
|
-
return r2 ? r2.i > 3 ? r2.i - 4 : r2.i : Array.isArray(n2) ? 1 : s(n2) ? 2 : v(n2) ? 3 : 0;
|
486
|
-
}
|
487
|
-
function u(n2, r2) {
|
488
|
-
return 2 === o(n2) ? n2.has(r2) : Object.prototype.hasOwnProperty.call(n2, r2);
|
489
|
-
}
|
490
|
-
function a(n2, r2) {
|
491
|
-
return 2 === o(n2) ? n2.get(r2) : n2[r2];
|
492
|
-
}
|
493
|
-
function f(n2, r2, t2) {
|
494
|
-
var e2 = o(n2);
|
495
|
-
2 === e2 ? n2.set(r2, t2) : 3 === e2 ? (n2.delete(r2), n2.add(t2)) : n2[r2] = t2;
|
496
|
-
}
|
497
|
-
function c(n2, r2) {
|
498
|
-
return n2 === r2 ? 0 !== n2 || 1 / n2 == 1 / r2 : n2 != n2 && r2 != r2;
|
499
|
-
}
|
500
|
-
function s(n2) {
|
501
|
-
return X && n2 instanceof Map;
|
502
|
-
}
|
503
|
-
function v(n2) {
|
504
|
-
return q && n2 instanceof Set;
|
505
|
-
}
|
506
|
-
function p(n2) {
|
507
|
-
return n2.o || n2.t;
|
508
|
-
}
|
509
|
-
function l(n2) {
|
510
|
-
if (Array.isArray(n2))
|
511
|
-
return Array.prototype.slice.call(n2);
|
512
|
-
var r2 = rn(n2);
|
513
|
-
delete r2[Q];
|
514
|
-
for (var t2 = nn(r2), e2 = 0; e2 < t2.length; e2++) {
|
515
|
-
var i2 = t2[e2], o2 = r2[i2];
|
516
|
-
false === o2.writable && (o2.writable = true, o2.configurable = true), (o2.get || o2.set) && (r2[i2] = { configurable: true, writable: true, enumerable: o2.enumerable, value: n2[i2] });
|
517
|
-
}
|
518
|
-
return Object.create(Object.getPrototypeOf(n2), r2);
|
519
|
-
}
|
520
|
-
function d(n2, e2) {
|
521
|
-
return void 0 === e2 && (e2 = false), y(n2) || r(n2) || !t(n2) ? n2 : (o(n2) > 1 && (n2.set = n2.add = n2.clear = n2.delete = h), Object.freeze(n2), e2 && i(n2, function(n3, r2) {
|
522
|
-
return d(r2, true);
|
523
|
-
}, true), n2);
|
524
|
-
}
|
525
|
-
function h() {
|
526
|
-
n(2);
|
527
|
-
}
|
528
|
-
function y(n2) {
|
529
|
-
return null == n2 || "object" != typeof n2 || Object.isFrozen(n2);
|
530
|
-
}
|
531
|
-
function b(r2) {
|
532
|
-
var t2 = tn[r2];
|
533
|
-
return t2 || n(18, r2), t2;
|
534
|
-
}
|
535
|
-
function _() {
|
536
|
-
return U;
|
537
|
-
}
|
538
|
-
function j(n2, r2) {
|
539
|
-
r2 && (b("Patches"), n2.u = [], n2.s = [], n2.v = r2);
|
540
|
-
}
|
541
|
-
function O(n2) {
|
542
|
-
g(n2), n2.p.forEach(S), n2.p = null;
|
543
|
-
}
|
544
|
-
function g(n2) {
|
545
|
-
n2 === U && (U = n2.l);
|
546
|
-
}
|
547
|
-
function w(n2) {
|
548
|
-
return U = { p: [], l: U, h: n2, m: true, _: 0 };
|
549
|
-
}
|
550
|
-
function S(n2) {
|
551
|
-
var r2 = n2[Q];
|
552
|
-
0 === r2.i || 1 === r2.i ? r2.j() : r2.O = true;
|
553
|
-
}
|
554
|
-
function P(r2, e2) {
|
555
|
-
e2._ = e2.p.length;
|
556
|
-
var i2 = e2.p[0], o2 = void 0 !== r2 && r2 !== i2;
|
557
|
-
return e2.h.g || b("ES5").S(e2, r2, o2), o2 ? (i2[Q].P && (O(e2), n(4)), t(r2) && (r2 = M(e2, r2), e2.l || x(e2, r2)), e2.u && b("Patches").M(i2[Q].t, r2, e2.u, e2.s)) : r2 = M(e2, i2, []), O(e2), e2.u && e2.v(e2.u, e2.s), r2 !== H ? r2 : void 0;
|
558
|
-
}
|
559
|
-
function M(n2, r2, t2) {
|
560
|
-
if (y(r2))
|
561
|
-
return r2;
|
562
|
-
var e2 = r2[Q];
|
563
|
-
if (!e2)
|
564
|
-
return i(r2, function(i2, o3) {
|
565
|
-
return A(n2, e2, r2, i2, o3, t2);
|
566
|
-
}, true), r2;
|
567
|
-
if (e2.A !== n2)
|
568
|
-
return r2;
|
569
|
-
if (!e2.P)
|
570
|
-
return x(n2, e2.t, true), e2.t;
|
571
|
-
if (!e2.I) {
|
572
|
-
e2.I = true, e2.A._--;
|
573
|
-
var o2 = 4 === e2.i || 5 === e2.i ? e2.o = l(e2.k) : e2.o;
|
574
|
-
i(3 === e2.i ? new Set(o2) : o2, function(r3, i2) {
|
575
|
-
return A(n2, e2, o2, r3, i2, t2);
|
576
|
-
}), x(n2, o2, false), t2 && n2.u && b("Patches").R(e2, t2, n2.u, n2.s);
|
577
|
-
}
|
578
|
-
return e2.o;
|
579
|
-
}
|
580
|
-
function A(e2, i2, o2, a2, c2, s2) {
|
581
|
-
if (r(c2)) {
|
582
|
-
var v2 = M(e2, c2, s2 && i2 && 3 !== i2.i && !u(i2.D, a2) ? s2.concat(a2) : void 0);
|
583
|
-
if (f(o2, a2, v2), !r(v2))
|
584
|
-
return;
|
585
|
-
e2.m = false;
|
586
|
-
}
|
587
|
-
if (t(c2) && !y(c2)) {
|
588
|
-
if (!e2.h.F && e2._ < 1)
|
589
|
-
return;
|
590
|
-
M(e2, c2), i2 && i2.A.l || x(e2, c2);
|
591
|
-
}
|
592
|
-
}
|
593
|
-
function x(n2, r2, t2) {
|
594
|
-
void 0 === t2 && (t2 = false), n2.h.F && n2.m && d(r2, t2);
|
595
|
-
}
|
596
|
-
function z(n2, r2) {
|
597
|
-
var t2 = n2[Q];
|
598
|
-
return (t2 ? p(t2) : n2)[r2];
|
599
|
-
}
|
600
|
-
function I(n2, r2) {
|
601
|
-
if (r2 in n2)
|
602
|
-
for (var t2 = Object.getPrototypeOf(n2); t2; ) {
|
603
|
-
var e2 = Object.getOwnPropertyDescriptor(t2, r2);
|
604
|
-
if (e2)
|
605
|
-
return e2;
|
606
|
-
t2 = Object.getPrototypeOf(t2);
|
607
|
-
}
|
608
|
-
}
|
609
|
-
function k(n2) {
|
610
|
-
n2.P || (n2.P = true, n2.l && k(n2.l));
|
611
|
-
}
|
612
|
-
function E(n2) {
|
613
|
-
n2.o || (n2.o = l(n2.t));
|
614
|
-
}
|
615
|
-
function R(n2, r2, t2) {
|
616
|
-
var e2 = s(r2) ? b("MapSet").N(r2, t2) : v(r2) ? b("MapSet").T(r2, t2) : n2.g ? function(n3, r3) {
|
617
|
-
var t3 = Array.isArray(n3), e3 = { i: t3 ? 1 : 0, A: r3 ? r3.A : _(), P: false, I: false, D: {}, l: r3, t: n3, k: null, o: null, j: null, C: false }, i2 = e3, o2 = en;
|
618
|
-
t3 && (i2 = [e3], o2 = on);
|
619
|
-
var u2 = Proxy.revocable(i2, o2), a2 = u2.revoke, f2 = u2.proxy;
|
620
|
-
return e3.k = f2, e3.j = a2, f2;
|
621
|
-
}(r2, t2) : b("ES5").J(r2, t2);
|
622
|
-
return (t2 ? t2.A : _()).p.push(e2), e2;
|
623
|
-
}
|
624
|
-
function D(e2) {
|
625
|
-
return r(e2) || n(22, e2), function n2(r2) {
|
626
|
-
if (!t(r2))
|
627
|
-
return r2;
|
628
|
-
var e3, u2 = r2[Q], c2 = o(r2);
|
629
|
-
if (u2) {
|
630
|
-
if (!u2.P && (u2.i < 4 || !b("ES5").K(u2)))
|
631
|
-
return u2.t;
|
632
|
-
u2.I = true, e3 = F(r2, c2), u2.I = false;
|
633
|
-
} else
|
634
|
-
e3 = F(r2, c2);
|
635
|
-
return i(e3, function(r3, t2) {
|
636
|
-
u2 && a(u2.t, r3) === t2 || f(e3, r3, n2(t2));
|
637
|
-
}), 3 === c2 ? new Set(e3) : e3;
|
638
|
-
}(e2);
|
639
|
-
}
|
640
|
-
function F(n2, r2) {
|
641
|
-
switch (r2) {
|
642
|
-
case 2:
|
643
|
-
return new Map(n2);
|
644
|
-
case 3:
|
645
|
-
return Array.from(n2);
|
646
|
-
}
|
647
|
-
return l(n2);
|
648
|
-
}
|
649
|
-
var G, U, W = "undefined" != typeof Symbol && "symbol" == typeof Symbol("x"), X = "undefined" != typeof Map, q = "undefined" != typeof Set, B = "undefined" != typeof Proxy && void 0 !== Proxy.revocable && "undefined" != typeof Reflect, H = W ? Symbol.for("immer-nothing") : ((G = {})["immer-nothing"] = true, G), L = W ? Symbol.for("immer-draftable") : "__$immer_draftable", Q = W ? Symbol.for("immer-state") : "__$immer_state", Z = "" + Object.prototype.constructor, nn = "undefined" != typeof Reflect && Reflect.ownKeys ? Reflect.ownKeys : void 0 !== Object.getOwnPropertySymbols ? function(n2) {
|
650
|
-
return Object.getOwnPropertyNames(n2).concat(Object.getOwnPropertySymbols(n2));
|
651
|
-
} : Object.getOwnPropertyNames, rn = Object.getOwnPropertyDescriptors || function(n2) {
|
652
|
-
var r2 = {};
|
653
|
-
return nn(n2).forEach(function(t2) {
|
654
|
-
r2[t2] = Object.getOwnPropertyDescriptor(n2, t2);
|
655
|
-
}), r2;
|
656
|
-
}, tn = {}, en = { get: function(n2, r2) {
|
657
|
-
if (r2 === Q)
|
658
|
-
return n2;
|
659
|
-
var e2 = p(n2);
|
660
|
-
if (!u(e2, r2))
|
661
|
-
return function(n3, r3, t2) {
|
662
|
-
var e3, i3 = I(r3, t2);
|
663
|
-
return i3 ? "value" in i3 ? i3.value : null === (e3 = i3.get) || void 0 === e3 ? void 0 : e3.call(n3.k) : void 0;
|
664
|
-
}(n2, e2, r2);
|
665
|
-
var i2 = e2[r2];
|
666
|
-
return n2.I || !t(i2) ? i2 : i2 === z(n2.t, r2) ? (E(n2), n2.o[r2] = R(n2.A.h, i2, n2)) : i2;
|
667
|
-
}, has: function(n2, r2) {
|
668
|
-
return r2 in p(n2);
|
669
|
-
}, ownKeys: function(n2) {
|
670
|
-
return Reflect.ownKeys(p(n2));
|
671
|
-
}, set: function(n2, r2, t2) {
|
672
|
-
var e2 = I(p(n2), r2);
|
673
|
-
if (null == e2 ? void 0 : e2.set)
|
674
|
-
return e2.set.call(n2.k, t2), true;
|
675
|
-
if (!n2.P) {
|
676
|
-
var i2 = z(p(n2), r2), o2 = null == i2 ? void 0 : i2[Q];
|
677
|
-
if (o2 && o2.t === t2)
|
678
|
-
return n2.o[r2] = t2, n2.D[r2] = false, true;
|
679
|
-
if (c(t2, i2) && (void 0 !== t2 || u(n2.t, r2)))
|
680
|
-
return true;
|
681
|
-
E(n2), k(n2);
|
682
|
-
}
|
683
|
-
return n2.o[r2] === t2 && "number" != typeof t2 && (void 0 !== t2 || r2 in n2.o) || (n2.o[r2] = t2, n2.D[r2] = true, true);
|
684
|
-
}, deleteProperty: function(n2, r2) {
|
685
|
-
return void 0 !== z(n2.t, r2) || r2 in n2.t ? (n2.D[r2] = false, E(n2), k(n2)) : delete n2.D[r2], n2.o && delete n2.o[r2], true;
|
686
|
-
}, getOwnPropertyDescriptor: function(n2, r2) {
|
687
|
-
var t2 = p(n2), e2 = Reflect.getOwnPropertyDescriptor(t2, r2);
|
688
|
-
return e2 ? { writable: true, configurable: 1 !== n2.i || "length" !== r2, enumerable: e2.enumerable, value: t2[r2] } : e2;
|
689
|
-
}, defineProperty: function() {
|
690
|
-
n(11);
|
691
|
-
}, getPrototypeOf: function(n2) {
|
692
|
-
return Object.getPrototypeOf(n2.t);
|
693
|
-
}, setPrototypeOf: function() {
|
694
|
-
n(12);
|
695
|
-
} }, on = {};
|
696
|
-
i(en, function(n2, r2) {
|
697
|
-
on[n2] = function() {
|
698
|
-
return arguments[0] = arguments[0][0], r2.apply(this, arguments);
|
699
|
-
};
|
700
|
-
}), on.deleteProperty = function(r2, t2) {
|
701
|
-
return on.set.call(this, r2, t2, void 0);
|
702
|
-
}, on.set = function(r2, t2, e2) {
|
703
|
-
return en.set.call(this, r2[0], t2, e2, r2[0]);
|
704
|
-
};
|
705
|
-
var un = function() {
|
706
|
-
function e2(r2) {
|
707
|
-
var e3 = this;
|
708
|
-
this.g = B, this.F = true, this.produce = function(r3, i3, o2) {
|
709
|
-
if ("function" == typeof r3 && "function" != typeof i3) {
|
710
|
-
var u2 = i3;
|
711
|
-
i3 = r3;
|
712
|
-
var a2 = e3;
|
713
|
-
return function(n2) {
|
714
|
-
var r4 = this;
|
715
|
-
void 0 === n2 && (n2 = u2);
|
716
|
-
for (var t2 = arguments.length, e4 = Array(t2 > 1 ? t2 - 1 : 0), o3 = 1; o3 < t2; o3++)
|
717
|
-
e4[o3 - 1] = arguments[o3];
|
718
|
-
return a2.produce(n2, function(n3) {
|
719
|
-
var t3;
|
720
|
-
return (t3 = i3).call.apply(t3, [r4, n3].concat(e4));
|
721
|
-
});
|
722
|
-
};
|
723
|
-
}
|
724
|
-
var f2;
|
725
|
-
if ("function" != typeof i3 && n(6), void 0 !== o2 && "function" != typeof o2 && n(7), t(r3)) {
|
726
|
-
var c2 = w(e3), s2 = R(e3, r3, void 0), v2 = true;
|
727
|
-
try {
|
728
|
-
f2 = i3(s2), v2 = false;
|
729
|
-
} finally {
|
730
|
-
v2 ? O(c2) : g(c2);
|
731
|
-
}
|
732
|
-
return "undefined" != typeof Promise && f2 instanceof Promise ? f2.then(function(n2) {
|
733
|
-
return j(c2, o2), P(n2, c2);
|
734
|
-
}, function(n2) {
|
735
|
-
throw O(c2), n2;
|
736
|
-
}) : (j(c2, o2), P(f2, c2));
|
737
|
-
}
|
738
|
-
if (!r3 || "object" != typeof r3) {
|
739
|
-
if (void 0 === (f2 = i3(r3)) && (f2 = r3), f2 === H && (f2 = void 0), e3.F && d(f2, true), o2) {
|
740
|
-
var p2 = [], l2 = [];
|
741
|
-
b("Patches").M(r3, f2, p2, l2), o2(p2, l2);
|
742
|
-
}
|
743
|
-
return f2;
|
744
|
-
}
|
745
|
-
n(21, r3);
|
746
|
-
}, this.produceWithPatches = function(n2, r3) {
|
747
|
-
if ("function" == typeof n2)
|
748
|
-
return function(r4) {
|
749
|
-
for (var t3 = arguments.length, i4 = Array(t3 > 1 ? t3 - 1 : 0), o3 = 1; o3 < t3; o3++)
|
750
|
-
i4[o3 - 1] = arguments[o3];
|
751
|
-
return e3.produceWithPatches(r4, function(r5) {
|
752
|
-
return n2.apply(void 0, [r5].concat(i4));
|
753
|
-
});
|
754
|
-
};
|
755
|
-
var t2, i3, o2 = e3.produce(n2, r3, function(n3, r4) {
|
756
|
-
t2 = n3, i3 = r4;
|
757
|
-
});
|
758
|
-
return "undefined" != typeof Promise && o2 instanceof Promise ? o2.then(function(n3) {
|
759
|
-
return [n3, t2, i3];
|
760
|
-
}) : [o2, t2, i3];
|
761
|
-
}, "boolean" == typeof (null == r2 ? void 0 : r2.useProxies) && this.setUseProxies(r2.useProxies), "boolean" == typeof (null == r2 ? void 0 : r2.autoFreeze) && this.setAutoFreeze(r2.autoFreeze);
|
762
|
-
}
|
763
|
-
var i2 = e2.prototype;
|
764
|
-
return i2.createDraft = function(e3) {
|
765
|
-
t(e3) || n(8), r(e3) && (e3 = D(e3));
|
766
|
-
var i3 = w(this), o2 = R(this, e3, void 0);
|
767
|
-
return o2[Q].C = true, g(i3), o2;
|
768
|
-
}, i2.finishDraft = function(r2, t2) {
|
769
|
-
var e3 = r2 && r2[Q];
|
770
|
-
var i3 = e3.A;
|
771
|
-
return j(i3, t2), P(void 0, i3);
|
772
|
-
}, i2.setAutoFreeze = function(n2) {
|
773
|
-
this.F = n2;
|
774
|
-
}, i2.setUseProxies = function(r2) {
|
775
|
-
r2 && !B && n(20), this.g = r2;
|
776
|
-
}, i2.applyPatches = function(n2, t2) {
|
777
|
-
var e3;
|
778
|
-
for (e3 = t2.length - 1; e3 >= 0; e3--) {
|
779
|
-
var i3 = t2[e3];
|
780
|
-
if (0 === i3.path.length && "replace" === i3.op) {
|
781
|
-
n2 = i3.value;
|
782
|
-
break;
|
783
|
-
}
|
784
|
-
}
|
785
|
-
e3 > -1 && (t2 = t2.slice(e3 + 1));
|
786
|
-
var o2 = b("Patches").$;
|
787
|
-
return r(n2) ? o2(n2, t2) : this.produce(n2, function(n3) {
|
788
|
-
return o2(n3, t2);
|
789
|
-
});
|
790
|
-
}, e2;
|
791
|
-
}(), an = new un(), fn = an.produce;
|
792
|
-
an.produceWithPatches.bind(an);
|
793
|
-
an.setAutoFreeze.bind(an);
|
794
|
-
an.setUseProxies.bind(an);
|
795
|
-
an.applyPatches.bind(an);
|
796
|
-
an.createDraft.bind(an);
|
797
|
-
an.finishDraft.bind(an);
|
798
|
-
const immerImpl = (initializer) => (set, get, store) => {
|
799
|
-
store.setState = (updater, replace2, ...a2) => {
|
800
|
-
const nextState = typeof updater === "function" ? fn(updater) : updater;
|
801
|
-
return set(nextState, replace2, ...a2);
|
802
|
-
};
|
803
|
-
return initializer(store.setState, get, store);
|
804
|
-
};
|
805
|
-
const immer = immerImpl;
|
806
|
-
const requestSubmit = (element, submitter) => {
|
157
|
+
|
158
|
+
// src/internal/state/createFormStore.ts
|
159
|
+
import invariant2 from "tiny-invariant";
|
160
|
+
import create from "zustand";
|
161
|
+
import { immer } from "zustand/middleware/immer";
|
162
|
+
|
163
|
+
// src/internal/logic/requestSubmit.ts
|
164
|
+
var requestSubmit = (element, submitter) => {
|
807
165
|
if (typeof Object.getPrototypeOf(element).requestSubmit === "function" && true) {
|
808
166
|
element.requestSubmit(submitter);
|
809
167
|
return;
|
@@ -842,7 +200,38 @@ function raise(errorConstructor, message, name) {
|
|
842
200
|
name
|
843
201
|
);
|
844
202
|
}
|
845
|
-
|
203
|
+
if (void 0) {
|
204
|
+
const { it, expect } = void 0;
|
205
|
+
it("should validate the submitter", () => {
|
206
|
+
const form = document.createElement("form");
|
207
|
+
document.body.appendChild(form);
|
208
|
+
const submitter = document.createElement("input");
|
209
|
+
expect(() => validateSubmitter(null, null)).toThrow();
|
210
|
+
expect(() => validateSubmitter(form, null)).toThrow();
|
211
|
+
expect(() => validateSubmitter(form, submitter)).toThrow();
|
212
|
+
expect(
|
213
|
+
() => validateSubmitter(form, document.createElement("div"))
|
214
|
+
).toThrow();
|
215
|
+
submitter.type = "submit";
|
216
|
+
expect(() => validateSubmitter(form, submitter)).toThrow();
|
217
|
+
form.appendChild(submitter);
|
218
|
+
expect(() => validateSubmitter(form, submitter)).not.toThrow();
|
219
|
+
form.removeChild(submitter);
|
220
|
+
expect(() => validateSubmitter(form, submitter)).toThrow();
|
221
|
+
document.body.appendChild(submitter);
|
222
|
+
form.id = "test-form";
|
223
|
+
submitter.setAttribute("form", "test-form");
|
224
|
+
expect(() => validateSubmitter(form, submitter)).not.toThrow();
|
225
|
+
const button = document.createElement("button");
|
226
|
+
button.type = "submit";
|
227
|
+
form.appendChild(button);
|
228
|
+
expect(() => validateSubmitter(form, button)).not.toThrow();
|
229
|
+
});
|
230
|
+
}
|
231
|
+
|
232
|
+
// src/internal/state/arrayUtil.ts
|
233
|
+
import invariant from "tiny-invariant";
|
234
|
+
var getArray = (values, field) => {
|
846
235
|
const value = getPath(values, field);
|
847
236
|
if (value === void 0 || value === null) {
|
848
237
|
const newValue = [];
|
@@ -855,7 +244,7 @@ const getArray = (values, field) => {
|
|
855
244
|
);
|
856
245
|
return value;
|
857
246
|
};
|
858
|
-
|
247
|
+
var swap = (array, indexA, indexB) => {
|
859
248
|
const itemA = array[indexA];
|
860
249
|
const itemB = array[indexB];
|
861
250
|
const hasItemA = indexA in array;
|
@@ -879,20 +268,20 @@ function sparseSplice(array, start, deleteCount, item) {
|
|
879
268
|
return array.splice(start, deleteCount, item);
|
880
269
|
return array.splice(start, deleteCount);
|
881
270
|
}
|
882
|
-
|
271
|
+
var move = (array, from2, to) => {
|
883
272
|
const [item] = sparseSplice(array, from2, 1);
|
884
273
|
sparseSplice(array, to, 0, item);
|
885
274
|
};
|
886
|
-
|
275
|
+
var insert = (array, index, value) => {
|
887
276
|
sparseSplice(array, index, 0, value);
|
888
277
|
};
|
889
|
-
|
278
|
+
var remove = (array, index) => {
|
890
279
|
sparseSplice(array, index, 1);
|
891
280
|
};
|
892
|
-
|
281
|
+
var replace = (array, index, value) => {
|
893
282
|
sparseSplice(array, index, 1, value);
|
894
283
|
};
|
895
|
-
|
284
|
+
var mutateAsArray = (field, obj, mutate) => {
|
896
285
|
const beforeKeys = /* @__PURE__ */ new Set();
|
897
286
|
const arr = [];
|
898
287
|
for (const [key, value] of Object.entries(obj)) {
|
@@ -913,7 +302,7 @@ const mutateAsArray = (field, obj, mutate) => {
|
|
913
302
|
}
|
914
303
|
}
|
915
304
|
};
|
916
|
-
|
305
|
+
var getDeepArrayPaths = (obj, basePath = "") => {
|
917
306
|
if (Array.isArray(obj)) {
|
918
307
|
return obj.flatMap(
|
919
308
|
(item, index) => getDeepArrayPaths(item, `${basePath}[${index}]`)
|
@@ -926,9 +315,259 @@ const getDeepArrayPaths = (obj, basePath = "") => {
|
|
926
315
|
}
|
927
316
|
return [basePath];
|
928
317
|
};
|
929
|
-
|
318
|
+
if (void 0) {
|
319
|
+
const { describe, expect, it } = void 0;
|
320
|
+
const countArrayItems = (arr) => {
|
321
|
+
let count = 0;
|
322
|
+
arr.forEach(() => count++);
|
323
|
+
return count;
|
324
|
+
};
|
325
|
+
describe("getArray", () => {
|
326
|
+
it("shoud get a deeply nested array that can be mutated to update the nested value", () => {
|
327
|
+
const values = {
|
328
|
+
d: [
|
329
|
+
{ foo: "bar", baz: [true, false] },
|
330
|
+
{ e: true, f: "hi" }
|
331
|
+
]
|
332
|
+
};
|
333
|
+
const result = getArray(values, "d[0].baz");
|
334
|
+
const finalValues = {
|
335
|
+
d: [
|
336
|
+
{ foo: "bar", baz: [true, false, true] },
|
337
|
+
{ e: true, f: "hi" }
|
338
|
+
]
|
339
|
+
};
|
340
|
+
expect(result).toEqual([true, false]);
|
341
|
+
result.push(true);
|
342
|
+
expect(values).toEqual(finalValues);
|
343
|
+
});
|
344
|
+
it("should return an empty array that can be mutated if result is null or undefined", () => {
|
345
|
+
const values = {};
|
346
|
+
const result = getArray(values, "a.foo[0].bar");
|
347
|
+
const finalValues = {
|
348
|
+
a: { foo: [{ bar: ["Bob ross"] }] }
|
349
|
+
};
|
350
|
+
expect(result).toEqual([]);
|
351
|
+
result.push("Bob ross");
|
352
|
+
expect(values).toEqual(finalValues);
|
353
|
+
});
|
354
|
+
it("should throw if the value is defined and not an array", () => {
|
355
|
+
const values = { foo: "foo" };
|
356
|
+
expect(() => getArray(values, "foo")).toThrow();
|
357
|
+
});
|
358
|
+
});
|
359
|
+
describe("swap", () => {
|
360
|
+
it("should swap two items", () => {
|
361
|
+
const array = [1, 2, 3];
|
362
|
+
swap(array, 0, 1);
|
363
|
+
expect(array).toEqual([2, 1, 3]);
|
364
|
+
});
|
365
|
+
it("should work for sparse arrays", () => {
|
366
|
+
const arr = [];
|
367
|
+
arr[0] = true;
|
368
|
+
swap(arr, 0, 2);
|
369
|
+
expect(countArrayItems(arr)).toEqual(1);
|
370
|
+
expect(0 in arr).toBe(false);
|
371
|
+
expect(2 in arr).toBe(true);
|
372
|
+
expect(arr[2]).toEqual(true);
|
373
|
+
});
|
374
|
+
});
|
375
|
+
describe("move", () => {
|
376
|
+
it("should move an item to a new index", () => {
|
377
|
+
const array = [1, 2, 3];
|
378
|
+
move(array, 0, 1);
|
379
|
+
expect(array).toEqual([2, 1, 3]);
|
380
|
+
});
|
381
|
+
it("should work with sparse arrays", () => {
|
382
|
+
const array = [1];
|
383
|
+
move(array, 0, 2);
|
384
|
+
expect(countArrayItems(array)).toEqual(1);
|
385
|
+
expect(array).toEqual([void 0, void 0, 1]);
|
386
|
+
});
|
387
|
+
});
|
388
|
+
describe("insert", () => {
|
389
|
+
it("should insert an item at a new index", () => {
|
390
|
+
const array = [1, 2, 3];
|
391
|
+
insert(array, 1, 4);
|
392
|
+
expect(array).toEqual([1, 4, 2, 3]);
|
393
|
+
});
|
394
|
+
it("should be able to insert falsey values", () => {
|
395
|
+
const array = [1, 2, 3];
|
396
|
+
insert(array, 1, null);
|
397
|
+
expect(array).toEqual([1, null, 2, 3]);
|
398
|
+
});
|
399
|
+
it("should handle sparse arrays", () => {
|
400
|
+
const array = [];
|
401
|
+
array[2] = true;
|
402
|
+
insert(array, 0, true);
|
403
|
+
expect(countArrayItems(array)).toEqual(2);
|
404
|
+
expect(array).toEqual([true, void 0, void 0, true]);
|
405
|
+
});
|
406
|
+
});
|
407
|
+
describe("remove", () => {
|
408
|
+
it("should remove an item at a given index", () => {
|
409
|
+
const array = [1, 2, 3];
|
410
|
+
remove(array, 1);
|
411
|
+
expect(array).toEqual([1, 3]);
|
412
|
+
});
|
413
|
+
it("should handle sparse arrays", () => {
|
414
|
+
const array = [];
|
415
|
+
array[2] = true;
|
416
|
+
remove(array, 0);
|
417
|
+
expect(countArrayItems(array)).toEqual(1);
|
418
|
+
expect(array).toEqual([void 0, true]);
|
419
|
+
});
|
420
|
+
});
|
421
|
+
describe("replace", () => {
|
422
|
+
it("should replace an item at a given index", () => {
|
423
|
+
const array = [1, 2, 3];
|
424
|
+
replace(array, 1, 4);
|
425
|
+
expect(array).toEqual([1, 4, 3]);
|
426
|
+
});
|
427
|
+
it("should handle sparse arrays", () => {
|
428
|
+
const array = [];
|
429
|
+
array[2] = true;
|
430
|
+
replace(array, 0, true);
|
431
|
+
expect(countArrayItems(array)).toEqual(2);
|
432
|
+
expect(array).toEqual([true, void 0, true]);
|
433
|
+
});
|
434
|
+
});
|
435
|
+
describe("mutateAsArray", () => {
|
436
|
+
it("should handle swap", () => {
|
437
|
+
const values = {
|
438
|
+
myField: "something",
|
439
|
+
"myField[0]": "foo",
|
440
|
+
"myField[2]": "bar",
|
441
|
+
otherField: "baz",
|
442
|
+
"otherField[0]": "something else"
|
443
|
+
};
|
444
|
+
mutateAsArray("myField", values, (arr) => {
|
445
|
+
swap(arr, 0, 2);
|
446
|
+
});
|
447
|
+
expect(values).toEqual({
|
448
|
+
myField: "something",
|
449
|
+
"myField[0]": "bar",
|
450
|
+
"myField[2]": "foo",
|
451
|
+
otherField: "baz",
|
452
|
+
"otherField[0]": "something else"
|
453
|
+
});
|
454
|
+
});
|
455
|
+
it("should swap sparse arrays", () => {
|
456
|
+
const values = {
|
457
|
+
myField: "something",
|
458
|
+
"myField[0]": "foo",
|
459
|
+
otherField: "baz",
|
460
|
+
"otherField[0]": "something else"
|
461
|
+
};
|
462
|
+
mutateAsArray("myField", values, (arr) => {
|
463
|
+
swap(arr, 0, 2);
|
464
|
+
});
|
465
|
+
expect(values).toEqual({
|
466
|
+
myField: "something",
|
467
|
+
"myField[2]": "foo",
|
468
|
+
otherField: "baz",
|
469
|
+
"otherField[0]": "something else"
|
470
|
+
});
|
471
|
+
});
|
472
|
+
it("should handle arrays with nested values", () => {
|
473
|
+
const values = {
|
474
|
+
myField: "something",
|
475
|
+
"myField[0].title": "foo",
|
476
|
+
"myField[0].note": "bar",
|
477
|
+
"myField[2].title": "other",
|
478
|
+
"myField[2].note": "other",
|
479
|
+
otherField: "baz",
|
480
|
+
"otherField[0]": "something else"
|
481
|
+
};
|
482
|
+
mutateAsArray("myField", values, (arr) => {
|
483
|
+
swap(arr, 0, 2);
|
484
|
+
});
|
485
|
+
expect(values).toEqual({
|
486
|
+
myField: "something",
|
487
|
+
"myField[0].title": "other",
|
488
|
+
"myField[0].note": "other",
|
489
|
+
"myField[2].title": "foo",
|
490
|
+
"myField[2].note": "bar",
|
491
|
+
otherField: "baz",
|
492
|
+
"otherField[0]": "something else"
|
493
|
+
});
|
494
|
+
});
|
495
|
+
it("should handle move", () => {
|
496
|
+
const values = {
|
497
|
+
myField: "something",
|
498
|
+
"myField[0]": "foo",
|
499
|
+
"myField[1]": "bar",
|
500
|
+
"myField[2]": "baz",
|
501
|
+
"otherField[0]": "something else"
|
502
|
+
};
|
503
|
+
mutateAsArray("myField", values, (arr) => {
|
504
|
+
move(arr, 0, 2);
|
505
|
+
});
|
506
|
+
expect(values).toEqual({
|
507
|
+
myField: "something",
|
508
|
+
"myField[0]": "bar",
|
509
|
+
"myField[1]": "baz",
|
510
|
+
"myField[2]": "foo",
|
511
|
+
"otherField[0]": "something else"
|
512
|
+
});
|
513
|
+
});
|
514
|
+
it("should not create keys for `undefined`", () => {
|
515
|
+
const values = {
|
516
|
+
"myField[0]": "foo"
|
517
|
+
};
|
518
|
+
mutateAsArray("myField", values, (arr) => {
|
519
|
+
arr.unshift(void 0);
|
520
|
+
});
|
521
|
+
expect(Object.keys(values)).toHaveLength(1);
|
522
|
+
expect(values).toEqual({
|
523
|
+
"myField[1]": "foo"
|
524
|
+
});
|
525
|
+
});
|
526
|
+
it("should handle remove", () => {
|
527
|
+
const values = {
|
528
|
+
myField: "something",
|
529
|
+
"myField[0]": "foo",
|
530
|
+
"myField[1]": "bar",
|
531
|
+
"myField[2]": "baz",
|
532
|
+
"otherField[0]": "something else"
|
533
|
+
};
|
534
|
+
mutateAsArray("myField", values, (arr) => {
|
535
|
+
remove(arr, 1);
|
536
|
+
});
|
537
|
+
expect(values).toEqual({
|
538
|
+
myField: "something",
|
539
|
+
"myField[0]": "foo",
|
540
|
+
"myField[1]": "baz",
|
541
|
+
"otherField[0]": "something else"
|
542
|
+
});
|
543
|
+
expect("myField[2]" in values).toBe(false);
|
544
|
+
});
|
545
|
+
});
|
546
|
+
describe("getDeepArrayPaths", () => {
|
547
|
+
it("should return all paths recursively", () => {
|
548
|
+
const obj = [
|
549
|
+
true,
|
550
|
+
true,
|
551
|
+
[true, true],
|
552
|
+
{ foo: true, bar: { baz: true, test: [true] } }
|
553
|
+
];
|
554
|
+
expect(getDeepArrayPaths(obj, "myField")).toEqual([
|
555
|
+
"myField[0]",
|
556
|
+
"myField[1]",
|
557
|
+
"myField[2][0]",
|
558
|
+
"myField[2][1]",
|
559
|
+
"myField[3].foo",
|
560
|
+
"myField[3].bar.baz",
|
561
|
+
"myField[3].bar.test[0]"
|
562
|
+
]);
|
563
|
+
});
|
564
|
+
});
|
565
|
+
}
|
566
|
+
|
567
|
+
// src/internal/state/createFormStore.ts
|
568
|
+
var noOp = () => {
|
930
569
|
};
|
931
|
-
|
570
|
+
var defaultFormState = {
|
932
571
|
isHydrated: false,
|
933
572
|
isSubmitting: false,
|
934
573
|
hasBeenSubmitted: false,
|
@@ -980,7 +619,7 @@ const defaultFormState = {
|
|
980
619
|
}
|
981
620
|
}
|
982
621
|
};
|
983
|
-
|
622
|
+
var createFormState = (set, get2) => ({
|
984
623
|
isHydrated: false,
|
985
624
|
isSubmitting: false,
|
986
625
|
hasBeenSubmitted: false,
|
@@ -988,7 +627,7 @@ const createFormState = (set, get) => ({
|
|
988
627
|
fieldErrors: {},
|
989
628
|
formElement: null,
|
990
629
|
currentDefaultValues: {},
|
991
|
-
isValid: () => Object.keys(
|
630
|
+
isValid: () => Object.keys(get2().fieldErrors).length === 0,
|
992
631
|
startSubmit: () => set((state) => {
|
993
632
|
state.isSubmitting = true;
|
994
633
|
state.hasBeenSubmitted = true;
|
@@ -1026,7 +665,7 @@ const createFormState = (set, get) => ({
|
|
1026
665
|
state.isHydrated = true;
|
1027
666
|
}),
|
1028
667
|
setFormElement: (formElement) => {
|
1029
|
-
if (
|
668
|
+
if (get2().formElement === formElement)
|
1030
669
|
return;
|
1031
670
|
set((state) => {
|
1032
671
|
state.formElement = formElement;
|
@@ -1034,49 +673,49 @@ const createFormState = (set, get) => ({
|
|
1034
673
|
},
|
1035
674
|
validateField: async (field) => {
|
1036
675
|
var _a, _b, _c;
|
1037
|
-
const formElement =
|
1038
|
-
|
676
|
+
const formElement = get2().formElement;
|
677
|
+
invariant2(
|
1039
678
|
formElement,
|
1040
679
|
"Cannot find reference to form. This is probably a bug in remix-validated-form."
|
1041
680
|
);
|
1042
|
-
const validator = (_a =
|
1043
|
-
|
681
|
+
const validator = (_a = get2().formProps) == null ? void 0 : _a.validator;
|
682
|
+
invariant2(
|
1044
683
|
validator,
|
1045
684
|
"Cannot validator. This is probably a bug in remix-validated-form."
|
1046
685
|
);
|
1047
|
-
await ((_c = (_b =
|
686
|
+
await ((_c = (_b = get2().controlledFields).awaitValueUpdate) == null ? void 0 : _c.call(_b, field));
|
1048
687
|
const { error } = await validator.validateField(
|
1049
688
|
new FormData(formElement),
|
1050
689
|
field
|
1051
690
|
);
|
1052
691
|
if (error) {
|
1053
|
-
|
692
|
+
get2().setFieldError(field, error);
|
1054
693
|
return error;
|
1055
694
|
} else {
|
1056
|
-
|
695
|
+
get2().clearFieldError(field);
|
1057
696
|
return null;
|
1058
697
|
}
|
1059
698
|
},
|
1060
699
|
validate: async () => {
|
1061
700
|
var _a;
|
1062
|
-
const formElement =
|
1063
|
-
|
701
|
+
const formElement = get2().formElement;
|
702
|
+
invariant2(
|
1064
703
|
formElement,
|
1065
704
|
"Cannot find reference to form. This is probably a bug in remix-validated-form."
|
1066
705
|
);
|
1067
|
-
const validator = (_a =
|
1068
|
-
|
706
|
+
const validator = (_a = get2().formProps) == null ? void 0 : _a.validator;
|
707
|
+
invariant2(
|
1069
708
|
validator,
|
1070
709
|
"Cannot validator. This is probably a bug in remix-validated-form."
|
1071
710
|
);
|
1072
711
|
const result = await validator.validate(new FormData(formElement));
|
1073
712
|
if (result.error)
|
1074
|
-
|
713
|
+
get2().setFieldErrors(result.error.fieldErrors);
|
1075
714
|
return result;
|
1076
715
|
},
|
1077
716
|
submit: () => {
|
1078
|
-
const formElement =
|
1079
|
-
|
717
|
+
const formElement = get2().formElement;
|
718
|
+
invariant2(
|
1080
719
|
formElement,
|
1081
720
|
"Cannot find reference to form. This is probably a bug in remix-validated-form."
|
1082
721
|
);
|
@@ -1084,11 +723,11 @@ const createFormState = (set, get) => ({
|
|
1084
723
|
},
|
1085
724
|
getValues: () => {
|
1086
725
|
var _a;
|
1087
|
-
return new FormData((_a =
|
726
|
+
return new FormData((_a = get2().formElement) != null ? _a : void 0);
|
1088
727
|
},
|
1089
728
|
resetFormElement: () => {
|
1090
729
|
var _a;
|
1091
|
-
return (_a =
|
730
|
+
return (_a = get2().formElement) == null ? void 0 : _a.reset();
|
1092
731
|
},
|
1093
732
|
controlledFields: {
|
1094
733
|
values: {},
|
@@ -1103,7 +742,7 @@ const createFormState = (set, get) => ({
|
|
1103
742
|
});
|
1104
743
|
},
|
1105
744
|
unregister: (fieldName) => {
|
1106
|
-
if (
|
745
|
+
if (get2() === null || get2() === void 0)
|
1107
746
|
return;
|
1108
747
|
set((state) => {
|
1109
748
|
var _a, _b, _c;
|
@@ -1130,12 +769,12 @@ const createFormState = (set, get) => ({
|
|
1130
769
|
delete state.controlledFields.refCounts[fieldName];
|
1131
770
|
});
|
1132
771
|
},
|
1133
|
-
getValue: (fieldName) => getPath(
|
772
|
+
getValue: (fieldName) => getPath(get2().controlledFields.values, fieldName),
|
1134
773
|
setValue: (fieldName, value) => {
|
1135
774
|
set((state) => {
|
1136
775
|
setPath(state.controlledFields.values, fieldName, value);
|
1137
776
|
});
|
1138
|
-
|
777
|
+
get2().controlledFields.kickoffValueUpdate(fieldName);
|
1139
778
|
},
|
1140
779
|
kickoffValueUpdate: (fieldName) => {
|
1141
780
|
const clear = () => set((state) => {
|
@@ -1150,7 +789,7 @@ const createFormState = (set, get) => ({
|
|
1150
789
|
});
|
1151
790
|
},
|
1152
791
|
awaitValueUpdate: async (fieldName) => {
|
1153
|
-
await
|
792
|
+
await get2().controlledFields.valueUpdatePromises[fieldName];
|
1154
793
|
},
|
1155
794
|
array: {
|
1156
795
|
push: (fieldName, item) => {
|
@@ -1158,7 +797,7 @@ const createFormState = (set, get) => ({
|
|
1158
797
|
getArray(state.controlledFields.values, fieldName).push(item);
|
1159
798
|
getArray(state.currentDefaultValues, fieldName).push(item);
|
1160
799
|
});
|
1161
|
-
|
800
|
+
get2().controlledFields.kickoffValueUpdate(fieldName);
|
1162
801
|
},
|
1163
802
|
swap: (fieldName, indexA, indexB) => {
|
1164
803
|
set((state) => {
|
@@ -1183,7 +822,7 @@ const createFormState = (set, get) => ({
|
|
1183
822
|
(array) => swap(array, indexA, indexB)
|
1184
823
|
);
|
1185
824
|
});
|
1186
|
-
|
825
|
+
get2().controlledFields.kickoffValueUpdate(fieldName);
|
1187
826
|
},
|
1188
827
|
move: (fieldName, from2, to) => {
|
1189
828
|
set((state) => {
|
@@ -1208,7 +847,7 @@ const createFormState = (set, get) => ({
|
|
1208
847
|
(array) => move(array, from2, to)
|
1209
848
|
);
|
1210
849
|
});
|
1211
|
-
|
850
|
+
get2().controlledFields.kickoffValueUpdate(fieldName);
|
1212
851
|
},
|
1213
852
|
insert: (fieldName, index, item) => {
|
1214
853
|
set((state) => {
|
@@ -1233,7 +872,7 @@ const createFormState = (set, get) => ({
|
|
1233
872
|
(array) => insert(array, index, void 0)
|
1234
873
|
);
|
1235
874
|
});
|
1236
|
-
|
875
|
+
get2().controlledFields.kickoffValueUpdate(fieldName);
|
1237
876
|
},
|
1238
877
|
remove: (fieldName, index) => {
|
1239
878
|
set((state) => {
|
@@ -1256,7 +895,7 @@ const createFormState = (set, get) => ({
|
|
1256
895
|
(array) => remove(array, index)
|
1257
896
|
);
|
1258
897
|
});
|
1259
|
-
|
898
|
+
get2().controlledFields.kickoffValueUpdate(fieldName);
|
1260
899
|
},
|
1261
900
|
pop: (fieldName) => {
|
1262
901
|
set((state) => {
|
@@ -1273,7 +912,7 @@ const createFormState = (set, get) => ({
|
|
1273
912
|
(array) => array.pop()
|
1274
913
|
);
|
1275
914
|
});
|
1276
|
-
|
915
|
+
get2().controlledFields.kickoffValueUpdate(fieldName);
|
1277
916
|
},
|
1278
917
|
unshift: (fieldName, value) => {
|
1279
918
|
set((state) => {
|
@@ -1314,17 +953,17 @@ const createFormState = (set, get) => ({
|
|
1314
953
|
(array) => replace(array, index, item)
|
1315
954
|
);
|
1316
955
|
});
|
1317
|
-
|
956
|
+
get2().controlledFields.kickoffValueUpdate(fieldName);
|
1318
957
|
}
|
1319
958
|
}
|
1320
959
|
}
|
1321
960
|
});
|
1322
|
-
|
1323
|
-
immer((set,
|
961
|
+
var useRootFormStore = create()(
|
962
|
+
immer((set, get2) => ({
|
1324
963
|
forms: {},
|
1325
964
|
form: (formId) => {
|
1326
965
|
var _a;
|
1327
|
-
return (_a =
|
966
|
+
return (_a = get2().forms[formId]) != null ? _a : defaultFormState;
|
1328
967
|
},
|
1329
968
|
cleanupForm: (formId) => {
|
1330
969
|
set((state) => {
|
@@ -1332,21 +971,25 @@ const useRootFormStore = create()(
|
|
1332
971
|
});
|
1333
972
|
},
|
1334
973
|
registerForm: (formId) => {
|
1335
|
-
if (
|
974
|
+
if (get2().forms[formId])
|
1336
975
|
return;
|
1337
976
|
set((state) => {
|
1338
977
|
state.forms[formId] = createFormState(
|
1339
978
|
(setter) => set((state2) => setter(state2.forms[formId])),
|
1340
|
-
() =>
|
979
|
+
() => get2().forms[formId]
|
1341
980
|
);
|
1342
981
|
});
|
1343
982
|
}
|
1344
983
|
}))
|
1345
984
|
);
|
1346
|
-
|
985
|
+
|
986
|
+
// src/internal/state/storeHooks.ts
|
987
|
+
var useFormStore = (formId, selector) => {
|
1347
988
|
return useRootFormStore((state) => selector(state.form(formId)));
|
1348
989
|
};
|
1349
|
-
|
990
|
+
|
991
|
+
// src/internal/hooks.ts
|
992
|
+
var useInternalFormContext = (formId, hookName) => {
|
1350
993
|
const formContext = useContext(InternalFormContext);
|
1351
994
|
if (formId)
|
1352
995
|
return { formId };
|
@@ -1376,12 +1019,12 @@ function useErrorResponseForForm({
|
|
1376
1019
|
return actionData;
|
1377
1020
|
return null;
|
1378
1021
|
}
|
1379
|
-
|
1022
|
+
var useFieldErrorsForForm = (context) => {
|
1380
1023
|
const response = useErrorResponseForForm(context);
|
1381
1024
|
const hydrated = useFormStore(context.formId, (state) => state.isHydrated);
|
1382
1025
|
return hydratable.from(response == null ? void 0 : response.fieldErrors, hydrated);
|
1383
1026
|
};
|
1384
|
-
|
1027
|
+
var useDefaultValuesFromLoader = ({
|
1385
1028
|
formId
|
1386
1029
|
}) => {
|
1387
1030
|
const matches = useMatches();
|
@@ -1392,7 +1035,7 @@ const useDefaultValuesFromLoader = ({
|
|
1392
1035
|
}
|
1393
1036
|
return null;
|
1394
1037
|
};
|
1395
|
-
|
1038
|
+
var useDefaultValuesForForm = (context) => {
|
1396
1039
|
const { formId, defaultValuesProp } = context;
|
1397
1040
|
const hydrated = useFormStore(formId, (state) => state.isHydrated);
|
1398
1041
|
const errorResponse = useErrorResponseForForm(context);
|
@@ -1400,7 +1043,7 @@ const useDefaultValuesForForm = (context) => {
|
|
1400
1043
|
if (hydrated)
|
1401
1044
|
return hydratable.hydratedData();
|
1402
1045
|
if (errorResponse == null ? void 0 : errorResponse.repopulateFields) {
|
1403
|
-
|
1046
|
+
invariant3(
|
1404
1047
|
typeof errorResponse.repopulateFields === "object",
|
1405
1048
|
"repopulateFields returned something other than an object"
|
1406
1049
|
);
|
@@ -1410,14 +1053,14 @@ const useDefaultValuesForForm = (context) => {
|
|
1410
1053
|
return hydratable.serverData(defaultValuesProp);
|
1411
1054
|
return hydratable.serverData(defaultValuesFromLoader);
|
1412
1055
|
};
|
1413
|
-
|
1056
|
+
var useHasActiveFormSubmit = ({
|
1414
1057
|
fetcher
|
1415
1058
|
}) => {
|
1416
1059
|
const transition = useTransition();
|
1417
1060
|
const hasActiveSubmission = fetcher ? fetcher.state === "submitting" : !!transition.submission;
|
1418
1061
|
return hasActiveSubmission;
|
1419
1062
|
};
|
1420
|
-
|
1063
|
+
var useFieldTouched = (field, { formId }) => {
|
1421
1064
|
const touched = useFormStore(formId, (state) => state.touchedFields[field]);
|
1422
1065
|
const setFieldTouched = useFormStore(formId, (state) => state.setTouched);
|
1423
1066
|
const setTouched = useCallback(
|
@@ -1426,7 +1069,7 @@ const useFieldTouched = (field, { formId }) => {
|
|
1426
1069
|
);
|
1427
1070
|
return [touched, setTouched];
|
1428
1071
|
};
|
1429
|
-
|
1072
|
+
var useFieldError = (name, context) => {
|
1430
1073
|
const fieldErrors = useFieldErrorsForForm(context);
|
1431
1074
|
const state = useFormStore(
|
1432
1075
|
context.formId,
|
@@ -1434,54 +1077,57 @@ const useFieldError = (name, context) => {
|
|
1434
1077
|
);
|
1435
1078
|
return fieldErrors.map((fieldErrors2) => fieldErrors2 == null ? void 0 : fieldErrors2[name]).hydrateTo(state);
|
1436
1079
|
};
|
1437
|
-
|
1080
|
+
var useClearError = (context) => {
|
1438
1081
|
const { formId } = context;
|
1439
1082
|
return useFormStore(formId, (state) => state.clearFieldError);
|
1440
1083
|
};
|
1441
|
-
|
1442
|
-
|
1084
|
+
var useCurrentDefaultValueForField = (formId, field) => useFormStore(formId, (state) => getPath(state.currentDefaultValues, field));
|
1085
|
+
var useFieldDefaultValue = (name, context) => {
|
1443
1086
|
const defaultValues = useDefaultValuesForForm(context);
|
1444
1087
|
const state = useCurrentDefaultValueForField(context.formId, name);
|
1445
1088
|
return defaultValues.map((val) => getPath(val, name)).hydrateTo(state);
|
1446
1089
|
};
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1452
|
-
|
1090
|
+
var useInternalIsSubmitting = (formId) => useFormStore(formId, (state) => state.isSubmitting);
|
1091
|
+
var useInternalIsValid = (formId) => useFormStore(formId, (state) => state.isValid());
|
1092
|
+
var useInternalHasBeenSubmitted = (formId) => useFormStore(formId, (state) => state.hasBeenSubmitted);
|
1093
|
+
var useValidateField = (formId) => useFormStore(formId, (state) => state.validateField);
|
1094
|
+
var useValidate = (formId) => useFormStore(formId, (state) => state.validate);
|
1095
|
+
var noOpReceiver = () => () => {
|
1453
1096
|
};
|
1454
|
-
|
1097
|
+
var useRegisterReceiveFocus = (formId) => useFormStore(
|
1455
1098
|
formId,
|
1456
1099
|
(state) => {
|
1457
1100
|
var _a, _b;
|
1458
1101
|
return (_b = (_a = state.formProps) == null ? void 0 : _a.registerReceiveFocus) != null ? _b : noOpReceiver;
|
1459
1102
|
}
|
1460
1103
|
);
|
1461
|
-
|
1462
|
-
|
1104
|
+
var defaultDefaultValues = {};
|
1105
|
+
var useSyncedDefaultValues = (formId) => useFormStore(
|
1463
1106
|
formId,
|
1464
1107
|
(state) => {
|
1465
1108
|
var _a, _b;
|
1466
1109
|
return (_b = (_a = state.formProps) == null ? void 0 : _a.defaultValues) != null ? _b : defaultDefaultValues;
|
1467
1110
|
}
|
1468
1111
|
);
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1112
|
+
var useSetTouched = ({ formId }) => useFormStore(formId, (state) => state.setTouched);
|
1113
|
+
var useTouchedFields = (formId) => useFormStore(formId, (state) => state.touchedFields);
|
1114
|
+
var useFieldErrors = (formId) => useFormStore(formId, (state) => state.fieldErrors);
|
1115
|
+
var useSetFieldErrors = (formId) => useFormStore(formId, (state) => state.setFieldErrors);
|
1116
|
+
var useResetFormElement = (formId) => useFormStore(formId, (state) => state.resetFormElement);
|
1117
|
+
var useSubmitForm = (formId) => useFormStore(formId, (state) => state.submit);
|
1118
|
+
var useFormActionProp = (formId) => useFormStore(formId, (state) => {
|
1476
1119
|
var _a;
|
1477
1120
|
return (_a = state.formProps) == null ? void 0 : _a.action;
|
1478
1121
|
});
|
1479
|
-
|
1122
|
+
var useFormSubactionProp = (formId) => useFormStore(formId, (state) => {
|
1480
1123
|
var _a;
|
1481
1124
|
return (_a = state.formProps) == null ? void 0 : _a.subaction;
|
1482
1125
|
});
|
1483
|
-
|
1484
|
-
|
1126
|
+
var useFormValues = (formId) => useFormStore(formId, (state) => state.getValues);
|
1127
|
+
|
1128
|
+
// src/internal/state/controlledFields.ts
|
1129
|
+
import { useCallback as useCallback2, useEffect } from "react";
|
1130
|
+
var useControlledFieldValue = (context, field) => {
|
1485
1131
|
const value = useFormStore(
|
1486
1132
|
context.formId,
|
1487
1133
|
(state) => state.controlledFields.getValue(field)
|
@@ -1493,7 +1139,7 @@ const useControlledFieldValue = (context, field) => {
|
|
1493
1139
|
const defaultValue = useFieldDefaultValue(field, context);
|
1494
1140
|
return isFormHydrated ? value : defaultValue;
|
1495
1141
|
};
|
1496
|
-
|
1142
|
+
var useRegisterControlledField = (context, field) => {
|
1497
1143
|
const resolveUpdate = useFormStore(
|
1498
1144
|
context.formId,
|
1499
1145
|
(state) => state.controlledFields.valueUpdateResolvers[field]
|
@@ -1514,38 +1160,40 @@ const useRegisterControlledField = (context, field) => {
|
|
1514
1160
|
return () => unregister(field);
|
1515
1161
|
}, [context.formId, field, register, unregister]);
|
1516
1162
|
};
|
1517
|
-
|
1163
|
+
var useControllableValue = (context, field) => {
|
1518
1164
|
useRegisterControlledField(context, field);
|
1519
1165
|
const setControlledFieldValue = useFormStore(
|
1520
1166
|
context.formId,
|
1521
1167
|
(state) => state.controlledFields.setValue
|
1522
1168
|
);
|
1523
|
-
const setValue =
|
1169
|
+
const setValue = useCallback2(
|
1524
1170
|
(value2) => setControlledFieldValue(field, value2),
|
1525
1171
|
[field, setControlledFieldValue]
|
1526
1172
|
);
|
1527
1173
|
const value = useControlledFieldValue(context, field);
|
1528
1174
|
return [value, setValue];
|
1529
1175
|
};
|
1530
|
-
|
1176
|
+
var useUpdateControllableValue = (formId) => {
|
1531
1177
|
const setValue = useFormStore(
|
1532
1178
|
formId,
|
1533
1179
|
(state) => state.controlledFields.setValue
|
1534
1180
|
);
|
1535
|
-
return
|
1181
|
+
return useCallback2(
|
1536
1182
|
(field, value) => setValue(field, value),
|
1537
1183
|
[setValue]
|
1538
1184
|
);
|
1539
1185
|
};
|
1540
|
-
|
1186
|
+
|
1187
|
+
// src/hooks.ts
|
1188
|
+
var useIsSubmitting = (formId) => {
|
1541
1189
|
const formContext = useInternalFormContext(formId, "useIsSubmitting");
|
1542
1190
|
return useInternalIsSubmitting(formContext.formId);
|
1543
1191
|
};
|
1544
|
-
|
1192
|
+
var useIsValid = (formId) => {
|
1545
1193
|
const formContext = useInternalFormContext(formId, "useIsValid");
|
1546
1194
|
return useInternalIsValid(formContext.formId);
|
1547
1195
|
};
|
1548
|
-
|
1196
|
+
var useField = (name, options) => {
|
1549
1197
|
const { formId: providedFormId, handleReceiveFocus } = options != null ? options : {};
|
1550
1198
|
const formContext = useInternalFormContext(providedFormId, "useField");
|
1551
1199
|
const defaultValue = useFieldDefaultValue(name, formContext);
|
@@ -1555,7 +1203,7 @@ const useField = (name, options) => {
|
|
1555
1203
|
const hasBeenSubmitted = useInternalHasBeenSubmitted(formContext.formId);
|
1556
1204
|
const validateField = useValidateField(formContext.formId);
|
1557
1205
|
const registerReceiveFocus = useRegisterReceiveFocus(formContext.formId);
|
1558
|
-
|
1206
|
+
useEffect2(() => {
|
1559
1207
|
if (handleReceiveFocus)
|
1560
1208
|
return registerReceiveFocus(name, handleReceiveFocus);
|
1561
1209
|
}, [handleReceiveFocus, name, registerReceiveFocus]);
|
@@ -1593,38 +1241,18 @@ const useField = (name, options) => {
|
|
1593
1241
|
]);
|
1594
1242
|
return field;
|
1595
1243
|
};
|
1596
|
-
|
1244
|
+
var useControlField = (name, formId) => {
|
1597
1245
|
const context = useInternalFormContext(formId, "useControlField");
|
1598
1246
|
const [value, setValue] = useControllableValue(context, name);
|
1599
1247
|
return [value, setValue];
|
1600
1248
|
};
|
1601
|
-
|
1249
|
+
var useUpdateControlledField = (formId) => {
|
1602
1250
|
const context = useInternalFormContext(formId, "useControlField");
|
1603
1251
|
return useUpdateControllableValue(context.formId);
|
1604
1252
|
};
|
1605
|
-
|
1606
|
-
|
1607
|
-
|
1608
|
-
* Copyright (c) Remix Software Inc.
|
1609
|
-
*
|
1610
|
-
* This source code is licensed under the MIT license found in the
|
1611
|
-
* LICENSE.md file in the root directory of this source tree.
|
1612
|
-
*
|
1613
|
-
* @license MIT
|
1614
|
-
*/
|
1615
|
-
const json = (data, init = {}) => {
|
1616
|
-
let responseInit = typeof init === "number" ? {
|
1617
|
-
status: init
|
1618
|
-
} : init;
|
1619
|
-
let headers = new Headers(responseInit.headers);
|
1620
|
-
if (!headers.has("Content-Type")) {
|
1621
|
-
headers.set("Content-Type", "application/json; charset=utf-8");
|
1622
|
-
}
|
1623
|
-
return new Response(JSON.stringify(data), {
|
1624
|
-
...responseInit,
|
1625
|
-
headers
|
1626
|
-
});
|
1627
|
-
};
|
1253
|
+
|
1254
|
+
// src/server.ts
|
1255
|
+
import { json } from "@remix-run/server-runtime";
|
1628
1256
|
function validationError(error, repopulateFields, init) {
|
1629
1257
|
return json(
|
1630
1258
|
{
|
@@ -1636,10 +1264,27 @@ function validationError(error, repopulateFields, init) {
|
|
1636
1264
|
{ status: 422, ...init }
|
1637
1265
|
);
|
1638
1266
|
}
|
1639
|
-
|
1267
|
+
var setFormDefaults = (formId, defaultValues) => ({
|
1640
1268
|
[formDefaultValuesKey(formId)]: defaultValues
|
1641
1269
|
});
|
1642
|
-
|
1270
|
+
|
1271
|
+
// src/ValidatedForm.tsx
|
1272
|
+
import {
|
1273
|
+
Form as RemixForm,
|
1274
|
+
useSubmit
|
1275
|
+
} from "@remix-run/react";
|
1276
|
+
import {
|
1277
|
+
useCallback as useCallback4,
|
1278
|
+
useEffect as useEffect5,
|
1279
|
+
useMemo as useMemo2,
|
1280
|
+
useRef as useRef4,
|
1281
|
+
useState
|
1282
|
+
} from "react";
|
1283
|
+
import * as R3 from "remeda";
|
1284
|
+
|
1285
|
+
// src/internal/MultiValueMap.ts
|
1286
|
+
import { useCallback as useCallback3, useRef } from "react";
|
1287
|
+
var MultiValueMap = class {
|
1643
1288
|
constructor() {
|
1644
1289
|
this.dict = /* @__PURE__ */ new Map();
|
1645
1290
|
this.add = (key, value) => {
|
@@ -1670,19 +1315,22 @@ class MultiValueMap {
|
|
1670
1315
|
this.values = () => this.dict.values();
|
1671
1316
|
this.has = (key) => this.dict.has(key);
|
1672
1317
|
}
|
1673
|
-
}
|
1674
|
-
|
1318
|
+
};
|
1319
|
+
var useMultiValueMap = () => {
|
1675
1320
|
const ref = useRef(null);
|
1676
|
-
return
|
1321
|
+
return useCallback3(() => {
|
1677
1322
|
if (ref.current)
|
1678
1323
|
return ref.current;
|
1679
1324
|
ref.current = new MultiValueMap();
|
1680
1325
|
return ref.current;
|
1681
1326
|
}, []);
|
1682
1327
|
};
|
1328
|
+
|
1329
|
+
// src/internal/submissionCallbacks.ts
|
1330
|
+
import { useEffect as useEffect3, useRef as useRef2 } from "react";
|
1683
1331
|
function useSubmitComplete(isSubmitting, callback) {
|
1684
|
-
const isPending =
|
1685
|
-
|
1332
|
+
const isPending = useRef2(false);
|
1333
|
+
useEffect3(() => {
|
1686
1334
|
if (isSubmitting) {
|
1687
1335
|
isPending.current = true;
|
1688
1336
|
}
|
@@ -1692,7 +1340,11 @@ function useSubmitComplete(isSubmitting, callback) {
|
|
1692
1340
|
}
|
1693
1341
|
});
|
1694
1342
|
}
|
1695
|
-
|
1343
|
+
|
1344
|
+
// src/internal/util.ts
|
1345
|
+
import { useEffect as useEffect4, useLayoutEffect, useRef as useRef3 } from "react";
|
1346
|
+
import * as R2 from "remeda";
|
1347
|
+
var mergeRefs = (refs) => {
|
1696
1348
|
return (value) => {
|
1697
1349
|
refs.filter(Boolean).forEach((ref) => {
|
1698
1350
|
if (typeof ref === "function") {
|
@@ -1703,22 +1355,25 @@ const mergeRefs = (refs) => {
|
|
1703
1355
|
});
|
1704
1356
|
};
|
1705
1357
|
};
|
1706
|
-
|
1707
|
-
|
1708
|
-
const ref =
|
1709
|
-
const areEqual = ref.current === item || equals(ref.current, item);
|
1710
|
-
|
1358
|
+
var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect4;
|
1359
|
+
var useDeepEqualsMemo = (item) => {
|
1360
|
+
const ref = useRef3(item);
|
1361
|
+
const areEqual = ref.current === item || R2.equals(ref.current, item);
|
1362
|
+
useEffect4(() => {
|
1711
1363
|
if (!areEqual) {
|
1712
1364
|
ref.current = item;
|
1713
1365
|
}
|
1714
1366
|
});
|
1715
1367
|
return areEqual ? ref.current : item;
|
1716
1368
|
};
|
1717
|
-
|
1369
|
+
|
1370
|
+
// src/ValidatedForm.tsx
|
1371
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
1372
|
+
var getDataFromForm = (el) => new FormData(el);
|
1718
1373
|
function nonNull(value) {
|
1719
1374
|
return value !== null;
|
1720
1375
|
}
|
1721
|
-
|
1376
|
+
var focusFirstInvalidInput = (fieldErrors, customFocusHandlers, formElement) => {
|
1722
1377
|
var _a;
|
1723
1378
|
const namesInOrder = [...formElement.elements].map((el) => {
|
1724
1379
|
const input = el instanceof RadioNodeList ? el[0] : el;
|
@@ -1726,7 +1381,7 @@ const focusFirstInvalidInput = (fieldErrors, customFocusHandlers, formElement) =
|
|
1726
1381
|
return input.name;
|
1727
1382
|
return null;
|
1728
1383
|
}).filter(nonNull).filter((name) => name in fieldErrors);
|
1729
|
-
const uniqueNamesInOrder = uniq(namesInOrder);
|
1384
|
+
const uniqueNamesInOrder = R3.uniq(namesInOrder);
|
1730
1385
|
for (const fieldName of uniqueNamesInOrder) {
|
1731
1386
|
if (customFocusHandlers.has(fieldName)) {
|
1732
1387
|
customFocusHandlers.getAll(fieldName).forEach((handler) => {
|
@@ -1755,11 +1410,11 @@ const focusFirstInvalidInput = (fieldErrors, customFocusHandlers, formElement) =
|
|
1755
1410
|
}
|
1756
1411
|
}
|
1757
1412
|
};
|
1758
|
-
|
1413
|
+
var useFormId = (providedId) => {
|
1759
1414
|
const [symbolId] = useState(() => Symbol("remix-validated-form-id"));
|
1760
1415
|
return providedId != null ? providedId : symbolId;
|
1761
1416
|
};
|
1762
|
-
|
1417
|
+
var FormResetter = ({
|
1763
1418
|
resetAfterSubmit,
|
1764
1419
|
formRef
|
1765
1420
|
}) => {
|
@@ -1809,7 +1464,7 @@ function ValidatedForm({
|
|
1809
1464
|
var _a;
|
1810
1465
|
const formId = useFormId(id);
|
1811
1466
|
const providedDefaultValues = useDeepEqualsMemo(unMemoizedDefaults);
|
1812
|
-
const contextValue =
|
1467
|
+
const contextValue = useMemo2(
|
1813
1468
|
() => ({
|
1814
1469
|
formId,
|
1815
1470
|
action,
|
@@ -1822,8 +1477,8 @@ function ValidatedForm({
|
|
1822
1477
|
const backendError = useErrorResponseForForm(contextValue);
|
1823
1478
|
const backendDefaultValues = useDefaultValuesFromLoader(contextValue);
|
1824
1479
|
const hasActiveSubmission = useHasActiveFormSubmit(contextValue);
|
1825
|
-
const formRef =
|
1826
|
-
const Form
|
1480
|
+
const formRef = useRef4(null);
|
1481
|
+
const Form = (_a = fetcher == null ? void 0 : fetcher.Form) != null ? _a : RemixForm;
|
1827
1482
|
const submit = useSubmit();
|
1828
1483
|
const setFieldErrors = useSetFieldErrors(formId);
|
1829
1484
|
const setFieldError = useFormStore(formId, (state) => state.setFieldError);
|
@@ -1838,7 +1493,7 @@ function ValidatedForm({
|
|
1838
1493
|
const cleanupForm = useRootFormStore((state) => state.cleanupForm);
|
1839
1494
|
const registerForm = useRootFormStore((state) => state.registerForm);
|
1840
1495
|
const customFocusHandlers = useMultiValueMap();
|
1841
|
-
const registerReceiveFocus =
|
1496
|
+
const registerReceiveFocus = useCallback4(
|
1842
1497
|
(fieldName, handler) => {
|
1843
1498
|
customFocusHandlers().add(fieldName, handler);
|
1844
1499
|
return () => {
|
@@ -1872,17 +1527,30 @@ function ValidatedForm({
|
|
1872
1527
|
useIsomorphicLayoutEffect(() => {
|
1873
1528
|
setFormElementInState(formRef.current);
|
1874
1529
|
}, [setFormElementInState]);
|
1875
|
-
|
1530
|
+
useEffect5(() => {
|
1876
1531
|
var _a2;
|
1877
1532
|
setFieldErrors((_a2 = backendError == null ? void 0 : backendError.fieldErrors) != null ? _a2 : {});
|
1878
|
-
|
1533
|
+
if (!disableFocusOnError && (backendError == null ? void 0 : backendError.fieldErrors)) {
|
1534
|
+
focusFirstInvalidInput(
|
1535
|
+
backendError.fieldErrors,
|
1536
|
+
customFocusHandlers(),
|
1537
|
+
formRef.current
|
1538
|
+
);
|
1539
|
+
}
|
1540
|
+
}, [
|
1541
|
+
backendError == null ? void 0 : backendError.fieldErrors,
|
1542
|
+
customFocusHandlers,
|
1543
|
+
disableFocusOnError,
|
1544
|
+
setFieldErrors,
|
1545
|
+
setFieldError
|
1546
|
+
]);
|
1879
1547
|
useSubmitComplete(hasActiveSubmission, () => {
|
1880
1548
|
endSubmit();
|
1881
1549
|
});
|
1882
|
-
const handleSubmit = async (
|
1550
|
+
const handleSubmit = async (e, target, nativeEvent) => {
|
1883
1551
|
startSubmit();
|
1884
1552
|
const submitter = nativeEvent.submitter;
|
1885
|
-
const formDataToValidate = getDataFromForm(
|
1553
|
+
const formDataToValidate = getDataFromForm(e.currentTarget);
|
1886
1554
|
if (submitter == null ? void 0 : submitter.name) {
|
1887
1555
|
formDataToValidate.append(submitter.name, submitter.value);
|
1888
1556
|
}
|
@@ -1899,14 +1567,14 @@ function ValidatedForm({
|
|
1899
1567
|
}
|
1900
1568
|
} else {
|
1901
1569
|
setFieldErrors({});
|
1902
|
-
const eventProxy = formEventProxy(
|
1570
|
+
const eventProxy = formEventProxy(e);
|
1903
1571
|
await (onSubmit == null ? void 0 : onSubmit(result.data, eventProxy));
|
1904
1572
|
if (eventProxy.defaultPrevented) {
|
1905
1573
|
endSubmit();
|
1906
1574
|
return;
|
1907
1575
|
}
|
1908
1576
|
if (fetcher)
|
1909
|
-
fetcher.submit(submitter ||
|
1577
|
+
fetcher.submit(submitter || e.currentTarget);
|
1910
1578
|
else
|
1911
1579
|
submit(submitter || target, {
|
1912
1580
|
replace: replace2,
|
@@ -1914,43 +1582,44 @@ function ValidatedForm({
|
|
1914
1582
|
});
|
1915
1583
|
}
|
1916
1584
|
};
|
1917
|
-
return /* @__PURE__ */
|
1918
|
-
|
1919
|
-
|
1920
|
-
|
1921
|
-
|
1922
|
-
|
1923
|
-
|
1924
|
-
|
1925
|
-
|
1926
|
-
|
1927
|
-
|
1928
|
-
|
1929
|
-
|
1930
|
-
|
1931
|
-
|
1932
|
-
|
1933
|
-
|
1934
|
-
|
1935
|
-
|
1936
|
-
|
1585
|
+
return /* @__PURE__ */ jsx(
|
1586
|
+
Form,
|
1587
|
+
{
|
1588
|
+
ref: mergeRefs([formRef, formRefProp]),
|
1589
|
+
...rest,
|
1590
|
+
id,
|
1591
|
+
action,
|
1592
|
+
method,
|
1593
|
+
replace: replace2,
|
1594
|
+
onSubmit: (e) => {
|
1595
|
+
e.preventDefault();
|
1596
|
+
handleSubmit(
|
1597
|
+
e,
|
1598
|
+
e.currentTarget,
|
1599
|
+
e.nativeEvent
|
1600
|
+
);
|
1601
|
+
},
|
1602
|
+
onReset: (event) => {
|
1603
|
+
onReset == null ? void 0 : onReset(event);
|
1604
|
+
if (event.defaultPrevented)
|
1605
|
+
return;
|
1606
|
+
reset();
|
1607
|
+
},
|
1608
|
+
children: /* @__PURE__ */ jsx(InternalFormContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsxs(Fragment, { children: [
|
1609
|
+
/* @__PURE__ */ jsx(FormResetter, { formRef, resetAfterSubmit }),
|
1610
|
+
subaction && /* @__PURE__ */ jsx("input", { type: "hidden", value: subaction, name: "subaction" }),
|
1611
|
+
id && /* @__PURE__ */ jsx("input", { type: "hidden", value: id, name: FORM_ID_FIELD }),
|
1612
|
+
children
|
1613
|
+
] }) })
|
1937
1614
|
}
|
1938
|
-
|
1939
|
-
value: contextValue
|
1940
|
-
}, /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(FormResetter, {
|
1941
|
-
formRef,
|
1942
|
-
resetAfterSubmit
|
1943
|
-
}), subaction && /* @__PURE__ */ React.createElement("input", {
|
1944
|
-
type: "hidden",
|
1945
|
-
value: subaction,
|
1946
|
-
name: "subaction"
|
1947
|
-
}), id && /* @__PURE__ */ React.createElement("input", {
|
1948
|
-
type: "hidden",
|
1949
|
-
value: id,
|
1950
|
-
name: FORM_ID_FIELD
|
1951
|
-
}), children)));
|
1615
|
+
);
|
1952
1616
|
}
|
1953
|
-
|
1617
|
+
|
1618
|
+
// src/validation/createValidator.ts
|
1619
|
+
import * as R4 from "remeda";
|
1620
|
+
|
1621
|
+
// src/internal/flatten.ts
|
1622
|
+
var objectFromPathEntries = (entries) => {
|
1954
1623
|
const map = new MultiValueMap();
|
1955
1624
|
entries.forEach(([key, value]) => map.add(key, value));
|
1956
1625
|
return [...map.entries()].reduce(
|
@@ -1958,12 +1627,14 @@ const objectFromPathEntries = (entries) => {
|
|
1958
1627
|
{}
|
1959
1628
|
);
|
1960
1629
|
};
|
1961
|
-
|
1630
|
+
|
1631
|
+
// src/validation/createValidator.ts
|
1632
|
+
var preprocessFormData = (data) => {
|
1962
1633
|
if ("entries" in data && typeof data.entries === "function")
|
1963
1634
|
return objectFromPathEntries([...data.entries()]);
|
1964
1635
|
return objectFromPathEntries(Object.entries(data));
|
1965
1636
|
};
|
1966
|
-
|
1637
|
+
var omitInternalFields = (data) => R4.omit(data, [FORM_ID_FIELD]);
|
1967
1638
|
function createValidator(validator) {
|
1968
1639
|
return {
|
1969
1640
|
validate: async (value) => {
|
@@ -1991,7 +1662,13 @@ function createValidator(validator) {
|
|
1991
1662
|
validateField: (data, field) => validator.validateField(preprocessFormData(data), field)
|
1992
1663
|
};
|
1993
1664
|
}
|
1994
|
-
|
1665
|
+
|
1666
|
+
// src/userFacingFormContext.ts
|
1667
|
+
import { useCallback as useCallback5, useMemo as useMemo4 } from "react";
|
1668
|
+
|
1669
|
+
// src/unreleased/formStateHooks.ts
|
1670
|
+
import { useMemo as useMemo3 } from "react";
|
1671
|
+
var useFormState = (formId) => {
|
1995
1672
|
const formContext = useInternalFormContext(formId, "useFormState");
|
1996
1673
|
const isSubmitting = useInternalIsSubmitting(formContext.formId);
|
1997
1674
|
const hasBeenSubmitted = useInternalHasBeenSubmitted(formContext.formId);
|
@@ -2005,7 +1682,7 @@ const useFormState = (formId) => {
|
|
2005
1682
|
const fieldErrorsFromState = useFieldErrors(formContext.formId);
|
2006
1683
|
const fieldErrorsToUse = useFieldErrorsForForm(formContext);
|
2007
1684
|
const hydratedFieldErrors = fieldErrorsToUse.hydrateTo(fieldErrorsFromState);
|
2008
|
-
return
|
1685
|
+
return useMemo3(
|
2009
1686
|
() => ({
|
2010
1687
|
action,
|
2011
1688
|
subaction,
|
@@ -2028,7 +1705,7 @@ const useFormState = (formId) => {
|
|
2028
1705
|
]
|
2029
1706
|
);
|
2030
1707
|
};
|
2031
|
-
|
1708
|
+
var useFormHelpers = (formId) => {
|
2032
1709
|
const formContext = useInternalFormContext(formId, "useFormHelpers");
|
2033
1710
|
const setTouched = useSetTouched(formContext);
|
2034
1711
|
const validateField = useValidateField(formContext.formId);
|
@@ -2038,7 +1715,7 @@ const useFormHelpers = (formId) => {
|
|
2038
1715
|
const reset = useResetFormElement(formContext.formId);
|
2039
1716
|
const submit = useSubmitForm(formContext.formId);
|
2040
1717
|
const getValues = useFormValues(formContext.formId);
|
2041
|
-
return
|
1718
|
+
return useMemo3(
|
2042
1719
|
() => ({
|
2043
1720
|
setTouched,
|
2044
1721
|
validateField,
|
@@ -2061,7 +1738,9 @@ const useFormHelpers = (formId) => {
|
|
2061
1738
|
]
|
2062
1739
|
);
|
2063
1740
|
};
|
2064
|
-
|
1741
|
+
|
1742
|
+
// src/userFacingFormContext.ts
|
1743
|
+
var useFormContext = (formId) => {
|
2065
1744
|
const context = useInternalFormContext(formId, "useFormContext");
|
2066
1745
|
const state = useFormState(formId);
|
2067
1746
|
const {
|
@@ -2075,7 +1754,7 @@ const useFormContext = (formId) => {
|
|
2075
1754
|
getValues
|
2076
1755
|
} = useFormHelpers(formId);
|
2077
1756
|
const registerReceiveFocus = useRegisterReceiveFocus(context.formId);
|
2078
|
-
const clearError =
|
1757
|
+
const clearError = useCallback5(
|
2079
1758
|
(...names) => {
|
2080
1759
|
names.forEach((name) => {
|
2081
1760
|
internalClearError(name);
|
@@ -2083,7 +1762,7 @@ const useFormContext = (formId) => {
|
|
2083
1762
|
},
|
2084
1763
|
[internalClearError]
|
2085
1764
|
);
|
2086
|
-
return
|
1765
|
+
return useMemo4(
|
2087
1766
|
() => ({
|
2088
1767
|
...state,
|
2089
1768
|
setFieldTouched: setTouched,
|
@@ -2110,7 +1789,13 @@ const useFormContext = (formId) => {
|
|
2110
1789
|
]
|
2111
1790
|
);
|
2112
1791
|
};
|
2113
|
-
|
1792
|
+
|
1793
|
+
// src/internal/state/fieldArray.tsx
|
1794
|
+
import { useMemo as useMemo5 } from "react";
|
1795
|
+
import { useCallback as useCallback6 } from "react";
|
1796
|
+
import invariant4 from "tiny-invariant";
|
1797
|
+
import { Fragment as Fragment2, jsx as jsx2 } from "react/jsx-runtime";
|
1798
|
+
var useInternalFieldArray = (context, field, validationBehavior) => {
|
2114
1799
|
const value = useFieldDefaultValue(field, context);
|
2115
1800
|
useRegisterControlledField(context, field);
|
2116
1801
|
const hasBeenSubmitted = useInternalHasBeenSubmitted(context.formId);
|
@@ -2122,12 +1807,12 @@ const useInternalFieldArray = (context, field, validationBehavior) => {
|
|
2122
1807
|
...validationBehavior
|
2123
1808
|
};
|
2124
1809
|
const behavior = hasBeenSubmitted ? resolvedValidationBehavior.whenSubmitted : resolvedValidationBehavior.initial;
|
2125
|
-
const maybeValidate =
|
1810
|
+
const maybeValidate = useCallback6(() => {
|
2126
1811
|
if (behavior === "onChange") {
|
2127
1812
|
validateField(field);
|
2128
1813
|
}
|
2129
1814
|
}, [behavior, field, validateField]);
|
2130
|
-
|
1815
|
+
invariant4(
|
2131
1816
|
value === void 0 || value === null || Array.isArray(value),
|
2132
1817
|
`FieldArray: defaultValue value for ${field} must be an array, null, or undefined`
|
2133
1818
|
);
|
@@ -2135,7 +1820,7 @@ const useInternalFieldArray = (context, field, validationBehavior) => {
|
|
2135
1820
|
context.formId,
|
2136
1821
|
(state) => state.controlledFields.array
|
2137
1822
|
);
|
2138
|
-
const helpers =
|
1823
|
+
const helpers = useMemo5(
|
2139
1824
|
() => ({
|
2140
1825
|
push: (item) => {
|
2141
1826
|
arr.push(field, item);
|
@@ -2172,14 +1857,14 @@ const useInternalFieldArray = (context, field, validationBehavior) => {
|
|
2172
1857
|
}),
|
2173
1858
|
[arr, field, maybeValidate]
|
2174
1859
|
);
|
2175
|
-
const arrayValue =
|
1860
|
+
const arrayValue = useMemo5(() => value != null ? value : [], [value]);
|
2176
1861
|
return [arrayValue, helpers, error];
|
2177
1862
|
};
|
2178
1863
|
function useFieldArray(name, { formId, validationBehavior } = {}) {
|
2179
1864
|
const context = useInternalFormContext(formId, "FieldArray");
|
2180
1865
|
return useInternalFieldArray(context, name, validationBehavior);
|
2181
1866
|
}
|
2182
|
-
|
1867
|
+
var FieldArray = ({
|
2183
1868
|
name,
|
2184
1869
|
children,
|
2185
1870
|
formId,
|
@@ -2191,7 +1876,20 @@ const FieldArray = ({
|
|
2191
1876
|
name,
|
2192
1877
|
validationBehavior
|
2193
1878
|
);
|
2194
|
-
return /* @__PURE__ */
|
1879
|
+
return /* @__PURE__ */ jsx2(Fragment2, { children: children(value, helpers, error) });
|
1880
|
+
};
|
1881
|
+
export {
|
1882
|
+
FieldArray,
|
1883
|
+
ValidatedForm,
|
1884
|
+
createValidator,
|
1885
|
+
setFormDefaults,
|
1886
|
+
useControlField,
|
1887
|
+
useField,
|
1888
|
+
useFieldArray,
|
1889
|
+
useFormContext,
|
1890
|
+
useIsSubmitting,
|
1891
|
+
useIsValid,
|
1892
|
+
useUpdateControlledField,
|
1893
|
+
validationError
|
2195
1894
|
};
|
2196
|
-
|
2197
|
-
//# sourceMappingURL=remix-validated-form.es.js.map
|
1895
|
+
//# sourceMappingURL=index.esm.js.map
|