@ilokesto/form 1.0.1 → 1.0.3

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 (137) hide show
  1. package/dist/adapters/FormInput.d.ts +1 -1
  2. package/dist/adapters/FormInput.js +10 -0
  3. package/dist/adapters/FormStateSummary.d.ts +1 -1
  4. package/dist/adapters/FormStateSummary.js +27 -0
  5. package/dist/adapters/dom/FieldValue.d.ts +1 -1
  6. package/dist/adapters/dom/FieldValue.js +34 -0
  7. package/dist/adapters/dom/RegisterBinding.d.ts +2 -2
  8. package/dist/adapters/dom/RegisterBinding.js +110 -0
  9. package/dist/adapters/dom/SubmitHandler.d.ts +2 -2
  10. package/dist/adapters/dom/SubmitHandler.js +6 -0
  11. package/dist/adapters/dom/index.d.ts +5 -5
  12. package/dist/adapters/dom/index.js +3 -0
  13. package/dist/adapters/dom/types.d.ts +1 -1
  14. package/dist/adapters/dom/types.js +1 -0
  15. package/dist/core/array/ArrayItemReorder.js +24 -0
  16. package/dist/core/array/ArrayKeyGenerator.js +7 -0
  17. package/dist/core/array/FormArrayController.d.ts +3 -3
  18. package/dist/core/array/FormArrayController.js +54 -0
  19. package/dist/core/array/FormArrayFactory.d.ts +2 -2
  20. package/dist/core/array/FormArrayFactory.js +13 -0
  21. package/dist/core/array/FormArrayMutationPlanner.d.ts +1 -1
  22. package/dist/core/array/FormArrayMutationPlanner.js +68 -0
  23. package/dist/core/array/FormArrayPath.d.ts +1 -1
  24. package/dist/core/array/FormArrayPath.js +11 -0
  25. package/dist/core/array/FormArrayRebaser.d.ts +4 -4
  26. package/dist/core/array/FormArrayRebaser.js +54 -0
  27. package/dist/core/array/index.d.ts +1 -1
  28. package/dist/core/array/index.js +1 -0
  29. package/dist/core/form/CreateForm.d.ts +1 -1
  30. package/dist/core/form/CreateForm.js +64 -0
  31. package/dist/core/form/FormFieldCommands.d.ts +3 -3
  32. package/dist/core/form/FormFieldCommands.js +40 -0
  33. package/dist/core/form/FormSubmitter.d.ts +3 -3
  34. package/dist/core/form/FormSubmitter.js +26 -0
  35. package/dist/core/form/index.d.ts +1 -1
  36. package/dist/core/form/index.js +1 -0
  37. package/dist/core/index.d.ts +2 -2
  38. package/dist/core/index.js +1 -0
  39. package/dist/core/path/FormPath.d.ts +1 -1
  40. package/dist/core/path/FormPath.js +36 -0
  41. package/dist/core/path/index.d.ts +1 -1
  42. package/dist/core/path/index.js +1 -0
  43. package/dist/core/state/FieldStateFactory.d.ts +1 -1
  44. package/dist/core/state/FieldStateFactory.js +19 -0
  45. package/dist/core/state/FormStateInitializer.d.ts +1 -1
  46. package/dist/core/state/FormStateInitializer.js +45 -0
  47. package/dist/core/state/FormStateReader.d.ts +1 -1
  48. package/dist/core/state/FormStateReader.js +30 -0
  49. package/dist/core/state/FormStateStore.d.ts +1 -1
  50. package/dist/core/state/FormStateStore.js +66 -0
  51. package/dist/core/state/FormStateWriter.d.ts +1 -1
  52. package/dist/core/state/FormStateWriter.js +107 -0
  53. package/dist/core/state/index.d.ts +1 -1
  54. package/dist/core/state/index.js +1 -0
  55. package/dist/core/types.d.ts +7 -0
  56. package/dist/core/types.js +1 -0
  57. package/dist/core/validation/StandardSchemaValidator.d.ts +1 -1
  58. package/dist/core/validation/StandardSchemaValidator.js +63 -0
  59. package/dist/core/validation/ValidationEngine.d.ts +2 -2
  60. package/dist/core/validation/ValidationEngine.js +116 -0
  61. package/dist/core/validation/index.d.ts +1 -1
  62. package/dist/core/validation/index.js +1 -0
  63. package/dist/core/value/ValueHelper.d.ts +1 -1
  64. package/dist/core/value/ValueHelper.js +66 -0
  65. package/dist/core/value/index.d.ts +1 -1
  66. package/dist/core/value/index.js +1 -0
  67. package/dist/index.d.ts +2 -2
  68. package/dist/index.js +1 -4
  69. package/dist/react/RegisterBinding.d.ts +3 -3
  70. package/dist/react/RegisterBinding.js +21 -0
  71. package/dist/react/index.d.ts +2 -2
  72. package/dist/react/index.js +1 -87
  73. package/dist/react/types.d.ts +5 -3
  74. package/dist/react/types.js +1 -0
  75. package/dist/react/useField.d.ts +2 -2
  76. package/dist/react/useField.js +23 -0
  77. package/dist/react/useFieldSchemaRegistration.d.ts +2 -2
  78. package/dist/react/useFieldSchemaRegistration.js +25 -0
  79. package/dist/react/useFieldState.d.ts +3 -0
  80. package/dist/react/useFieldState.js +6 -0
  81. package/dist/react/useForm.d.ts +2 -2
  82. package/dist/react/useForm.js +30 -0
  83. package/dist/react/useFormSnapshot.d.ts +1 -1
  84. package/dist/react/useFormSnapshot.js +4 -0
  85. package/dist/react/useFormState.d.ts +2 -2
  86. package/dist/react/useFormState.js +7 -0
  87. package/dist/react/useRegister.d.ts +2 -2
  88. package/dist/react/useRegister.js +26 -0
  89. package/dist/solid/RegisterBinding.d.ts +3 -3
  90. package/dist/solid/RegisterBinding.js +28 -0
  91. package/dist/solid/index.d.ts +2 -2
  92. package/dist/solid/index.js +1 -117
  93. package/dist/solid/types.d.ts +3 -3
  94. package/dist/solid/types.js +1 -0
  95. package/dist/solid/useField.d.ts +2 -2
  96. package/dist/solid/useField.js +28 -0
  97. package/dist/solid/useFieldSchemaRegistration.d.ts +2 -2
  98. package/dist/solid/useFieldSchemaRegistration.js +15 -0
  99. package/dist/solid/useForm.d.ts +2 -2
  100. package/dist/solid/useForm.js +13 -0
  101. package/dist/solid/useFormSnapshot.d.ts +1 -1
  102. package/dist/solid/useFormSnapshot.js +9 -0
  103. package/dist/solid/useFormState.d.ts +2 -2
  104. package/dist/solid/useFormState.js +38 -0
  105. package/dist/solid/useRegister.d.ts +2 -2
  106. package/dist/solid/useRegister.js +21 -0
  107. package/dist/svelte/RegisterAction.d.ts +2 -2
  108. package/dist/svelte/RegisterAction.js +121 -0
  109. package/dist/svelte/index.d.ts +2 -2
  110. package/dist/svelte/index.js +1 -111
  111. package/dist/svelte/types.d.ts +4 -4
  112. package/dist/svelte/types.js +1 -0
  113. package/dist/svelte/useForm.d.ts +2 -2
  114. package/dist/svelte/useForm.js +17 -0
  115. package/dist/svelte/useFormState.d.ts +2 -2
  116. package/dist/svelte/useFormState.js +10 -0
  117. package/dist/vue/RegisterBinding.d.ts +3 -3
  118. package/dist/vue/RegisterBinding.js +28 -0
  119. package/dist/vue/index.d.ts +2 -2
  120. package/dist/vue/index.js +1 -117
  121. package/dist/vue/types.d.ts +3 -3
  122. package/dist/vue/types.js +1 -0
  123. package/dist/vue/useField.d.ts +2 -2
  124. package/dist/vue/useField.js +28 -0
  125. package/dist/vue/useFieldSchemaRegistration.d.ts +2 -2
  126. package/dist/vue/useFieldSchemaRegistration.js +17 -0
  127. package/dist/vue/useForm.d.ts +2 -2
  128. package/dist/vue/useForm.js +13 -0
  129. package/dist/vue/useFormSnapshot.d.ts +1 -1
  130. package/dist/vue/useFormSnapshot.js +11 -0
  131. package/dist/vue/useFormState.d.ts +2 -2
  132. package/dist/vue/useFormState.js +38 -0
  133. package/dist/vue/useRegister.d.ts +2 -2
  134. package/dist/vue/useRegister.js +21 -0
  135. package/package.json +2 -2
  136. package/dist/CreateForm-hQ4cxAK-.js +0 -1283
  137. package/dist/FormStateSummary-gmEQdNXj.js +0 -140
