@ilokesto/form 1.0.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.ko.md +86 -13
- package/README.md +86 -15
- package/dist/FormInput-DuT7QFe-.d.ts +37 -0
- package/dist/chunk-EPQC3LKB.js +1649 -0
- package/dist/chunk-EPQC3LKB.js.map +1 -0
- package/dist/chunk-OBMSHDV2.js +215 -0
- package/dist/chunk-OBMSHDV2.js.map +1 -0
- package/dist/index.d.ts +58 -2
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -0
- package/dist/react/index.d.ts +87 -2
- package/dist/react/index.js +131 -1
- package/dist/react/index.js.map +1 -0
- package/dist/solid/index.d.ts +89 -2
- package/dist/solid/index.js +170 -1
- package/dist/solid/index.js.map +1 -0
- package/dist/svelte/index.d.ts +68 -2
- package/dist/svelte/index.js +198 -1
- package/dist/svelte/index.js.map +1 -0
- package/dist/types-DBlvL6AC.d.ts +229 -0
- package/dist/vue/index.d.ts +87 -2
- package/dist/vue/index.js +192 -1
- package/dist/vue/index.js.map +1 -0
- package/package.json +15 -8
- package/dist/adapters/FormInput.d.ts +0 -8
- package/dist/adapters/FormInput.js +0 -10
- package/dist/adapters/FormStateSummary.d.ts +0 -14
- package/dist/adapters/FormStateSummary.js +0 -27
- package/dist/adapters/dom/FieldValue.d.ts +0 -2
- package/dist/adapters/dom/FieldValue.js +0 -34
- package/dist/adapters/dom/RegisterBinding.d.ts +0 -14
- package/dist/adapters/dom/RegisterBinding.js +0 -110
- package/dist/adapters/dom/SubmitHandler.d.ts +0 -9
- package/dist/adapters/dom/SubmitHandler.js +0 -6
- package/dist/adapters/dom/index.d.ts +0 -5
- package/dist/adapters/dom/index.js +0 -3
- package/dist/adapters/dom/types.d.ts +0 -22
- package/dist/adapters/dom/types.js +0 -1
- package/dist/core/array/ArrayItemReorder.d.ts +0 -6
- package/dist/core/array/ArrayItemReorder.js +0 -24
- package/dist/core/array/ArrayKeyGenerator.d.ts +0 -4
- package/dist/core/array/ArrayKeyGenerator.js +0 -7
- package/dist/core/array/FormArrayController.d.ts +0 -21
- package/dist/core/array/FormArrayController.js +0 -54
- package/dist/core/array/FormArrayFactory.d.ts +0 -8
- package/dist/core/array/FormArrayFactory.js +0 -13
- package/dist/core/array/FormArrayMutationPlanner.d.ts +0 -14
- package/dist/core/array/FormArrayMutationPlanner.js +0 -68
- package/dist/core/array/FormArrayPath.d.ts +0 -6
- package/dist/core/array/FormArrayPath.js +0 -11
- package/dist/core/array/FormArrayRebaser.d.ts +0 -7
- package/dist/core/array/FormArrayRebaser.js +0 -54
- package/dist/core/array/index.d.ts +0 -1
- package/dist/core/array/index.js +0 -1
- package/dist/core/form/CreateForm.d.ts +0 -23
- package/dist/core/form/CreateForm.js +0 -64
- package/dist/core/form/FormFieldCommands.d.ts +0 -15
- package/dist/core/form/FormFieldCommands.js +0 -40
- package/dist/core/form/FormSubmitter.d.ts +0 -9
- package/dist/core/form/FormSubmitter.js +0 -26
- package/dist/core/form/index.d.ts +0 -1
- package/dist/core/form/index.js +0 -1
- package/dist/core/index.d.ts +0 -2
- package/dist/core/index.js +0 -1
- package/dist/core/path/FormPath.d.ts +0 -9
- package/dist/core/path/FormPath.js +0 -36
- package/dist/core/path/index.d.ts +0 -1
- package/dist/core/path/index.js +0 -1
- package/dist/core/state/FieldStateFactory.d.ts +0 -6
- package/dist/core/state/FieldStateFactory.js +0 -19
- package/dist/core/state/FormStateInitializer.d.ts +0 -7
- package/dist/core/state/FormStateInitializer.js +0 -45
- package/dist/core/state/FormStateReader.d.ts +0 -11
- package/dist/core/state/FormStateReader.js +0 -30
- package/dist/core/state/FormStateStore.d.ts +0 -24
- package/dist/core/state/FormStateStore.js +0 -66
- package/dist/core/state/FormStateWriter.d.ts +0 -17
- package/dist/core/state/FormStateWriter.js +0 -107
- package/dist/core/state/index.d.ts +0 -1
- package/dist/core/state/index.js +0 -1
- package/dist/core/types.d.ts +0 -112
- package/dist/core/types.js +0 -1
- package/dist/core/validation/StandardSchemaValidator.d.ts +0 -14
- package/dist/core/validation/StandardSchemaValidator.js +0 -63
- package/dist/core/validation/ValidationEngine.d.ts +0 -25
- package/dist/core/validation/ValidationEngine.js +0 -116
- package/dist/core/validation/index.d.ts +0 -1
- package/dist/core/validation/index.js +0 -1
- package/dist/core/value/ValueHelper.d.ts +0 -7
- package/dist/core/value/ValueHelper.js +0 -66
- package/dist/core/value/index.d.ts +0 -1
- package/dist/core/value/index.js +0 -1
- package/dist/react/RegisterBinding.d.ts +0 -5
- package/dist/react/RegisterBinding.js +0 -21
- package/dist/react/types.d.ts +0 -73
- package/dist/react/types.js +0 -1
- package/dist/react/useField.d.ts +0 -3
- package/dist/react/useField.js +0 -23
- package/dist/react/useFieldSchemaRegistration.d.ts +0 -4
- package/dist/react/useFieldSchemaRegistration.js +0 -25
- package/dist/react/useFieldState.d.ts +0 -3
- package/dist/react/useFieldState.js +0 -6
- package/dist/react/useForm.d.ts +0 -4
- package/dist/react/useForm.js +0 -30
- package/dist/react/useFormSnapshot.d.ts +0 -2
- package/dist/react/useFormSnapshot.js +0 -4
- package/dist/react/useFormState.d.ts +0 -3
- package/dist/react/useFormState.js +0 -7
- package/dist/react/useRegister.d.ts +0 -6
- package/dist/react/useRegister.js +0 -26
- package/dist/solid/RegisterBinding.d.ts +0 -5
- package/dist/solid/RegisterBinding.js +0 -28
- package/dist/solid/types.d.ts +0 -71
- package/dist/solid/types.js +0 -1
- package/dist/solid/useField.d.ts +0 -3
- package/dist/solid/useField.js +0 -28
- package/dist/solid/useFieldSchemaRegistration.d.ts +0 -3
- package/dist/solid/useFieldSchemaRegistration.js +0 -15
- package/dist/solid/useForm.d.ts +0 -4
- package/dist/solid/useForm.js +0 -13
- package/dist/solid/useFormSnapshot.d.ts +0 -3
- package/dist/solid/useFormSnapshot.js +0 -9
- package/dist/solid/useFormState.d.ts +0 -3
- package/dist/solid/useFormState.js +0 -38
- package/dist/solid/useRegister.d.ts +0 -6
- package/dist/solid/useRegister.js +0 -21
- package/dist/svelte/RegisterAction.d.ts +0 -4
- package/dist/svelte/RegisterAction.js +0 -121
- package/dist/svelte/types.d.ts +0 -13
- package/dist/svelte/types.js +0 -1
- package/dist/svelte/useForm.d.ts +0 -4
- package/dist/svelte/useForm.js +0 -17
- package/dist/svelte/useFormState.d.ts +0 -4
- package/dist/svelte/useFormState.js +0 -10
- package/dist/vue/RegisterBinding.d.ts +0 -5
- package/dist/vue/RegisterBinding.js +0 -28
- package/dist/vue/types.d.ts +0 -71
- package/dist/vue/types.js +0 -1
- package/dist/vue/useField.d.ts +0 -3
- package/dist/vue/useField.js +0 -28
- package/dist/vue/useFieldSchemaRegistration.d.ts +0 -3
- package/dist/vue/useFieldSchemaRegistration.js +0 -17
- package/dist/vue/useForm.d.ts +0 -4
- package/dist/vue/useForm.js +0 -13
- package/dist/vue/useFormSnapshot.d.ts +0 -3
- package/dist/vue/useFormSnapshot.js +0 -11
- package/dist/vue/useFormState.d.ts +0 -3
- package/dist/vue/useFormState.js +0 -38
- package/dist/vue/useRegister.d.ts +0 -6
- package/dist/vue/useRegister.js +0 -21
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { Store } from '@ilokesto/store';
|
|
2
|
-
import { FieldStateFactory } from './FieldStateFactory';
|
|
3
|
-
import { FormStateReader } from './FormStateReader';
|
|
4
|
-
import { FormStateWriter } from './FormStateWriter';
|
|
5
|
-
import { FormStateInitializer } from './FormStateInitializer';
|
|
6
|
-
export class FormStateStore {
|
|
7
|
-
store;
|
|
8
|
-
reader;
|
|
9
|
-
writer;
|
|
10
|
-
static getDefaultFieldState() {
|
|
11
|
-
return FieldStateFactory.createDefault();
|
|
12
|
-
}
|
|
13
|
-
constructor(defaultValues) {
|
|
14
|
-
this.store = new Store(FormStateInitializer.initialize(defaultValues));
|
|
15
|
-
this.reader = new FormStateReader(() => this.store.getState());
|
|
16
|
-
this.writer = new FormStateWriter(this.store);
|
|
17
|
-
}
|
|
18
|
-
getState() {
|
|
19
|
-
return this.store.getState();
|
|
20
|
-
}
|
|
21
|
-
subscribe(listener) {
|
|
22
|
-
return this.store.subscribe(listener);
|
|
23
|
-
}
|
|
24
|
-
getKnownFieldPaths() {
|
|
25
|
-
return this.reader.getKnownFieldPaths();
|
|
26
|
-
}
|
|
27
|
-
getFieldStateByKey(fieldKey) {
|
|
28
|
-
return this.reader.getFieldStateByKey(fieldKey);
|
|
29
|
-
}
|
|
30
|
-
getFieldState(fieldPath) {
|
|
31
|
-
return this.reader.getFieldState(fieldPath);
|
|
32
|
-
}
|
|
33
|
-
getValue(fieldPath) {
|
|
34
|
-
return this.reader.getValue(fieldPath);
|
|
35
|
-
}
|
|
36
|
-
getValues() {
|
|
37
|
-
return this.reader.getValues();
|
|
38
|
-
}
|
|
39
|
-
getValueAtPath(fieldPath) {
|
|
40
|
-
return this.reader.getValueAtPath(fieldPath);
|
|
41
|
-
}
|
|
42
|
-
setValue(fieldPath, value, options = {}) {
|
|
43
|
-
return this.writer.setValue(fieldPath, value, options);
|
|
44
|
-
}
|
|
45
|
-
touchField(fieldKey) {
|
|
46
|
-
this.writer.touchField(fieldKey);
|
|
47
|
-
}
|
|
48
|
-
setErrorsByKey(fieldKey, errors) {
|
|
49
|
-
this.writer.setErrorsByKey(fieldKey, errors);
|
|
50
|
-
}
|
|
51
|
-
clearErrors(fieldKeys) {
|
|
52
|
-
this.writer.clearErrors(fieldKeys);
|
|
53
|
-
}
|
|
54
|
-
reset(values, options) {
|
|
55
|
-
this.writer.reset(values, options);
|
|
56
|
-
}
|
|
57
|
-
beginSubmit() {
|
|
58
|
-
this.writer.beginSubmit();
|
|
59
|
-
}
|
|
60
|
-
completeSubmit(successful) {
|
|
61
|
-
this.writer.completeSubmit(successful);
|
|
62
|
-
}
|
|
63
|
-
replaceState(updater) {
|
|
64
|
-
this.writer.replaceState(updater);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { Store } from '@ilokesto/store';
|
|
2
|
-
import type { FieldPath, FormError, FormState, PathKey, ResetOptions, SetValueOptions } from '../types';
|
|
3
|
-
type WritableFormStore<TValues> = Pick<Store<FormState<TValues>>, 'getState' | 'setState'>;
|
|
4
|
-
export declare class FormStateWriter<TValues> {
|
|
5
|
-
private readonly store;
|
|
6
|
-
constructor(store: WritableFormStore<TValues>);
|
|
7
|
-
setValue(fieldPath: FieldPath, value: unknown, options?: SetValueOptions): PathKey;
|
|
8
|
-
touchField(fieldKey: PathKey): void;
|
|
9
|
-
setErrorsByKey(fieldKey: PathKey, errors: readonly FormError[]): void;
|
|
10
|
-
clearErrors(fieldKeys?: readonly PathKey[]): void;
|
|
11
|
-
reset(values?: TValues, options?: ResetOptions): void;
|
|
12
|
-
beginSubmit(): void;
|
|
13
|
-
completeSubmit(successful: boolean): void;
|
|
14
|
-
replaceState(updater: (previousState: FormState<TValues>) => FormState<TValues>): void;
|
|
15
|
-
private static isSameValue;
|
|
16
|
-
}
|
|
17
|
-
export {};
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { produce } from 'immer';
|
|
2
|
-
import { FieldStateFactory } from './FieldStateFactory';
|
|
3
|
-
import { FormStateInitializer } from './FormStateInitializer';
|
|
4
|
-
import { FormPath } from '../path/index';
|
|
5
|
-
import { ValueHelper } from '../value/index';
|
|
6
|
-
export class FormStateWriter {
|
|
7
|
-
store;
|
|
8
|
-
constructor(store) {
|
|
9
|
-
this.store = store;
|
|
10
|
-
}
|
|
11
|
-
setValue(fieldPath, value, options = {}) {
|
|
12
|
-
const fieldKey = FormPath.pathToKey(fieldPath);
|
|
13
|
-
const defaultValue = ValueHelper.getValueAtPath(this.store.getState().defaultValues, fieldPath);
|
|
14
|
-
this.store.setState(prevState => produce(prevState, draft => {
|
|
15
|
-
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
16
|
-
draft.fields[fieldKey] = {
|
|
17
|
-
...previousField,
|
|
18
|
-
value,
|
|
19
|
-
dirty: !FormStateWriter.isSameValue(value, defaultValue),
|
|
20
|
-
modified: options.source === 'user' ? true : previousField.modified,
|
|
21
|
-
};
|
|
22
|
-
}));
|
|
23
|
-
return fieldKey;
|
|
24
|
-
}
|
|
25
|
-
touchField(fieldKey) {
|
|
26
|
-
this.store.setState(prevState => produce(prevState, draft => {
|
|
27
|
-
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
28
|
-
draft.fields[fieldKey] = {
|
|
29
|
-
...previousField,
|
|
30
|
-
touched: true,
|
|
31
|
-
};
|
|
32
|
-
}));
|
|
33
|
-
}
|
|
34
|
-
setErrorsByKey(fieldKey, errors) {
|
|
35
|
-
this.store.setState(prevState => produce(prevState, draft => {
|
|
36
|
-
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
37
|
-
draft.fields[fieldKey] = {
|
|
38
|
-
...previousField,
|
|
39
|
-
errors: [...errors],
|
|
40
|
-
};
|
|
41
|
-
}));
|
|
42
|
-
}
|
|
43
|
-
clearErrors(fieldKeys) {
|
|
44
|
-
this.store.setState(prevState => produce(prevState, draft => {
|
|
45
|
-
const keys = fieldKeys && fieldKeys.length > 0 ? fieldKeys : Object.keys(draft.fields);
|
|
46
|
-
keys.forEach(fieldKey => {
|
|
47
|
-
const previousField = draft.fields[fieldKey] ?? FieldStateFactory.createDefault();
|
|
48
|
-
draft.fields[fieldKey] = {
|
|
49
|
-
...previousField,
|
|
50
|
-
errors: [],
|
|
51
|
-
};
|
|
52
|
-
});
|
|
53
|
-
}));
|
|
54
|
-
}
|
|
55
|
-
reset(values, options = {}) {
|
|
56
|
-
const previousState = this.store.getState();
|
|
57
|
-
const nextDefaultValues = values ?? previousState.defaultValues;
|
|
58
|
-
const initializedState = FormStateInitializer.initialize(nextDefaultValues);
|
|
59
|
-
this.store.setState(produce(initializedState, draft => {
|
|
60
|
-
Object.entries(draft.fields).forEach(([fieldKey, nextField]) => {
|
|
61
|
-
const previousField = previousState.fields[fieldKey];
|
|
62
|
-
if (!previousField) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (options.keepDirtyValues && previousField.dirty) {
|
|
66
|
-
const fieldPath = FormPath.keyToPath(fieldKey);
|
|
67
|
-
const defaultValue = ValueHelper.getValueAtPath(nextDefaultValues, fieldPath);
|
|
68
|
-
nextField.value = previousField.value;
|
|
69
|
-
nextField.dirty = !FormStateWriter.isSameValue(previousField.value, defaultValue);
|
|
70
|
-
nextField.modified = previousField.modified;
|
|
71
|
-
}
|
|
72
|
-
if (options.keepErrors) {
|
|
73
|
-
nextField.errors = [...previousField.errors];
|
|
74
|
-
}
|
|
75
|
-
if (options.keepTouched) {
|
|
76
|
-
nextField.touched = previousField.touched;
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
if (options.keepSubmitState) {
|
|
80
|
-
draft.submitCount = previousState.submitCount;
|
|
81
|
-
draft.isSubmitting = previousState.isSubmitting;
|
|
82
|
-
draft.isSubmitted = previousState.isSubmitted;
|
|
83
|
-
draft.isSubmitSuccessful = previousState.isSubmitSuccessful;
|
|
84
|
-
}
|
|
85
|
-
}));
|
|
86
|
-
}
|
|
87
|
-
beginSubmit() {
|
|
88
|
-
this.store.setState(prevState => produce(prevState, draft => {
|
|
89
|
-
draft.submitCount += 1;
|
|
90
|
-
draft.isSubmitting = true;
|
|
91
|
-
draft.isSubmitSuccessful = false;
|
|
92
|
-
}));
|
|
93
|
-
}
|
|
94
|
-
completeSubmit(successful) {
|
|
95
|
-
this.store.setState(prevState => produce(prevState, draft => {
|
|
96
|
-
draft.isSubmitting = false;
|
|
97
|
-
draft.isSubmitted = true;
|
|
98
|
-
draft.isSubmitSuccessful = successful;
|
|
99
|
-
}));
|
|
100
|
-
}
|
|
101
|
-
replaceState(updater) {
|
|
102
|
-
this.store.setState(updater);
|
|
103
|
-
}
|
|
104
|
-
static isSameValue(left, right) {
|
|
105
|
-
return Object.is(left, right);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { FormStateStore } from './FormStateStore';
|
package/dist/core/state/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { FormStateStore } from './FormStateStore';
|
package/dist/core/types.d.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
export type FieldPathSegment = string | number;
|
|
2
|
-
export type FieldPath = readonly FieldPathSegment[];
|
|
3
|
-
export type FieldPathInput = string | FieldPath;
|
|
4
|
-
export type PathKey = string;
|
|
5
|
-
export type FormError = {
|
|
6
|
-
type?: string;
|
|
7
|
-
message: string;
|
|
8
|
-
};
|
|
9
|
-
export type ValidationTrigger = 'change' | 'blur' | 'submit' | 'manual';
|
|
10
|
-
export type StandardSchemaV1<Input = unknown, Output = Input> = {
|
|
11
|
-
readonly '~standard': StandardSchemaV1.Props<Input, Output>;
|
|
12
|
-
};
|
|
13
|
-
export declare namespace StandardSchemaV1 {
|
|
14
|
-
type Props<Input = unknown, Output = Input> = {
|
|
15
|
-
readonly version: 1;
|
|
16
|
-
readonly vendor: string;
|
|
17
|
-
readonly types?: Types<Input, Output> | undefined;
|
|
18
|
-
readonly validate: (value: unknown, options?: Options | undefined) => Result<Output> | Promise<Result<Output>>;
|
|
19
|
-
};
|
|
20
|
-
type Types<Input = unknown, Output = Input> = {
|
|
21
|
-
readonly input: Input;
|
|
22
|
-
readonly output: Output;
|
|
23
|
-
};
|
|
24
|
-
type Options = {
|
|
25
|
-
readonly libraryOptions?: Record<string, unknown> | undefined;
|
|
26
|
-
};
|
|
27
|
-
type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
28
|
-
type SuccessResult<Output> = {
|
|
29
|
-
readonly value: Output;
|
|
30
|
-
readonly issues?: undefined;
|
|
31
|
-
};
|
|
32
|
-
type FailureResult = {
|
|
33
|
-
readonly issues: readonly Issue[];
|
|
34
|
-
};
|
|
35
|
-
type Issue = {
|
|
36
|
-
readonly message: string;
|
|
37
|
-
readonly path?: readonly (PropertyKey | PathSegment)[] | undefined;
|
|
38
|
-
};
|
|
39
|
-
type PathSegment = {
|
|
40
|
-
readonly key: PropertyKey;
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
export type FieldState<TValue = unknown> = {
|
|
44
|
-
value: TValue;
|
|
45
|
-
errors: FormError[];
|
|
46
|
-
touched: boolean;
|
|
47
|
-
dirty: boolean;
|
|
48
|
-
modified: boolean;
|
|
49
|
-
};
|
|
50
|
-
export type FieldPathValue<TValues, TName extends FieldPathInput> = TName extends string ? StringFieldValue<TValues, TName> : TName extends FieldPath ? TuplePathValue<TValues, TName> : unknown;
|
|
51
|
-
type StringFieldValue<TValues, TName extends string> = TName extends keyof NonNullable<TValues> ? NonNullable<TValues>[TName] | MissingContainer<TValues> : unknown;
|
|
52
|
-
type TuplePathValue<TValue, TPath extends FieldPath> = TPath extends readonly [] ? TValue : TPath extends readonly [infer THead, ...infer TRest] ? THead extends FieldPathSegment ? TRest extends FieldPath ? SegmentValue<TValue, THead, TRest> : unknown : unknown : unknown;
|
|
53
|
-
type SegmentValue<TValue, THead extends FieldPathSegment, TRest extends FieldPath> = NonNullable<TValue> extends readonly unknown[] ? THead extends number ? TuplePathValue<ArrayElement<NonNullable<TValue>, THead>, TRest> | MissingContainer<TValue> : unknown : THead extends keyof NonNullable<TValue> ? TuplePathValue<NonNullable<TValue>[THead], TRest> | MissingContainer<TValue> : unknown;
|
|
54
|
-
type ArrayElement<TArray, TIndex extends number> = TArray extends readonly unknown[] ? TIndex extends keyof TArray ? TArray[TIndex] : TArray[number] : unknown;
|
|
55
|
-
type MissingContainer<TValue> = Extract<TValue, null | undefined> extends never ? never : undefined;
|
|
56
|
-
export type ArrayKeys = Record<PathKey, string[]>;
|
|
57
|
-
export type FormState<TValues> = {
|
|
58
|
-
defaultValues: TValues;
|
|
59
|
-
fields: Record<PathKey, FieldState>;
|
|
60
|
-
submitCount: number;
|
|
61
|
-
isSubmitting: boolean;
|
|
62
|
-
isSubmitted: boolean;
|
|
63
|
-
isSubmitSuccessful: boolean;
|
|
64
|
-
arrayKeys: ArrayKeys;
|
|
65
|
-
};
|
|
66
|
-
export type CreateFormOptions<TValues> = {
|
|
67
|
-
defaultValues: TValues;
|
|
68
|
-
schema?: StandardSchemaV1<unknown, TValues>;
|
|
69
|
-
schemaOptions?: StandardSchemaV1.Options;
|
|
70
|
-
validateOn?: readonly ValidationTrigger[];
|
|
71
|
-
};
|
|
72
|
-
export type ResetOptions = {
|
|
73
|
-
keepDirtyValues?: boolean;
|
|
74
|
-
keepErrors?: boolean;
|
|
75
|
-
keepTouched?: boolean;
|
|
76
|
-
keepSubmitState?: boolean;
|
|
77
|
-
};
|
|
78
|
-
export type FieldSchemaOptions = {
|
|
79
|
-
schema: StandardSchemaV1<unknown, unknown>;
|
|
80
|
-
schemaOptions?: StandardSchemaV1.Options;
|
|
81
|
-
};
|
|
82
|
-
export type Form<TValues> = {
|
|
83
|
-
getState(): Readonly<FormState<TValues>>;
|
|
84
|
-
subscribe(listener: () => void): () => void;
|
|
85
|
-
registerFieldSchema(path: FieldPathInput, options: FieldSchemaOptions): () => void;
|
|
86
|
-
getFieldState(path: FieldPathInput): Readonly<FieldState>;
|
|
87
|
-
getValue(path: FieldPathInput): unknown;
|
|
88
|
-
getValues(): TValues;
|
|
89
|
-
setValue(path: FieldPathInput, value: unknown, options?: SetValueOptions): void;
|
|
90
|
-
blur(path: FieldPathInput): Promise<boolean>;
|
|
91
|
-
focus(path: FieldPathInput): void;
|
|
92
|
-
setErrors(path: FieldPathInput, errors: readonly FormError[]): void;
|
|
93
|
-
clearErrors(...paths: FieldPathInput[]): void;
|
|
94
|
-
trigger(...paths: FieldPathInput[]): Promise<boolean>;
|
|
95
|
-
array(path: FieldPathInput): FormArray;
|
|
96
|
-
reset(values?: TValues, options?: ResetOptions): void;
|
|
97
|
-
submit<TResult>(onValid: (values: TValues) => TResult | Promise<TResult>, onInvalid?: (fields: Readonly<Record<PathKey, FieldState>>) => void): Promise<TResult | undefined>;
|
|
98
|
-
};
|
|
99
|
-
export type FormArray = {
|
|
100
|
-
keys(): readonly string[];
|
|
101
|
-
insert(index: number, value: unknown): void;
|
|
102
|
-
push(value: unknown): void;
|
|
103
|
-
remove(index: number): void;
|
|
104
|
-
move(fromIndex: number, toIndex: number): void;
|
|
105
|
-
swap(leftIndex: number, rightIndex: number): void;
|
|
106
|
-
replace(values: readonly unknown[]): void;
|
|
107
|
-
};
|
|
108
|
-
export type SetValueOptions = {
|
|
109
|
-
source?: 'user' | 'program';
|
|
110
|
-
validate?: boolean;
|
|
111
|
-
};
|
|
112
|
-
export {};
|
package/dist/core/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { FormError, PathKey, StandardSchemaV1 } from '../types';
|
|
2
|
-
export declare class StandardSchemaValidator<TValues> {
|
|
3
|
-
private readonly schema;
|
|
4
|
-
private readonly options?;
|
|
5
|
-
constructor(schema: StandardSchemaV1<unknown, TValues>, options?: StandardSchemaV1.Options | undefined);
|
|
6
|
-
validate(values: TValues): Promise<StandardSchemaValidationResult>;
|
|
7
|
-
validateValue(value: unknown): Promise<FormError[]>;
|
|
8
|
-
private static issuePathToFieldPath;
|
|
9
|
-
private static issuePathSegmentToKey;
|
|
10
|
-
}
|
|
11
|
-
export type StandardSchemaValidationResult = {
|
|
12
|
-
valid: boolean;
|
|
13
|
-
errorsByKey: Record<PathKey, FormError[]>;
|
|
14
|
-
};
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { FormPath } from '../path/index';
|
|
2
|
-
export class StandardSchemaValidator {
|
|
3
|
-
schema;
|
|
4
|
-
options;
|
|
5
|
-
constructor(schema, options) {
|
|
6
|
-
this.schema = schema;
|
|
7
|
-
this.options = options;
|
|
8
|
-
}
|
|
9
|
-
async validate(values) {
|
|
10
|
-
const result = await this.schema['~standard'].validate(values, this.options);
|
|
11
|
-
if (!result.issues) {
|
|
12
|
-
return {
|
|
13
|
-
valid: true,
|
|
14
|
-
errorsByKey: {},
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
return {
|
|
18
|
-
valid: false,
|
|
19
|
-
errorsByKey: result.issues.reduce((errorsByKey, issue) => {
|
|
20
|
-
const fieldKey = FormPath.pathToKey(StandardSchemaValidator.issuePathToFieldPath(issue.path));
|
|
21
|
-
errorsByKey[fieldKey] = [
|
|
22
|
-
...(errorsByKey[fieldKey] ?? []),
|
|
23
|
-
{
|
|
24
|
-
type: 'standard_schema',
|
|
25
|
-
message: issue.message,
|
|
26
|
-
},
|
|
27
|
-
];
|
|
28
|
-
return errorsByKey;
|
|
29
|
-
}, {}),
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
async validateValue(value) {
|
|
33
|
-
const result = await this.schema['~standard'].validate(value, this.options);
|
|
34
|
-
if (!result.issues) {
|
|
35
|
-
return [];
|
|
36
|
-
}
|
|
37
|
-
return result.issues.map(issue => ({
|
|
38
|
-
type: 'standard_schema',
|
|
39
|
-
message: issue.message,
|
|
40
|
-
}));
|
|
41
|
-
}
|
|
42
|
-
static issuePathToFieldPath(issuePath) {
|
|
43
|
-
if (!issuePath || issuePath.length === 0) {
|
|
44
|
-
return [];
|
|
45
|
-
}
|
|
46
|
-
const fieldPath = [];
|
|
47
|
-
for (const segment of issuePath) {
|
|
48
|
-
const key = StandardSchemaValidator.issuePathSegmentToKey(segment);
|
|
49
|
-
if (key === undefined) {
|
|
50
|
-
return [];
|
|
51
|
-
}
|
|
52
|
-
fieldPath.push(key);
|
|
53
|
-
}
|
|
54
|
-
return fieldPath;
|
|
55
|
-
}
|
|
56
|
-
static issuePathSegmentToKey(segment) {
|
|
57
|
-
const key = typeof segment === 'object' ? segment.key : segment;
|
|
58
|
-
if (typeof key === 'string' || typeof key === 'number') {
|
|
59
|
-
return key;
|
|
60
|
-
}
|
|
61
|
-
return undefined;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { FormStateStore } from '../state/index';
|
|
2
|
-
import type { CreateFormOptions, FieldSchemaOptions, PathKey, ValidationTrigger } from '../types';
|
|
3
|
-
export declare class ValidationEngine<TValues> {
|
|
4
|
-
private static readonly DEFAULT_VALIDATE_ON;
|
|
5
|
-
private readonly store;
|
|
6
|
-
private readonly schema?;
|
|
7
|
-
private readonly fieldSchemas;
|
|
8
|
-
private readonly validateOn;
|
|
9
|
-
private nextFieldSchemaId;
|
|
10
|
-
constructor(store: FormStateStore<TValues>, options: CreateFormOptions<TValues>);
|
|
11
|
-
shouldValidateOn(trigger: ValidationTrigger): boolean;
|
|
12
|
-
registerFieldSchema(fieldKey: PathKey, options: FieldSchemaOptions): () => void;
|
|
13
|
-
validateField(fieldKey: PathKey, _trigger: ValidationTrigger): Promise<boolean>;
|
|
14
|
-
validateFields(fieldKeys: readonly PathKey[], _trigger: ValidationTrigger): Promise<boolean>;
|
|
15
|
-
validateRegisteredFields(_trigger: ValidationTrigger): Promise<boolean>;
|
|
16
|
-
private validateSchema;
|
|
17
|
-
private applyErrors;
|
|
18
|
-
private validateFieldSchema;
|
|
19
|
-
private validateFieldSchemas;
|
|
20
|
-
private validateRegisteredFieldSchemas;
|
|
21
|
-
private mergeErrorsByKey;
|
|
22
|
-
private getValidationWriteKeys;
|
|
23
|
-
private static unique;
|
|
24
|
-
private static createValidSchemaResult;
|
|
25
|
-
}
|
|
@@ -1,116 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
1
|
-
export { ValidationEngine } from './ValidationEngine';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { ValidationEngine } from './ValidationEngine';
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { FieldPath, FormState, PathKey } from '../types';
|
|
2
|
-
export declare class ValueHelper {
|
|
3
|
-
private static createEmptyRoot;
|
|
4
|
-
static getValueAtPath(source: unknown, path: FieldPath): unknown;
|
|
5
|
-
static setValueAtPath<TValues>(source: TValues, path: FieldPath, value: unknown): TValues;
|
|
6
|
-
static getValuesFromFields<TValues>(state: FormState<TValues>, fieldPaths: Readonly<Record<PathKey, FieldPath>>): TValues;
|
|
7
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
1
|
-
export { ValueHelper } from './ValueHelper';
|
package/dist/core/value/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { ValueHelper } from './ValueHelper';
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import type { FieldState, Form } from '../core/index';
|
|
2
|
-
import { type RegisterOptions, fieldPathToDomName } from '../adapters/dom';
|
|
3
|
-
import type { RegisterElement, RegisterPropsForElement } from './types';
|
|
4
|
-
export declare function createRegisterProps<TValues, TElement extends RegisterElement>(form: Form<TValues>, options: RegisterOptions, field: Readonly<FieldState>, domName: string): RegisterPropsForElement<TElement>;
|
|
5
|
-
export { fieldPathToDomName };
|
|
@@ -1,21 +0,0 @@
|
|
|
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 };
|