mobx-formly 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -0
- package/dist/create-form.d.ts +11 -0
- package/dist/create-form.js +7 -0
- package/dist/form.d.ts +193 -0
- package/dist/form.js +569 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -0
- package/dist/src/form.d.ts +48 -0
- package/dist/src/form.js +261 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +1 -0
- package/dist/src/types.d.ts +88 -0
- package/dist/src/types.js +1 -0
- package/dist/src/utils.d.ts +6 -0
- package/dist/src/utils.js +59 -0
- package/dist/types.d.ts +132 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.js +60 -0
- package/package.json +88 -0
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# mobx-formly
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.org/package/mobx-formly)
|
|
4
|
+
[](https://github.com/js2me/mobx-formly/actions/workflows/main.yml)
|
|
5
|
+
[](https://js2me.github.io/mobx-formly/)
|
|
6
|
+
|
|
7
|
+
Observable, framework-agnostic forms for MobX 6. No React dependency and no hooks required. Supports Zod and Valibot schemas, async validation, nested paths, and granular field state.
|
|
8
|
+
|
|
9
|
+
## Documentation
|
|
10
|
+
|
|
11
|
+
[Read the documentation →](https://js2me.github.io/mobx-formly/)
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Install mobx-formly together with MobX. Zod and Valibot are optional peer dependencies
|
|
16
|
+
for schema-based validation.
|
|
17
|
+
|
|
18
|
+
## How it works
|
|
19
|
+
|
|
20
|
+
Create a form controller with initial values and, optionally, a Zod or Valibot schema.
|
|
21
|
+
Register fields with your UI layer, observe their values and state, then submit through
|
|
22
|
+
the controller. The controller exposes field-level errors and status as well as aggregate
|
|
23
|
+
form state.
|
|
24
|
+
|
|
25
|
+
## Features
|
|
26
|
+
|
|
27
|
+
- Stable, granular `fieldState[name]` branches.
|
|
28
|
+
- Zod validation, including async refinements.
|
|
29
|
+
- Valibot schema support alongside Zod.
|
|
30
|
+
- Field rules such as `required`, `minLength`, `pattern`, and async `validate`.
|
|
31
|
+
- Nested object and array paths such as `profile.email` and `items.0.name`.
|
|
32
|
+
- Direct observable mutations through `mutate()` with dirty-path detection.
|
|
33
|
+
- Framework agnostic: usable with React, Vue, Solid, or plain TypeScript.
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
[MIT](https://github.com/js2me/mobx-formly/blob/main/LICENSE)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Form } from './form.js';
|
|
2
|
+
import type { FieldValues, FormOptions, FormSchema, SchemaOutput } from './types.js';
|
|
3
|
+
export type InferredFormValues<S> = Extract<SchemaOutput<S>, FieldValues>;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a form with values inferred from its schema.
|
|
6
|
+
*
|
|
7
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/guide/getting-started.html)
|
|
8
|
+
*/
|
|
9
|
+
export declare const createForm: <S extends FormSchema<any>>(options: FormOptions<InferredFormValues<S>> & {
|
|
10
|
+
schema: S;
|
|
11
|
+
}) => Form<InferredFormValues<S>>;
|
package/dist/form.d.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { type Ref } from 'yummies/mobx';
|
|
2
|
+
import type { FieldError, FieldPath, FieldPathValue, FieldState, FieldValues, FormOptions, RegisterOptions, RegisterReturn, ResetOptions, SetValueConfig, SubmitHandlers } from './types.js';
|
|
3
|
+
export declare class Form<T extends FieldValues = FieldValues> {
|
|
4
|
+
/**
|
|
5
|
+
* Current form values.
|
|
6
|
+
*
|
|
7
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#values)
|
|
8
|
+
*/
|
|
9
|
+
values: T;
|
|
10
|
+
/**
|
|
11
|
+
* Errors keyed by field path.
|
|
12
|
+
*
|
|
13
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#errors)
|
|
14
|
+
*/
|
|
15
|
+
errors: Record<string, FieldError | undefined>;
|
|
16
|
+
/**
|
|
17
|
+
* Field paths whose values differ from their defaults.
|
|
18
|
+
*
|
|
19
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#dirtyfields)
|
|
20
|
+
*/
|
|
21
|
+
dirtyFields: Record<string, true | undefined>;
|
|
22
|
+
/**
|
|
23
|
+
* Field paths that have been touched.
|
|
24
|
+
*
|
|
25
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#touchedfields)
|
|
26
|
+
*/
|
|
27
|
+
touchedFields: Record<string, true | undefined>;
|
|
28
|
+
/**
|
|
29
|
+
* Field paths that are currently being validated.
|
|
30
|
+
*
|
|
31
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#validatingfields)
|
|
32
|
+
*/
|
|
33
|
+
validatingFields: Record<string, true | undefined>;
|
|
34
|
+
/**
|
|
35
|
+
* Observable state for each registered field.
|
|
36
|
+
*
|
|
37
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#fieldstate)
|
|
38
|
+
*/
|
|
39
|
+
fieldState: Record<string, FieldState | undefined>;
|
|
40
|
+
/**
|
|
41
|
+
* Whether a submission is currently running.
|
|
42
|
+
*
|
|
43
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#issubmitting)
|
|
44
|
+
*/
|
|
45
|
+
isSubmitting: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Whether the form has been submitted at least once.
|
|
48
|
+
*
|
|
49
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#issubmitted)
|
|
50
|
+
*/
|
|
51
|
+
isSubmitted: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Whether the latest submission succeeded.
|
|
54
|
+
*
|
|
55
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#issubmitsuccessful)
|
|
56
|
+
*/
|
|
57
|
+
isSubmitSuccessful: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Number of submission attempts.
|
|
60
|
+
*
|
|
61
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#submitcount)
|
|
62
|
+
*/
|
|
63
|
+
submitCount: number;
|
|
64
|
+
/**
|
|
65
|
+
* Refs registered for fields.
|
|
66
|
+
*
|
|
67
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#refs)
|
|
68
|
+
*/
|
|
69
|
+
readonly refs: Map<string, Ref<HTMLElement | null, import("yummies/types").AnyObject>>;
|
|
70
|
+
private readonly defaultValues;
|
|
71
|
+
private readonly options;
|
|
72
|
+
private readonly fieldOptions;
|
|
73
|
+
private valueObservers?;
|
|
74
|
+
private observerTimer?;
|
|
75
|
+
private readonly changedPaths;
|
|
76
|
+
private isMutating;
|
|
77
|
+
private observerTreeChanged;
|
|
78
|
+
private activeSubmissions;
|
|
79
|
+
private resetVersion;
|
|
80
|
+
private validationVersion;
|
|
81
|
+
private readonly fieldValidationVersions;
|
|
82
|
+
/** Creates a form with optional initial values, schema, and validation settings.
|
|
83
|
+
*
|
|
84
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#constructor-options)
|
|
85
|
+
*/
|
|
86
|
+
constructor(options?: FormOptions<T>);
|
|
87
|
+
/**
|
|
88
|
+
* Whether registered event handlers ignore changes and blur events.
|
|
89
|
+
*
|
|
90
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#disabled)
|
|
91
|
+
*/
|
|
92
|
+
get disabled(): boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Whether any field is dirty.
|
|
95
|
+
*
|
|
96
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#isdirty)
|
|
97
|
+
*/
|
|
98
|
+
get isDirty(): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Whether the form has no errors.
|
|
101
|
+
*
|
|
102
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#isvalid)
|
|
103
|
+
*/
|
|
104
|
+
get isValid(): boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Registers a field and returns its ref and event handlers.
|
|
107
|
+
*
|
|
108
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#registername-options)
|
|
109
|
+
*/
|
|
110
|
+
register(name: FieldPath<T>, options?: RegisterOptions<T>): RegisterReturn;
|
|
111
|
+
/**
|
|
112
|
+
* Removes a field, its value, and its associated state.
|
|
113
|
+
*
|
|
114
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#unregistername)
|
|
115
|
+
*/
|
|
116
|
+
unregister(name: FieldPath<T>): void;
|
|
117
|
+
/**
|
|
118
|
+
* Updates a field value and optionally changes its state or validates it.
|
|
119
|
+
*
|
|
120
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#setvaluename-value-config)
|
|
121
|
+
*/
|
|
122
|
+
setValue<P extends FieldPath<T>>(name: P, value: FieldPathValue<T, P>, config?: SetValueConfig): void;
|
|
123
|
+
/**
|
|
124
|
+
* Groups direct value changes and processes their changed paths together.
|
|
125
|
+
*
|
|
126
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#mutatemutator-config)
|
|
127
|
+
*/
|
|
128
|
+
mutate(mutator: () => void, config?: SetValueConfig): void;
|
|
129
|
+
private applyValueChange;
|
|
130
|
+
private ensureValueObservers;
|
|
131
|
+
private scheduleObserverCleanup;
|
|
132
|
+
private disposeValueObservers;
|
|
133
|
+
private observeValueTree;
|
|
134
|
+
/**
|
|
135
|
+
* Sets an error for a field.
|
|
136
|
+
*
|
|
137
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#seterrorname-error)
|
|
138
|
+
*/
|
|
139
|
+
setError(name: FieldPath<T>, error: FieldError): void;
|
|
140
|
+
/**
|
|
141
|
+
* Clears one, several, or all field errors.
|
|
142
|
+
*
|
|
143
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#clearerrorsname)
|
|
144
|
+
*/
|
|
145
|
+
clearErrors(name?: FieldPath<T> | FieldPath<T>[]): void;
|
|
146
|
+
/**
|
|
147
|
+
* Validates one field, several fields, or the complete form.
|
|
148
|
+
*
|
|
149
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#triggername)
|
|
150
|
+
*/
|
|
151
|
+
trigger(name?: FieldPath<T> | FieldPath<T>[]): Promise<boolean>;
|
|
152
|
+
/**
|
|
153
|
+
* Creates an asynchronous submit handler with validation and result callbacks.
|
|
154
|
+
*
|
|
155
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#handlesubmithandlers)
|
|
156
|
+
*/
|
|
157
|
+
handleSubmit({ onValid, onInvalid }: SubmitHandlers<T>): () => Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Resets values and selected form state.
|
|
160
|
+
*
|
|
161
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#resetvalues-options)
|
|
162
|
+
*/
|
|
163
|
+
reset(values?: Partial<T>, options?: ResetOptions): void;
|
|
164
|
+
/**
|
|
165
|
+
* Resets one field to its current default value and clears its state.
|
|
166
|
+
*
|
|
167
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#resetfieldname)
|
|
168
|
+
*/
|
|
169
|
+
resetField(name: FieldPath<T>): void;
|
|
170
|
+
/**
|
|
171
|
+
* Focuses a registered field when its ref points to a focusable element.
|
|
172
|
+
*
|
|
173
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#setfocusname)
|
|
174
|
+
*/
|
|
175
|
+
setFocus(name: FieldPath<T>): void;
|
|
176
|
+
/**
|
|
177
|
+
* Returns a plain copy of the current values.
|
|
178
|
+
*
|
|
179
|
+
* [**Documentation**](https://js2me.github.io/mobx-formly/api/form.html#snapshot)
|
|
180
|
+
*/
|
|
181
|
+
get snapshot(): T;
|
|
182
|
+
private markTouched;
|
|
183
|
+
private updateDirty;
|
|
184
|
+
private shouldValidateOnChange;
|
|
185
|
+
private isValidationCurrent;
|
|
186
|
+
private transformValue;
|
|
187
|
+
private validateSchema;
|
|
188
|
+
private normalizeSchemaErrors;
|
|
189
|
+
private validateRules;
|
|
190
|
+
private ensureFieldState;
|
|
191
|
+
private applyFieldState;
|
|
192
|
+
private applyError;
|
|
193
|
+
}
|