@@ -1,4 +1,4 @@
1
- import { type CreateFormOptions, type Form, type ResetOptions } from '../core/index.js';
1
+ import { type CreateFormOptions, type Form, type ResetOptions } from '../core/index';
2
2
  export type ReactiveFormOptions<TValues> = CreateFormOptions<TValues> & {
3
3
  values?: TValues;
4
4
  resetOptions?: ResetOptions;
@@ -0,0 +1,10 @@
1
+ import { CreateForm } from '../core/index';
2
+ export function isFormInstance(input) {
3
+ return 'subscribe' in input && 'submit' in input;
4
+ }
5
+ export function createFormFromOptions(options) {
6
+ const { values, resetOptions, ...createOptions } = options;
7
+ void values;
8
+ void resetOptions;
9
+ return new CreateForm(createOptions);
10
+ }
@@ -1,4 +1,4 @@
1
- import type { FormError, FormState } from '../core/index.js';
1
+ import type { FormError, FormState } from '../core/index';
2
2
  export type FormStateSummary<TValues> = {
3
3
  state: Readonly<FormState<TValues>>;
4
4
  errors: Record<string, FormError[]>;
@@ -0,0 +1,27 @@
1
+ export function createFormStateSummary(state) {
2
+ const errors = collectErrors(state);
3
+ const dirtyFields = collectFlaggedFields(state, 'dirty');
4
+ const touchedFields = collectFlaggedFields(state, 'touched');
5
+ return {
6
+ state,
7
+ errors,
8
+ dirtyFields,
9
+ touchedFields,
10
+ isDirty: Object.keys(dirtyFields).length > 0,
11
+ isValid: Object.keys(errors).length === 0,
12
+ isSubmitting: state.isSubmitting,
13
+ isSubmitted: state.isSubmitted,
14
+ isSubmitSuccessful: state.isSubmitSuccessful,
15
+ submitCount: state.submitCount,
16
+ };
17
+ }
18
+ function collectErrors(state) {
19
+ return Object.fromEntries(Object.entries(state.fields)
20
+ .filter(([, field]) => field.errors.length > 0)
21
+ .map(([key, field]) => [key, [...field.errors]]));
22
+ }
23
+ function collectFlaggedFields(state, flag) {
24
+ return Object.fromEntries(Object.entries(state.fields)
25
+ .filter(([, field]) => field[flag])
26
+ .map(([key]) => [key, true]));
27
+ }
@@ -1,2 +1,2 @@
1
- import type { FieldPathInput, FieldState, Form, FormState } from '../../core/index.js';
1
+ import type { FieldPathInput, FieldState, Form, FormState } from '../../core/index';
2
2
  export declare function getFieldState<TValues>(form: Form<TValues>, state: Readonly<FormState<TValues>>, name: FieldPathInput): Readonly<FieldState>;
@@ -0,0 +1,34 @@
1
+ export function getFieldState(form, state, name) {
2
+ const field = form.getFieldState(name);
3
+ if (fieldPathExists(state, name)) {
4
+ return field;
5
+ }
6
+ const value = getValueAtPath(form.getValues(), toFieldPath(name));
7
+ return {
8
+ ...field,
9
+ value,
10
+ };
11
+ }
12
+ function toFieldPath(name) {
13
+ return typeof name === 'string' ? [name] : name;
14
+ }
15
+ function fieldPathExists(state, name) {
16
+ return pathInputToKey(name) in state.fields;
17
+ }
18
+ function pathInputToKey(name) {
19
+ const path = toFieldPath(name);
20
+ return path.length === 0 ? '$' : JSON.stringify(path);
21
+ }
22
+ function getValueAtPath(source, path) {
23
+ let current = source;
24
+ for (const segment of path) {
25
+ if (current === null || current === undefined) {
26
+ return undefined;
27
+ }
28
+ if (typeof current !== 'object' && typeof current !== 'function') {
29
+ return undefined;
30
+ }
31
+ current = current[segment];
32
+ }
33
+ return current;
34
+ }
@@ -1,5 +1,5 @@
1
- import type { FieldPathInput, FieldState, Form } from '../../core/index.js';
2
- import type { DomValue, FieldEventTarget, RegisterOptions } from './types.js';
1
+ import type { FieldPathInput, FieldState, Form } from '../../core/index';
2
+ import type { DomValue, FieldEventTarget, RegisterOptions } from './types';
3
3
  export type DomBindingCore = {
4
4
  readonly name: string;
5
5
  readonly type: string;
@@ -0,0 +1,110 @@
1
+ const SKIP_CHANGE = Symbol('skip change');
2
+ export function createDomBinding(form, options, getField, domName) {
3
+ const inputType = options.type ?? 'text';
4
+ return {
5
+ name: domName,
6
+ type: inputType,
7
+ get value() {
8
+ return getBoundValue(inputType, getField().value, getOptionValue(options));
9
+ },
10
+ get checked() {
11
+ return getBoundChecked(inputType, getField().value, getOptionValue(options));
12
+ },
13
+ input(target) {
14
+ if (shouldIgnoreInputEvent(target, options)) {
15
+ return;
16
+ }
17
+ commitEventValue(form, options, getField().value, target);
18
+ },
19
+ change(target) {
20
+ commitEventValue(form, options, getField().value, target);
21
+ },
22
+ blur() {
23
+ void form.blur(options.name);
24
+ },
25
+ focus() {
26
+ form.focus(options.name);
27
+ },
28
+ };
29
+ }
30
+ export function fieldPathToDomName(name) {
31
+ return typeof name === 'string' ? name : JSON.stringify(name);
32
+ }
33
+ function commitEventValue(form, options, currentValue, target) {
34
+ const nextValue = getEventValue(target, options, currentValue);
35
+ if (nextValue !== SKIP_CHANGE) {
36
+ form.setValue(options.name, nextValue, { source: 'user' });
37
+ }
38
+ }
39
+ function shouldIgnoreInputEvent(target, options) {
40
+ const type = options.type ?? target.type;
41
+ return type === 'checkbox' || type === 'radio' || Boolean(target.multiple);
42
+ }
43
+ function getOptionValue(options) {
44
+ return options.value ?? options.checkedValue;
45
+ }
46
+ function getBoundValue(kind, fieldValue, optionValue) {
47
+ if (kind === 'checkbox') {
48
+ return toDomValue(optionValue);
49
+ }
50
+ if (kind === 'radio') {
51
+ return toDomValue(optionValue) ?? '';
52
+ }
53
+ return toDomValue(fieldValue) ?? '';
54
+ }
55
+ function getBoundChecked(kind, fieldValue, optionValue) {
56
+ if (kind === 'checkbox') {
57
+ if (optionValue !== undefined && Array.isArray(fieldValue)) {
58
+ return fieldValue.some(item => Object.is(item, optionValue));
59
+ }
60
+ if (optionValue !== undefined) {
61
+ return Object.is(fieldValue, optionValue);
62
+ }
63
+ return Boolean(fieldValue);
64
+ }
65
+ if (kind === 'radio') {
66
+ return Object.is(fieldValue, optionValue);
67
+ }
68
+ return undefined;
69
+ }
70
+ function getEventValue(target, options, currentValue) {
71
+ const type = options.type ?? target.type;
72
+ if (type === 'checkbox') {
73
+ return getCheckboxEventValue(target, options, currentValue);
74
+ }
75
+ if (type === 'radio') {
76
+ if (!target.checked) {
77
+ return SKIP_CHANGE;
78
+ }
79
+ return getOptionValue(options) ?? target.value;
80
+ }
81
+ if (target.multiple && target.selectedOptions) {
82
+ return [...target.selectedOptions].map(option => option.value);
83
+ }
84
+ return target.value;
85
+ }
86
+ function getCheckboxEventValue(target, options, currentValue) {
87
+ if (options.checkedValue === undefined && options.value === undefined) {
88
+ return Boolean(target.checked);
89
+ }
90
+ if (Array.isArray(currentValue)) {
91
+ const optionValue = getOptionValue(options);
92
+ if (target.checked) {
93
+ return currentValue.some(item => Object.is(item, optionValue)) ? currentValue : [...currentValue, optionValue];
94
+ }
95
+ return currentValue.filter(item => !Object.is(item, optionValue));
96
+ }
97
+ return target.checked ? getOptionValue(options) : (options.uncheckedValue ?? false);
98
+ }
99
+ function toDomValue(value) {
100
+ if (value === undefined || value === null) {
101
+ return undefined;
102
+ }
103
+ if (typeof value === 'string' || typeof value === 'number') {
104
+ return value;
105
+ }
106
+ if (Array.isArray(value)) {
107
+ return value.map(item => String(item));
108
+ }
109
+ return String(value);
110
+ }
@@ -1,5 +1,5 @@
1
- import type { FieldState, Form } from '../../core/index.js';
2
- import type { PathKey } from '../../core/types.js';
1
+ import type { FieldState, Form } from '../../core/index';
2
+ import type { PathKey } from '../../core/types';
3
3
  export type SubmitEventLike = {
4
4
  preventDefault?: () => void;
5
5
  };
@@ -0,0 +1,6 @@
1
+ export function createSubmitHandler(form, onValid, onInvalid) {
2
+ return async (event) => {
3
+ event?.preventDefault?.();
4
+ return form.submit(onValid, onInvalid);
5
+ };
6
+ }
@@ -1,5 +1,5 @@
1
- export { getFieldState } from './FieldValue.js';
2
- export { createDomBinding, fieldPathToDomName, type DomBindingCore } from './RegisterBinding.js';
3
- export { createSubmitHandler } from './SubmitHandler.js';
4
- export type { SubmitEventLike, SubmitHandler, SubmitInvalidHandler, SubmitValidHandler } from './SubmitHandler.js';
5
- export type { DomValue, FieldEventTarget, FieldHookOptions, RegisterOptions } from './types.js';
1
+ export { getFieldState } from './FieldValue';
2
+ export { createDomBinding, fieldPathToDomName, type DomBindingCore } from './RegisterBinding';
3
+ export { createSubmitHandler } from './SubmitHandler';
4
+ export type { SubmitEventLike, SubmitHandler, SubmitInvalidHandler, SubmitValidHandler } from './SubmitHandler';
5
+ export type { DomValue, FieldEventTarget, FieldHookOptions, RegisterOptions } from './types';
@@ -0,0 +1,3 @@
1
+ export { getFieldState } from './FieldValue';
2
+ export { createDomBinding, fieldPathToDomName } from './RegisterBinding';
3
+ export { createSubmitHandler } from './SubmitHandler';
@@ -1,4 +1,4 @@
1
- import type { FieldPathInput, StandardSchemaV1 } from '../../core/index.js';
1
+ import type { FieldPathInput, StandardSchemaV1 } from '../../core/index';
2
2
  export type FieldHookOptions = {
3
3
  name: FieldPathInput;
4
4
  schema?: StandardSchemaV1<unknown, unknown>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,24 @@
1
+ export class ArrayItemReorder {
2
+ static createIndexMapper(previousLength, nextOrder) {
3
+ const positions = new Map();
4
+ nextOrder.forEach((previousIndex, nextIndex) => {
5
+ if (previousIndex >= 0 && previousIndex < previousLength) {
6
+ positions.set(previousIndex, nextIndex);
7
+ }
8
+ });
9
+ return index => positions.get(index);
10
+ }
11
+ static moveItem(items, fromIndex, toIndex) {
12
+ const nextItems = [...items];
13
+ const [item] = nextItems.splice(fromIndex, 1);
14
+ nextItems.splice(toIndex, 0, item);
15
+ return nextItems;
16
+ }
17
+ static swapItems(items, leftIndex, rightIndex) {
18
+ const nextItems = [...items];
19
+ const left = nextItems[leftIndex];
20
+ nextItems[leftIndex] = nextItems[rightIndex];
21
+ nextItems[rightIndex] = left;
22
+ return nextItems;
23
+ }
24
+ }
@@ -0,0 +1,7 @@
1
+ export class ArrayKeyGenerator {
2
+ id = 0;
3
+ create() {
4
+ this.id += 1;
5
+ return `item-${this.id}`;
6
+ }
7
+ }
@@ -1,6 +1,6 @@
1
- import { ArrayKeyGenerator } from './ArrayKeyGenerator.js';
2
- import { FormStateStore } from '../state/index.js';
3
- import type { FieldPath, FormArray } from '../types.js';
1
+ import { ArrayKeyGenerator } from './ArrayKeyGenerator';
2
+ import { FormStateStore } from '../state/index';
3
+ import type { FieldPath, FormArray } from '../types';
4
4
  export declare class FormArrayController<TValues> implements FormArray {
5
5
  private readonly store;
6
6
  private readonly keyGenerator;
@@ -0,0 +1,54 @@
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,5 +1,5 @@
1
- import { FormStateStore } from '../state/index.js';
2
- import type { FieldPath, FormArray } from '../types.js';
1
+ import { FormStateStore } from '../state/index';
2
+ import type { FieldPath, FormArray } from '../types';
3
3
  export declare class FormArrayFactory<TValues> {
4
4
  private readonly store;
5
5
  private readonly keys;
@@ -0,0 +1,13 @@
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,4 +1,4 @@
1
- import { type IndexMapper } from './ArrayItemReorder.js';
1
+ import { type IndexMapper } from './ArrayItemReorder';
2
2
  export type FormArrayMutation = {
3
3
  values: readonly unknown[];
4
4
  keys: readonly string[];
@@ -0,0 +1,68 @@
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,4 +1,4 @@
1
- import type { FieldPath } from '../types.js';
1
+ import type { FieldPath } from '../types';
2
2
  export declare class FormArrayPath {
3
3
  static hasPathPrefix(fieldPath: FieldPath, prefix: FieldPath): boolean;
4
4
  static isArrayChildPath(fieldPath: FieldPath, arrayPath: FieldPath): boolean;
@@ -0,0 +1,11 @@
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 +1,7 @@
1
- import type { IndexMapper } from './ArrayItemReorder.js';
2
- import { FormStateStore } from '../state/index.js';
3
- import type { FieldPath, FormState } from '../types.js';
4
- export type { IndexMapper } from './ArrayItemReorder.js';
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
5
  export declare class FormArrayRebaser {
6
6
  static rebase<TValues>(store: FormStateStore<TValues>, fieldPath: FieldPath, nextArray: readonly unknown[], nextKeys: readonly string[], mapPreviousIndex: IndexMapper): FormState<TValues>;
7
7
  }
@@ -0,0 +1,54 @@
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 +1 @@
1
- export { FormArrayFactory } from './FormArrayFactory.js';
1
+ export { FormArrayFactory } from './FormArrayFactory';
@@ -0,0 +1 @@
1
+ export { FormArrayFactory } from './FormArrayFactory';
@@ -1,4 +1,4 @@
1
- import type { CreateFormOptions, FieldPathInput, FieldSchemaOptions, FieldState, Form, FormArray, FormError, FormState, PathKey, ResetOptions, SetValueOptions } from '../types.js';
1
+ import type { CreateFormOptions, FieldPathInput, FieldSchemaOptions, FieldState, Form, FormArray, FormError, FormState, PathKey, ResetOptions, SetValueOptions } from '../types';
2
2
  export declare class CreateForm<TValues> implements Form<TValues> {
3
3
  private readonly store;
4
4
  private readonly fields;
@@ -0,0 +1,64 @@
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,6 +1,6 @@
1
- import type { FormStateStore } from '../state/index.js';
2
- import type { ValidationEngine } from '../validation/index.js';
3
- import type { FieldPathInput, FieldSchemaOptions, FormError, SetValueOptions } from '../types.js';
1
+ import type { FormStateStore } from '../state/index';
2
+ import type { ValidationEngine } from '../validation/index';
3
+ import type { FieldPathInput, FieldSchemaOptions, FormError, SetValueOptions } from '../types';
4
4
  export declare class FormFieldCommands<TValues> {
5
5
  private readonly store;
6
6
  private readonly validation;