remix-validated-form 5.1.4 → 5.1.6

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.
Files changed (75) hide show
  1. package/.turbo/turbo-build.log +10 -10
  2. package/dist/index.cjs.js +4 -1
  3. package/dist/index.cjs.js.map +1 -1
  4. package/dist/index.esm.js +4 -1
  5. package/dist/index.esm.js.map +1 -1
  6. package/package.json +1 -4
  7. package/.turbo/turbo-dev.log +0 -0
  8. package/.turbo/turbo-typecheck.log +0 -1
  9. package/browser/ValidatedForm.d.ts +0 -50
  10. package/browser/ValidatedForm.js +0 -210
  11. package/browser/hooks.d.ts +0 -67
  12. package/browser/hooks.js +0 -84
  13. package/browser/index.d.ts +0 -7
  14. package/browser/index.js +0 -7
  15. package/browser/internal/MultiValueMap.d.ts +0 -11
  16. package/browser/internal/MultiValueMap.js +0 -43
  17. package/browser/internal/constants.d.ts +0 -3
  18. package/browser/internal/constants.js +0 -3
  19. package/browser/internal/flatten.d.ts +0 -1
  20. package/browser/internal/flatten.js +0 -7
  21. package/browser/internal/formContext.d.ts +0 -12
  22. package/browser/internal/formContext.js +0 -2
  23. package/browser/internal/getInputProps.d.ts +0 -29
  24. package/browser/internal/getInputProps.js +0 -51
  25. package/browser/internal/hooks.d.ts +0 -35
  26. package/browser/internal/hooks.js +0 -118
  27. package/browser/internal/hydratable.d.ts +0 -14
  28. package/browser/internal/hydratable.js +0 -14
  29. package/browser/internal/logic/getCheckboxChecked.d.ts +0 -1
  30. package/browser/internal/logic/getCheckboxChecked.js +0 -9
  31. package/browser/internal/logic/getRadioChecked.d.ts +0 -1
  32. package/browser/internal/logic/getRadioChecked.js +0 -15
  33. package/browser/internal/logic/nestedObjectToPathObject.d.ts +0 -1
  34. package/browser/internal/logic/nestedObjectToPathObject.js +0 -47
  35. package/browser/internal/logic/requestSubmit.d.ts +0 -5
  36. package/browser/internal/logic/requestSubmit.js +0 -66
  37. package/browser/internal/reset.d.ts +0 -28
  38. package/browser/internal/reset.js +0 -13
  39. package/browser/internal/state/arrayUtil.d.ts +0 -12
  40. package/browser/internal/state/arrayUtil.js +0 -350
  41. package/browser/internal/state/atomUtils.d.ts +0 -38
  42. package/browser/internal/state/atomUtils.js +0 -5
  43. package/browser/internal/state/cleanup.d.ts +0 -2
  44. package/browser/internal/state/cleanup.js +0 -6
  45. package/browser/internal/state/controlledFieldStore.d.ts +0 -26
  46. package/browser/internal/state/controlledFieldStore.js +0 -70
  47. package/browser/internal/state/controlledFields.d.ts +0 -7
  48. package/browser/internal/state/controlledFields.js +0 -36
  49. package/browser/internal/state/createFormStore.d.ts +0 -79
  50. package/browser/internal/state/createFormStore.js +0 -306
  51. package/browser/internal/state/fieldArray.d.ts +0 -28
  52. package/browser/internal/state/fieldArray.js +0 -74
  53. package/browser/internal/state/storeFamily.d.ts +0 -9
  54. package/browser/internal/state/storeFamily.js +0 -18
  55. package/browser/internal/state/storeHooks.d.ts +0 -3
  56. package/browser/internal/state/storeHooks.js +0 -4
  57. package/browser/internal/state/types.d.ts +0 -1
  58. package/browser/internal/state/types.js +0 -1
  59. package/browser/internal/state.d.ts +0 -343
  60. package/browser/internal/state.js +0 -64
  61. package/browser/internal/submissionCallbacks.d.ts +0 -1
  62. package/browser/internal/submissionCallbacks.js +0 -13
  63. package/browser/internal/util.d.ts +0 -5
  64. package/browser/internal/util.js +0 -32
  65. package/browser/server.d.ts +0 -21
  66. package/browser/server.js +0 -27
  67. package/browser/unreleased/formStateHooks.d.ts +0 -64
  68. package/browser/unreleased/formStateHooks.js +0 -76
  69. package/browser/userFacingFormContext.d.ts +0 -85
  70. package/browser/userFacingFormContext.js +0 -41
  71. package/browser/validation/createValidator.d.ts +0 -7
  72. package/browser/validation/createValidator.js +0 -43
  73. package/browser/validation/types.d.ts +0 -58
  74. package/browser/validation/types.js +0 -1
  75. package/stats.html +0 -4044
