@ilokesto/form 1.0.3 → 2.0.1

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 (151) hide show
  1. package/LICENSE +21 -0
  2. package/README.ko.md +153 -487
  3. package/README.md +155 -490
  4. package/dist/FormInput-DuT7QFe-.d.ts +37 -0
  5. package/dist/chunk-4F4SAGQ5.js +1465 -0
  6. package/dist/chunk-4F4SAGQ5.js.map +1 -0
  7. package/dist/chunk-JK644RR7.js +215 -0
  8. package/dist/chunk-JK644RR7.js.map +1 -0
  9. package/dist/index.d.ts +58 -2
  10. package/dist/index.js +3 -1
  11. package/dist/index.js.map +1 -0
  12. package/dist/react/index.d.ts +87 -2
  13. package/dist/react/index.js +131 -1
  14. package/dist/react/index.js.map +1 -0
  15. package/dist/solid/index.d.ts +89 -2
  16. package/dist/solid/index.js +170 -1
  17. package/dist/solid/index.js.map +1 -0
  18. package/dist/svelte/index.d.ts +68 -2
  19. package/dist/svelte/index.js +198 -1
  20. package/dist/svelte/index.js.map +1 -0
  21. package/dist/types-DBlvL6AC.d.ts +229 -0
  22. package/dist/vue/index.d.ts +87 -2
  23. package/dist/vue/index.js +192 -1
  24. package/dist/vue/index.js.map +1 -0
  25. package/package.json +15 -8
  26. package/dist/adapters/FormInput.d.ts +0 -8
  27. package/dist/adapters/FormInput.js +0 -10
  28. package/dist/adapters/FormStateSummary.d.ts +0 -14
  29. package/dist/adapters/FormStateSummary.js +0 -27
  30. package/dist/adapters/dom/FieldValue.d.ts +0 -2
  31. package/dist/adapters/dom/FieldValue.js +0 -34
  32. package/dist/adapters/dom/RegisterBinding.d.ts +0 -14
  33. package/dist/adapters/dom/RegisterBinding.js +0 -110
  34. package/dist/adapters/dom/SubmitHandler.d.ts +0 -9
  35. package/dist/adapters/dom/SubmitHandler.js +0 -6
  36. package/dist/adapters/dom/index.d.ts +0 -5
  37. package/dist/adapters/dom/index.js +0 -3
  38. package/dist/adapters/dom/types.d.ts +0 -22
  39. package/dist/adapters/dom/types.js +0 -1
  40. package/dist/core/array/ArrayItemReorder.d.ts +0 -6
  41. package/dist/core/array/ArrayItemReorder.js +0 -24
  42. package/dist/core/array/ArrayKeyGenerator.d.ts +0 -4
  43. package/dist/core/array/ArrayKeyGenerator.js +0 -7
  44. package/dist/core/array/FormArrayController.d.ts +0 -21
  45. package/dist/core/array/FormArrayController.js +0 -54
  46. package/dist/core/array/FormArrayFactory.d.ts +0 -8
  47. package/dist/core/array/FormArrayFactory.js +0 -13
  48. package/dist/core/array/FormArrayMutationPlanner.d.ts +0 -14
  49. package/dist/core/array/FormArrayMutationPlanner.js +0 -68
  50. package/dist/core/array/FormArrayPath.d.ts +0 -6
  51. package/dist/core/array/FormArrayPath.js +0 -11
  52. package/dist/core/array/FormArrayRebaser.d.ts +0 -7
  53. package/dist/core/array/FormArrayRebaser.js +0 -54
  54. package/dist/core/array/index.d.ts +0 -1
  55. package/dist/core/array/index.js +0 -1
  56. package/dist/core/form/CreateForm.d.ts +0 -23
  57. package/dist/core/form/CreateForm.js +0 -64
  58. package/dist/core/form/FormFieldCommands.d.ts +0 -15
  59. package/dist/core/form/FormFieldCommands.js +0 -40
  60. package/dist/core/form/FormSubmitter.d.ts +0 -9
  61. package/dist/core/form/FormSubmitter.js +0 -26
  62. package/dist/core/form/index.d.ts +0 -1
  63. package/dist/core/form/index.js +0 -1
  64. package/dist/core/index.d.ts +0 -2
  65. package/dist/core/index.js +0 -1
  66. package/dist/core/path/FormPath.d.ts +0 -9
  67. package/dist/core/path/FormPath.js +0 -36
  68. package/dist/core/path/index.d.ts +0 -1
  69. package/dist/core/path/index.js +0 -1
  70. package/dist/core/state/FieldStateFactory.d.ts +0 -6
  71. package/dist/core/state/FieldStateFactory.js +0 -19
  72. package/dist/core/state/FormStateInitializer.d.ts +0 -7
  73. package/dist/core/state/FormStateInitializer.js +0 -45
  74. package/dist/core/state/FormStateReader.d.ts +0 -11
  75. package/dist/core/state/FormStateReader.js +0 -30
  76. package/dist/core/state/FormStateStore.d.ts +0 -24
  77. package/dist/core/state/FormStateStore.js +0 -66
  78. package/dist/core/state/FormStateWriter.d.ts +0 -17
  79. package/dist/core/state/FormStateWriter.js +0 -107
  80. package/dist/core/state/index.d.ts +0 -1
  81. package/dist/core/state/index.js +0 -1
  82. package/dist/core/types.d.ts +0 -112
  83. package/dist/core/types.js +0 -1
  84. package/dist/core/validation/StandardSchemaValidator.d.ts +0 -14
  85. package/dist/core/validation/StandardSchemaValidator.js +0 -63
  86. package/dist/core/validation/ValidationEngine.d.ts +0 -25
  87. package/dist/core/validation/ValidationEngine.js +0 -116
  88. package/dist/core/validation/index.d.ts +0 -1
  89. package/dist/core/validation/index.js +0 -1
  90. package/dist/core/value/ValueHelper.d.ts +0 -7
  91. package/dist/core/value/ValueHelper.js +0 -66
  92. package/dist/core/value/index.d.ts +0 -1
  93. package/dist/core/value/index.js +0 -1
  94. package/dist/react/RegisterBinding.d.ts +0 -5
  95. package/dist/react/RegisterBinding.js +0 -21
  96. package/dist/react/types.d.ts +0 -73
  97. package/dist/react/types.js +0 -1
  98. package/dist/react/useField.d.ts +0 -3
  99. package/dist/react/useField.js +0 -23
  100. package/dist/react/useFieldSchemaRegistration.d.ts +0 -4
  101. package/dist/react/useFieldSchemaRegistration.js +0 -25
  102. package/dist/react/useFieldState.d.ts +0 -3
  103. package/dist/react/useFieldState.js +0 -6
  104. package/dist/react/useForm.d.ts +0 -4
  105. package/dist/react/useForm.js +0 -30
  106. package/dist/react/useFormSnapshot.d.ts +0 -2
  107. package/dist/react/useFormSnapshot.js +0 -4
  108. package/dist/react/useFormState.d.ts +0 -3
  109. package/dist/react/useFormState.js +0 -7
  110. package/dist/react/useRegister.d.ts +0 -6
  111. package/dist/react/useRegister.js +0 -26
  112. package/dist/solid/RegisterBinding.d.ts +0 -5
  113. package/dist/solid/RegisterBinding.js +0 -28
  114. package/dist/solid/types.d.ts +0 -71
  115. package/dist/solid/types.js +0 -1
  116. package/dist/solid/useField.d.ts +0 -3
  117. package/dist/solid/useField.js +0 -28
  118. package/dist/solid/useFieldSchemaRegistration.d.ts +0 -3
  119. package/dist/solid/useFieldSchemaRegistration.js +0 -15
  120. package/dist/solid/useForm.d.ts +0 -4
  121. package/dist/solid/useForm.js +0 -13
  122. package/dist/solid/useFormSnapshot.d.ts +0 -3
  123. package/dist/solid/useFormSnapshot.js +0 -9
  124. package/dist/solid/useFormState.d.ts +0 -3
  125. package/dist/solid/useFormState.js +0 -38
  126. package/dist/solid/useRegister.d.ts +0 -6
  127. package/dist/solid/useRegister.js +0 -21
  128. package/dist/svelte/RegisterAction.d.ts +0 -4
  129. package/dist/svelte/RegisterAction.js +0 -121
  130. package/dist/svelte/types.d.ts +0 -13
  131. package/dist/svelte/types.js +0 -1
  132. package/dist/svelte/useForm.d.ts +0 -4
  133. package/dist/svelte/useForm.js +0 -17
  134. package/dist/svelte/useFormState.d.ts +0 -4
  135. package/dist/svelte/useFormState.js +0 -10
  136. package/dist/vue/RegisterBinding.d.ts +0 -5
  137. package/dist/vue/RegisterBinding.js +0 -28
  138. package/dist/vue/types.d.ts +0 -71
  139. package/dist/vue/types.js +0 -1
  140. package/dist/vue/useField.d.ts +0 -3
  141. package/dist/vue/useField.js +0 -28
  142. package/dist/vue/useFieldSchemaRegistration.d.ts +0 -3
  143. package/dist/vue/useFieldSchemaRegistration.js +0 -17
  144. package/dist/vue/useForm.d.ts +0 -4
  145. package/dist/vue/useForm.js +0 -13
  146. package/dist/vue/useFormSnapshot.d.ts +0 -3
  147. package/dist/vue/useFormSnapshot.js +0 -11
  148. package/dist/vue/useFormState.d.ts +0 -3
  149. package/dist/vue/useFormState.js +0 -38
  150. package/dist/vue/useRegister.d.ts +0 -6
  151. package/dist/vue/useRegister.js +0 -21
