@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
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { StandardSchemaValidator } from './StandardSchemaValidator';
|
|
2
|
+
export class ValidationEngine {
|
|
3
|
+
static DEFAULT_VALIDATE_ON = ['submit'];
|
|
4
|
+
store;
|
|
5
|
+
schema;
|
|
6
|
+
fieldSchemas = new Map();
|
|
7
|
+
validateOn;
|
|
8
|
+
nextFieldSchemaId = 0;
|
|
9
|
+
constructor(store, options) {
|
|
10
|
+
this.store = store;
|
|
11
|
+
this.schema = options.schema ? new StandardSchemaValidator(options.schema, options.schemaOptions) : undefined;
|
|
12
|
+
this.validateOn = options.validateOn ?? ValidationEngine.DEFAULT_VALIDATE_ON;
|
|
13
|
+
}
|
|
14
|
+
shouldValidateOn(trigger) {
|
|
15
|
+
return this.validateOn.includes(trigger);
|
|
16
|
+
}
|
|
17
|
+
registerFieldSchema(fieldKey, options) {
|
|
18
|
+
const id = ++this.nextFieldSchemaId;
|
|
19
|
+
this.fieldSchemas.set(fieldKey, {
|
|
20
|
+
id,
|
|
21
|
+
validator: new StandardSchemaValidator(options.schema, options.schemaOptions),
|
|
22
|
+
});
|
|
23
|
+
return () => {
|
|
24
|
+
if (this.fieldSchemas.get(fieldKey)?.id === id) {
|
|
25
|
+
this.fieldSchemas.delete(fieldKey);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
async validateField(fieldKey, _trigger) {
|
|
30
|
+
const fieldSchemaErrors = await this.validateFieldSchema(fieldKey);
|
|
31
|
+
if (fieldSchemaErrors) {
|
|
32
|
+
this.applyErrors([fieldKey], { [fieldKey]: fieldSchemaErrors });
|
|
33
|
+
return fieldSchemaErrors.length === 0;
|
|
34
|
+
}
|
|
35
|
+
const schemaResult = await this.validateSchema();
|
|
36
|
+
this.applyErrors([fieldKey], schemaResult.errorsByKey);
|
|
37
|
+
return (schemaResult.errorsByKey[fieldKey] ?? []).length === 0;
|
|
38
|
+
}
|
|
39
|
+
async validateFields(fieldKeys, _trigger) {
|
|
40
|
+
const targetFieldKeys = ValidationEngine.unique(fieldKeys);
|
|
41
|
+
if (targetFieldKeys.length === 0) {
|
|
42
|
+
return this.validateRegisteredFields('manual');
|
|
43
|
+
}
|
|
44
|
+
const localErrorsByKey = await this.validateFieldSchemas(targetFieldKeys);
|
|
45
|
+
const formSchemaFieldKeys = targetFieldKeys.filter(fieldKey => !this.fieldSchemas.has(fieldKey));
|
|
46
|
+
const schemaResult = formSchemaFieldKeys.length > 0
|
|
47
|
+
? await this.validateSchema()
|
|
48
|
+
: ValidationEngine.createValidSchemaResult();
|
|
49
|
+
const errorsByKey = this.mergeErrorsByKey(schemaResult.errorsByKey, localErrorsByKey);
|
|
50
|
+
this.applyErrors([...formSchemaFieldKeys, ...Object.keys(localErrorsByKey)], errorsByKey);
|
|
51
|
+
return targetFieldKeys.every(fieldKey => (errorsByKey[fieldKey] ?? []).length === 0);
|
|
52
|
+
}
|
|
53
|
+
async validateRegisteredFields(_trigger) {
|
|
54
|
+
const schemaResult = await this.validateSchema();
|
|
55
|
+
const localErrorsByKey = await this.validateRegisteredFieldSchemas();
|
|
56
|
+
const errorsByKey = this.mergeErrorsByKey(schemaResult.errorsByKey, localErrorsByKey);
|
|
57
|
+
const keysToWrite = this.getValidationWriteKeys(errorsByKey);
|
|
58
|
+
this.applyErrors(keysToWrite, errorsByKey);
|
|
59
|
+
return keysToWrite.every(fieldKey => (errorsByKey[fieldKey] ?? []).length === 0);
|
|
60
|
+
}
|
|
61
|
+
validateSchema() {
|
|
62
|
+
if (!this.schema) {
|
|
63
|
+
return Promise.resolve(ValidationEngine.createValidSchemaResult());
|
|
64
|
+
}
|
|
65
|
+
return this.schema.validate(this.store.getValues());
|
|
66
|
+
}
|
|
67
|
+
applyErrors(fieldKeys, errorsByKey) {
|
|
68
|
+
fieldKeys.forEach(fieldKey => {
|
|
69
|
+
this.store.setErrorsByKey(fieldKey, errorsByKey[fieldKey] ?? []);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
async validateFieldSchema(fieldKey) {
|
|
73
|
+
const fieldSchema = this.fieldSchemas.get(fieldKey);
|
|
74
|
+
if (!fieldSchema) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return fieldSchema.validator.validateValue(this.store.getFieldStateByKey(fieldKey).value);
|
|
78
|
+
}
|
|
79
|
+
async validateFieldSchemas(fieldKeys) {
|
|
80
|
+
const errorsByKey = {};
|
|
81
|
+
await Promise.all(fieldKeys.map(async (fieldKey) => {
|
|
82
|
+
const errors = await this.validateFieldSchema(fieldKey);
|
|
83
|
+
if (errors) {
|
|
84
|
+
errorsByKey[fieldKey] = errors;
|
|
85
|
+
}
|
|
86
|
+
}));
|
|
87
|
+
return errorsByKey;
|
|
88
|
+
}
|
|
89
|
+
validateRegisteredFieldSchemas() {
|
|
90
|
+
return this.validateFieldSchemas([...this.fieldSchemas.keys()]);
|
|
91
|
+
}
|
|
92
|
+
mergeErrorsByKey(formErrorsByKey, fieldErrorsByKey) {
|
|
93
|
+
const merged = Object.entries(formErrorsByKey).reduce((errorsByKey, [fieldKey, errors]) => {
|
|
94
|
+
if (!this.fieldSchemas.has(fieldKey)) {
|
|
95
|
+
errorsByKey[fieldKey] = [...errors];
|
|
96
|
+
}
|
|
97
|
+
return errorsByKey;
|
|
98
|
+
}, {});
|
|
99
|
+
Object.entries(fieldErrorsByKey).forEach(([fieldKey, errors]) => {
|
|
100
|
+
merged[fieldKey] = [...errors];
|
|
101
|
+
});
|
|
102
|
+
return merged;
|
|
103
|
+
}
|
|
104
|
+
getValidationWriteKeys(schemaErrorsByKey) {
|
|
105
|
+
return ValidationEngine.unique([...Object.keys(this.store.getState().fields), ...Object.keys(schemaErrorsByKey)]);
|
|
106
|
+
}
|
|
107
|
+
static unique(fieldKeys) {
|
|
108
|
+
return [...new Set(fieldKeys)];
|
|
109
|
+
}
|
|
110
|
+
static createValidSchemaResult() {
|
|
111
|
+
return {
|
|
112
|
+
valid: true,
|
|
113
|
+
errorsByKey: {},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { ValidationEngine } from './ValidationEngine
|
|
1
|
+
export { ValidationEngine } from './ValidationEngine';
|
|
@@ -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, } from './types
|
|
1
|
+
export { useForm } from './useForm';
|
|
2
|
+
export type { ReactFormOptions, RegisterOptions, UseFieldStateReturn, } from './types';
|
package/dist/react/index.js
CHANGED
|
@@ -1,87 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { c as b, f as g, g as v, b as O, a as A, i as k, d as E } from "../FormStateSummary-gmEQdNXj.js";
|
|
3
|
-
function f(e, n, r, s) {
|
|
4
|
-
const t = b(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
|
-
h(() => {
|
|
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
|
-
h(() => {
|
|
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 F(e) {
|
|
38
|
-
return y(
|
|
39
|
-
(n) => e.subscribe(n),
|
|
40
|
-
() => e.getState(),
|
|
41
|
-
() => e.getState()
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
function W(e, n) {
|
|
45
|
-
N(e, n);
|
|
46
|
-
const r = F(e), s = v(e, r, n.name), t = g(n.name), a = i(() => f(e, n, s, t), [e, n, s, t]), c = R((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 x(e) {
|
|
59
|
-
const n = F(e);
|
|
60
|
-
return i(() => O(n), [n]);
|
|
61
|
-
}
|
|
62
|
-
function B(e, n, ...r) {
|
|
63
|
-
const s = Array.isArray(n) || r.length > 0, t = C(n, r);
|
|
64
|
-
V(e, t);
|
|
65
|
-
const a = F(e), c = i(
|
|
66
|
-
() => t.map((u) => {
|
|
67
|
-
const m = v(e, a, u.name), l = g(u.name);
|
|
68
|
-
return f(e, u, m, l);
|
|
69
|
-
}),
|
|
70
|
-
[e, t, a]
|
|
71
|
-
);
|
|
72
|
-
return s ? c : c[0];
|
|
73
|
-
}
|
|
74
|
-
function C(e, n) {
|
|
75
|
-
return Array.isArray(e) ? e : n.length > 0 ? [e, ...n] : [e];
|
|
76
|
-
}
|
|
77
|
-
function P(e) {
|
|
78
|
-
const n = S(void 0), r = S(void 0), s = k(e), t = s ? e : n.current ??= A(e), a = s ? void 0 : e.values, c = s ? void 0 : e.resetOptions;
|
|
79
|
-
h(() => {
|
|
80
|
-
a === void 0 || r.current === a || (r.current = a, t.reset(a, c));
|
|
81
|
-
}, [t, c, a]);
|
|
82
|
-
const u = ((o, ...d) => B(t, o, ...d)), m = ((o) => W(t, o)), l = () => x(t), p = ((o, d) => E(t, o, d));
|
|
83
|
-
return i(() => ({ form: t, useRegister: u, useField: m, useFormState: l, handleSubmit: p }), [t]);
|
|
84
|
-
}
|
|
85
|
-
export {
|
|
86
|
-
P as useForm
|
|
87
|
-
};
|
|
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, 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;
|
|
@@ -45,6 +45,7 @@ export type UseFieldReturn<TProps extends RegisterProps = InputRegisterProps> =
|
|
|
45
45
|
dirty: boolean;
|
|
46
46
|
touched: boolean;
|
|
47
47
|
};
|
|
48
|
+
export type UseFieldStateReturn<TValues, TName extends FieldPathInput> = Readonly<FieldState<FieldPathValue<TValues, TName>>>;
|
|
48
49
|
export type UseFormStateReturn<TValues> = {
|
|
49
50
|
state: Readonly<FormState<TValues>>;
|
|
50
51
|
errors: Record<string, FormError[]>;
|
|
@@ -66,6 +67,7 @@ export type ReactForm<TValues> = {
|
|
|
66
67
|
useRegister<TElement extends RegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(options: TOptions): RegisterPropsList<TElement, TOptions>;
|
|
67
68
|
useRegister<TElement extends RegisterElement = HTMLInputElement, TOptions extends readonly RegisterOptions[] = readonly RegisterOptions[]>(...options: TOptions): RegisterPropsList<TElement, TOptions>;
|
|
68
69
|
useField<TElement extends RegisterElement = HTMLInputElement>(options: RegisterOptions): UseFieldReturn<RegisterPropsForElement<TElement>>;
|
|
70
|
+
useFieldState<const TName extends FieldPathInput>(name: TName): UseFieldStateReturn<TValues, TName>;
|
|
69
71
|
useFormState(): UseFormStateReturn<TValues>;
|
|
70
72
|
handleSubmit<TResult>(onValid: SubmitValidHandler<TValues, TResult>, onInvalid?: SubmitInvalidHandler): SubmitHandler<FormEvent<HTMLFormElement>, TResult>;
|
|
71
73
|
};
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
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>;
|
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';
|