@ilokesto/form 1.0.2 → 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.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 -91
- package/dist/react/types.d.ts +3 -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 +2 -2
- 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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ValidationEngine } from './ValidationEngine';
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { FormPath } from '../path/index';
|
|
2
|
+
export class ValueHelper {
|
|
3
|
+
static createEmptyRoot(source) {
|
|
4
|
+
if (Array.isArray(source)) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
if (typeof source === 'object' && source !== null) {
|
|
8
|
+
return {};
|
|
9
|
+
}
|
|
10
|
+
return source;
|
|
11
|
+
}
|
|
12
|
+
static getValueAtPath(source, path) {
|
|
13
|
+
let current = source;
|
|
14
|
+
for (const segment of path) {
|
|
15
|
+
if (current === null || current === undefined) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
if (typeof current !== 'object' && typeof current !== 'function') {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
current = current[segment];
|
|
22
|
+
}
|
|
23
|
+
return current;
|
|
24
|
+
}
|
|
25
|
+
static setValueAtPath(source, path, value) {
|
|
26
|
+
if (path.length === 0) {
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
const cloneRoot = Array.isArray(source)
|
|
30
|
+
? [...source]
|
|
31
|
+
: typeof source === 'object' && source !== null
|
|
32
|
+
? { ...source }
|
|
33
|
+
: {};
|
|
34
|
+
let current = cloneRoot;
|
|
35
|
+
path.forEach((segment, index) => {
|
|
36
|
+
if (index === path.length - 1) {
|
|
37
|
+
current[segment] = value;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const nextSegment = path[index + 1];
|
|
41
|
+
const previous = current[segment];
|
|
42
|
+
const next = Array.isArray(previous)
|
|
43
|
+
? [...previous]
|
|
44
|
+
: typeof previous === 'object' && previous !== null
|
|
45
|
+
? { ...previous }
|
|
46
|
+
: typeof nextSegment === 'number'
|
|
47
|
+
? []
|
|
48
|
+
: {};
|
|
49
|
+
current[segment] = next;
|
|
50
|
+
current = next;
|
|
51
|
+
});
|
|
52
|
+
return cloneRoot;
|
|
53
|
+
}
|
|
54
|
+
static getValuesFromFields(state, fieldPaths) {
|
|
55
|
+
const valueWithArrays = Object.entries(state.arrayKeys).reduce((values, [key, keys]) => {
|
|
56
|
+
return this.setValueAtPath(values, FormPath.keyToPath(key), new Array(keys.length));
|
|
57
|
+
}, this.createEmptyRoot(state.defaultValues));
|
|
58
|
+
return Object.entries(state.fields).reduce((values, [key, field]) => {
|
|
59
|
+
const fieldPath = fieldPaths[key];
|
|
60
|
+
if (!fieldPath) {
|
|
61
|
+
return values;
|
|
62
|
+
}
|
|
63
|
+
return this.setValueAtPath(values, fieldPath, field.value);
|
|
64
|
+
}, valueWithArrays);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { ValueHelper } from './ValueHelper
|
|
1
|
+
export { ValueHelper } from './ValueHelper';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ValueHelper } from './ValueHelper';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { CreateForm, } from './core/index
|
|
2
|
-
export type { CreateFormOptions, FieldPathInput, Form, FormError, ResetOptions, StandardSchemaV1, } from './core/index
|
|
1
|
+
export { CreateForm, } from './core/index';
|
|
2
|
+
export type { CreateFormOptions, FieldPathInput, Form, FormError, ResetOptions, StandardSchemaV1, } from './core/index';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { FieldState, Form } from '../core/index
|
|
2
|
-
import { type RegisterOptions, fieldPathToDomName } from '../adapters/dom
|
|
3
|
-
import type { RegisterElement, RegisterPropsForElement } from './types
|
|
1
|
+
import type { FieldState, Form } from '../core/index';
|
|
2
|
+
import { type RegisterOptions, fieldPathToDomName } from '../adapters/dom';
|
|
3
|
+
import type { RegisterElement, RegisterPropsForElement } from './types';
|
|
4
4
|
export declare function createRegisterProps<TValues, TElement extends RegisterElement>(form: Form<TValues>, options: RegisterOptions, field: Readonly<FieldState>, domName: string): RegisterPropsForElement<TElement>;
|
|
5
5
|
export { fieldPathToDomName };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createDomBinding, fieldPathToDomName } from '../adapters/dom';
|
|
2
|
+
export function createRegisterProps(form, options, field, domName) {
|
|
3
|
+
const binding = createDomBinding(form, options, () => field, domName);
|
|
4
|
+
const props = {
|
|
5
|
+
name: binding.name,
|
|
6
|
+
type: binding.type,
|
|
7
|
+
value: binding.value,
|
|
8
|
+
checked: binding.checked,
|
|
9
|
+
onChange: (event) => {
|
|
10
|
+
binding.change(event.currentTarget);
|
|
11
|
+
},
|
|
12
|
+
onBlur: () => {
|
|
13
|
+
binding.blur();
|
|
14
|
+
},
|
|
15
|
+
onFocus: () => {
|
|
16
|
+
binding.focus();
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
return props;
|
|
20
|
+
}
|
|
21
|
+
export { fieldPathToDomName };
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { useForm } from './useForm
|
|
2
|
-
export type { ReactFormOptions, RegisterOptions, UseFieldStateReturn, } from './types
|
|
1
|
+
export { useForm } from './useForm';
|
|
2
|
+
export type { ReactFormOptions, RegisterOptions, UseFieldStateReturn, } from './types';
|
package/dist/react/index.js
CHANGED
|
@@ -1,91 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { c as O, f as g, g as S, b as A, a as W, i as k, d as E } from "../FormStateSummary-gmEQdNXj.js";
|
|
3
|
-
function f(e, n, r, s) {
|
|
4
|
-
const t = O(e, n, () => r, s);
|
|
5
|
-
return {
|
|
6
|
-
name: t.name,
|
|
7
|
-
type: t.type,
|
|
8
|
-
value: t.value,
|
|
9
|
-
checked: t.checked,
|
|
10
|
-
onChange: (c) => {
|
|
11
|
-
t.change(c.currentTarget);
|
|
12
|
-
},
|
|
13
|
-
onBlur: () => {
|
|
14
|
-
t.blur();
|
|
15
|
-
},
|
|
16
|
-
onFocus: () => {
|
|
17
|
-
t.focus();
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
function N(e, n) {
|
|
22
|
-
const { name: r, schema: s, schemaOptions: t } = n;
|
|
23
|
-
F(() => {
|
|
24
|
-
if (s)
|
|
25
|
-
return e.registerFieldSchema(r, { schema: s, schemaOptions: t });
|
|
26
|
-
}, [e, r, s, t]);
|
|
27
|
-
}
|
|
28
|
-
function V(e, n) {
|
|
29
|
-
const r = n.map((s) => `${g(s.name)}:${s.schema ? "schema" : "none"}`).join("|");
|
|
30
|
-
F(() => {
|
|
31
|
-
const s = n.flatMap((t) => t.schema ? [e.registerFieldSchema(t.name, { schema: t.schema, schemaOptions: t.schemaOptions })] : []);
|
|
32
|
-
return () => {
|
|
33
|
-
s.forEach((t) => t());
|
|
34
|
-
};
|
|
35
|
-
}, [e, n, r]);
|
|
36
|
-
}
|
|
37
|
-
function m(e) {
|
|
38
|
-
return R(
|
|
39
|
-
(n) => e.subscribe(n),
|
|
40
|
-
() => e.getState(),
|
|
41
|
-
() => e.getState()
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
function x(e, n) {
|
|
45
|
-
N(e, n);
|
|
46
|
-
const r = m(e), s = S(e, r, n.name), t = g(n.name), a = i(() => f(e, n, s, t), [e, n, s, t]), c = b((u) => {
|
|
47
|
-
e.setValue(n.name, u, { source: "program" });
|
|
48
|
-
}, [e, n.name]);
|
|
49
|
-
return i(() => ({
|
|
50
|
-
props: a,
|
|
51
|
-
value: s.value,
|
|
52
|
-
setValue: c,
|
|
53
|
-
errors: [...s.errors],
|
|
54
|
-
dirty: s.dirty,
|
|
55
|
-
touched: s.touched
|
|
56
|
-
}), [s, a, c]);
|
|
57
|
-
}
|
|
58
|
-
function B(e, n) {
|
|
59
|
-
const r = m(e);
|
|
60
|
-
return S(e, r, n);
|
|
61
|
-
}
|
|
62
|
-
function C(e) {
|
|
63
|
-
const n = m(e);
|
|
64
|
-
return i(() => A(n), [n]);
|
|
65
|
-
}
|
|
66
|
-
function D(e, n, ...r) {
|
|
67
|
-
const s = Array.isArray(n) || r.length > 0, t = M(n, r);
|
|
68
|
-
V(e, t);
|
|
69
|
-
const a = m(e), c = i(
|
|
70
|
-
() => t.map((u) => {
|
|
71
|
-
const l = S(e, a, u.name), d = g(u.name);
|
|
72
|
-
return f(e, u, l, d);
|
|
73
|
-
}),
|
|
74
|
-
[e, t, a]
|
|
75
|
-
);
|
|
76
|
-
return s ? c : c[0];
|
|
77
|
-
}
|
|
78
|
-
function M(e, n) {
|
|
79
|
-
return Array.isArray(e) ? e : n.length > 0 ? [e, ...n] : [e];
|
|
80
|
-
}
|
|
81
|
-
function $(e) {
|
|
82
|
-
const n = v(void 0), r = v(void 0), s = k(e), t = s ? e : n.current ??= W(e), a = s ? void 0 : e.values, c = s ? void 0 : e.resetOptions;
|
|
83
|
-
F(() => {
|
|
84
|
-
a === void 0 || r.current === a || (r.current = a, t.reset(a, c));
|
|
85
|
-
}, [t, c, a]);
|
|
86
|
-
const u = ((o, ...h) => D(t, o, ...h)), l = ((o) => x(t, o)), d = ((o) => B(t, o)), p = () => C(t), y = ((o, h) => E(t, o, h));
|
|
87
|
-
return i(() => ({ form: t, useRegister: u, useField: l, useFieldState: d, useFormState: p, handleSubmit: y }), [t]);
|
|
88
|
-
}
|
|
89
|
-
export {
|
|
90
|
-
$ as useForm
|
|
91
|
-
};
|
|
1
|
+
export { useForm } from './useForm';
|
package/dist/react/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
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
|
|
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
5
|
export type ReactFormOptions<TValues> = CreateFormOptions<TValues> & {
|
|
6
6
|
values?: TValues;
|
|
7
7
|
resetOptions?: ResetOptions;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/react/useField.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { Form } from '../core/index
|
|
2
|
-
import type { RegisterElement, RegisterOptions, RegisterPropsForElement, UseFieldReturn } from './types
|
|
1
|
+
import type { Form } from '../core/index';
|
|
2
|
+
import type { RegisterElement, RegisterOptions, RegisterPropsForElement, UseFieldReturn } from './types';
|
|
3
3
|
export declare function useFieldWithForm<TValues, TElement extends RegisterElement = HTMLInputElement>(form: Form<TValues>, options: RegisterOptions): UseFieldReturn<RegisterPropsForElement<TElement>>;
|
|
@@ -0,0 +1,23 @@
|
|
|
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 +1,4 @@
|
|
|
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 useFieldSchemaRegistration<TValues>(form: Form<TValues>, options: RegisterOptions): void;
|
|
4
4
|
export declare function useFieldSchemaRegistrations<TValues>(form: Form<TValues>, options: readonly RegisterOptions[]): void;
|
|
@@ -0,0 +1,25 @@
|
|
|
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 +1,3 @@
|
|
|
1
|
-
import type { FieldPathInput, Form } from '../core/index
|
|
2
|
-
import type { UseFieldStateReturn } from './types
|
|
1
|
+
import type { FieldPathInput, Form } from '../core/index';
|
|
2
|
+
import type { UseFieldStateReturn } from './types';
|
|
3
3
|
export declare function useFieldStateWithForm<TValues, const TName extends FieldPathInput>(form: Form<TValues>, name: TName): UseFieldStateReturn<TValues, TName>;
|
package/dist/react/useForm.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Form } from '../core/index
|
|
2
|
-
import type { ReactForm, ReactFormOptions } from './types
|
|
1
|
+
import type { Form } from '../core/index';
|
|
2
|
+
import type { ReactForm, ReactFormOptions } from './types';
|
|
3
3
|
export declare function useForm<TValues>(form: Form<TValues>): ReactForm<TValues>;
|
|
4
4
|
export declare function useForm<TValues>(options: ReactFormOptions<TValues>): ReactForm<TValues>;
|
|
@@ -0,0 +1,30 @@
|
|
|
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 +1,2 @@
|
|
|
1
|
-
import type { Form, FormState } from '../core/index
|
|
1
|
+
import type { Form, FormState } from '../core/index';
|
|
2
2
|
export declare function useFormSnapshot<TValues>(form: Form<TValues>): Readonly<FormState<TValues>>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { Form } from '../core/index
|
|
2
|
-
import type { UseFormStateReturn } from './types
|
|
1
|
+
import type { Form } from '../core/index';
|
|
2
|
+
import type { UseFormStateReturn } from './types';
|
|
3
3
|
export declare function useFormStateWithForm<TValues>(form: Form<TValues>): UseFormStateReturn<TValues>;
|
|
@@ -0,0 +1,7 @@
|
|
|
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,5 +1,5 @@
|
|
|
1
|
-
import type { Form } from '../core/index
|
|
2
|
-
import type { RegisterElement, RegisterOptions, RegisterPropsForElement, RegisterPropsList } from './types
|
|
1
|
+
import type { Form } from '../core/index';
|
|
2
|
+
import type { RegisterElement, RegisterOptions, RegisterPropsForElement, RegisterPropsList } from './types';
|
|
3
3
|
export declare function useRegisterWithForm<TValues, TElement extends RegisterElement = HTMLInputElement>(form: Form<TValues>, options: RegisterOptions): RegisterPropsForElement<TElement>;
|
|
4
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
5
|
export declare function useRegisterWithForm<TValues, TElement extends RegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(form: Form<TValues>, ...options: TOptions): RegisterPropsList<TElement, TOptions>;
|
|
@@ -0,0 +1,26 @@
|
|
|
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 +1,5 @@
|
|
|
1
|
-
import type { FieldState, Form } from '../core/index
|
|
2
|
-
import { type RegisterOptions, fieldPathToDomName } from '../adapters/dom
|
|
3
|
-
import type { SolidRegisterElement, SolidRegisterPropsForElement } from './types
|
|
1
|
+
import type { FieldState, Form } from '../core/index';
|
|
2
|
+
import { type RegisterOptions, fieldPathToDomName } from '../adapters/dom';
|
|
3
|
+
import type { SolidRegisterElement, SolidRegisterPropsForElement } from './types';
|
|
4
4
|
export declare function createRegisterProps<TValues, TElement extends SolidRegisterElement>(form: Form<TValues>, options: RegisterOptions, getField: () => Readonly<FieldState>, domName: string): SolidRegisterPropsForElement<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/solid/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/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;
|