@@ -1,350 +0,0 @@
1
- import { getPath, setPath } from "set-get";
2
- import invariant from "tiny-invariant";
3
- ////
4
- // All of these array helpers are written in a way that mutates the original array.
5
- // This is because we're working with immer.
6
- ////
7
- export const getArray = (values, field) => {
8
- const value = getPath(values, field);
9
- if (value === undefined || value === null) {
10
- const newValue = [];
11
- setPath(values, field, newValue);
12
- return newValue;
13
- }
14
- invariant(Array.isArray(value), `FieldArray: defaultValue value for ${field} must be an array, null, or undefined`);
15
- return value;
16
- };
17
- export const swap = (array, indexA, indexB) => {
18
- const itemA = array[indexA];
19
- const itemB = array[indexB];
20
- const hasItemA = indexA in array;
21
- const hasItemB = indexB in array;
22
- // If we're dealing with a sparse array (i.e. one of the indeces doesn't exist),
23
- // we should keep it sparse
24
- if (hasItemA) {
25
- array[indexB] = itemA;
26
- }
27
- else {
28
- delete array[indexB];
29
- }
30
- if (hasItemB) {
31
- array[indexA] = itemB;
32
- }
33
- else {
34
- delete array[indexA];
35
- }
36
- };
37
- // A splice that can handle sparse arrays
38
- function sparseSplice(array, start, deleteCount, item) {
39
- // Inserting an item into an array won't behave as we need it to if the array isn't
40
- // at least as long as the start index. We can force the array to be long enough like this.
41
- if (array.length < start && item) {
42
- array.length = start;
43
- }
44
- // If we just pass item in, it'll be undefined and splice will delete the item.
45
- if (arguments.length === 4)
46
- return array.splice(start, deleteCount, item);
47
- return array.splice(start, deleteCount);
48
- }
49
- export const move = (array, from, to) => {
50
- const [item] = sparseSplice(array, from, 1);
51
- sparseSplice(array, to, 0, item);
52
- };
53
- export const insert = (array, index, value) => {
54
- sparseSplice(array, index, 0, value);
55
- };
56
- export const remove = (array, index) => {
57
- sparseSplice(array, index, 1);
58
- };
59
- export const replace = (array, index, value) => {
60
- sparseSplice(array, index, 1, value);
61
- };
62
- /**
63
- * The purpose of this helper is to make it easier to update `fieldErrors` and `touchedFields`.
64
- * We key those objects by full paths to the fields.
65
- * When we're doing array mutations, that makes it difficult to update those objects.
66
- */
67
- export const mutateAsArray = (field, obj, mutate) => {
68
- const beforeKeys = new Set();
69
- const arr = [];
70
- for (const [key, value] of Object.entries(obj)) {
71
- if (key.startsWith(field) && key !== field) {
72
- beforeKeys.add(key);
73
- setPath(arr, key.substring(field.length), value);
74
- }
75
- }
76
- mutate(arr);
77
- for (const key of beforeKeys) {
78
- delete obj[key];
79
- }
80
- const newKeys = getDeepArrayPaths(arr);
81
- for (const key of newKeys) {
82
- const val = getPath(arr, key);
83
- if (val !== undefined) {
84
- obj[`${field}${key}`] = val;
85
- }
86
- }
87
- };
88
- const getDeepArrayPaths = (obj, basePath = "") => {
89
- // This only needs to handle arrays and plain objects
90
- // and we can assume the first call is always an array.
91
- if (Array.isArray(obj)) {
92
- return obj.flatMap((item, index) => getDeepArrayPaths(item, `${basePath}[${index}]`));
93
- }
94
- if (typeof obj === "object") {
95
- return Object.keys(obj).flatMap((key) => getDeepArrayPaths(obj[key], `${basePath}.${key}`));
96
- }
97
- return [basePath];
98
- };
99
- if (import.meta.vitest) {
100
- const { describe, expect, it } = import.meta.vitest;
101
- // Count the actual number of items in the array
102
- // instead of just getting the length.
103
- // This is useful for validating that sparse arrays are handled correctly.
104
- const countArrayItems = (arr) => {
105
- let count = 0;
106
- arr.forEach(() => count++);
107
- return count;
108
- };
109
- describe("getArray", () => {
110
- it("shoud get a deeply nested array that can be mutated to update the nested value", () => {
111
- const values = {
112
- d: [
113
- { foo: "bar", baz: [true, false] },
114
- { e: true, f: "hi" },
115
- ],
116
- };
117
- const result = getArray(values, "d[0].baz");
118
- const finalValues = {
119
- d: [
120
- { foo: "bar", baz: [true, false, true] },
121
- { e: true, f: "hi" },
122
- ],
123
- };
124
- expect(result).toEqual([true, false]);
125
- result.push(true);
126
- expect(values).toEqual(finalValues);
127
- });
128
- it("should return an empty array that can be mutated if result is null or undefined", () => {
129
- const values = {};
130
- const result = getArray(values, "a.foo[0].bar");
131
- const finalValues = {
132
- a: { foo: [{ bar: ["Bob ross"] }] },
133
- };
134
- expect(result).toEqual([]);
135
- result.push("Bob ross");
136
- expect(values).toEqual(finalValues);
137
- });
138
- it("should throw if the value is defined and not an array", () => {
139
- const values = { foo: "foo" };
140
- expect(() => getArray(values, "foo")).toThrow();
141
- });
142
- });
143
- describe("swap", () => {
144
- it("should swap two items", () => {
145
- const array = [1, 2, 3];
146
- swap(array, 0, 1);
147
- expect(array).toEqual([2, 1, 3]);
148
- });
149
- it("should work for sparse arrays", () => {
150
- // A bit of a sanity check for native array behavior
151
- const arr = [];
152
- arr[0] = true;
153
- swap(arr, 0, 2);
154
- expect(countArrayItems(arr)).toEqual(1);
155
- expect(0 in arr).toBe(false);
156
- expect(2 in arr).toBe(true);
157
- expect(arr[2]).toEqual(true);
158
- });
159
- });
160
- describe("move", () => {
161
- it("should move an item to a new index", () => {
162
- const array = [1, 2, 3];
163
- move(array, 0, 1);
164
- expect(array).toEqual([2, 1, 3]);
165
- });
166
- it("should work with sparse arrays", () => {
167
- const array = [1];
168
- move(array, 0, 2);
169
- expect(countArrayItems(array)).toEqual(1);
170
- expect(array).toEqual([undefined, undefined, 1]);
171
- });
172
- });
173
- describe("insert", () => {
174
- it("should insert an item at a new index", () => {
175
- const array = [1, 2, 3];
176
- insert(array, 1, 4);
177
- expect(array).toEqual([1, 4, 2, 3]);
178
- });
179
- it("should be able to insert falsey values", () => {
180
- const array = [1, 2, 3];
181
- insert(array, 1, null);
182
- expect(array).toEqual([1, null, 2, 3]);
183
- });
184
- it("should handle sparse arrays", () => {
185
- const array = [];
186
- array[2] = true;
187
- insert(array, 0, true);
188
- expect(countArrayItems(array)).toEqual(2);
189
- expect(array).toEqual([true, undefined, undefined, true]);
190
- });
191
- });
192
- describe("remove", () => {
193
- it("should remove an item at a given index", () => {
194
- const array = [1, 2, 3];
195
- remove(array, 1);
196
- expect(array).toEqual([1, 3]);
197
- });
198
- it("should handle sparse arrays", () => {
199
- const array = [];
200
- array[2] = true;
201
- remove(array, 0);
202
- expect(countArrayItems(array)).toEqual(1);
203
- expect(array).toEqual([undefined, true]);
204
- });
205
- });
206
- describe("replace", () => {
207
- it("should replace an item at a given index", () => {
208
- const array = [1, 2, 3];
209
- replace(array, 1, 4);
210
- expect(array).toEqual([1, 4, 3]);
211
- });
212
- it("should handle sparse arrays", () => {
213
- const array = [];
214
- array[2] = true;
215
- replace(array, 0, true);
216
- expect(countArrayItems(array)).toEqual(2);
217
- expect(array).toEqual([true, undefined, true]);
218
- });
219
- });
220
- describe("mutateAsArray", () => {
221
- it("should handle swap", () => {
222
- const values = {
223
- myField: "something",
224
- "myField[0]": "foo",
225
- "myField[2]": "bar",
226
- otherField: "baz",
227
- "otherField[0]": "something else",
228
- };
229
- mutateAsArray("myField", values, (arr) => {
230
- swap(arr, 0, 2);
231
- });
232
- expect(values).toEqual({
233
- myField: "something",
234
- "myField[0]": "bar",
235
- "myField[2]": "foo",
236
- otherField: "baz",
237
- "otherField[0]": "something else",
238
- });
239
- });
240
- it("should swap sparse arrays", () => {
241
- const values = {
242
- myField: "something",
243
- "myField[0]": "foo",
244
- otherField: "baz",
245
- "otherField[0]": "something else",
246
- };
247
- mutateAsArray("myField", values, (arr) => {
248
- swap(arr, 0, 2);
249
- });
250
- expect(values).toEqual({
251
- myField: "something",
252
- "myField[2]": "foo",
253
- otherField: "baz",
254
- "otherField[0]": "something else",
255
- });
256
- });
257
- it("should handle arrays with nested values", () => {
258
- const values = {
259
- myField: "something",
260
- "myField[0].title": "foo",
261
- "myField[0].note": "bar",
262
- "myField[2].title": "other",
263
- "myField[2].note": "other",
264
- otherField: "baz",
265
- "otherField[0]": "something else",
266
- };
267
- mutateAsArray("myField", values, (arr) => {
268
- swap(arr, 0, 2);
269
- });
270
- expect(values).toEqual({
271
- myField: "something",
272
- "myField[0].title": "other",
273
- "myField[0].note": "other",
274
- "myField[2].title": "foo",
275
- "myField[2].note": "bar",
276
- otherField: "baz",
277
- "otherField[0]": "something else",
278
- });
279
- });
280
- it("should handle move", () => {
281
- const values = {
282
- myField: "something",
283
- "myField[0]": "foo",
284
- "myField[1]": "bar",
285
- "myField[2]": "baz",
286
- "otherField[0]": "something else",
287
- };
288
- mutateAsArray("myField", values, (arr) => {
289
- move(arr, 0, 2);
290
- });
291
- expect(values).toEqual({
292
- myField: "something",
293
- "myField[0]": "bar",
294
- "myField[1]": "baz",
295
- "myField[2]": "foo",
296
- "otherField[0]": "something else",
297
- });
298
- });
299
- it("should not create keys for `undefined`", () => {
300
- const values = {
301
- "myField[0]": "foo",
302
- };
303
- mutateAsArray("myField", values, (arr) => {
304
- arr.unshift(undefined);
305
- });
306
- expect(Object.keys(values)).toHaveLength(1);
307
- expect(values).toEqual({
308
- "myField[1]": "foo",
309
- });
310
- });
311
- it("should handle remove", () => {
312
- const values = {
313
- myField: "something",
314
- "myField[0]": "foo",
315
- "myField[1]": "bar",
316
- "myField[2]": "baz",
317
- "otherField[0]": "something else",
318
- };
319
- mutateAsArray("myField", values, (arr) => {
320
- remove(arr, 1);
321
- });
322
- expect(values).toEqual({
323
- myField: "something",
324
- "myField[0]": "foo",
325
- "myField[1]": "baz",
326
- "otherField[0]": "something else",
327
- });
328
- expect("myField[2]" in values).toBe(false);
329
- });
330
- });
331
- describe("getDeepArrayPaths", () => {
332
- it("should return all paths recursively", () => {
333
- const obj = [
334
- true,
335
- true,
336
- [true, true],
337
- { foo: true, bar: { baz: true, test: [true] } },
338
- ];
339
- expect(getDeepArrayPaths(obj, "myField")).toEqual([
340
- "myField[0]",
341
- "myField[1]",
342
- "myField[2][0]",
343
- "myField[2][1]",
344
- "myField[3].foo",
345
- "myField[3].bar.baz",
346
- "myField[3].bar.test[0]",
347
- ]);
348
- });
349
- });
350
- }
@@ -1,38 +0,0 @@
1
- import { Atom } from "jotai";
2
- export declare type InternalFormId = string | symbol;
3
- export declare const formAtomFamily: <T>(data: T) => {
4
- (param: InternalFormId): Atom<T> & {
5
- write: (get: {
6
- <Value>(atom: Atom<Value | Promise<Value>>): Value;
7
- <Value_1>(atom: Atom<Promise<Value_1>>): Value_1;
8
- <Value_2>(atom: Atom<Value_2>): Value_2 extends Promise<infer V> ? V : Value_2;
9
- } & {
10
- <Value_3>(atom: Atom<Value_3 | Promise<Value_3>>, options: {
11
- unstable_promise: true;
12
- }): Value_3 | Promise<Value_3>;
13
- <Value_4>(atom: Atom<Promise<Value_4>>, options: {
14
- unstable_promise: true;
15
- }): Value_4 | Promise<Value_4>;
16
- <Value_5>(atom: Atom<Value_5>, options: {
17
- unstable_promise: true;
18
- }): (Value_5 extends Promise<infer V> ? V : Value_5) | Promise<Value_5 extends Promise<infer V> ? V : Value_5>;
19
- }, set: {
20
- <Value_6, Result extends void | Promise<void>>(atom: import("jotai").WritableAtom<Value_6, undefined, Result>): Result;
21
- <Value_7, Update, Result_1 extends void | Promise<void>>(atom: import("jotai").WritableAtom<Value_7, Update, Result_1>, update: Update): Result_1;
22
- }, update: T | ((prev: T) => T)) => void;
23
- onMount?: (<S extends import("jotai/core/atom").SetAtom<T | ((prev: T) => T), void>>(setAtom: S) => void | (() => void)) | undefined;
24
- } & {
25
- init: T;
26
- };
27
- remove(param: InternalFormId): void;
28
- setShouldRemove(shouldRemove: ((createdAt: number, param: InternalFormId) => boolean) | null): void;
29
- };
30
- export declare type FieldAtomKey = {
31
- formId: InternalFormId;
32
- field: string;
33
- };
34
- export declare const fieldAtomFamily: <T extends Atom<unknown>>(func: (key: FieldAtomKey) => T) => {
35
- (param: FieldAtomKey): T;
36
- remove(param: FieldAtomKey): void;
37
- setShouldRemove(shouldRemove: ((createdAt: number, param: FieldAtomKey) => boolean) | null): void;
38
- };
@@ -1,5 +0,0 @@
1
- import { atom } from "jotai";
2
- import { atomFamily } from "jotai/utils";
3
- import isEqual from "lodash/isEqual";
4
- export const formAtomFamily = (data) => atomFamily((_) => atom(data));
5
- export const fieldAtomFamily = (func) => atomFamily(func, isEqual);
@@ -1,2 +0,0 @@
1
- import { InternalFormId } from "./storeFamily";
2
- export declare const cleanupFormState: (formId: InternalFormId) => void;
@@ -1,6 +0,0 @@
1
- import { controlledFieldStore } from "./controlledFieldStore";
2
- import { formStore } from "./createFormStore";
3
- export const cleanupFormState = (formId) => {
4
- formStore.remove(formId);
5
- controlledFieldStore.remove(formId);
6
- };
@@ -1,26 +0,0 @@
1
- import { InternalFormId } from "./types";
2
- export declare type FieldState = {
3
- refCount: number;
4
- value: unknown;
5
- defaultValue?: unknown;
6
- hydrated: boolean;
7
- valueUpdatePromise: Promise<void> | undefined;
8
- resolveValueUpdate: (() => void) | undefined;
9
- };
10
- export declare type ControlledFieldState = {
11
- forms: {
12
- [formId: InternalFormId]: {
13
- [fieldName: string]: FieldState | undefined;
14
- };
15
- };
16
- register: (formId: InternalFormId, fieldName: string) => void;
17
- unregister: (formId: InternalFormId, fieldName: string) => void;
18
- getField: (formId: InternalFormId, fieldName: string) => FieldState | undefined;
19
- setValue: (formId: InternalFormId, fieldName: string, value: unknown) => void;
20
- hydrateWithDefault: (formId: InternalFormId, fieldName: string, defaultValue: unknown) => void;
21
- awaitValueUpdate: (formId: InternalFormId, fieldName: string) => Promise<void>;
22
- reset: (formId: InternalFormId) => void;
23
- };
24
- export declare const useControlledFieldStore: import("zustand").UseBoundStore<Omit<import("zustand").StoreApi<ControlledFieldState>, "setState"> & {
25
- setState(nextStateOrUpdater: ControlledFieldState | Partial<ControlledFieldState> | ((state: import("immer/dist/internal").WritableDraft<ControlledFieldState>) => void), shouldReplace?: boolean | undefined): void;
26
- }>;
@@ -1,70 +0,0 @@
1
- import create from "zustand";
2
- import { immer } from "zustand/middleware/immer";
3
- export const useControlledFieldStore = create()(immer((set, get) => ({
4
- forms: {},
5
- register: (formId, field) => set((state) => {
6
- if (!state.forms[formId]) {
7
- state.forms[formId] = {};
8
- }
9
- if (state.forms[formId][field]) {
10
- state.forms[formId][field].refCount++;
11
- }
12
- else {
13
- state.forms[formId][field] = {
14
- refCount: 1,
15
- value: undefined,
16
- hydrated: false,
17
- valueUpdatePromise: undefined,
18
- resolveValueUpdate: undefined,
19
- };
20
- }
21
- }),
22
- unregister: (formId, field) => set((state) => {
23
- var _a;
24
- const formState = (_a = state.forms) === null || _a === void 0 ? void 0 : _a[formId];
25
- const fieldState = formState === null || formState === void 0 ? void 0 : formState[field];
26
- if (!fieldState)
27
- return;
28
- fieldState.refCount--;
29
- if (fieldState.refCount === 0)
30
- delete formState[field];
31
- }),
32
- getField: (formId, field) => {
33
- var _a, _b;
34
- return (_b = (_a = get().forms) === null || _a === void 0 ? void 0 : _a[formId]) === null || _b === void 0 ? void 0 : _b[field];
35
- },
36
- setValue: (formId, field, value) => set((state) => {
37
- var _a, _b;
38
- const fieldState = (_b = (_a = state.forms) === null || _a === void 0 ? void 0 : _a[formId]) === null || _b === void 0 ? void 0 : _b[field];
39
- if (!fieldState)
40
- return;
41
- fieldState.value = value;
42
- const promise = new Promise((resolve) => {
43
- fieldState.resolveValueUpdate = resolve;
44
- });
45
- fieldState.valueUpdatePromise = promise;
46
- }),
47
- hydrateWithDefault: (formId, field, defaultValue) => set((state) => {
48
- var _a;
49
- const fieldState = (_a = state.forms[formId]) === null || _a === void 0 ? void 0 : _a[field];
50
- if (!fieldState)
51
- return;
52
- fieldState.value = defaultValue;
53
- fieldState.defaultValue = defaultValue;
54
- fieldState.hydrated = true;
55
- }),
56
- awaitValueUpdate: async (formId, field) => {
57
- var _a;
58
- await ((_a = get().getField(formId, field)) === null || _a === void 0 ? void 0 : _a.valueUpdatePromise);
59
- },
60
- reset: (formId) => set((state) => {
61
- const formState = state.forms[formId];
62
- if (!formState)
63
- return;
64
- Object.values(formState).forEach((field) => {
65
- if (!field)
66
- return;
67
- field.value = field.defaultValue;
68
- });
69
- }),
70
- })));
@@ -1,7 +0,0 @@
1
- import { InternalFormContextValue } from "../formContext";
2
- import { InternalFormId } from "./types";
3
- export declare const useControlledFieldValue: (context: InternalFormContextValue, field: string) => any;
4
- export declare const useRegisterControlledField: (context: InternalFormContextValue, field: string) => void;
5
- export declare const useControllableValue: (context: InternalFormContextValue, field: string) => readonly [any, (value: unknown) => void];
6
- export declare const useUpdateControllableValue: (formId: InternalFormId) => (field: string, value: unknown) => void;
7
- export declare const useAwaitValue: (formId: InternalFormId) => (field: string) => Promise<void>;
@@ -1,36 +0,0 @@
1
- import { useCallback, useEffect } from "react";
2
- import { useFieldDefaultValue } from "../hooks";
3
- import { useFormStore } from "./storeHooks";
4
- export const useControlledFieldValue = (context, field) => {
5
- const value = useFormStore(context.formId, (state) => state.controlledFields.getValue(field));
6
- const isFormHydrated = useFormStore(context.formId, (state) => state.isHydrated);
7
- const defaultValue = useFieldDefaultValue(field, context);
8
- return isFormHydrated ? value : defaultValue;
9
- };
10
- export const useRegisterControlledField = (context, field) => {
11
- const resolveUpdate = useFormStore(context.formId, (state) => state.controlledFields.valueUpdateResolvers[field]);
12
- useEffect(() => {
13
- resolveUpdate === null || resolveUpdate === void 0 ? void 0 : resolveUpdate();
14
- }, [resolveUpdate]);
15
- const register = useFormStore(context.formId, (state) => state.controlledFields.register);
16
- const unregister = useFormStore(context.formId, (state) => state.controlledFields.unregister);
17
- useEffect(() => {
18
- register(field);
19
- return () => unregister(field);
20
- }, [context.formId, field, register, unregister]);
21
- };
22
- export const useControllableValue = (context, field) => {
23
- useRegisterControlledField(context, field);
24
- const setControlledFieldValue = useFormStore(context.formId, (state) => state.controlledFields.setValue);
25
- const setValue = useCallback((value) => setControlledFieldValue(field, value), [field, setControlledFieldValue]);
26
- const value = useControlledFieldValue(context, field);
27
- return [value, setValue];
28
- };
29
- export const useUpdateControllableValue = (formId) => {
30
- const setValue = useFormStore(formId, (state) => state.controlledFields.setValue);
31
- return useCallback((field, value) => setValue(field, value), [setValue]);
32
- };
33
- export const useAwaitValue = (formId) => {
34
- const awaitValue = useFormStore(formId, (state) => state.controlledFields.awaitValueUpdate);
35
- return useCallback((field) => awaitValue(field), [awaitValue]);
36
- };
@@ -1,79 +0,0 @@
1
- import { WritableDraft } from "immer/dist/internal";
2
- import { FieldErrors, TouchedFields, ValidationResult, Validator } from "../../validation/types";
3
- import { InternalFormId } from "./types";
4
- export type SyncedFormProps = {
5
- formId?: string;
6
- action?: string;
7
- subaction?: string;
8
- defaultValues: {
9
- [fieldName: string]: any;
10
- };
11
- registerReceiveFocus: (fieldName: string, handler: () => void) => () => void;
12
- validator: Validator<unknown>;
13
- };
14
- export type FormStoreState = {
15
- forms: {
16
- [formId: InternalFormId]: FormState;
17
- };
18
- form: (formId: InternalFormId) => FormState;
19
- registerForm: (formId: InternalFormId) => void;
20
- cleanupForm: (formId: InternalFormId) => void;
21
- };
22
- export type FormState = {
23
- isHydrated: boolean;
24
- isSubmitting: boolean;
25
- hasBeenSubmitted: boolean;
26
- fieldErrors: FieldErrors;
27
- touchedFields: TouchedFields;
28
- formProps?: SyncedFormProps;
29
- formElement: HTMLFormElement | null;
30
- currentDefaultValues: Record<string, any>;
31
- isValid: () => boolean;
32
- startSubmit: () => void;
33
- endSubmit: () => void;
34
- setTouched: (field: string, touched: boolean) => void;
35
- setFieldError: (field: string, error: string) => void;
36
- setFieldErrors: (errors: FieldErrors) => void;
37
- clearFieldError: (field: string) => void;
38
- reset: () => void;
39
- syncFormProps: (props: SyncedFormProps) => void;
40
- setFormElement: (formElement: HTMLFormElement | null) => void;
41
- validateField: (fieldName: string) => Promise<string | null>;
42
- validate: () => Promise<ValidationResult<unknown>>;
43
- resetFormElement: () => void;
44
- submit: () => void;
45
- getValues: () => FormData;
46
- controlledFields: {
47
- values: {
48
- [fieldName: string]: any;
49
- };
50
- refCounts: {
51
- [fieldName: string]: number;
52
- };
53
- valueUpdatePromises: {
54
- [fieldName: string]: Promise<void>;
55
- };
56
- valueUpdateResolvers: {
57
- [fieldName: string]: () => void;
58
- };
59
- register: (fieldName: string) => void;
60
- unregister: (fieldName: string) => void;
61
- setValue: (fieldName: string, value: unknown) => void;
62
- kickoffValueUpdate: (fieldName: string) => void;
63
- getValue: (fieldName: string) => unknown;
64
- awaitValueUpdate: (fieldName: string) => Promise<void>;
65
- array: {
66
- push: (fieldName: string, value: unknown) => void;
67
- swap: (fieldName: string, indexA: number, indexB: number) => void;
68
- move: (fieldName: string, fromIndex: number, toIndex: number) => void;
69
- insert: (fieldName: string, index: number, value: unknown) => void;
70
- unshift: (fieldName: string, value: unknown) => void;
71
- remove: (fieldName: string, index: number) => void;
72
- pop: (fieldName: string) => void;
73
- replace: (fieldName: string, index: number, value: unknown) => void;
74
- };
75
- };
76
- };
77
- export declare const useRootFormStore: import("zustand").UseBoundStore<Omit<import("zustand").StoreApi<FormStoreState>, "setState"> & {
78
- setState(nextStateOrUpdater: FormStoreState | Partial<FormStoreState> | ((state: WritableDraft<FormStoreState>) => void), shouldReplace?: boolean | undefined): void;
79
- }>;