@ilokesto/form 1.0.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/LICENSE +21 -0
  2. package/README.ko.md +86 -13
  3. package/README.md +86 -15
  4. package/dist/FormInput-DuT7QFe-.d.ts +37 -0
  5. package/dist/chunk-EPQC3LKB.js +1649 -0
  6. package/dist/chunk-EPQC3LKB.js.map +1 -0
  7. package/dist/chunk-OBMSHDV2.js +215 -0
  8. package/dist/chunk-OBMSHDV2.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,121 +0,0 @@
1
- import { createDomBinding, fieldPathToDomName, getFieldState } from '../adapters/dom';
2
- export function createRegisterAction(form) {
3
- return (node, initialOptions) => {
4
- let options = initialOptions;
5
- let cleanupSchema = registerSchema(form, options);
6
- const getBinding = () => createDomBinding(form, resolveOptionsFromNode(node, options), () => getFieldState(form, form.getState(), options.name), fieldPathToDomName(options.name));
7
- const syncNode = () => {
8
- applyBindingToNode(node, getBinding(), options);
9
- };
10
- const onInput = (event) => {
11
- getBinding().input(event.currentTarget);
12
- };
13
- const onChange = (event) => {
14
- getBinding().change(event.currentTarget);
15
- };
16
- const onBlur = () => {
17
- getBinding().blur();
18
- };
19
- const onFocus = () => {
20
- getBinding().focus();
21
- };
22
- node.addEventListener('input', onInput);
23
- node.addEventListener('change', onChange);
24
- node.addEventListener('blur', onBlur);
25
- node.addEventListener('focus', onFocus);
26
- const unsubscribe = form.subscribe(syncNode);
27
- syncNode();
28
- return {
29
- update(nextOptions) {
30
- cleanupSchema();
31
- options = nextOptions;
32
- cleanupSchema = registerSchema(form, options);
33
- syncNode();
34
- },
35
- destroy() {
36
- unsubscribe();
37
- cleanupSchema();
38
- node.removeEventListener('input', onInput);
39
- node.removeEventListener('change', onChange);
40
- node.removeEventListener('blur', onBlur);
41
- node.removeEventListener('focus', onFocus);
42
- },
43
- };
44
- };
45
- }
46
- function registerSchema(form, options) {
47
- if (!options.schema) {
48
- return () => undefined;
49
- }
50
- return form.registerFieldSchema(options.name, { schema: options.schema, schemaOptions: options.schemaOptions });
51
- }
52
- function resolveOptionsFromNode(node, options) {
53
- if (options.type !== undefined || !(node instanceof HTMLInputElement)) {
54
- return options;
55
- }
56
- return {
57
- ...options,
58
- type: node.type || 'text',
59
- };
60
- }
61
- function applyBindingToNode(node, binding, options) {
62
- setNodeName(node, binding.name);
63
- if (node instanceof HTMLInputElement) {
64
- applyInputBinding(node, binding, options);
65
- return;
66
- }
67
- if (node instanceof HTMLSelectElement) {
68
- applySelectBinding(node, binding);
69
- return;
70
- }
71
- if (node instanceof HTMLTextAreaElement) {
72
- setStringValue(node, binding.value);
73
- return;
74
- }
75
- applyCustomBinding(node, binding);
76
- }
77
- function setNodeName(node, name) {
78
- if ('name' in node) {
79
- node.name = name;
80
- return;
81
- }
82
- node.setAttribute('name', name);
83
- }
84
- function applyInputBinding(node, binding, options) {
85
- if (options.type !== undefined && node.type !== options.type) {
86
- node.type = options.type;
87
- }
88
- if (node.type === 'checkbox' || node.type === 'radio') {
89
- if (binding.value !== undefined) {
90
- node.value = String(Array.isArray(binding.value) ? binding.value[0] : binding.value);
91
- }
92
- node.checked = Boolean(binding.checked);
93
- return;
94
- }
95
- setStringValue(node, binding.value);
96
- }
97
- function applySelectBinding(node, binding) {
98
- const value = binding.value;
99
- if (node.multiple) {
100
- const values = new Set((Array.isArray(value) ? value : value === undefined ? [] : [value]).map(String));
101
- Array.from(node.options).forEach(option => {
102
- option.selected = values.has(option.value);
103
- });
104
- return;
105
- }
106
- setStringValue(node, value);
107
- }
108
- function setStringValue(node, value) {
109
- const nextValue = value === undefined ? '' : String(Array.isArray(value) ? value[0] ?? '' : value);
110
- if (node.value !== nextValue) {
111
- node.value = nextValue;
112
- }
113
- }
114
- function applyCustomBinding(node, binding) {
115
- if ('value' in node) {
116
- node.value = binding.value;
117
- }
118
- if ('checked' in node) {
119
- node.checked = Boolean(binding.checked);
120
- }
121
- }
@@ -1,13 +0,0 @@
1
- import type { Action } from 'svelte/action';
2
- import type { Readable } from 'svelte/store';
3
- import type { Form } from '../core/index';
4
- import type { FormStateSummary } from '../adapters/FormStateSummary';
5
- import type { RegisterOptions, SubmitHandler, SubmitInvalidHandler, SubmitValidHandler } from '../adapters/dom';
6
- export type { RegisterOptions } from '../adapters/dom';
7
- export type SvelteRegisterAction = Action<HTMLElement, RegisterOptions>;
8
- export type SvelteForm<TValues> = {
9
- form: Form<TValues>;
10
- register: SvelteRegisterAction;
11
- useFormState(): Readable<FormStateSummary<TValues>>;
12
- handleSubmit<TResult>(onValid: SubmitValidHandler<TValues, TResult>, onInvalid?: SubmitInvalidHandler): SubmitHandler<Event, TResult>;
13
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,4 +0,0 @@
1
- import type { CreateFormOptions, Form } from '../core/index';
2
- import type { SvelteForm } from './types';
3
- export declare function useForm<TValues>(form: Form<TValues>): SvelteForm<TValues>;
4
- export declare function useForm<TValues>(options: CreateFormOptions<TValues>): SvelteForm<TValues>;
@@ -1,17 +0,0 @@
1
- import { createSubmitHandler } from '../adapters/dom';
2
- import { createFormFromOptions, isFormInstance } from '../adapters/FormInput';
3
- import { createRegisterAction } from './RegisterAction';
4
- import { useFormStateWithForm } from './useFormState';
5
- export function useForm(input) {
6
- const form = isFormInstance(input) ? input : createFormFromOptions(input);
7
- return {
8
- form,
9
- register: createRegisterAction(form),
10
- useFormState() {
11
- return useFormStateWithForm(form);
12
- },
13
- handleSubmit(onValid, onInvalid) {
14
- return createSubmitHandler(form, onValid, onInvalid);
15
- },
16
- };
17
- }
@@ -1,4 +0,0 @@
1
- import { type Readable } from 'svelte/store';
2
- import type { Form } from '../core/index';
3
- import { type FormStateSummary } from '../adapters/FormStateSummary';
4
- export declare function useFormStateWithForm<TValues>(form: Form<TValues>): Readable<FormStateSummary<TValues>>;
@@ -1,10 +0,0 @@
1
- import { readable } from 'svelte/store';
2
- import { createFormStateSummary } from '../adapters/FormStateSummary';
3
- export function useFormStateWithForm(form) {
4
- return readable(createFormStateSummary(form.getState()), set => {
5
- set(createFormStateSummary(form.getState()));
6
- return form.subscribe(() => {
7
- set(createFormStateSummary(form.getState()));
8
- });
9
- });
10
- }
@@ -1,5 +0,0 @@
1
- import type { FieldState, Form } from '../core/index';
2
- import { type RegisterOptions, fieldPathToDomName } from '../adapters/dom';
3
- import type { VueRegisterElement, VueRegisterPropsForElement } from './types';
4
- export declare function createRegisterProps<TValues, TElement extends VueRegisterElement>(form: Form<TValues>, options: RegisterOptions, getField: () => Readonly<FieldState>, domName: string): VueRegisterPropsForElement<TElement>;
5
- export { fieldPathToDomName };
@@ -1,28 +0,0 @@
1
- import { createDomBinding, fieldPathToDomName } from '../adapters/dom';
2
- export function createRegisterProps(form, options, getField, domName) {
3
- const binding = createDomBinding(form, options, getField, domName);
4
- const props = {
5
- name: binding.name,
6
- type: binding.type,
7
- get value() {
8
- return binding.value;
9
- },
10
- get checked() {
11
- return binding.checked;
12
- },
13
- onInput: (event) => {
14
- binding.input(event.currentTarget);
15
- },
16
- onChange: (event) => {
17
- binding.change(event.currentTarget);
18
- },
19
- onBlur: () => {
20
- binding.blur();
21
- },
22
- onFocus: () => {
23
- binding.focus();
24
- },
25
- };
26
- return props;
27
- }
28
- export { fieldPathToDomName };
@@ -1,71 +0,0 @@
1
- import type { Form, FormError, FormState } from '../core/index';
2
- import type { DomValue, RegisterOptions, SubmitHandler, SubmitInvalidHandler, SubmitValidHandler } from '../adapters/dom';
3
- export type { RegisterOptions } from '../adapters/dom';
4
- type VueBindingHandlers<TElement extends HTMLElement> = {
5
- onInput: (event: Event & {
6
- currentTarget: TElement;
7
- }) => void;
8
- onChange: (event: Event & {
9
- currentTarget: TElement;
10
- }) => void;
11
- onBlur: (event: FocusEvent & {
12
- currentTarget: TElement;
13
- }) => void;
14
- onFocus: (event: FocusEvent & {
15
- currentTarget: TElement;
16
- }) => void;
17
- };
18
- export type VueInputRegisterProps = VueBindingHandlers<HTMLInputElement> & {
19
- readonly name: string;
20
- readonly type: string;
21
- readonly value?: DomValue;
22
- readonly checked?: boolean;
23
- };
24
- export type VueSelectRegisterProps = VueBindingHandlers<HTMLSelectElement> & {
25
- readonly name: string;
26
- readonly value?: DomValue;
27
- };
28
- export type VueTextareaRegisterProps = VueBindingHandlers<HTMLTextAreaElement> & {
29
- readonly name: string;
30
- readonly value?: string | number;
31
- };
32
- export type VueCustomRegisterProps<TElement extends HTMLElement = HTMLElement> = VueBindingHandlers<TElement> & {
33
- readonly name: string;
34
- readonly value?: unknown;
35
- readonly checked?: boolean;
36
- };
37
- export type VueRegisterElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | HTMLElement;
38
- export type VueRegisterPropsForElement<TElement extends VueRegisterElement> = TElement extends HTMLSelectElement ? VueSelectRegisterProps : TElement extends HTMLTextAreaElement ? VueTextareaRegisterProps : TElement extends HTMLInputElement ? VueInputRegisterProps : VueCustomRegisterProps<TElement>;
39
- export type VueRegisterProps = VueRegisterPropsForElement<VueRegisterElement>;
40
- export type VueRegisterPropsList<TElement extends VueRegisterElement, TOptions extends readonly RegisterOptions[]> = {
41
- [Index in keyof TOptions]: TOptions[Index] extends RegisterOptions ? VueRegisterPropsForElement<TElement> : never;
42
- };
43
- export type VueFieldReturn<TProps extends VueRegisterProps = VueInputRegisterProps> = {
44
- readonly props: TProps;
45
- readonly value: unknown;
46
- setValue(value: unknown): void;
47
- readonly errors: FormError[];
48
- readonly dirty: boolean;
49
- readonly touched: boolean;
50
- };
51
- export type VueFormStateReturn<TValues> = {
52
- readonly state: Readonly<FormState<TValues>>;
53
- readonly errors: Record<string, FormError[]>;
54
- readonly dirtyFields: Record<string, true>;
55
- readonly touchedFields: Record<string, true>;
56
- readonly isDirty: boolean;
57
- readonly isValid: boolean;
58
- readonly isSubmitting: boolean;
59
- readonly isSubmitted: boolean;
60
- readonly isSubmitSuccessful: boolean;
61
- readonly submitCount: number;
62
- };
63
- export type VueForm<TValues> = {
64
- form: Form<TValues>;
65
- useRegister<TElement extends VueRegisterElement = HTMLInputElement>(options: RegisterOptions): VueRegisterPropsForElement<TElement>;
66
- useRegister<TElement extends VueRegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(options: TOptions): VueRegisterPropsList<TElement, TOptions>;
67
- useRegister<TElement extends VueRegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(...options: TOptions): VueRegisterPropsList<TElement, TOptions>;
68
- useField<TElement extends VueRegisterElement = HTMLInputElement>(options: RegisterOptions): VueFieldReturn<VueRegisterPropsForElement<TElement>>;
69
- useFormState(): VueFormStateReturn<TValues>;
70
- handleSubmit<TResult>(onValid: SubmitValidHandler<TValues, TResult>, onInvalid?: SubmitInvalidHandler): SubmitHandler<Event, TResult>;
71
- };
package/dist/vue/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import type { Form } from '../core/index';
2
- import type { RegisterOptions, VueFieldReturn, VueRegisterElement, VueRegisterPropsForElement } from './types';
3
- export declare function useFieldWithForm<TValues, TElement extends VueRegisterElement = HTMLInputElement>(form: Form<TValues>, options: RegisterOptions): VueFieldReturn<VueRegisterPropsForElement<TElement>>;
@@ -1,28 +0,0 @@
1
- import { getFieldState } from '../adapters/dom';
2
- import { createRegisterProps, fieldPathToDomName } from './RegisterBinding';
3
- import { useFieldSchemaRegistrations } from './useFieldSchemaRegistration';
4
- import { useFormSnapshot } from './useFormSnapshot';
5
- export function useFieldWithForm(form, options) {
6
- useFieldSchemaRegistrations(form, [options]);
7
- const snapshot = useFormSnapshot(form);
8
- const getField = () => getFieldState(form, snapshot.value, options.name);
9
- const props = createRegisterProps(form, options, getField, fieldPathToDomName(options.name));
10
- return {
11
- props,
12
- get value() {
13
- return getField().value;
14
- },
15
- setValue(value) {
16
- form.setValue(options.name, value, { source: 'program' });
17
- },
18
- get errors() {
19
- return [...getField().errors];
20
- },
21
- get dirty() {
22
- return getField().dirty;
23
- },
24
- get touched() {
25
- return getField().touched;
26
- },
27
- };
28
- }
@@ -1,3 +0,0 @@
1
- import type { Form } from '../core/index';
2
- import type { RegisterOptions } from './types';
3
- export declare function useFieldSchemaRegistrations<TValues>(form: Form<TValues>, options: readonly RegisterOptions[]): void;
@@ -1,17 +0,0 @@
1
- import { getCurrentScope, onScopeDispose } from 'vue';
2
- export function useFieldSchemaRegistrations(form, options) {
3
- const cleanups = options.flatMap(option => {
4
- if (!option.schema) {
5
- return [];
6
- }
7
- return [form.registerFieldSchema(option.name, { schema: option.schema, schemaOptions: option.schemaOptions })];
8
- });
9
- if (cleanups.length === 0) {
10
- return;
11
- }
12
- if (getCurrentScope()) {
13
- onScopeDispose(() => {
14
- cleanups.forEach(cleanup => cleanup());
15
- });
16
- }
17
- }
@@ -1,4 +0,0 @@
1
- import type { CreateFormOptions, Form } from '../core/index';
2
- import type { VueForm } from './types';
3
- export declare function useForm<TValues>(form: Form<TValues>): VueForm<TValues>;
4
- export declare function useForm<TValues>(options: CreateFormOptions<TValues>): VueForm<TValues>;
@@ -1,13 +0,0 @@
1
- import { createSubmitHandler } from '../adapters/dom';
2
- import { createFormFromOptions, isFormInstance } from '../adapters/FormInput';
3
- import { useFieldWithForm } from './useField';
4
- import { useFormStateWithForm } from './useFormState';
5
- import { useRegisterWithForm } from './useRegister';
6
- export function useForm(input) {
7
- const form = isFormInstance(input) ? input : createFormFromOptions(input);
8
- const useRegister = ((first, ...rest) => useRegisterWithForm(form, first, ...rest));
9
- const useField = ((options) => useFieldWithForm(form, options));
10
- const useFormState = () => useFormStateWithForm(form);
11
- const handleSubmit = ((onValid, onInvalid) => createSubmitHandler(form, onValid, onInvalid));
12
- return { form, useRegister, useField, useFormState, handleSubmit };
13
- }
@@ -1,3 +0,0 @@
1
- import { type ShallowRef } from 'vue';
2
- import type { Form, FormState } from '../core/index';
3
- export declare function useFormSnapshot<TValues>(form: Form<TValues>): ShallowRef<Readonly<FormState<TValues>>>;
@@ -1,11 +0,0 @@
1
- import { getCurrentScope, onScopeDispose, shallowRef } from 'vue';
2
- export function useFormSnapshot(form) {
3
- const snapshot = shallowRef(form.getState());
4
- const unsubscribe = form.subscribe(() => {
5
- snapshot.value = form.getState();
6
- });
7
- if (getCurrentScope()) {
8
- onScopeDispose(unsubscribe);
9
- }
10
- return snapshot;
11
- }
@@ -1,3 +0,0 @@
1
- import type { Form } from '../core/index';
2
- import type { VueFormStateReturn } from './types';
3
- export declare function useFormStateWithForm<TValues>(form: Form<TValues>): VueFormStateReturn<TValues>;
@@ -1,38 +0,0 @@
1
- import { createFormStateSummary } from '../adapters/FormStateSummary';
2
- import { useFormSnapshot } from './useFormSnapshot';
3
- export function useFormStateWithForm(form) {
4
- const snapshot = useFormSnapshot(form);
5
- const getSummary = () => createFormStateSummary(snapshot.value);
6
- return {
7
- get state() {
8
- return snapshot.value;
9
- },
10
- get errors() {
11
- return getSummary().errors;
12
- },
13
- get dirtyFields() {
14
- return getSummary().dirtyFields;
15
- },
16
- get touchedFields() {
17
- return getSummary().touchedFields;
18
- },
19
- get isDirty() {
20
- return getSummary().isDirty;
21
- },
22
- get isValid() {
23
- return getSummary().isValid;
24
- },
25
- get isSubmitting() {
26
- return getSummary().isSubmitting;
27
- },
28
- get isSubmitted() {
29
- return getSummary().isSubmitted;
30
- },
31
- get isSubmitSuccessful() {
32
- return getSummary().isSubmitSuccessful;
33
- },
34
- get submitCount() {
35
- return getSummary().submitCount;
36
- },
37
- };
38
- }
@@ -1,6 +0,0 @@
1
- import type { Form } from '../core/index';
2
- import type { RegisterOptions, VueRegisterElement, VueRegisterPropsForElement, VueRegisterPropsList } from './types';
3
- export declare function useRegisterWithForm<TValues, TElement extends VueRegisterElement = HTMLInputElement>(form: Form<TValues>, options: RegisterOptions): VueRegisterPropsForElement<TElement>;
4
- export declare function useRegisterWithForm<TValues, TElement extends VueRegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(form: Form<TValues>, options: TOptions): VueRegisterPropsList<TElement, TOptions>;
5
- export declare function useRegisterWithForm<TValues, TElement extends VueRegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(form: Form<TValues>, ...options: TOptions): VueRegisterPropsList<TElement, TOptions>;
6
- export declare function useRegisterWithForm<TValues>(form: Form<TValues>, first: RegisterOptions | readonly RegisterOptions[], ...rest: readonly RegisterOptions[]): VueRegisterPropsForElement<VueRegisterElement> | VueRegisterPropsForElement<VueRegisterElement>[];
@@ -1,21 +0,0 @@
1
- import { getFieldState } from '../adapters/dom';
2
- import { createRegisterProps, fieldPathToDomName } from './RegisterBinding';
3
- import { useFieldSchemaRegistrations } from './useFieldSchemaRegistration';
4
- import { useFormSnapshot } from './useFormSnapshot';
5
- export function useRegisterWithForm(form, first, ...rest) {
6
- const shouldReturnList = Array.isArray(first) || rest.length > 0;
7
- const options = normalizeRegisterOptions(first, rest);
8
- useFieldSchemaRegistrations(form, options);
9
- const snapshot = useFormSnapshot(form);
10
- const bindings = options.map(option => createRegisterProps(form, option, () => getFieldState(form, snapshot.value, option.name), fieldPathToDomName(option.name)));
11
- return shouldReturnList ? bindings : bindings[0];
12
- }
13
- function normalizeRegisterOptions(first, rest) {
14
- if (Array.isArray(first)) {
15
- return first;
16
- }
17
- if (rest.length > 0) {
18
- return [first, ...rest];
19
- }
20
- return [first];
21
- }