@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.
- package/dist/adapters/FormInput.d.ts +1 -1
- package/dist/adapters/FormInput.js +10 -0
- package/dist/adapters/FormStateSummary.d.ts +1 -1
- package/dist/adapters/FormStateSummary.js +27 -0
- package/dist/adapters/dom/FieldValue.d.ts +1 -1
- package/dist/adapters/dom/FieldValue.js +34 -0
- package/dist/adapters/dom/RegisterBinding.d.ts +2 -2
- package/dist/adapters/dom/RegisterBinding.js +110 -0
- package/dist/adapters/dom/SubmitHandler.d.ts +2 -2
- package/dist/adapters/dom/SubmitHandler.js +6 -0
- package/dist/adapters/dom/index.d.ts +5 -5
- package/dist/adapters/dom/index.js +3 -0
- package/dist/adapters/dom/types.d.ts +1 -1
- package/dist/adapters/dom/types.js +1 -0
- package/dist/core/array/ArrayItemReorder.js +24 -0
- package/dist/core/array/ArrayKeyGenerator.js +7 -0
- package/dist/core/array/FormArrayController.d.ts +3 -3
- package/dist/core/array/FormArrayController.js +54 -0
- package/dist/core/array/FormArrayFactory.d.ts +2 -2
- package/dist/core/array/FormArrayFactory.js +13 -0
- package/dist/core/array/FormArrayMutationPlanner.d.ts +1 -1
- package/dist/core/array/FormArrayMutationPlanner.js +68 -0
- package/dist/core/array/FormArrayPath.d.ts +1 -1
- package/dist/core/array/FormArrayPath.js +11 -0
- package/dist/core/array/FormArrayRebaser.d.ts +4 -4
- package/dist/core/array/FormArrayRebaser.js +54 -0
- package/dist/core/array/index.d.ts +1 -1
- package/dist/core/array/index.js +1 -0
- package/dist/core/form/CreateForm.d.ts +1 -1
- package/dist/core/form/CreateForm.js +64 -0
- package/dist/core/form/FormFieldCommands.d.ts +3 -3
- package/dist/core/form/FormFieldCommands.js +40 -0
- package/dist/core/form/FormSubmitter.d.ts +3 -3
- package/dist/core/form/FormSubmitter.js +26 -0
- package/dist/core/form/index.d.ts +1 -1
- package/dist/core/form/index.js +1 -0
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +1 -0
- package/dist/core/path/FormPath.d.ts +1 -1
- package/dist/core/path/FormPath.js +36 -0
- package/dist/core/path/index.d.ts +1 -1
- package/dist/core/path/index.js +1 -0
- package/dist/core/state/FieldStateFactory.d.ts +1 -1
- package/dist/core/state/FieldStateFactory.js +19 -0
- package/dist/core/state/FormStateInitializer.d.ts +1 -1
- package/dist/core/state/FormStateInitializer.js +45 -0
- package/dist/core/state/FormStateReader.d.ts +1 -1
- package/dist/core/state/FormStateReader.js +30 -0
- package/dist/core/state/FormStateStore.d.ts +1 -1
- package/dist/core/state/FormStateStore.js +66 -0
- package/dist/core/state/FormStateWriter.d.ts +1 -1
- package/dist/core/state/FormStateWriter.js +107 -0
- package/dist/core/state/index.d.ts +1 -1
- package/dist/core/state/index.js +1 -0
- package/dist/core/types.d.ts +7 -0
- package/dist/core/types.js +1 -0
- package/dist/core/validation/StandardSchemaValidator.d.ts +1 -1
- package/dist/core/validation/StandardSchemaValidator.js +63 -0
- package/dist/core/validation/ValidationEngine.d.ts +2 -2
- package/dist/core/validation/ValidationEngine.js +116 -0
- package/dist/core/validation/index.d.ts +1 -1
- package/dist/core/validation/index.js +1 -0
- package/dist/core/value/ValueHelper.d.ts +1 -1
- package/dist/core/value/ValueHelper.js +66 -0
- package/dist/core/value/index.d.ts +1 -1
- package/dist/core/value/index.js +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -4
- package/dist/react/RegisterBinding.d.ts +3 -3
- package/dist/react/RegisterBinding.js +21 -0
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.js +1 -87
- package/dist/react/types.d.ts +5 -3
- package/dist/react/types.js +1 -0
- package/dist/react/useField.d.ts +2 -2
- package/dist/react/useField.js +23 -0
- package/dist/react/useFieldSchemaRegistration.d.ts +2 -2
- package/dist/react/useFieldSchemaRegistration.js +25 -0
- package/dist/react/useFieldState.d.ts +3 -0
- package/dist/react/useFieldState.js +6 -0
- package/dist/react/useForm.d.ts +2 -2
- package/dist/react/useForm.js +30 -0
- package/dist/react/useFormSnapshot.d.ts +1 -1
- package/dist/react/useFormSnapshot.js +4 -0
- package/dist/react/useFormState.d.ts +2 -2
- package/dist/react/useFormState.js +7 -0
- package/dist/react/useRegister.d.ts +2 -2
- package/dist/react/useRegister.js +26 -0
- package/dist/solid/RegisterBinding.d.ts +3 -3
- package/dist/solid/RegisterBinding.js +28 -0
- package/dist/solid/index.d.ts +2 -2
- package/dist/solid/index.js +1 -117
- package/dist/solid/types.d.ts +3 -3
- package/dist/solid/types.js +1 -0
- package/dist/solid/useField.d.ts +2 -2
- package/dist/solid/useField.js +28 -0
- package/dist/solid/useFieldSchemaRegistration.d.ts +2 -2
- package/dist/solid/useFieldSchemaRegistration.js +15 -0
- package/dist/solid/useForm.d.ts +2 -2
- package/dist/solid/useForm.js +13 -0
- package/dist/solid/useFormSnapshot.d.ts +1 -1
- package/dist/solid/useFormSnapshot.js +9 -0
- package/dist/solid/useFormState.d.ts +2 -2
- package/dist/solid/useFormState.js +38 -0
- package/dist/solid/useRegister.d.ts +2 -2
- package/dist/solid/useRegister.js +21 -0
- package/dist/svelte/RegisterAction.d.ts +2 -2
- package/dist/svelte/RegisterAction.js +121 -0
- package/dist/svelte/index.d.ts +2 -2
- package/dist/svelte/index.js +1 -111
- package/dist/svelte/types.d.ts +4 -4
- package/dist/svelte/types.js +1 -0
- package/dist/svelte/useForm.d.ts +2 -2
- package/dist/svelte/useForm.js +17 -0
- package/dist/svelte/useFormState.d.ts +2 -2
- package/dist/svelte/useFormState.js +10 -0
- package/dist/vue/RegisterBinding.d.ts +3 -3
- package/dist/vue/RegisterBinding.js +28 -0
- package/dist/vue/index.d.ts +2 -2
- package/dist/vue/index.js +1 -117
- package/dist/vue/types.d.ts +3 -3
- package/dist/vue/types.js +1 -0
- package/dist/vue/useField.d.ts +2 -2
- package/dist/vue/useField.js +28 -0
- package/dist/vue/useFieldSchemaRegistration.d.ts +2 -2
- package/dist/vue/useFieldSchemaRegistration.js +17 -0
- package/dist/vue/useForm.d.ts +2 -2
- package/dist/vue/useForm.js +13 -0
- package/dist/vue/useFormSnapshot.d.ts +1 -1
- package/dist/vue/useFormSnapshot.js +11 -0
- package/dist/vue/useFormState.d.ts +2 -2
- package/dist/vue/useFormState.js +38 -0
- package/dist/vue/useRegister.d.ts +2 -2
- package/dist/vue/useRegister.js +21 -0
- package/package.json +2 -2
- package/dist/CreateForm-hQ4cxAK-.js +0 -1283
- package/dist/FormStateSummary-gmEQdNXj.js +0 -140
package/dist/solid/index.js
CHANGED
|
@@ -1,117 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { onCleanup as g, createSignal as y } from "solid-js";
|
|
3
|
-
function l(e, t, r, n) {
|
|
4
|
-
const s = h(e, t, r, n);
|
|
5
|
-
return {
|
|
6
|
-
name: s.name,
|
|
7
|
-
type: s.type,
|
|
8
|
-
get value() {
|
|
9
|
-
return s.value;
|
|
10
|
-
},
|
|
11
|
-
get checked() {
|
|
12
|
-
return s.checked;
|
|
13
|
-
},
|
|
14
|
-
onInput: (u) => {
|
|
15
|
-
s.input(u.currentTarget);
|
|
16
|
-
},
|
|
17
|
-
onChange: (u) => {
|
|
18
|
-
s.change(u.currentTarget);
|
|
19
|
-
},
|
|
20
|
-
onBlur: () => {
|
|
21
|
-
s.blur();
|
|
22
|
-
},
|
|
23
|
-
onFocus: () => {
|
|
24
|
-
s.focus();
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
function d(e, t) {
|
|
29
|
-
const r = t.flatMap((n) => n.schema ? [e.registerFieldSchema(n.name, { schema: n.schema, schemaOptions: n.schemaOptions })] : []);
|
|
30
|
-
r.length !== 0 && g(() => {
|
|
31
|
-
r.forEach((n) => n());
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
function c(e) {
|
|
35
|
-
const [t, r] = y(e.getState(), { equals: !1 }), n = e.subscribe(() => {
|
|
36
|
-
r(e.getState());
|
|
37
|
-
});
|
|
38
|
-
return g(n), t;
|
|
39
|
-
}
|
|
40
|
-
function f(e, t) {
|
|
41
|
-
d(e, [t]);
|
|
42
|
-
const r = c(e), n = () => m(e, r(), t.name);
|
|
43
|
-
return {
|
|
44
|
-
props: l(e, t, n, o(t.name)),
|
|
45
|
-
get value() {
|
|
46
|
-
return n().value;
|
|
47
|
-
},
|
|
48
|
-
setValue(i) {
|
|
49
|
-
e.setValue(t.name, i, { source: "program" });
|
|
50
|
-
},
|
|
51
|
-
get errors() {
|
|
52
|
-
return [...n().errors];
|
|
53
|
-
},
|
|
54
|
-
get dirty() {
|
|
55
|
-
return n().dirty;
|
|
56
|
-
},
|
|
57
|
-
get touched() {
|
|
58
|
-
return n().touched;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
function R(e) {
|
|
63
|
-
const t = c(e), r = () => S(t());
|
|
64
|
-
return {
|
|
65
|
-
get state() {
|
|
66
|
-
return t();
|
|
67
|
-
},
|
|
68
|
-
get errors() {
|
|
69
|
-
return r().errors;
|
|
70
|
-
},
|
|
71
|
-
get dirtyFields() {
|
|
72
|
-
return r().dirtyFields;
|
|
73
|
-
},
|
|
74
|
-
get touchedFields() {
|
|
75
|
-
return r().touchedFields;
|
|
76
|
-
},
|
|
77
|
-
get isDirty() {
|
|
78
|
-
return r().isDirty;
|
|
79
|
-
},
|
|
80
|
-
get isValid() {
|
|
81
|
-
return r().isValid;
|
|
82
|
-
},
|
|
83
|
-
get isSubmitting() {
|
|
84
|
-
return r().isSubmitting;
|
|
85
|
-
},
|
|
86
|
-
get isSubmitted() {
|
|
87
|
-
return r().isSubmitted;
|
|
88
|
-
},
|
|
89
|
-
get isSubmitSuccessful() {
|
|
90
|
-
return r().isSubmitSuccessful;
|
|
91
|
-
},
|
|
92
|
-
get submitCount() {
|
|
93
|
-
return r().submitCount;
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
function v(e, t, ...r) {
|
|
98
|
-
const n = Array.isArray(t) || r.length > 0, s = A(t, r);
|
|
99
|
-
d(e, s);
|
|
100
|
-
const i = c(e), u = s.map((a) => l(
|
|
101
|
-
e,
|
|
102
|
-
a,
|
|
103
|
-
() => m(e, i(), a.name),
|
|
104
|
-
o(a.name)
|
|
105
|
-
));
|
|
106
|
-
return n ? u : u[0];
|
|
107
|
-
}
|
|
108
|
-
function A(e, t) {
|
|
109
|
-
return Array.isArray(e) ? e : t.length > 0 ? [e, ...t] : [e];
|
|
110
|
-
}
|
|
111
|
-
function O(e) {
|
|
112
|
-
const t = F(e) ? e : p(e);
|
|
113
|
-
return { form: t, useRegister: ((u, ...a) => v(t, u, ...a)), useField: ((u) => f(t, u)), useFormState: () => R(t), handleSubmit: ((u, a) => b(t, u, a)) };
|
|
114
|
-
}
|
|
115
|
-
export {
|
|
116
|
-
O as useForm
|
|
117
|
-
};
|
|
1
|
+
export { useForm } from './useForm';
|
package/dist/solid/types.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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
|
|
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
4
|
type SolidBindingHandlers<TElement extends HTMLElement> = {
|
|
5
5
|
onInput: (event: InputEvent & {
|
|
6
6
|
currentTarget: TElement;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/solid/useField.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { Form } from '../core/index
|
|
2
|
-
import type { RegisterOptions, SolidFieldReturn, SolidRegisterElement, SolidRegisterPropsForElement } from './types
|
|
1
|
+
import type { Form } from '../core/index';
|
|
2
|
+
import type { RegisterOptions, SolidFieldReturn, SolidRegisterElement, SolidRegisterPropsForElement } from './types';
|
|
3
3
|
export declare function useFieldWithForm<TValues, TElement extends SolidRegisterElement = HTMLInputElement>(form: Form<TValues>, options: RegisterOptions): SolidFieldReturn<SolidRegisterPropsForElement<TElement>>;
|
|
@@ -0,0 +1,28 @@
|
|
|
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 +1,3 @@
|
|
|
1
|
-
import type { Form } from '../core/index
|
|
2
|
-
import type { RegisterOptions } from './types
|
|
1
|
+
import type { Form } from '../core/index';
|
|
2
|
+
import type { RegisterOptions } from './types';
|
|
3
3
|
export declare function useFieldSchemaRegistrations<TValues>(form: Form<TValues>, options: readonly RegisterOptions[]): void;
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
}
|
package/dist/solid/useForm.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CreateFormOptions, Form } from '../core/index
|
|
2
|
-
import type { SolidForm } from './types
|
|
1
|
+
import type { CreateFormOptions, Form } from '../core/index';
|
|
2
|
+
import type { SolidForm } from './types';
|
|
3
3
|
export declare function useForm<TValues>(form: Form<TValues>): SolidForm<TValues>;
|
|
4
4
|
export declare function useForm<TValues>(options: CreateFormOptions<TValues>): SolidForm<TValues>;
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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 +1,3 @@
|
|
|
1
|
-
import type { Form } from '../core/index
|
|
2
|
-
import type { SolidFormStateReturn } from './types
|
|
1
|
+
import type { Form } from '../core/index';
|
|
2
|
+
import type { SolidFormStateReturn } from './types';
|
|
3
3
|
export declare function useFormStateWithForm<TValues>(form: Form<TValues>): SolidFormStateReturn<TValues>;
|
|
@@ -0,0 +1,38 @@
|
|
|
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,5 +1,5 @@
|
|
|
1
|
-
import type { Form } from '../core/index
|
|
2
|
-
import type { RegisterOptions, SolidRegisterElement, SolidRegisterPropsForElement, SolidRegisterPropsList } from './types
|
|
1
|
+
import type { Form } from '../core/index';
|
|
2
|
+
import type { RegisterOptions, SolidRegisterElement, SolidRegisterPropsForElement, SolidRegisterPropsList } from './types';
|
|
3
3
|
export declare function useRegisterWithForm<TValues, TElement extends SolidRegisterElement = HTMLInputElement>(form: Form<TValues>, options: RegisterOptions): SolidRegisterPropsForElement<TElement>;
|
|
4
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
5
|
export declare function useRegisterWithForm<TValues, TElement extends SolidRegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(form: Form<TValues>, ...options: TOptions): SolidRegisterPropsList<TElement, TOptions>;
|
|
@@ -0,0 +1,21 @@
|
|
|
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 +1,4 @@
|
|
|
1
1
|
import type { Action } from 'svelte/action';
|
|
2
|
-
import type { Form } from '../core/index
|
|
3
|
-
import { type RegisterOptions } from '../adapters/dom
|
|
2
|
+
import type { Form } from '../core/index';
|
|
3
|
+
import { type RegisterOptions } from '../adapters/dom';
|
|
4
4
|
export declare function createRegisterAction<TValues>(form: Form<TValues>): Action<HTMLElement, RegisterOptions>;
|
|
@@ -0,0 +1,121 @@
|
|
|
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
|
+
}
|
package/dist/svelte/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { useForm } from './useForm
|
|
2
|
-
export type { RegisterOptions, } from './types
|
|
1
|
+
export { useForm } from './useForm';
|
|
2
|
+
export type { RegisterOptions, } from './types';
|
package/dist/svelte/index.js
CHANGED
|
@@ -1,111 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { readable as A } from "svelte/store";
|
|
3
|
-
function B(e) {
|
|
4
|
-
return (t, r) => {
|
|
5
|
-
let a = r, n = p(e, a);
|
|
6
|
-
const u = () => h(
|
|
7
|
-
e,
|
|
8
|
-
T(t, a),
|
|
9
|
-
() => g(e, e.getState(), a.name),
|
|
10
|
-
S(a.name)
|
|
11
|
-
), s = () => {
|
|
12
|
-
k(t, u(), a);
|
|
13
|
-
}, m = (c) => {
|
|
14
|
-
u().input(c.currentTarget);
|
|
15
|
-
}, o = (c) => {
|
|
16
|
-
u().change(c.currentTarget);
|
|
17
|
-
}, v = () => {
|
|
18
|
-
u().blur();
|
|
19
|
-
}, f = () => {
|
|
20
|
-
u().focus();
|
|
21
|
-
};
|
|
22
|
-
t.addEventListener("input", m), t.addEventListener("change", o), t.addEventListener("blur", v), t.addEventListener("focus", f);
|
|
23
|
-
const y = e.subscribe(s);
|
|
24
|
-
return s(), {
|
|
25
|
-
update(c) {
|
|
26
|
-
n(), a = c, n = p(e, a), s();
|
|
27
|
-
},
|
|
28
|
-
destroy() {
|
|
29
|
-
y(), n(), t.removeEventListener("input", m), t.removeEventListener("change", o), t.removeEventListener("blur", v), t.removeEventListener("focus", f);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
function p(e, t) {
|
|
35
|
-
return t.schema ? e.registerFieldSchema(t.name, { schema: t.schema, schemaOptions: t.schemaOptions }) : () => {
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
function T(e, t) {
|
|
39
|
-
return t.type !== void 0 || !(e instanceof HTMLInputElement) ? t : {
|
|
40
|
-
...t,
|
|
41
|
-
type: e.type || "text"
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
function k(e, t, r) {
|
|
45
|
-
if (N(e, t.name), e instanceof HTMLInputElement) {
|
|
46
|
-
b(e, t, r);
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
if (e instanceof HTMLSelectElement) {
|
|
50
|
-
x(e, t);
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
if (e instanceof HTMLTextAreaElement) {
|
|
54
|
-
l(e, t.value);
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
H(e, t);
|
|
58
|
-
}
|
|
59
|
-
function N(e, t) {
|
|
60
|
-
if ("name" in e) {
|
|
61
|
-
e.name = t;
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
e.setAttribute("name", t);
|
|
65
|
-
}
|
|
66
|
-
function b(e, t, r) {
|
|
67
|
-
if (r.type !== void 0 && e.type !== r.type && (e.type = r.type), e.type === "checkbox" || e.type === "radio") {
|
|
68
|
-
t.value !== void 0 && (e.value = String(Array.isArray(t.value) ? t.value[0] : t.value)), e.checked = !!t.checked;
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
l(e, t.value);
|
|
72
|
-
}
|
|
73
|
-
function x(e, t) {
|
|
74
|
-
const r = t.value;
|
|
75
|
-
if (e.multiple) {
|
|
76
|
-
const a = new Set((Array.isArray(r) ? r : r === void 0 ? [] : [r]).map(String));
|
|
77
|
-
Array.from(e.options).forEach((n) => {
|
|
78
|
-
n.selected = a.has(n.value);
|
|
79
|
-
});
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
l(e, r);
|
|
83
|
-
}
|
|
84
|
-
function l(e, t) {
|
|
85
|
-
const r = t === void 0 ? "" : String(Array.isArray(t) ? t[0] ?? "" : t);
|
|
86
|
-
e.value !== r && (e.value = r);
|
|
87
|
-
}
|
|
88
|
-
function H(e, t) {
|
|
89
|
-
"value" in e && (e.value = t.value), "checked" in e && (e.checked = !!t.checked);
|
|
90
|
-
}
|
|
91
|
-
function I(e) {
|
|
92
|
-
return A(i(e.getState()), (t) => (t(i(e.getState())), e.subscribe(() => {
|
|
93
|
-
t(i(e.getState()));
|
|
94
|
-
})));
|
|
95
|
-
}
|
|
96
|
-
function C(e) {
|
|
97
|
-
const t = E(e) ? e : F(e);
|
|
98
|
-
return {
|
|
99
|
-
form: t,
|
|
100
|
-
register: B(t),
|
|
101
|
-
useFormState() {
|
|
102
|
-
return I(t);
|
|
103
|
-
},
|
|
104
|
-
handleSubmit(r, a) {
|
|
105
|
-
return L(t, r, a);
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
export {
|
|
110
|
-
C as useForm
|
|
111
|
-
};
|
|
1
|
+
export { useForm } from './useForm';
|
package/dist/svelte/types.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { Action } from 'svelte/action';
|
|
2
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
|
|
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
7
|
export type SvelteRegisterAction = Action<HTMLElement, RegisterOptions>;
|
|
8
8
|
export type SvelteForm<TValues> = {
|
|
9
9
|
form: Form<TValues>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/svelte/useForm.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CreateFormOptions, Form } from '../core/index
|
|
2
|
-
import type { SvelteForm } from './types
|
|
1
|
+
import type { CreateFormOptions, Form } from '../core/index';
|
|
2
|
+
import type { SvelteForm } from './types';
|
|
3
3
|
export declare function useForm<TValues>(form: Form<TValues>): SvelteForm<TValues>;
|
|
4
4
|
export declare function useForm<TValues>(options: CreateFormOptions<TValues>): SvelteForm<TValues>;
|
|
@@ -0,0 +1,17 @@
|
|
|
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 +1,4 @@
|
|
|
1
1
|
import { type Readable } from 'svelte/store';
|
|
2
|
-
import type { Form } from '../core/index
|
|
3
|
-
import { type FormStateSummary } from '../adapters/FormStateSummary
|
|
2
|
+
import type { Form } from '../core/index';
|
|
3
|
+
import { type FormStateSummary } from '../adapters/FormStateSummary';
|
|
4
4
|
export declare function useFormStateWithForm<TValues>(form: Form<TValues>): Readable<FormStateSummary<TValues>>;
|
|
@@ -0,0 +1,10 @@
|
|
|
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 +1,5 @@
|
|
|
1
|
-
import type { FieldState, Form } from '../core/index
|
|
2
|
-
import { type RegisterOptions, fieldPathToDomName } from '../adapters/dom
|
|
3
|
-
import type { VueRegisterElement, VueRegisterPropsForElement } from './types
|
|
1
|
+
import type { FieldState, Form } from '../core/index';
|
|
2
|
+
import { type RegisterOptions, fieldPathToDomName } from '../adapters/dom';
|
|
3
|
+
import type { VueRegisterElement, VueRegisterPropsForElement } from './types';
|
|
4
4
|
export declare function createRegisterProps<TValues, TElement extends VueRegisterElement>(form: Form<TValues>, options: RegisterOptions, getField: () => Readonly<FieldState>, domName: string): VueRegisterPropsForElement<TElement>;
|
|
5
5
|
export { fieldPathToDomName };
|
|
@@ -0,0 +1,28 @@
|
|
|
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 };
|
package/dist/vue/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { useForm } from './useForm
|
|
2
|
-
export type { RegisterOptions, } from './types
|
|
1
|
+
export { useForm } from './useForm';
|
|
2
|
+
export type { RegisterOptions, } from './types';
|