mobx-form 14.5.0 → 14.7.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.
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
// Generated by dts-bundle v0.7.3
|
|
2
|
+
// Dependencies for this module:
|
|
3
|
+
// ../lodash
|
|
4
|
+
|
|
5
|
+
declare module 'mobx-form' {
|
|
6
|
+
export * from "mobx-form/FormModel";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare module 'mobx-form/FormModel' {
|
|
10
|
+
import type { DebouncedFunc } from "lodash";
|
|
11
|
+
export type Descriptors<T> = {
|
|
12
|
+
[P in keyof T]: FieldDescriptor<T[P], T>;
|
|
13
|
+
};
|
|
14
|
+
export type FormModelArgs<T> = {
|
|
15
|
+
descriptors: Partial<Descriptors<T>>;
|
|
16
|
+
initialState?: Partial<T>;
|
|
17
|
+
options?: ThrowIfMissingFieldType;
|
|
18
|
+
};
|
|
19
|
+
export type ResultObj = {
|
|
20
|
+
error: string;
|
|
21
|
+
};
|
|
22
|
+
export type ErrorLike = {
|
|
23
|
+
message: string;
|
|
24
|
+
} | Error;
|
|
25
|
+
export type ValidatorResult = boolean | ResultObj | void;
|
|
26
|
+
export type ValidateFnArgs<T, K> = {
|
|
27
|
+
field: Field<T, K>;
|
|
28
|
+
fields: FormModel<K>["fields"];
|
|
29
|
+
model: FormModel<K>;
|
|
30
|
+
value: Field<T, K>["value"];
|
|
31
|
+
};
|
|
32
|
+
export type ValidateFn<T, K> = (args: ValidateFnArgs<T, K>) => Promise<ValidatorResult> | ValidatorResult;
|
|
33
|
+
export type ResetInteractedFlagType = {
|
|
34
|
+
resetInteractedFlag?: boolean;
|
|
35
|
+
};
|
|
36
|
+
export type ThrowIfMissingFieldType = {
|
|
37
|
+
throwIfMissingField?: boolean;
|
|
38
|
+
};
|
|
39
|
+
export interface FieldDescriptor<T, K> {
|
|
40
|
+
waitForBlur?: boolean;
|
|
41
|
+
disabled?: boolean;
|
|
42
|
+
errorMessage?: string;
|
|
43
|
+
validator?: ValidateFn<T, K> | ValidateFn<T, K>[];
|
|
44
|
+
hasValue?: (value: T) => boolean;
|
|
45
|
+
value?: T;
|
|
46
|
+
required?: boolean | string;
|
|
47
|
+
autoValidate?: boolean;
|
|
48
|
+
validationDebounceThreshold?: number;
|
|
49
|
+
clearErrorOnValueChange?: boolean;
|
|
50
|
+
meta?: Record<string, any>;
|
|
51
|
+
}
|
|
52
|
+
export type CommitType = {
|
|
53
|
+
commit?: boolean;
|
|
54
|
+
};
|
|
55
|
+
export type ForceType = {
|
|
56
|
+
force?: boolean;
|
|
57
|
+
};
|
|
58
|
+
export type SetValueFnArgs = ResetInteractedFlagType & CommitType;
|
|
59
|
+
/**
|
|
60
|
+
* Field class provides abstract the validation of a single field
|
|
61
|
+
*/
|
|
62
|
+
export class Field<T, K> {
|
|
63
|
+
_name: string;
|
|
64
|
+
meta?: Record<string, any>;
|
|
65
|
+
_model: FormModel<K>;
|
|
66
|
+
_waitForBlur?: boolean | undefined;
|
|
67
|
+
_disabled?: boolean | undefined;
|
|
68
|
+
_required?: boolean | string;
|
|
69
|
+
_validatedOnce: boolean;
|
|
70
|
+
_clearErrorOnValueChange?: boolean | undefined;
|
|
71
|
+
_hasValueFn?: (value: T) => boolean;
|
|
72
|
+
get name(): string;
|
|
73
|
+
get model(): FormModel<K>;
|
|
74
|
+
get validatedAtLeastOnce(): boolean;
|
|
75
|
+
get waitForBlur(): boolean;
|
|
76
|
+
get disabled(): boolean;
|
|
77
|
+
get required(): boolean;
|
|
78
|
+
resetInteractedFlag(): void;
|
|
79
|
+
markAsInteracted(): void;
|
|
80
|
+
resetValidatedOnce(): void;
|
|
81
|
+
get hasValue(): boolean;
|
|
82
|
+
_validationTs: number;
|
|
83
|
+
/**
|
|
84
|
+
* flag to know if a validation is in progress on this field
|
|
85
|
+
*/
|
|
86
|
+
_validating: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* field to store the initial value set on this field
|
|
89
|
+
* */
|
|
90
|
+
_initialValue?: T;
|
|
91
|
+
/**
|
|
92
|
+
* the value of the field
|
|
93
|
+
* */
|
|
94
|
+
_value?: T;
|
|
95
|
+
/**
|
|
96
|
+
* whether the user interacted with the field
|
|
97
|
+
* this means if there is any value set on the field
|
|
98
|
+
* either setting it using the `setValue` or using
|
|
99
|
+
* the setter `value`. This is useful to know if
|
|
100
|
+
* the user has interacted with teh form in any way
|
|
101
|
+
*/
|
|
102
|
+
_interacted: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* whether the field was blurred at least once
|
|
105
|
+
* usually validators should only start being applied
|
|
106
|
+
* after the first blur, otherwise they become
|
|
107
|
+
* too invasive. This flag be used to keep track of
|
|
108
|
+
* the fact that the user already blurred of a field
|
|
109
|
+
*/
|
|
110
|
+
_blurredOnce: boolean;
|
|
111
|
+
get blurred(): boolean;
|
|
112
|
+
/** the raw error in caes validator throws a real error */
|
|
113
|
+
rawError?: ErrorLike;
|
|
114
|
+
/**
|
|
115
|
+
* the error message associated with this field.
|
|
116
|
+
* This is used to indicate what error happened during
|
|
117
|
+
* the validation process
|
|
118
|
+
*/
|
|
119
|
+
get errorMessage(): string | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* whether the validation should be launch after a
|
|
122
|
+
* new value is set in the field. This is usually associated
|
|
123
|
+
* to forms that set the value on the fields after each
|
|
124
|
+
* onChange event
|
|
125
|
+
*/
|
|
126
|
+
_autoValidate: boolean;
|
|
127
|
+
get autoValidate(): boolean;
|
|
128
|
+
/**
|
|
129
|
+
* used to keep track of the original message
|
|
130
|
+
*/
|
|
131
|
+
_originalErrorMessage?: string;
|
|
132
|
+
/**
|
|
133
|
+
* whether the field is valid or not
|
|
134
|
+
*/
|
|
135
|
+
get valid(): boolean;
|
|
136
|
+
/**
|
|
137
|
+
* whether the user has interacted or not with the field
|
|
138
|
+
*/
|
|
139
|
+
get interacted(): boolean;
|
|
140
|
+
/**
|
|
141
|
+
* get the value set on the field
|
|
142
|
+
*/
|
|
143
|
+
get value(): T | undefined;
|
|
144
|
+
_setValueOnly: (val?: T) => void;
|
|
145
|
+
_setValue: (val?: T) => void;
|
|
146
|
+
/**
|
|
147
|
+
* setter for the value of the field
|
|
148
|
+
*/
|
|
149
|
+
set value(val: T);
|
|
150
|
+
setValue: (value?: T, { resetInteractedFlag, commit }?: SetValueFnArgs) => void;
|
|
151
|
+
/**
|
|
152
|
+
* Restore the initial value of the field
|
|
153
|
+
*/
|
|
154
|
+
restoreInitialValue: ({ resetInteractedFlag, commit }?: SetValueFnArgs) => void;
|
|
155
|
+
get dirty(): boolean;
|
|
156
|
+
commit(): void;
|
|
157
|
+
/**
|
|
158
|
+
* clear the valid state of the field by
|
|
159
|
+
* removing the errorMessage string. A field is
|
|
160
|
+
* considered valid if the errorMessage is not empty
|
|
161
|
+
*/
|
|
162
|
+
resetError(): void;
|
|
163
|
+
clearValidation(): void;
|
|
164
|
+
/**
|
|
165
|
+
* mark the field as already blurred so validation can
|
|
166
|
+
* start to be applied to the field.
|
|
167
|
+
*/
|
|
168
|
+
markBlurredAndValidate: () => void;
|
|
169
|
+
_validateFn?: ValidateFn<T, K> | Array<ValidateFn<T, K>>;
|
|
170
|
+
_doValidate: () => Promise<ValidatorResult | undefined>;
|
|
171
|
+
setDisabled(disabled: boolean): void;
|
|
172
|
+
validate: (opts?: ForceType) => Promise<void>;
|
|
173
|
+
get originalErrorMessage(): string;
|
|
174
|
+
setValidating: (validating: boolean) => void;
|
|
175
|
+
get validating(): boolean;
|
|
176
|
+
/**
|
|
177
|
+
* validate the field. If force is true the validation will be perform
|
|
178
|
+
* even if the field was not initially interacted or blurred
|
|
179
|
+
*
|
|
180
|
+
*/
|
|
181
|
+
_validate: ({ force }?: ForceType) => Promise<void>;
|
|
182
|
+
setRequired: (val: boolean | string) => void;
|
|
183
|
+
setErrorMessage: (msg?: string) => void;
|
|
184
|
+
setError: (error: ErrorLike) => void;
|
|
185
|
+
get error(): string | undefined;
|
|
186
|
+
_debouncedValidation?: DebouncedFunc<Field<T, K>["_validate"]>;
|
|
187
|
+
constructor(model: FormModel<K>, value: T, validatorDescriptor: FieldDescriptor<T, K>, fieldName: string);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* a helper class to generate a dynamic form
|
|
191
|
+
* provided some keys and validators descriptors
|
|
192
|
+
*
|
|
193
|
+
* @export
|
|
194
|
+
* @class FormModel
|
|
195
|
+
*/
|
|
196
|
+
export class FormModel<K> {
|
|
197
|
+
get validatedAtLeastOnce(): boolean;
|
|
198
|
+
get dataIsReady(): boolean;
|
|
199
|
+
get requiredFields(): (keyof K)[];
|
|
200
|
+
get requiredAreFilled(): boolean;
|
|
201
|
+
fields: {
|
|
202
|
+
[P in keyof K]: Field<K[P], K>;
|
|
203
|
+
};
|
|
204
|
+
_validating: boolean;
|
|
205
|
+
get valid(): boolean;
|
|
206
|
+
/**
|
|
207
|
+
* whether or not the form has been "interacted", meaning that at
|
|
208
|
+
* least a value has set on any of the fields after the model
|
|
209
|
+
* has been created
|
|
210
|
+
*/
|
|
211
|
+
get interacted(): boolean;
|
|
212
|
+
/**
|
|
213
|
+
* Restore the initial values set at the creation time of the model
|
|
214
|
+
* */
|
|
215
|
+
restoreInitialValues(opts?: SetValueFnArgs): void;
|
|
216
|
+
commit(): void;
|
|
217
|
+
get dirty(): boolean;
|
|
218
|
+
/**
|
|
219
|
+
* Set multiple values to more than one field a time using an object
|
|
220
|
+
* where each key is the name of a field. The value will be set to each
|
|
221
|
+
* field and from that point on the values set are considered the new
|
|
222
|
+
* initial values. Validation and interacted flags are also reset if the second argument is true
|
|
223
|
+
* */
|
|
224
|
+
updateFrom(obj: Partial<K>, { resetInteractedFlag, ...opts }?: SetValueFnArgs & ThrowIfMissingFieldType): void;
|
|
225
|
+
/**
|
|
226
|
+
* return the array of errors found. The array is an Array<String>
|
|
227
|
+
* */
|
|
228
|
+
get summary(): string[];
|
|
229
|
+
setValidating: (validating: boolean) => void;
|
|
230
|
+
get validating(): boolean;
|
|
231
|
+
/**
|
|
232
|
+
* Manually perform the form validation
|
|
233
|
+
* */
|
|
234
|
+
validate: () => Promise<void>;
|
|
235
|
+
/**
|
|
236
|
+
* Update the value of the field identified by the provided name.
|
|
237
|
+
* Optionally if reset is set to true, interacted and
|
|
238
|
+
* errorMessage are cleared in the Field.
|
|
239
|
+
* */
|
|
240
|
+
updateField: (name: keyof K, value?: K[keyof K], opts?: SetValueFnArgs & ThrowIfMissingFieldType) => void;
|
|
241
|
+
/**
|
|
242
|
+
* return the data as plain Javascript object (mobx magic removed from the fields)
|
|
243
|
+
* */
|
|
244
|
+
get serializedData(): K;
|
|
245
|
+
/**
|
|
246
|
+
* Creates an instance of FormModel.
|
|
247
|
+
* initialState => an object which keys are the names of the fields and the values the initial values for the form.
|
|
248
|
+
* validators => an object which keys are the names of the fields and the values are the descriptors for the validators
|
|
249
|
+
*/
|
|
250
|
+
constructor(args: FormModelArgs<K>);
|
|
251
|
+
_getField(name: keyof K, { throwIfMissingField }?: ThrowIfMissingFieldType): { [P in keyof K]: Field<K[P], K>; }[keyof K];
|
|
252
|
+
_eachField(cb: (field: Field<K[keyof K], K>) => void): void;
|
|
253
|
+
get _fieldKeys(): (keyof K)[];
|
|
254
|
+
resetInteractedFlag(): void;
|
|
255
|
+
disableFields: (fieldKeys: (keyof K)[]) => void;
|
|
256
|
+
_createField({ name, descriptor, }: {
|
|
257
|
+
name: keyof K;
|
|
258
|
+
descriptor: FieldDescriptor<K[keyof K], K>;
|
|
259
|
+
}): void;
|
|
260
|
+
addFields: (fieldsDescriptor: Partial<Descriptors<K>>) => void;
|
|
261
|
+
enableFields(fieldKeys: (keyof K)[]): void;
|
|
262
|
+
resetValidatedOnce(): void;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* return an instance of a FormModel refer to the constructor
|
|
266
|
+
*
|
|
267
|
+
*/
|
|
268
|
+
export const createModel: <T>(args: FormModelArgs<T>) => FormModel<T>;
|
|
269
|
+
export const createModelFromState: <T>(initialState?: Partial<T>, validators?: Descriptors<T>, options?: ThrowIfMissingFieldType) => FormModel<T>;
|
|
270
|
+
}
|
|
271
|
+
|
package/package.json
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mobx-form",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.7.0",
|
|
4
4
|
"description": "A simple form helper for mobx",
|
|
5
|
-
"type": "module",
|
|
6
5
|
"main": "dist/index.cjs",
|
|
7
|
-
"module": "dist/index.
|
|
6
|
+
"module": "dist/index.mjs",
|
|
8
7
|
"types": "dist/mobx-form.d.ts",
|
|
9
8
|
"files": [
|
|
10
9
|
"dist/"
|
|
11
10
|
],
|
|
12
11
|
"exports": {
|
|
13
12
|
".": {
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/mobx-form.d.mts",
|
|
15
|
+
"default": "./dist/index.mjs"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/mobx-form.d.ts",
|
|
19
|
+
"default": "./dist/index.cjs"
|
|
20
|
+
}
|
|
17
21
|
}
|
|
18
22
|
},
|
|
19
23
|
"scripts": {
|
|
File without changes
|
|
File without changes
|