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