@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,73 +0,0 @@
1
- import type { ChangeEventHandler, FocusEventHandler, InputHTMLAttributes, SelectHTMLAttributes, TextareaHTMLAttributes, FormEvent } from 'react';
2
- import type { CreateFormOptions, FieldPathInput, FieldPathValue, FieldState, Form, FormError, FormState, ResetOptions } from '../core/index';
3
- import type { RegisterOptions, SubmitHandler, SubmitInvalidHandler, SubmitValidHandler } from '../adapters/dom';
4
- export type { RegisterOptions } from '../adapters/dom';
5
- export type ReactFormOptions<TValues> = CreateFormOptions<TValues> & {
6
- values?: TValues;
7
- resetOptions?: ResetOptions;
8
- };
9
- type InputValue = InputHTMLAttributes<HTMLInputElement>['value'];
10
- type InputType = InputHTMLAttributes<HTMLInputElement>['type'];
11
- type SelectValue = SelectHTMLAttributes<HTMLSelectElement>['value'];
12
- type TextareaValue = TextareaHTMLAttributes<HTMLTextAreaElement>['value'];
13
- type BindingHandlers<TElement extends HTMLElement> = {
14
- onChange: ChangeEventHandler<TElement>;
15
- onBlur: FocusEventHandler<TElement>;
16
- onFocus: FocusEventHandler<TElement>;
17
- };
18
- export type InputRegisterProps = BindingHandlers<HTMLInputElement> & {
19
- name: string;
20
- type: InputType;
21
- value?: InputValue;
22
- checked?: boolean;
23
- };
24
- export type SelectRegisterProps = BindingHandlers<HTMLSelectElement> & {
25
- name: string;
26
- value?: SelectValue;
27
- };
28
- export type TextareaRegisterProps = BindingHandlers<HTMLTextAreaElement> & {
29
- name: string;
30
- value?: TextareaValue;
31
- };
32
- export type CustomRegisterProps<TElement extends HTMLElement = HTMLElement> = BindingHandlers<TElement> & {
33
- name: string;
34
- value?: unknown;
35
- checked?: boolean;
36
- };
37
- export type RegisterElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | HTMLElement;
38
- export type RegisterPropsForElement<TElement extends RegisterElement> = TElement extends HTMLSelectElement ? SelectRegisterProps : TElement extends HTMLTextAreaElement ? TextareaRegisterProps : TElement extends HTMLInputElement ? InputRegisterProps : CustomRegisterProps<TElement>;
39
- export type RegisterProps = RegisterPropsForElement<RegisterElement>;
40
- export type UseFieldReturn<TProps extends RegisterProps = InputRegisterProps> = {
41
- props: TProps;
42
- value: unknown;
43
- setValue: (value: unknown) => void;
44
- errors: FormError[];
45
- dirty: boolean;
46
- touched: boolean;
47
- };
48
- export type UseFieldStateReturn<TValues, TName extends FieldPathInput> = Readonly<FieldState<FieldPathValue<TValues, TName>>>;
49
- export type UseFormStateReturn<TValues> = {
50
- state: Readonly<FormState<TValues>>;
51
- errors: Record<string, FormError[]>;
52
- dirtyFields: Record<string, true>;
53
- touchedFields: Record<string, true>;
54
- isDirty: boolean;
55
- isValid: boolean;
56
- isSubmitting: boolean;
57
- isSubmitted: boolean;
58
- isSubmitSuccessful: boolean;
59
- submitCount: number;
60
- };
61
- export type RegisterPropsList<TElement extends RegisterElement, TOptions extends readonly RegisterOptions[]> = {
62
- [Index in keyof TOptions]: TOptions[Index] extends RegisterOptions ? RegisterPropsForElement<TElement> : never;
63
- };
64
- export type ReactForm<TValues> = {
65
- form: Form<TValues>;
66
- useRegister<TElement extends RegisterElement = HTMLInputElement>(options: RegisterOptions): RegisterPropsForElement<TElement>;
67
- useRegister<TElement extends RegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(options: TOptions): RegisterPropsList<TElement, TOptions>;
68
- useRegister<TElement extends RegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(...options: TOptions): RegisterPropsList<TElement, TOptions>;
69
- useField<TElement extends RegisterElement = HTMLInputElement>(options: RegisterOptions): UseFieldReturn<RegisterPropsForElement<TElement>>;
70
- useFieldState<const TName extends FieldPathInput>(name: TName): UseFieldStateReturn<TValues, TName>;
71
- useFormState(): UseFormStateReturn<TValues>;
72
- handleSubmit<TResult>(onValid: SubmitValidHandler<TValues, TResult>, onInvalid?: SubmitInvalidHandler): SubmitHandler<FormEvent<HTMLFormElement>, TResult>;
73
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import type { Form } from '../core/index';
2
- import type { RegisterElement, RegisterOptions, RegisterPropsForElement, UseFieldReturn } from './types';
3
- export declare function useFieldWithForm<TValues, TElement extends RegisterElement = HTMLInputElement>(form: Form<TValues>, options: RegisterOptions): UseFieldReturn<RegisterPropsForElement<TElement>>;
@@ -1,23 +0,0 @@
1
- import { useCallback, useMemo } from 'react';
2
- import { createRegisterProps, fieldPathToDomName } from './RegisterBinding';
3
- import { getFieldState } from '../adapters/dom';
4
- import { useFieldSchemaRegistration } from './useFieldSchemaRegistration';
5
- import { useFormSnapshot } from './useFormSnapshot';
6
- export function useFieldWithForm(form, options) {
7
- useFieldSchemaRegistration(form, options);
8
- const state = useFormSnapshot(form);
9
- const field = getFieldState(form, state, options.name);
10
- const domName = fieldPathToDomName(options.name);
11
- const props = useMemo(() => createRegisterProps(form, options, field, domName), [form, options, field, domName]);
12
- const setValue = useCallback((value) => {
13
- form.setValue(options.name, value, { source: 'program' });
14
- }, [form, options.name]);
15
- return useMemo(() => ({
16
- props,
17
- value: field.value,
18
- setValue,
19
- errors: [...field.errors],
20
- dirty: field.dirty,
21
- touched: field.touched,
22
- }), [field, props, setValue]);
23
- }
@@ -1,4 +0,0 @@
1
- import type { Form } from '../core/index';
2
- import type { RegisterOptions } from './types';
3
- export declare function useFieldSchemaRegistration<TValues>(form: Form<TValues>, options: RegisterOptions): void;
4
- export declare function useFieldSchemaRegistrations<TValues>(form: Form<TValues>, options: readonly RegisterOptions[]): void;
@@ -1,25 +0,0 @@
1
- import { useEffect } from 'react';
2
- import { fieldPathToDomName } from './RegisterBinding';
3
- export function useFieldSchemaRegistration(form, options) {
4
- const { name, schema, schemaOptions } = options;
5
- useEffect(() => {
6
- if (!schema) {
7
- return undefined;
8
- }
9
- return form.registerFieldSchema(name, { schema, schemaOptions });
10
- }, [form, name, schema, schemaOptions]);
11
- }
12
- export function useFieldSchemaRegistrations(form, options) {
13
- const schemaSignature = options.map(option => `${fieldPathToDomName(option.name)}:${option.schema ? 'schema' : 'none'}`).join('|');
14
- useEffect(() => {
15
- const cleanups = options.flatMap(option => {
16
- if (!option.schema) {
17
- return [];
18
- }
19
- return [form.registerFieldSchema(option.name, { schema: option.schema, schemaOptions: option.schemaOptions })];
20
- });
21
- return () => {
22
- cleanups.forEach(cleanup => cleanup());
23
- };
24
- }, [form, options, schemaSignature]);
25
- }
@@ -1,3 +0,0 @@
1
- import type { FieldPathInput, Form } from '../core/index';
2
- import type { UseFieldStateReturn } from './types';
3
- export declare function useFieldStateWithForm<TValues, const TName extends FieldPathInput>(form: Form<TValues>, name: TName): UseFieldStateReturn<TValues, TName>;
@@ -1,6 +0,0 @@
1
- import { getFieldState } from '../adapters/dom';
2
- import { useFormSnapshot } from './useFormSnapshot';
3
- export function useFieldStateWithForm(form, name) {
4
- const state = useFormSnapshot(form);
5
- return getFieldState(form, state, name);
6
- }
@@ -1,4 +0,0 @@
1
- import type { Form } from '../core/index';
2
- import type { ReactForm, ReactFormOptions } from './types';
3
- export declare function useForm<TValues>(form: Form<TValues>): ReactForm<TValues>;
4
- export declare function useForm<TValues>(options: ReactFormOptions<TValues>): ReactForm<TValues>;
@@ -1,30 +0,0 @@
1
- import { useEffect, useMemo, useRef } from 'react';
2
- import { createSubmitHandler } from '../adapters/dom';
3
- import { createFormFromOptions, isFormInstance } from '../adapters/FormInput';
4
- import { useFieldWithForm } from './useField';
5
- import { useFieldStateWithForm } from './useFieldState';
6
- import { useFormStateWithForm } from './useFormState';
7
- import { useRegisterWithForm } from './useRegister';
8
- export function useForm(input) {
9
- const optionsFormRef = useRef(undefined);
10
- const previousValuesRef = useRef(undefined);
11
- const isForm = isFormInstance(input);
12
- const form = isForm
13
- ? input
14
- : (optionsFormRef.current ??= createFormFromOptions(input));
15
- const values = isForm ? undefined : input.values;
16
- const resetOptions = isForm ? undefined : input.resetOptions;
17
- useEffect(() => {
18
- if (values === undefined || previousValuesRef.current === values) {
19
- return;
20
- }
21
- previousValuesRef.current = values;
22
- form.reset(values, resetOptions);
23
- }, [form, resetOptions, values]);
24
- const useRegister = ((first, ...rest) => useRegisterWithForm(form, first, ...rest));
25
- const useField = ((options) => useFieldWithForm(form, options));
26
- const useFieldState = ((name) => useFieldStateWithForm(form, name));
27
- const useFormState = () => useFormStateWithForm(form);
28
- const handleSubmit = ((onValid, onInvalid) => createSubmitHandler(form, onValid, onInvalid));
29
- return useMemo(() => ({ form, useRegister, useField, useFieldState, useFormState, handleSubmit }), [form]);
30
- }
@@ -1,2 +0,0 @@
1
- import type { Form, FormState } from '../core/index';
2
- export declare function useFormSnapshot<TValues>(form: Form<TValues>): Readonly<FormState<TValues>>;
@@ -1,4 +0,0 @@
1
- import { useSyncExternalStore } from 'react';
2
- export function useFormSnapshot(form) {
3
- return useSyncExternalStore(listener => form.subscribe(listener), () => form.getState(), () => form.getState());
4
- }
@@ -1,3 +0,0 @@
1
- import type { Form } from '../core/index';
2
- import type { UseFormStateReturn } from './types';
3
- export declare function useFormStateWithForm<TValues>(form: Form<TValues>): UseFormStateReturn<TValues>;
@@ -1,7 +0,0 @@
1
- import { useMemo } from 'react';
2
- import { createFormStateSummary } from '../adapters/FormStateSummary';
3
- import { useFormSnapshot } from './useFormSnapshot';
4
- export function useFormStateWithForm(form) {
5
- const state = useFormSnapshot(form);
6
- return useMemo(() => createFormStateSummary(state), [state]);
7
- }
@@ -1,6 +0,0 @@
1
- import type { Form } from '../core/index';
2
- import type { RegisterElement, RegisterOptions, RegisterPropsForElement, RegisterPropsList } from './types';
3
- export declare function useRegisterWithForm<TValues, TElement extends RegisterElement = HTMLInputElement>(form: Form<TValues>, options: RegisterOptions): RegisterPropsForElement<TElement>;
4
- export declare function useRegisterWithForm<TValues, TElement extends RegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(form: Form<TValues>, options: TOptions): RegisterPropsList<TElement, TOptions>;
5
- export declare function useRegisterWithForm<TValues, TElement extends RegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(form: Form<TValues>, ...options: TOptions): RegisterPropsList<TElement, TOptions>;
6
- export declare function useRegisterWithForm<TValues>(form: Form<TValues>, first: RegisterOptions | readonly RegisterOptions[], ...rest: readonly RegisterOptions[]): RegisterPropsForElement<RegisterElement> | RegisterPropsForElement<RegisterElement>[];
@@ -1,26 +0,0 @@
1
- import { useMemo } from 'react';
2
- import { getFieldState } from '../adapters/dom';
3
- import { createRegisterProps, fieldPathToDomName } from './RegisterBinding';
4
- import { useFieldSchemaRegistrations } from './useFieldSchemaRegistration';
5
- import { useFormSnapshot } from './useFormSnapshot';
6
- export function useRegisterWithForm(form, first, ...rest) {
7
- const shouldReturnList = Array.isArray(first) || rest.length > 0;
8
- const options = normalizeRegisterOptions(first, rest);
9
- useFieldSchemaRegistrations(form, options);
10
- const state = useFormSnapshot(form);
11
- const bindings = useMemo(() => options.map(option => {
12
- const field = getFieldState(form, state, option.name);
13
- const domName = fieldPathToDomName(option.name);
14
- return createRegisterProps(form, option, field, domName);
15
- }), [form, options, state]);
16
- return shouldReturnList ? bindings : bindings[0];
17
- }
18
- function normalizeRegisterOptions(first, rest) {
19
- if (Array.isArray(first)) {
20
- return first;
21
- }
22
- if (rest.length > 0) {
23
- return [first, ...rest];
24
- }
25
- return [first];
26
- }
@@ -1,5 +0,0 @@
1
- import type { FieldState, Form } from '../core/index';
2
- import { type RegisterOptions, fieldPathToDomName } from '../adapters/dom';
3
- import type { SolidRegisterElement, SolidRegisterPropsForElement } from './types';
4
- export declare function createRegisterProps<TValues, TElement extends SolidRegisterElement>(form: Form<TValues>, options: RegisterOptions, getField: () => Readonly<FieldState>, domName: string): SolidRegisterPropsForElement<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 SolidBindingHandlers<TElement extends HTMLElement> = {
5
- onInput: (event: InputEvent & {
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 SolidInputRegisterProps = SolidBindingHandlers<HTMLInputElement> & {
19
- readonly name: string;
20
- readonly type: string;
21
- readonly value?: DomValue;
22
- readonly checked?: boolean;
23
- };
24
- export type SolidSelectRegisterProps = SolidBindingHandlers<HTMLSelectElement> & {
25
- readonly name: string;
26
- readonly value?: DomValue;
27
- };
28
- export type SolidTextareaRegisterProps = SolidBindingHandlers<HTMLTextAreaElement> & {
29
- readonly name: string;
30
- readonly value?: string | number;
31
- };
32
- export type SolidCustomRegisterProps<TElement extends HTMLElement = HTMLElement> = SolidBindingHandlers<TElement> & {
33
- readonly name: string;
34
- readonly value?: unknown;
35
- readonly checked?: boolean;
36
- };
37
- export type SolidRegisterElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | HTMLElement;
38
- export type SolidRegisterPropsForElement<TElement extends SolidRegisterElement> = TElement extends HTMLSelectElement ? SolidSelectRegisterProps : TElement extends HTMLTextAreaElement ? SolidTextareaRegisterProps : TElement extends HTMLInputElement ? SolidInputRegisterProps : SolidCustomRegisterProps<TElement>;
39
- export type SolidRegisterProps = SolidRegisterPropsForElement<SolidRegisterElement>;
40
- export type SolidRegisterPropsList<TElement extends SolidRegisterElement, TOptions extends readonly RegisterOptions[]> = {
41
- [Index in keyof TOptions]: TOptions[Index] extends RegisterOptions ? SolidRegisterPropsForElement<TElement> : never;
42
- };
43
- export type SolidFieldReturn<TProps extends SolidRegisterProps = SolidInputRegisterProps> = {
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 SolidFormStateReturn<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 SolidForm<TValues> = {
64
- form: Form<TValues>;
65
- useRegister<TElement extends SolidRegisterElement = HTMLInputElement>(options: RegisterOptions): SolidRegisterPropsForElement<TElement>;
66
- useRegister<TElement extends SolidRegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(options: TOptions): SolidRegisterPropsList<TElement, TOptions>;
67
- useRegister<TElement extends SolidRegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(...options: TOptions): SolidRegisterPropsList<TElement, TOptions>;
68
- useField<TElement extends SolidRegisterElement = HTMLInputElement>(options: RegisterOptions): SolidFieldReturn<SolidRegisterPropsForElement<TElement>>;
69
- useFormState(): SolidFormStateReturn<TValues>;
70
- handleSubmit<TResult>(onValid: SubmitValidHandler<TValues, TResult>, onInvalid?: SubmitInvalidHandler): SubmitHandler<Event, TResult>;
71
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import type { Form } from '../core/index';
2
- import type { RegisterOptions, SolidFieldReturn, SolidRegisterElement, SolidRegisterPropsForElement } from './types';
3
- export declare function useFieldWithForm<TValues, TElement extends SolidRegisterElement = HTMLInputElement>(form: Form<TValues>, options: RegisterOptions): SolidFieldReturn<SolidRegisterPropsForElement<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(), 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,15 +0,0 @@
1
- import { onCleanup } from 'solid-js';
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
- onCleanup(() => {
13
- cleanups.forEach(cleanup => cleanup());
14
- });
15
- }
@@ -1,4 +0,0 @@
1
- import type { CreateFormOptions, Form } from '../core/index';
2
- import type { SolidForm } from './types';
3
- export declare function useForm<TValues>(form: Form<TValues>): SolidForm<TValues>;
4
- export declare function useForm<TValues>(options: CreateFormOptions<TValues>): SolidForm<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 Accessor } from 'solid-js';
2
- import type { Form, FormState } from '../core/index';
3
- export declare function useFormSnapshot<TValues>(form: Form<TValues>): Accessor<Readonly<FormState<TValues>>>;
@@ -1,9 +0,0 @@
1
- import { createSignal, onCleanup } from 'solid-js';
2
- export function useFormSnapshot(form) {
3
- const [snapshot, setSnapshot] = createSignal(form.getState(), { equals: false });
4
- const unsubscribe = form.subscribe(() => {
5
- setSnapshot(form.getState());
6
- });
7
- onCleanup(unsubscribe);
8
- return snapshot;
9
- }
@@ -1,3 +0,0 @@
1
- import type { Form } from '../core/index';
2
- import type { SolidFormStateReturn } from './types';
3
- export declare function useFormStateWithForm<TValues>(form: Form<TValues>): SolidFormStateReturn<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());
6
- return {
7
- get state() {
8
- return snapshot();
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, SolidRegisterElement, SolidRegisterPropsForElement, SolidRegisterPropsList } from './types';
3
- export declare function useRegisterWithForm<TValues, TElement extends SolidRegisterElement = HTMLInputElement>(form: Form<TValues>, options: RegisterOptions): SolidRegisterPropsForElement<TElement>;
4
- export declare function useRegisterWithForm<TValues, TElement extends SolidRegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(form: Form<TValues>, options: TOptions): SolidRegisterPropsList<TElement, TOptions>;
5
- export declare function useRegisterWithForm<TValues, TElement extends SolidRegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(form: Form<TValues>, ...options: TOptions): SolidRegisterPropsList<TElement, TOptions>;
6
- export declare function useRegisterWithForm<TValues>(form: Form<TValues>, first: RegisterOptions | readonly RegisterOptions[], ...rest: readonly RegisterOptions[]): SolidRegisterPropsForElement<SolidRegisterElement> | SolidRegisterPropsForElement<SolidRegisterElement>[];
@@ -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(), 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
- }
@@ -1,4 +0,0 @@
1
- import type { Action } from 'svelte/action';
2
- import type { Form } from '../core/index';
3
- import { type RegisterOptions } from '../adapters/dom';
4
- export declare function createRegisterAction<TValues>(form: Form<TValues>): Action<HTMLElement, RegisterOptions>;