@@ -1,54 +0,0 @@
1
- import { FormArrayMutationPlanner } from './FormArrayMutationPlanner';
2
- import { FormPath } from '../path/index';
3
- import { FormArrayRebaser } from './FormArrayRebaser';
4
- export class FormArrayController {
5
- store;
6
- keyGenerator;
7
- fieldPath;
8
- mutations;
9
- constructor(store, keys, fieldPath) {
10
- this.store = store;
11
- this.keyGenerator = keys;
12
- this.fieldPath = fieldPath;
13
- this.mutations = new FormArrayMutationPlanner();
14
- }
15
- keys() {
16
- return this.getKeys();
17
- }
18
- insert(index, value) {
19
- this.applyMutation(this.mutations.insert(this.getArray(), this.getKeys(), index, value, this.keyGenerator.create()));
20
- }
21
- push(value) {
22
- this.applyMutation(this.mutations.push(this.getArray(), this.getKeys(), value, this.keyGenerator.create()));
23
- }
24
- remove(index) {
25
- this.applyMutation(this.mutations.remove(this.getArray(), this.getKeys(), index));
26
- }
27
- move(fromIndex, toIndex) {
28
- this.applyMutation(this.mutations.move(this.getArray(), this.getKeys(), fromIndex, toIndex));
29
- }
30
- swap(leftIndex, rightIndex) {
31
- this.applyMutation(this.mutations.swap(this.getArray(), this.getKeys(), leftIndex, rightIndex));
32
- }
33
- replace(values) {
34
- this.applyMutation(this.mutations.replace(values, values.map(() => this.keyGenerator.create())));
35
- }
36
- getArray() {
37
- const value = this.store.getValueAtPath(this.fieldPath);
38
- return Array.isArray(value) ? value : [];
39
- }
40
- getKeys() {
41
- const arrayKey = FormPath.pathToKey(this.fieldPath);
42
- const currentArray = this.getArray();
43
- return this.store.getState().arrayKeys[arrayKey] ?? currentArray.map(() => this.keyGenerator.create());
44
- }
45
- applyMutation(mutation) {
46
- if (!mutation) {
47
- return;
48
- }
49
- this.rebase(mutation.values, mutation.keys, mutation.mapPreviousIndex);
50
- }
51
- rebase(nextArray, nextKeys, mapPreviousIndex) {
52
- this.store.replaceState(() => FormArrayRebaser.rebase(this.store, this.fieldPath, nextArray, nextKeys, mapPreviousIndex));
53
- }
54
- }
@@ -1,8 +0,0 @@
1
- import { FormStateStore } from '../state/index';
2
- import type { FieldPath, FormArray } from '../types';
3
- export declare class FormArrayFactory<TValues> {
4
- private readonly store;
5
- private readonly keys;
6
- constructor(store: FormStateStore<TValues>);
7
- create(fieldPath: FieldPath): FormArray;
8
- }
@@ -1,13 +0,0 @@
1
- import { ArrayKeyGenerator } from './ArrayKeyGenerator';
2
- import { FormArrayController } from './FormArrayController';
3
- export class FormArrayFactory {
4
- store;
5
- keys;
6
- constructor(store) {
7
- this.store = store;
8
- this.keys = new ArrayKeyGenerator();
9
- }
10
- create(fieldPath) {
11
- return new FormArrayController(this.store, this.keys, fieldPath);
12
- }
13
- }
@@ -1,14 +0,0 @@
1
- import { type IndexMapper } from './ArrayItemReorder';
2
- export type FormArrayMutation = {
3
- values: readonly unknown[];
4
- keys: readonly string[];
5
- mapPreviousIndex: IndexMapper;
6
- };
7
- export declare class FormArrayMutationPlanner {
8
- insert(currentArray: readonly unknown[], currentKeys: readonly string[], index: number, value: unknown, nextKey: string): FormArrayMutation;
9
- push(currentArray: readonly unknown[], currentKeys: readonly string[], value: unknown, nextKey: string): FormArrayMutation;
10
- remove(currentArray: readonly unknown[], currentKeys: readonly string[], index: number): FormArrayMutation | undefined;
11
- move(currentArray: readonly unknown[], currentKeys: readonly string[], fromIndex: number, toIndex: number): FormArrayMutation | undefined;
12
- swap(currentArray: readonly unknown[], currentKeys: readonly string[], leftIndex: number, rightIndex: number): FormArrayMutation | undefined;
13
- replace(values: readonly unknown[], nextKeys: readonly string[]): FormArrayMutation;
14
- }
@@ -1,68 +0,0 @@
1
- import { ArrayItemReorder } from './ArrayItemReorder';
2
- export class FormArrayMutationPlanner {
3
- insert(currentArray, currentKeys, index, value, nextKey) {
4
- const boundedIndex = Math.max(0, Math.min(index, currentArray.length));
5
- const nextArray = [...currentArray];
6
- const nextKeys = [...currentKeys];
7
- const nextOrder = [...currentArray.keys()];
8
- nextArray.splice(boundedIndex, 0, value);
9
- nextKeys.splice(boundedIndex, 0, nextKey);
10
- nextOrder.splice(boundedIndex, 0, -1);
11
- return {
12
- values: nextArray,
13
- keys: nextKeys,
14
- mapPreviousIndex: ArrayItemReorder.createIndexMapper(currentArray.length, nextOrder),
15
- };
16
- }
17
- push(currentArray, currentKeys, value, nextKey) {
18
- return {
19
- values: [...currentArray, value],
20
- keys: [...currentKeys, nextKey],
21
- mapPreviousIndex: ArrayItemReorder.createIndexMapper(currentArray.length, [...currentArray.keys(), -1]),
22
- };
23
- }
24
- remove(currentArray, currentKeys, index) {
25
- if (index < 0 || index >= currentArray.length) {
26
- return undefined;
27
- }
28
- const nextOrder = [...currentArray.keys()].filter(itemIndex => itemIndex !== index);
29
- return {
30
- values: currentArray.filter((_, itemIndex) => itemIndex !== index),
31
- keys: currentKeys.filter((_, itemIndex) => itemIndex !== index),
32
- mapPreviousIndex: ArrayItemReorder.createIndexMapper(currentArray.length, nextOrder),
33
- };
34
- }
35
- move(currentArray, currentKeys, fromIndex, toIndex) {
36
- if (fromIndex < 0 || fromIndex >= currentArray.length || toIndex < 0 || toIndex >= currentArray.length || fromIndex === toIndex) {
37
- return undefined;
38
- }
39
- const nextOrder = ArrayItemReorder.moveItem([...currentArray.keys()], fromIndex, toIndex);
40
- return {
41
- values: ArrayItemReorder.moveItem(currentArray, fromIndex, toIndex),
42
- keys: ArrayItemReorder.moveItem(currentKeys, fromIndex, toIndex),
43
- mapPreviousIndex: ArrayItemReorder.createIndexMapper(currentArray.length, nextOrder),
44
- };
45
- }
46
- swap(currentArray, currentKeys, leftIndex, rightIndex) {
47
- if (leftIndex < 0 ||
48
- rightIndex < 0 ||
49
- leftIndex >= currentArray.length ||
50
- rightIndex >= currentArray.length ||
51
- leftIndex === rightIndex) {
52
- return undefined;
53
- }
54
- const nextOrder = ArrayItemReorder.swapItems([...currentArray.keys()], leftIndex, rightIndex);
55
- return {
56
- values: ArrayItemReorder.swapItems(currentArray, leftIndex, rightIndex),
57
- keys: ArrayItemReorder.swapItems(currentKeys, leftIndex, rightIndex),
58
- mapPreviousIndex: ArrayItemReorder.createIndexMapper(currentArray.length, nextOrder),
59
- };
60
- }
61
- replace(values, nextKeys) {
62
- return {
63
- values: [...values],
64
- keys: [...nextKeys],
65
- mapPreviousIndex: () => undefined,
66
- };
67
- }
68
- }
@@ -1,6 +0,0 @@
1
- import type { FieldPath } from '../types';
2
- export declare class FormArrayPath {
3
- static hasPathPrefix(fieldPath: FieldPath, prefix: FieldPath): boolean;
4
- static isArrayChildPath(fieldPath: FieldPath, arrayPath: FieldPath): boolean;
5
- static replaceArrayIndex(fieldPath: FieldPath, arrayPath: FieldPath, nextIndex: number): FieldPath;
6
- }
@@ -1,11 +0,0 @@
1
- export class FormArrayPath {
2
- static hasPathPrefix(fieldPath, prefix) {
3
- return prefix.every((segment, index) => Object.is(fieldPath[index], segment));
4
- }
5
- static isArrayChildPath(fieldPath, arrayPath) {
6
- return fieldPath.length > arrayPath.length && this.hasPathPrefix(fieldPath, arrayPath) && typeof fieldPath[arrayPath.length] === 'number';
7
- }
8
- static replaceArrayIndex(fieldPath, arrayPath, nextIndex) {
9
- return [...arrayPath, nextIndex, ...fieldPath.slice(arrayPath.length + 1)];
10
- }
11
- }
@@ -1,7 +0,0 @@
1
- import type { IndexMapper } from './ArrayItemReorder';
2
- import { FormStateStore } from '../state/index';
3
- import type { FieldPath, FormState } from '../types';
4
- export type { IndexMapper } from './ArrayItemReorder';
5
- export declare class FormArrayRebaser {
6
- static rebase<TValues>(store: FormStateStore<TValues>, fieldPath: FieldPath, nextArray: readonly unknown[], nextKeys: readonly string[], mapPreviousIndex: IndexMapper): FormState<TValues>;
7
- }
@@ -1,54 +0,0 @@
1
- import { FormArrayPath } from './FormArrayPath';
2
- import { FormStateInitializer } from '../state/FormStateInitializer';
3
- import { FormPath } from '../path/index';
4
- import { ValueHelper } from '../value/index';
5
- import { FormStateStore } from '../state/index';
6
- export class FormArrayRebaser {
7
- static rebase(store, fieldPath, nextArray, nextKeys, mapPreviousIndex) {
8
- const fieldPaths = store.getKnownFieldPaths();
9
- const nextValues = ValueHelper.setValueAtPath(store.getValues(), fieldPath, [...nextArray]);
10
- const initializedState = FormStateInitializer.initialize(nextValues);
11
- const arrayKey = FormPath.pathToKey(fieldPath);
12
- const rebasedFields = { ...initializedState.fields };
13
- Object.entries(store.getState().fields).forEach(([fieldKey, previousField]) => {
14
- const currentFieldPath = fieldPaths[fieldKey];
15
- if (!currentFieldPath) {
16
- return;
17
- }
18
- if (!FormArrayPath.isArrayChildPath(currentFieldPath, fieldPath)) {
19
- rebasedFields[fieldKey] = previousField;
20
- return;
21
- }
22
- const previousIndex = currentFieldPath[fieldPath.length];
23
- if (typeof previousIndex !== 'number') {
24
- return;
25
- }
26
- const nextIndex = mapPreviousIndex(previousIndex);
27
- if (nextIndex === undefined) {
28
- return;
29
- }
30
- const nextFieldPath = FormArrayPath.replaceArrayIndex(currentFieldPath, fieldPath, nextIndex);
31
- const nextFieldKey = FormPath.pathToKey(nextFieldPath);
32
- const nextField = initializedState.fields[nextFieldKey] ?? FormStateStore.getDefaultFieldState();
33
- rebasedFields[nextFieldKey] = {
34
- ...nextField,
35
- errors: previousField.errors,
36
- touched: previousField.touched,
37
- dirty: previousField.dirty,
38
- modified: previousField.modified,
39
- };
40
- });
41
- return {
42
- ...initializedState,
43
- fields: rebasedFields,
44
- arrayKeys: {
45
- ...initializedState.arrayKeys,
46
- [arrayKey]: [...nextKeys],
47
- },
48
- submitCount: store.getState().submitCount,
49
- isSubmitting: store.getState().isSubmitting,
50
- isSubmitted: store.getState().isSubmitted,
51
- isSubmitSuccessful: store.getState().isSubmitSuccessful,
52
- };
53
- }
54
- }
@@ -1 +0,0 @@
1
- export { FormArrayFactory } from './FormArrayFactory';
@@ -1 +0,0 @@
1
- export { FormArrayFactory } from './FormArrayFactory';
@@ -1,23 +0,0 @@
1
- import type { CreateFormOptions, FieldPathInput, FieldSchemaOptions, FieldState, Form, FormArray, FormError, FormState, PathKey, ResetOptions, SetValueOptions } from '../types';
2
- export declare class CreateForm<TValues> implements Form<TValues> {
3
- private readonly store;
4
- private readonly fields;
5
- private readonly arrays;
6
- private readonly submitter;
7
- constructor(options: CreateFormOptions<TValues>);
8
- getState(): Readonly<FormState<TValues>>;
9
- subscribe(listener: () => void): () => void;
10
- registerFieldSchema(path: FieldPathInput, options: FieldSchemaOptions): () => void;
11
- getFieldState(path: FieldPathInput): Readonly<FieldState>;
12
- getValue(path: FieldPathInput): unknown;
13
- getValues(): TValues;
14
- setValue(path: FieldPathInput, value: unknown, options?: SetValueOptions): void;
15
- blur(path: FieldPathInput): Promise<boolean>;
16
- focus(path: FieldPathInput): void;
17
- setErrors(path: FieldPathInput, errors: readonly FormError[]): void;
18
- clearErrors(...paths: FieldPathInput[]): void;
19
- trigger(...paths: FieldPathInput[]): Promise<boolean>;
20
- array(path: FieldPathInput): FormArray;
21
- reset(values?: TValues, options?: ResetOptions): void;
22
- submit<TResult>(onValid: (values: TValues) => TResult | Promise<TResult>, onInvalid?: (fields: Readonly<Record<PathKey, FieldState>>) => void): Promise<TResult | undefined>;
23
- }
@@ -1,64 +0,0 @@
1
- import { FormArrayFactory } from '../array/index';
2
- import { FormFieldCommands } from './FormFieldCommands';
3
- import { FormSubmitter } from './FormSubmitter';
4
- import { FormStateStore } from '../state/index';
5
- import { FormPath } from '../path/index';
6
- import { ValidationEngine } from '../validation/index';
7
- export class CreateForm {
8
- store;
9
- fields;
10
- arrays;
11
- submitter;
12
- constructor(options) {
13
- this.store = new FormStateStore(options.defaultValues);
14
- const validation = new ValidationEngine(this.store, options);
15
- this.fields = new FormFieldCommands(this.store, validation);
16
- this.arrays = new FormArrayFactory(this.store);
17
- this.submitter = new FormSubmitter(this.store, validation);
18
- }
19
- getState() {
20
- return this.store.getState();
21
- }
22
- subscribe(listener) {
23
- return this.store.subscribe(listener);
24
- }
25
- registerFieldSchema(path, options) {
26
- return this.fields.registerFieldSchema(path, options);
27
- }
28
- getFieldState(path) {
29
- return this.store.getFieldState(path);
30
- }
31
- getValue(path) {
32
- return this.store.getValue(path);
33
- }
34
- getValues() {
35
- return this.store.getValues();
36
- }
37
- setValue(path, value, options = {}) {
38
- this.fields.setValue(path, value, options);
39
- }
40
- blur(path) {
41
- return this.fields.blur(path);
42
- }
43
- focus(path) {
44
- this.fields.focus(path);
45
- }
46
- setErrors(path, errors) {
47
- this.fields.setErrors(path, errors);
48
- }
49
- clearErrors(...paths) {
50
- this.fields.clearErrors(paths);
51
- }
52
- trigger(...paths) {
53
- return this.fields.trigger(paths);
54
- }
55
- array(path) {
56
- return this.arrays.create(FormPath.toFieldPath(path));
57
- }
58
- reset(values, options) {
59
- this.store.reset(values, options);
60
- }
61
- submit(onValid, onInvalid) {
62
- return this.submitter.submit(onValid, onInvalid);
63
- }
64
- }
@@ -1,15 +0,0 @@
1
- import type { FormStateStore } from '../state/index';
2
- import type { ValidationEngine } from '../validation/index';
3
- import type { FieldPathInput, FieldSchemaOptions, FormError, SetValueOptions } from '../types';
4
- export declare class FormFieldCommands<TValues> {
5
- private readonly store;
6
- private readonly validation;
7
- constructor(store: FormStateStore<TValues>, validation: ValidationEngine<TValues>);
8
- setValue(path: FieldPathInput, value: unknown, options?: SetValueOptions): void;
9
- registerFieldSchema(path: FieldPathInput, options: FieldSchemaOptions): () => void;
10
- blur(path: FieldPathInput): Promise<boolean>;
11
- focus(_path: FieldPathInput): void;
12
- setErrors(path: FieldPathInput, errors: readonly FormError[]): void;
13
- clearErrors(paths: readonly FieldPathInput[]): void;
14
- trigger(paths: readonly FieldPathInput[]): Promise<boolean>;
15
- }
@@ -1,40 +0,0 @@
1
- import { FormPath } from '../path/index';
2
- export class FormFieldCommands {
3
- store;
4
- validation;
5
- constructor(store, validation) {
6
- this.store = store;
7
- this.validation = validation;
8
- }
9
- setValue(path, value, options = {}) {
10
- const fieldKey = this.store.setValue(FormPath.toFieldPath(path), value, options);
11
- if (options.validate || this.validation.shouldValidateOn('change')) {
12
- void this.validation.validateField(fieldKey, 'change');
13
- }
14
- }
15
- registerFieldSchema(path, options) {
16
- return this.validation.registerFieldSchema(FormPath.pathInputToKey(path), options);
17
- }
18
- async blur(path) {
19
- const fieldKey = FormPath.pathInputToKey(path);
20
- this.store.touchField(fieldKey);
21
- if (!this.validation.shouldValidateOn('blur')) {
22
- return true;
23
- }
24
- return this.validation.validateField(fieldKey, 'blur');
25
- }
26
- focus(_path) {
27
- }
28
- setErrors(path, errors) {
29
- this.store.setErrorsByKey(FormPath.pathInputToKey(path), errors);
30
- }
31
- clearErrors(paths) {
32
- this.store.clearErrors(paths.length > 0 ? paths.map(path => FormPath.pathInputToKey(path)) : undefined);
33
- }
34
- trigger(paths) {
35
- if (paths.length === 0) {
36
- return this.validation.validateRegisteredFields('manual');
37
- }
38
- return this.validation.validateFields(paths.map(path => FormPath.pathInputToKey(path)), 'manual');
39
- }
40
- }
@@ -1,9 +0,0 @@
1
- import type { FormStateStore } from '../state/index';
2
- import type { ValidationEngine } from '../validation/index';
3
- import type { FieldState, PathKey } from '../types';
4
- export declare class FormSubmitter<TValues> {
5
- private readonly store;
6
- private readonly validation;
7
- constructor(store: FormStateStore<TValues>, validation: ValidationEngine<TValues>);
8
- submit<TResult>(onValid: (values: TValues) => TResult | Promise<TResult>, onInvalid?: (fields: Readonly<Record<PathKey, FieldState>>) => void): Promise<TResult | undefined>;
9
- }
@@ -1,26 +0,0 @@
1
- export class FormSubmitter {
2
- store;
3
- validation;
4
- constructor(store, validation) {
5
- this.store = store;
6
- this.validation = validation;
7
- }
8
- async submit(onValid, onInvalid) {
9
- this.store.beginSubmit();
10
- try {
11
- const isValid = await this.validation.validateRegisteredFields('submit');
12
- if (!isValid) {
13
- onInvalid?.(this.store.getState().fields);
14
- this.store.completeSubmit(false);
15
- return undefined;
16
- }
17
- const result = await onValid(this.store.getValues());
18
- this.store.completeSubmit(true);
19
- return result;
20
- }
21
- catch (error) {
22
- this.store.completeSubmit(false);
23
- throw error;
24
- }
25
- }
26
- }
@@ -1 +0,0 @@
1
- export { CreateForm } from './CreateForm';
@@ -1 +0,0 @@
1
- export { CreateForm } from './CreateForm';
@@ -1,2 +0,0 @@
1
- export { CreateForm } from './form/index';
2
- export type { CreateFormOptions, FieldSchemaOptions, FieldPathValue, FieldPathInput, FieldState, Form, FormArray, FormError, FormState, ResetOptions, SetValueOptions, StandardSchemaV1, ValidationTrigger } from './types';
@@ -1 +0,0 @@
1
- export { CreateForm } from './form/index';
@@ -1,9 +0,0 @@
1
- import type { FieldPath, FieldPathInput, PathKey } from '../types';
2
- export declare class FormPath {
3
- static readonly ROOT_PATH_KEY = "$";
4
- static path(...segments: FieldPath): FieldPath;
5
- static toFieldPath(fieldPath: FieldPathInput): FieldPath;
6
- static pathInputToKey(fieldPath: FieldPathInput): PathKey;
7
- static pathToKey(fieldPath: FieldPath): PathKey;
8
- static keyToPath(key: PathKey): FieldPath;
9
- }
@@ -1,36 +0,0 @@
1
- export class FormPath {
2
- static ROOT_PATH_KEY = '$';
3
- static path(...segments) {
4
- return segments;
5
- }
6
- static toFieldPath(fieldPath) {
7
- if (typeof fieldPath === 'string') {
8
- return [fieldPath];
9
- }
10
- return fieldPath;
11
- }
12
- static pathInputToKey(fieldPath) {
13
- return this.pathToKey(this.toFieldPath(fieldPath));
14
- }
15
- static pathToKey(fieldPath) {
16
- if (fieldPath.length === 0) {
17
- return this.ROOT_PATH_KEY;
18
- }
19
- return JSON.stringify(fieldPath);
20
- }
21
- static keyToPath(key) {
22
- if (key === this.ROOT_PATH_KEY) {
23
- return [];
24
- }
25
- const parsed = JSON.parse(key);
26
- if (!Array.isArray(parsed)) {
27
- throw new Error(`Invalid form path key: ${key}`);
28
- }
29
- return parsed.map(segment => {
30
- if (typeof segment === 'string' || typeof segment === 'number') {
31
- return segment;
32
- }
33
- throw new Error(`Invalid form path segment in key: ${key}`);
34
- });
35
- }
36
- }
@@ -1 +0,0 @@
1
- export { FormPath } from './FormPath';
@@ -1 +0,0 @@
1
- export { FormPath } from './FormPath';
@@ -1,6 +0,0 @@
1
- import type { FieldState } from '../types';
2
- export declare class FieldStateFactory {
3
- private static readonly DEFAULT_FIELD_STATE;
4
- static create(value: unknown): FieldState;
5
- static createDefault(): FieldState;
6
- }
@@ -1,19 +0,0 @@
1
- export class FieldStateFactory {
2
- static DEFAULT_FIELD_STATE = {
3
- value: undefined,
4
- errors: [],
5
- touched: false,
6
- dirty: false,
7
- modified: false,
8
- };
9
- static create(value) {
10
- return {
11
- ...this.DEFAULT_FIELD_STATE,
12
- value,
13
- errors: [],
14
- };
15
- }
16
- static createDefault() {
17
- return this.create(undefined);
18
- }
19
- }
@@ -1,7 +0,0 @@
1
- import type { FormState } from '../types';
2
- export declare class FormStateInitializer {
3
- static initialize<TValues>(defaultValues: TValues): FormState<TValues>;
4
- private static isTraversableObject;
5
- private static createInitialArrayKeys;
6
- private static visitInitialValue;
7
- }
@@ -1,45 +0,0 @@
1
- import { FieldStateFactory } from './FieldStateFactory';
2
- import { FormPath } from '../path/index';
3
- export class FormStateInitializer {
4
- static initialize(defaultValues) {
5
- const fields = {};
6
- const arrayKeys = {};
7
- this.visitInitialValue(defaultValues, [], fields, arrayKeys);
8
- return {
9
- defaultValues,
10
- fields,
11
- submitCount: 0,
12
- isSubmitting: false,
13
- isSubmitted: false,
14
- isSubmitSuccessful: false,
15
- arrayKeys,
16
- };
17
- }
18
- static isTraversableObject(value) {
19
- return Object.prototype.toString.call(value) === '[object Object]';
20
- }
21
- static createInitialArrayKeys(length) {
22
- return Array.from({ length }, (_, index) => `initial-${index}`);
23
- }
24
- static visitInitialValue(value, fieldPath, fields, arrayKeys) {
25
- if (Array.isArray(value)) {
26
- arrayKeys[FormPath.pathToKey(fieldPath)] = this.createInitialArrayKeys(value.length);
27
- value.forEach((item, index) => {
28
- this.visitInitialValue(item, [...fieldPath, index], fields, arrayKeys);
29
- });
30
- return;
31
- }
32
- if (this.isTraversableObject(value)) {
33
- const entries = Object.entries(value);
34
- if (entries.length === 0) {
35
- fields[FormPath.pathToKey(fieldPath)] = FieldStateFactory.create(value);
36
- return;
37
- }
38
- entries.forEach(([key, childValue]) => {
39
- this.visitInitialValue(childValue, [...fieldPath, key], fields, arrayKeys);
40
- });
41
- return;
42
- }
43
- fields[FormPath.pathToKey(fieldPath)] = FieldStateFactory.create(value);
44
- }
45
- }
@@ -1,11 +0,0 @@
1
- import type { FieldPath, FieldPathInput, FieldState, FormState, PathKey } from '../types';
2
- export declare class FormStateReader<TValues> {
3
- private readonly getSnapshot;
4
- constructor(getSnapshot: () => Readonly<FormState<TValues>>);
5
- getKnownFieldPaths(): Record<PathKey, FieldPath>;
6
- getFieldStateByKey(fieldKey: PathKey): Readonly<FieldState>;
7
- getFieldState(fieldPath: FieldPathInput): Readonly<FieldState>;
8
- getValue(fieldPath: FieldPathInput): unknown;
9
- getValues(): TValues;
10
- getValueAtPath(fieldPath: FieldPath): unknown;
11
- }
@@ -1,30 +0,0 @@
1
- import { FieldStateFactory } from './FieldStateFactory';
2
- import { FormPath } from '../path/index';
3
- import { ValueHelper } from '../value/index';
4
- export class FormStateReader {
5
- getSnapshot;
6
- constructor(getSnapshot) {
7
- this.getSnapshot = getSnapshot;
8
- }
9
- getKnownFieldPaths() {
10
- return Object.keys(this.getSnapshot().fields).reduce((paths, key) => {
11
- paths[key] = FormPath.keyToPath(key);
12
- return paths;
13
- }, {});
14
- }
15
- getFieldStateByKey(fieldKey) {
16
- return this.getSnapshot().fields[fieldKey] ?? FieldStateFactory.createDefault();
17
- }
18
- getFieldState(fieldPath) {
19
- return this.getFieldStateByKey(FormPath.pathInputToKey(fieldPath));
20
- }
21
- getValue(fieldPath) {
22
- return this.getFieldState(fieldPath).value;
23
- }
24
- getValues() {
25
- return ValueHelper.getValuesFromFields(this.getSnapshot(), this.getKnownFieldPaths());
26
- }
27
- getValueAtPath(fieldPath) {
28
- return ValueHelper.getValueAtPath(this.getValues(), fieldPath);
29
- }
30
- }
@@ -1,24 +0,0 @@
1
- import type { FieldPath, FieldPathInput, FieldState, FormError, FormState, PathKey, ResetOptions, SetValueOptions } from '../types';
2
- export declare class FormStateStore<TValues> {
3
- private readonly store;
4
- private readonly reader;
5
- private readonly writer;
6
- static getDefaultFieldState(): FieldState;
7
- constructor(defaultValues: TValues);
8
- getState(): Readonly<FormState<TValues>>;
9
- subscribe(listener: () => void): () => void;
10
- getKnownFieldPaths(): Record<PathKey, FieldPath>;
11
- getFieldStateByKey(fieldKey: PathKey): Readonly<FieldState>;
12
- getFieldState(fieldPath: FieldPathInput): Readonly<FieldState>;
13
- getValue(fieldPath: FieldPathInput): unknown;
14
- getValues(): TValues;
15
- getValueAtPath(fieldPath: FieldPath): unknown;
16
- setValue(fieldPath: FieldPath, value: unknown, options?: SetValueOptions): PathKey;
17
- touchField(fieldKey: PathKey): void;
18
- setErrorsByKey(fieldKey: PathKey, errors: readonly FormError[]): void;
19
- clearErrors(fieldKeys?: readonly PathKey[]): void;
20
- reset(values?: TValues, options?: ResetOptions): void;
21
- beginSubmit(): void;
22
- completeSubmit(successful: boolean): void;
23
- replaceState(updater: (previousState: FormState<TValues>) => FormState<TValues>): void;
24
- }