@flowgram.ai/form 0.1.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/dist/esm/index.js +1547 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/index.d.mts +655 -0
- package/dist/index.d.ts +655 -0
- package/dist/index.js +1607 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import React__default from 'react';
|
|
3
|
+
import * as _flowgram_ai_utils from '@flowgram.ai/utils';
|
|
4
|
+
import { MaybePromise, Disposable, Emitter, DisposableCollection } from '@flowgram.ai/utils';
|
|
5
|
+
import { ReactiveState } from '@flowgram.ai/reactive';
|
|
6
|
+
|
|
7
|
+
type Context = any;
|
|
8
|
+
|
|
9
|
+
declare enum FeedbackLevel {
|
|
10
|
+
Error = "error",
|
|
11
|
+
Warning = "warning"
|
|
12
|
+
}
|
|
13
|
+
interface Feedback<FeedbackLevel> {
|
|
14
|
+
/**
|
|
15
|
+
* The data path (or field path) that generate this feedback
|
|
16
|
+
*/
|
|
17
|
+
name: string;
|
|
18
|
+
/**
|
|
19
|
+
* The type of the feedback
|
|
20
|
+
*/
|
|
21
|
+
type?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Feedback level
|
|
24
|
+
*/
|
|
25
|
+
level: FeedbackLevel;
|
|
26
|
+
/**
|
|
27
|
+
* Feedback message
|
|
28
|
+
*/
|
|
29
|
+
message: string;
|
|
30
|
+
}
|
|
31
|
+
type FieldError = Feedback<FeedbackLevel.Error>;
|
|
32
|
+
type FieldWarning = Feedback<FeedbackLevel.Warning>;
|
|
33
|
+
type FormErrorOptions = Omit<FieldError, 'name'>;
|
|
34
|
+
type FormWarningOptions = Omit<FieldWarning, 'name'>;
|
|
35
|
+
type Validate<TFieldValue = any, TFormValues = any> = (props: {
|
|
36
|
+
/**
|
|
37
|
+
* Value of the data to validate
|
|
38
|
+
*/
|
|
39
|
+
value: TFieldValue;
|
|
40
|
+
/**
|
|
41
|
+
* Complete form values
|
|
42
|
+
*/
|
|
43
|
+
formValues: TFormValues;
|
|
44
|
+
/**
|
|
45
|
+
* The path of the data we are validating
|
|
46
|
+
*/
|
|
47
|
+
name: FieldName;
|
|
48
|
+
/**
|
|
49
|
+
* The custom context set when init form
|
|
50
|
+
*/
|
|
51
|
+
context: Context;
|
|
52
|
+
}) => MaybePromise<string> | MaybePromise<FormErrorOptions> | MaybePromise<FormWarningOptions> | MaybePromise<undefined>;
|
|
53
|
+
type Errors = Record<FieldName, FieldError[]>;
|
|
54
|
+
type Warnings = Record<FieldName, FieldWarning[]>;
|
|
55
|
+
declare enum ValidateTrigger {
|
|
56
|
+
onChange = "onChange",
|
|
57
|
+
onBlur = "onBlur"
|
|
58
|
+
}
|
|
59
|
+
type FormValidateReturn = (FieldError | FieldWarning)[];
|
|
60
|
+
|
|
61
|
+
declare class Path {
|
|
62
|
+
protected _path: string[];
|
|
63
|
+
constructor(path: string | string[]);
|
|
64
|
+
get parent(): Path | undefined;
|
|
65
|
+
toString(): string;
|
|
66
|
+
get value(): string[];
|
|
67
|
+
/**
|
|
68
|
+
* 仅计直系child
|
|
69
|
+
* @param path
|
|
70
|
+
*/
|
|
71
|
+
isChild(path: string): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* 比较两个数组path大小
|
|
74
|
+
* 返回小于0则path1<path2, 大于0 则path1>path2, 等于0则相等
|
|
75
|
+
* @param path1
|
|
76
|
+
* @param path2
|
|
77
|
+
*/
|
|
78
|
+
static compareArrayPath(path1: Path, path2: Path): number | void;
|
|
79
|
+
isChildOrGrandChild(path: string): boolean;
|
|
80
|
+
getArrayIndex(parent: Path): number;
|
|
81
|
+
concat(name: number | string): Path;
|
|
82
|
+
replaceParent(parent: Path, newParent: Path): Path;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
declare class Store<TValues = FieldValue> {
|
|
86
|
+
protected _values: TValues;
|
|
87
|
+
get values(): TValues;
|
|
88
|
+
setInitialValues<TValue = FieldValue>(values: TValues): void;
|
|
89
|
+
setIn<TValue = FieldValue>(path: Path, value: TValue): void;
|
|
90
|
+
getIn<TValue = FieldValue>(path: Path): TValue;
|
|
91
|
+
dispose(): void;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
interface OnValueChangePayload<TValue> {
|
|
95
|
+
value: TValue | undefined;
|
|
96
|
+
prevValue: TValue | undefined;
|
|
97
|
+
formValues: any;
|
|
98
|
+
prevFormValues: any;
|
|
99
|
+
}
|
|
100
|
+
declare class FieldModel<TValue extends FieldValue = FieldValue> implements Disposable {
|
|
101
|
+
readonly onValueChangeEmitter: Emitter<OnValueChangePayload<TValue>>;
|
|
102
|
+
readonly form: FormModel;
|
|
103
|
+
readonly id: string;
|
|
104
|
+
readonly onValueChange: _flowgram_ai_utils.Event<OnValueChangePayload<TValue>>;
|
|
105
|
+
protected toDispose: DisposableCollection;
|
|
106
|
+
protected _ref?: Ref;
|
|
107
|
+
protected _path: Path;
|
|
108
|
+
protected _state: ReactiveState<FieldModelState>;
|
|
109
|
+
/**
|
|
110
|
+
* @deprecated
|
|
111
|
+
* 原用于直接给field 设置validate 逻辑,现将该逻辑放到form._options.validate 中设置,该字段暂时弃用
|
|
112
|
+
*/
|
|
113
|
+
originalValidate?: Validate;
|
|
114
|
+
protected _renderCount: number;
|
|
115
|
+
constructor(path: Path, form: FormModel);
|
|
116
|
+
protected _mount: boolean;
|
|
117
|
+
get renderCount(): number;
|
|
118
|
+
set renderCount(n: number);
|
|
119
|
+
private initState;
|
|
120
|
+
get path(): Path;
|
|
121
|
+
get name(): FieldName;
|
|
122
|
+
set name(name: FieldName);
|
|
123
|
+
get ref(): Ref | undefined;
|
|
124
|
+
set ref(ref: Ref | undefined);
|
|
125
|
+
get state(): FieldModelState;
|
|
126
|
+
get reactiveState(): ReactiveState<FieldModelState>;
|
|
127
|
+
get value(): TValue | undefined;
|
|
128
|
+
set value(value: TValue | undefined);
|
|
129
|
+
updateNameForLeafState(newName: string): void;
|
|
130
|
+
/**
|
|
131
|
+
* @deprecated
|
|
132
|
+
* @param validate
|
|
133
|
+
* @param from
|
|
134
|
+
*/
|
|
135
|
+
updateValidate(validate: Validate | undefined, from?: 'ui'): void;
|
|
136
|
+
bubbleState(): void;
|
|
137
|
+
clearState(): void;
|
|
138
|
+
get children(): FieldModel[];
|
|
139
|
+
get parent(): FieldModel | undefined;
|
|
140
|
+
clear(): void;
|
|
141
|
+
validate(): Promise<void>;
|
|
142
|
+
validateSelf(): Promise<void>;
|
|
143
|
+
protected _runAsyncValidate(): Promise<{
|
|
144
|
+
errors?: FieldError[];
|
|
145
|
+
warnings?: FieldWarning[];
|
|
146
|
+
}>;
|
|
147
|
+
updateState(s: Partial<FieldModel>): void;
|
|
148
|
+
dispose(): void;
|
|
149
|
+
onDispose(fn: () => void): void;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
declare class FieldArrayModel<TValue = FieldValue> extends FieldModel<Array<TValue>> {
|
|
153
|
+
protected onAppendEmitter: Emitter<{
|
|
154
|
+
index: number;
|
|
155
|
+
value: TValue | undefined;
|
|
156
|
+
arrayValue: Array<TValue>;
|
|
157
|
+
}>;
|
|
158
|
+
readonly onAppend: _flowgram_ai_utils.Event<{
|
|
159
|
+
index: number;
|
|
160
|
+
value: TValue | undefined;
|
|
161
|
+
arrayValue: Array<TValue>;
|
|
162
|
+
}>;
|
|
163
|
+
protected onDeleteEmitter: Emitter<{
|
|
164
|
+
arrayValue: Array<TValue> | undefined;
|
|
165
|
+
index: number;
|
|
166
|
+
}>;
|
|
167
|
+
readonly onDelete: _flowgram_ai_utils.Event<{
|
|
168
|
+
arrayValue: Array<TValue> | undefined;
|
|
169
|
+
index: number;
|
|
170
|
+
}>;
|
|
171
|
+
get children(): FieldModel<any>[];
|
|
172
|
+
map<T>(cb: (f: FieldModel, index: number, arr: FieldModel[]) => T): T[];
|
|
173
|
+
append(value?: TValue): FieldModel<any>;
|
|
174
|
+
/**
|
|
175
|
+
* 删除数组项,该操作会删除数组项的值并销毁数组项的Field模型
|
|
176
|
+
* @param index
|
|
177
|
+
*/
|
|
178
|
+
delete(index: number): void;
|
|
179
|
+
_splice(start: number, deleteCount?: number): void;
|
|
180
|
+
swap(from: number, to: number): void;
|
|
181
|
+
move(from: number, to: number): void;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
declare class FormModel<TValues = any> implements Disposable {
|
|
185
|
+
protected _fieldMap: Map<string, FieldModel>;
|
|
186
|
+
readonly store: Store<any>;
|
|
187
|
+
protected _options: FormOptions;
|
|
188
|
+
protected onFieldModelCreateEmitter: Emitter<FieldModel<any>>;
|
|
189
|
+
readonly onFieldModelCreate: _flowgram_ai_utils.Event<FieldModel<any>>;
|
|
190
|
+
readonly onFormValuesChangeEmitter: Emitter<OnFormValuesChangePayload>;
|
|
191
|
+
readonly onFormValuesChange: _flowgram_ai_utils.Event<OnFormValuesChangePayload>;
|
|
192
|
+
readonly onFormValuesInitEmitter: Emitter<OnFormValuesInitPayload>;
|
|
193
|
+
readonly onFormValuesInit: _flowgram_ai_utils.Event<OnFormValuesInitPayload>;
|
|
194
|
+
readonly onFormValuesUpdatedEmitter: Emitter<OnFormValuesUpdatedPayload>;
|
|
195
|
+
readonly onFormValuesUpdated: _flowgram_ai_utils.Event<OnFormValuesUpdatedPayload>;
|
|
196
|
+
readonly onValidateEmitter: Emitter<FormModelState>;
|
|
197
|
+
readonly onValidate: _flowgram_ai_utils.Event<FormModelState>;
|
|
198
|
+
protected _state: ReactiveState<FormModelState>;
|
|
199
|
+
protected _initialized: boolean;
|
|
200
|
+
set fieldMap(map: Map<string, FieldModel<any>>);
|
|
201
|
+
/**
|
|
202
|
+
* 表单初始值,初始化设置后不可修改
|
|
203
|
+
* @protected
|
|
204
|
+
*/
|
|
205
|
+
get fieldMap(): Map<string, FieldModel<any>>;
|
|
206
|
+
get context(): any;
|
|
207
|
+
get initialValues(): any;
|
|
208
|
+
get values(): any;
|
|
209
|
+
get validationTrigger(): ValidateTrigger | undefined;
|
|
210
|
+
get state(): FormModelState;
|
|
211
|
+
get reactiveState(): ReactiveState<FormModelState>;
|
|
212
|
+
get fields(): FieldModel[];
|
|
213
|
+
updateState(state: Partial<FormState>): void;
|
|
214
|
+
get initialized(): boolean;
|
|
215
|
+
fireOnFormValuesChange(payload: OnFormValuesChangePayload): void;
|
|
216
|
+
fireOnFormValuesInit(payload: OnFormValuesInitPayload): void;
|
|
217
|
+
init(options: FormOptions<TValues>): void;
|
|
218
|
+
createField<TValue = FieldValue>(name: FieldName, isArray?: boolean): FieldModel<TValue>;
|
|
219
|
+
createFieldArray<TValue = FieldValue>(name: FieldName, value?: Array<TValue>): FieldArrayModel<TValue>;
|
|
220
|
+
/**
|
|
221
|
+
* 销毁Field 模型和子模型,但不会删除field的值
|
|
222
|
+
* @param name
|
|
223
|
+
*/
|
|
224
|
+
disposeField(name: string): void;
|
|
225
|
+
/**
|
|
226
|
+
* 删除field, 会删除值和 Field 模型, 以及对应的子模型
|
|
227
|
+
* @param name
|
|
228
|
+
*/
|
|
229
|
+
deleteField(name: string): void;
|
|
230
|
+
getField<TFieldModel extends FieldModel | FieldArrayModel = FieldModel>(name: FieldName): TFieldModel | undefined;
|
|
231
|
+
getValueIn<TValue>(name: FieldName): TValue;
|
|
232
|
+
setValueIn<TValue>(name: FieldName, value: TValue): void;
|
|
233
|
+
setInitValueIn<TValue = any>(name: FieldName, value: TValue): void;
|
|
234
|
+
clearValueIn(name: FieldName): void;
|
|
235
|
+
validateIn(name: FieldName): Promise<string | FormErrorOptions | FormWarningOptions | undefined>;
|
|
236
|
+
validate(): Promise<FormValidateReturn>;
|
|
237
|
+
dispose(): void;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
interface FormState {
|
|
241
|
+
/**
|
|
242
|
+
* If the form data is valid
|
|
243
|
+
*/
|
|
244
|
+
invalid: boolean;
|
|
245
|
+
/**
|
|
246
|
+
* If the form data is different from the intialValues
|
|
247
|
+
*/
|
|
248
|
+
isDirty: boolean;
|
|
249
|
+
/**
|
|
250
|
+
* If the form fields have been touched
|
|
251
|
+
*/
|
|
252
|
+
isTouched: boolean;
|
|
253
|
+
/**
|
|
254
|
+
* If the form is during validation
|
|
255
|
+
*/
|
|
256
|
+
isValidating: boolean;
|
|
257
|
+
/**
|
|
258
|
+
* Form errors
|
|
259
|
+
*/
|
|
260
|
+
errors?: Errors;
|
|
261
|
+
/**
|
|
262
|
+
* Form warnings
|
|
263
|
+
*/
|
|
264
|
+
warnings?: Warnings;
|
|
265
|
+
}
|
|
266
|
+
interface FormModelState extends Omit<FormState, 'errors' | 'warnings'> {
|
|
267
|
+
errors?: Errors;
|
|
268
|
+
warnings?: Warnings;
|
|
269
|
+
}
|
|
270
|
+
interface FormOptions<TValues = any> {
|
|
271
|
+
/**
|
|
272
|
+
* InitialValues of the form.
|
|
273
|
+
*/
|
|
274
|
+
initialValues?: TValues;
|
|
275
|
+
/**
|
|
276
|
+
* When should the validation trigger, for example onChange or onBlur.
|
|
277
|
+
*/
|
|
278
|
+
validateTrigger?: ValidateTrigger;
|
|
279
|
+
/**
|
|
280
|
+
* Form data's validation rules. It's a key value map, where the key is a pattern of data's path (or field name), the value is a validate function.
|
|
281
|
+
*/
|
|
282
|
+
validate?: Record<string, Validate>;
|
|
283
|
+
/**
|
|
284
|
+
* Custom context. It will be accessible via form instance or in validate function.
|
|
285
|
+
*/
|
|
286
|
+
context?: Context;
|
|
287
|
+
}
|
|
288
|
+
interface Form$1<TValues = any> {
|
|
289
|
+
/**
|
|
290
|
+
* The initialValues of the form.
|
|
291
|
+
*/
|
|
292
|
+
initialValues: TValues;
|
|
293
|
+
/**
|
|
294
|
+
* Form values. Returns a deep copy of the data in the store.
|
|
295
|
+
*/
|
|
296
|
+
values: TValues;
|
|
297
|
+
/**
|
|
298
|
+
* Form state
|
|
299
|
+
*/
|
|
300
|
+
state: FormState;
|
|
301
|
+
/**
|
|
302
|
+
* Get value in certain path
|
|
303
|
+
* @param name path
|
|
304
|
+
*/
|
|
305
|
+
getValueIn<TValue = FieldValue>(name: FieldName): TValue;
|
|
306
|
+
/**
|
|
307
|
+
* Set value in certain path.
|
|
308
|
+
* It will trigger the re-rendering of the Field Component if a Field is related to this path
|
|
309
|
+
* @param name path
|
|
310
|
+
*/
|
|
311
|
+
setValueIn<TValue>(name: FieldName, value: TValue): void;
|
|
312
|
+
}
|
|
313
|
+
interface FormRenderProps<TValues> {
|
|
314
|
+
/**
|
|
315
|
+
* Form instance.
|
|
316
|
+
*/
|
|
317
|
+
form: Form$1<TValues>;
|
|
318
|
+
}
|
|
319
|
+
interface FormControl<TValues> {
|
|
320
|
+
_formModel: FormModel<TValues>;
|
|
321
|
+
getField: <TValue = FieldValue, TField extends Field$1<TValue> | FieldArray$1<TValue> = Field$1<TValue>>(name: FieldName) => Field$1<TValue> | FieldArray$1<TValue> | undefined;
|
|
322
|
+
/** 手动初始化form */
|
|
323
|
+
init: () => void;
|
|
324
|
+
}
|
|
325
|
+
interface CreateFormReturn<TValues> {
|
|
326
|
+
form: Form$1<TValues>;
|
|
327
|
+
control: FormControl<TValues>;
|
|
328
|
+
}
|
|
329
|
+
interface OnFormValuesChangeOptions {
|
|
330
|
+
action?: 'array-append' | 'array-splice';
|
|
331
|
+
indexes?: number[];
|
|
332
|
+
}
|
|
333
|
+
interface OnFormValuesChangePayload {
|
|
334
|
+
values: FieldValue;
|
|
335
|
+
prevValues: FieldValue;
|
|
336
|
+
name: FieldName;
|
|
337
|
+
options?: OnFormValuesChangeOptions;
|
|
338
|
+
}
|
|
339
|
+
interface OnFormValuesInitPayload {
|
|
340
|
+
values: FieldValue;
|
|
341
|
+
prevValues: FieldValue;
|
|
342
|
+
name: FieldName;
|
|
343
|
+
}
|
|
344
|
+
interface OnFormValuesUpdatedPayload {
|
|
345
|
+
values: FieldValue;
|
|
346
|
+
prevValues: FieldValue;
|
|
347
|
+
name: FieldName;
|
|
348
|
+
options?: OnFormValuesChangeOptions;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
type FieldValue = any;
|
|
352
|
+
type FieldName = string;
|
|
353
|
+
type CustomElement = Partial<HTMLElement> & {
|
|
354
|
+
name: FieldName;
|
|
355
|
+
type?: string;
|
|
356
|
+
value?: any;
|
|
357
|
+
disabled?: boolean;
|
|
358
|
+
checked?: boolean;
|
|
359
|
+
options?: HTMLOptionsCollection;
|
|
360
|
+
files?: FileList | null;
|
|
361
|
+
focus?: () => void;
|
|
362
|
+
};
|
|
363
|
+
type FieldElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | CustomElement;
|
|
364
|
+
type Ref = FieldElement;
|
|
365
|
+
/**
|
|
366
|
+
* Field render model, it's only available when Field is rendered
|
|
367
|
+
*/
|
|
368
|
+
interface Field$1<TFieldValue extends FieldValue = FieldValue, E = React__default.ChangeEvent<any> | TFieldValue> {
|
|
369
|
+
/**
|
|
370
|
+
* Uniq key for the Field, you can use it for the child react component's uniq key.
|
|
371
|
+
*/
|
|
372
|
+
key: string;
|
|
373
|
+
/**
|
|
374
|
+
* A function which sends the input's value to Field.
|
|
375
|
+
* It should be assigned to the onChange prop of the input component
|
|
376
|
+
* @param e It can be the new value of the field or the event sent by original dom input or checkbox component.
|
|
377
|
+
*/
|
|
378
|
+
onChange: (e: E) => void;
|
|
379
|
+
/**
|
|
380
|
+
* The current value of Field
|
|
381
|
+
*/
|
|
382
|
+
value: TFieldValue;
|
|
383
|
+
/**
|
|
384
|
+
* Field's name (path)
|
|
385
|
+
*/
|
|
386
|
+
name: FieldName;
|
|
387
|
+
/**
|
|
388
|
+
* A function which sends the input's onFocus event to Field. It should be assigned to the input's onFocus prop.
|
|
389
|
+
*/
|
|
390
|
+
onFocus?: () => void;
|
|
391
|
+
/**
|
|
392
|
+
* A function which sends the input's onBlur event to Field. It should be assigned to the input's onBlur prop.
|
|
393
|
+
*/
|
|
394
|
+
onBlur?: () => void;
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* FieldArray render model, it's only available when FieldArray is rendered
|
|
398
|
+
*/
|
|
399
|
+
interface FieldArray$1<TFieldValue extends FieldValue = FieldValue> extends Field$1<Array<TFieldValue> | undefined, Array<TFieldValue> | undefined> {
|
|
400
|
+
/**
|
|
401
|
+
* Same as native Array.map, the first param of the callback function is the child field of this FieldArray.
|
|
402
|
+
* @param cb callback function
|
|
403
|
+
*/
|
|
404
|
+
map: <T = any>(cb: (f: Field$1, index: number) => T) => T[];
|
|
405
|
+
/**
|
|
406
|
+
* Append a value at the end of the array, it will create a new Field for this value as well.
|
|
407
|
+
* @param value the value to append
|
|
408
|
+
*/
|
|
409
|
+
append: (value: TFieldValue) => Field$1<TFieldValue>;
|
|
410
|
+
/**
|
|
411
|
+
* Delete the value and the related field at certain index of the array.
|
|
412
|
+
* @param index the index of the element to delete
|
|
413
|
+
*/
|
|
414
|
+
delete: (index: number) => void;
|
|
415
|
+
/**
|
|
416
|
+
* Move an array element from one position to another.
|
|
417
|
+
* @param from from position
|
|
418
|
+
* @param to to position
|
|
419
|
+
*/
|
|
420
|
+
move: (from: number, to: number) => void;
|
|
421
|
+
/**
|
|
422
|
+
* Swap the position of two elements of the array.
|
|
423
|
+
* @param from
|
|
424
|
+
* @param to
|
|
425
|
+
*/
|
|
426
|
+
swap: (from: number, to: number) => void;
|
|
427
|
+
}
|
|
428
|
+
interface FieldOptions<TValue, TFormValues = any> {
|
|
429
|
+
/**
|
|
430
|
+
* Field's name(path), it should be uniq within a form instance.
|
|
431
|
+
* Two Fields Rendered with the same name will link to the same part of data and field status such as errors is shared.
|
|
432
|
+
*/
|
|
433
|
+
name: FieldName;
|
|
434
|
+
/**
|
|
435
|
+
* Default value of the field. Please notice that Field is a render model, so this default value will only be set when
|
|
436
|
+
* the field is rendered. If you want to give a default value before field rendering, please set it in the Form's defaultValue.
|
|
437
|
+
*/
|
|
438
|
+
defaultValue?: TValue;
|
|
439
|
+
/**
|
|
440
|
+
* This is a render prop. A function that returns a React element and provides the ability to attach events and value into the component.
|
|
441
|
+
* This simplifies integrating with external controlled components with non-standard prop names. Provides field、fieldState and formState, to the child component.
|
|
442
|
+
* @param props
|
|
443
|
+
*/
|
|
444
|
+
render?: (props: FieldRenderProps<TValue>) => React__default.ReactElement;
|
|
445
|
+
}
|
|
446
|
+
interface FieldRenderProps<TValue> {
|
|
447
|
+
field: Field$1<TValue>;
|
|
448
|
+
fieldState?: Readonly<FieldState>;
|
|
449
|
+
formState?: Readonly<FormState>;
|
|
450
|
+
}
|
|
451
|
+
interface FieldArrayOptions<TValue> {
|
|
452
|
+
/**
|
|
453
|
+
* Field's name(path), it should be uniq within a form instance.
|
|
454
|
+
* Two Fields Rendered with the same name will link to the same part of data and field status such as errors is shared.
|
|
455
|
+
*/
|
|
456
|
+
name: FieldName;
|
|
457
|
+
/**
|
|
458
|
+
* Default value of the field. Please notice that Field is a render model, so this default value will only be set when
|
|
459
|
+
* the field is rendered. If you want to give a default value before field rendering, please set it in the Form's initialValues.
|
|
460
|
+
*/
|
|
461
|
+
defaultValue?: TValue[];
|
|
462
|
+
/**
|
|
463
|
+
* This is a render prop. A function that returns a React element and provides the ability to attach events and value into the component.
|
|
464
|
+
* This simplifies integrating with external controlled components with non-standard prop names. Provides field、fieldState and formState, to the child component.
|
|
465
|
+
* @param props
|
|
466
|
+
*/
|
|
467
|
+
render?: (props: FieldArrayRenderProps<TValue>) => React__default.ReactElement;
|
|
468
|
+
}
|
|
469
|
+
interface FieldArrayRenderProps<TValue> {
|
|
470
|
+
field: FieldArray$1<TValue>;
|
|
471
|
+
fieldState?: Readonly<FieldState>;
|
|
472
|
+
formState?: Readonly<FormState>;
|
|
473
|
+
}
|
|
474
|
+
interface FieldState {
|
|
475
|
+
/**
|
|
476
|
+
* If field value is invalid
|
|
477
|
+
*/
|
|
478
|
+
invalid: boolean;
|
|
479
|
+
/**
|
|
480
|
+
* If field input component is touched by user
|
|
481
|
+
*/
|
|
482
|
+
isTouched: boolean;
|
|
483
|
+
/**
|
|
484
|
+
* If field current value is different from the initialValue.
|
|
485
|
+
*/
|
|
486
|
+
isDirty: boolean;
|
|
487
|
+
/**
|
|
488
|
+
* If field is validating.
|
|
489
|
+
*/
|
|
490
|
+
isValidating: boolean;
|
|
491
|
+
/**
|
|
492
|
+
* Field errors, empty array means there is no errors.
|
|
493
|
+
*/
|
|
494
|
+
errors?: FieldError[];
|
|
495
|
+
/**
|
|
496
|
+
* Field warnings, empty array means there is no warnings.
|
|
497
|
+
*/
|
|
498
|
+
warnings?: FieldWarning[];
|
|
499
|
+
}
|
|
500
|
+
interface FieldModelState extends Omit<FieldState, 'errors' | 'warnings'> {
|
|
501
|
+
errors?: Errors;
|
|
502
|
+
warnings?: Warnings;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
type FieldProps<TValue> = FieldOptions<TValue> & {
|
|
506
|
+
/**
|
|
507
|
+
* A React element or a render prop
|
|
508
|
+
*/
|
|
509
|
+
children?: ((props: FieldRenderProps<TValue>) => React.ReactElement) | React.ReactElement;
|
|
510
|
+
/**
|
|
511
|
+
* Dependencies of the current field. If a field name is given in deps, current field will re-render if the given field name data is updated
|
|
512
|
+
*/
|
|
513
|
+
deps?: FieldName[];
|
|
514
|
+
};
|
|
515
|
+
/**
|
|
516
|
+
* HOC That declare a field, an Field model will be created it's rendered. Multiple Field rendering with a same name will link to the same model, which means they shared data、 status and methods
|
|
517
|
+
*/
|
|
518
|
+
declare function Field<TValue>({ name, defaultValue, render, children, deps, }: FieldProps<TValue>): React.ReactElement;
|
|
519
|
+
|
|
520
|
+
type FormProps<TValues> = FormOptions & {
|
|
521
|
+
/**
|
|
522
|
+
* React children or child render prop
|
|
523
|
+
*/
|
|
524
|
+
children?: ((props: FormRenderProps<TValues>) => React__default.ReactNode) | React__default.ReactNode;
|
|
525
|
+
/**
|
|
526
|
+
* If this prop is set to true, Form instance will be kept event thought<Form /> is destroyed.
|
|
527
|
+
* This means you can still use some form's api such as Form.validate and Form.setValueIn to handle pure data logic.
|
|
528
|
+
* @default false
|
|
529
|
+
*/
|
|
530
|
+
keepModelOnUnMount?: boolean;
|
|
531
|
+
/**
|
|
532
|
+
* provide form instance from outside. if control is given Form will use the form instance in the control instead of creating one.
|
|
533
|
+
*/
|
|
534
|
+
control?: FormControl<TValues>;
|
|
535
|
+
};
|
|
536
|
+
/**
|
|
537
|
+
* Hoc That init and provide Form instance. You can also provide form instance from outside by using control prop
|
|
538
|
+
* @param props
|
|
539
|
+
*/
|
|
540
|
+
declare function Form<TValues>(props: FormProps<TValues>): React__default.JSX.Element;
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Get Form instance. It should be use in a child component of <Form />
|
|
544
|
+
*/
|
|
545
|
+
declare function useForm(): Form$1;
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Listen to the field data change and refresh the React component.
|
|
549
|
+
* @param name the field's uniq name (path)
|
|
550
|
+
*/
|
|
551
|
+
declare function useWatch<TValue = FieldValue>(name: FieldName): TValue;
|
|
552
|
+
|
|
553
|
+
type FieldArrayProps<TValue> = FieldArrayOptions<TValue> & {
|
|
554
|
+
/**
|
|
555
|
+
* A React element or a render prop
|
|
556
|
+
*/
|
|
557
|
+
children?: ((props: FieldArrayRenderProps<TValue>) => React.ReactElement) | React.ReactElement;
|
|
558
|
+
/**
|
|
559
|
+
* Dependencies of the current field. If a field name is given in deps, current field will re-render if the given field name data is updated
|
|
560
|
+
*/
|
|
561
|
+
deps?: FieldName[];
|
|
562
|
+
};
|
|
563
|
+
/**
|
|
564
|
+
* HOC That declare an array field, an FieldArray model will be created when it's rendered. Multiple FieldArray rendering with a same name will link to the same model, which means they shared data、 status and methods
|
|
565
|
+
*/
|
|
566
|
+
declare function FieldArray<TValue extends FieldValue>({ name, defaultValue, deps, render, children, }: FieldArrayProps<TValue>): React.ReactElement;
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* @deprecated
|
|
570
|
+
* `useField` is deprecated because its return relies on React render. if the Field is not rendered, the return would be
|
|
571
|
+
* undefined. If you simply want to monitor the change of the value of a certain path, please use `useWatch(fieldName)`
|
|
572
|
+
* @param name
|
|
573
|
+
*/
|
|
574
|
+
declare function useField<TFieldValue = FieldValue, TField extends Field$1<TFieldValue> | FieldArray$1<TFieldValue> = Field$1<TFieldValue>>(name?: FieldName): TField | undefined;
|
|
575
|
+
|
|
576
|
+
declare function useFormState(control?: FormControl<any> | Form$1): FormState;
|
|
577
|
+
declare function useFormErrors(control?: FormControl<any> | Form$1): Errors | undefined;
|
|
578
|
+
declare function useFormWarnings(control?: FormControl<any> | Form$1): Warnings | undefined;
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Get validate method of a field with given name. the returned function could possibly do nothing if the field is not found.
|
|
582
|
+
* The reason could be that the field is not rendered yet or the name given is wrong.
|
|
583
|
+
* @param name
|
|
584
|
+
*/
|
|
585
|
+
declare function useFieldValidate(name?: FieldName): () => void;
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Get the current Field. It should be used in a child component of <Field />, otherwise it throws an error
|
|
589
|
+
*/
|
|
590
|
+
declare function useCurrentField<TFieldValue = FieldValue, TField extends Field$1<TFieldValue> | FieldArray$1<TFieldValue> = Field$1<TFieldValue>>(): Field$1<TFieldValue> | FieldArray$1<TFieldValue>;
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* Get the current field state. It should be used in a child component of <Field />, otherwise it throws an error
|
|
594
|
+
*/
|
|
595
|
+
declare function useCurrentFieldState(): FieldState;
|
|
596
|
+
|
|
597
|
+
type CreateFormOptions = FormOptions & {
|
|
598
|
+
/**
|
|
599
|
+
* 为 true 时,createForm 不会对form 初始化, 用户需要手动调用 control.init()
|
|
600
|
+
* 该配置主要为了解决,用户需要去监听一些form 的初始化事件,那么他需要再配置完监听后再初始化。
|
|
601
|
+
* 该配置默认为 false
|
|
602
|
+
**/
|
|
603
|
+
disableAutoInit?: boolean;
|
|
604
|
+
};
|
|
605
|
+
declare function createForm<TValues>(options?: CreateFormOptions): CreateFormReturn<TValues>;
|
|
606
|
+
|
|
607
|
+
declare namespace Glob {
|
|
608
|
+
const DIVIDER = ".";
|
|
609
|
+
const ALL = "*";
|
|
610
|
+
function isMatch(pattern: string, path: string): boolean;
|
|
611
|
+
/**
|
|
612
|
+
* 判断pattern 是否match pattern 或其parent
|
|
613
|
+
* @param pattern
|
|
614
|
+
* @param path
|
|
615
|
+
*/
|
|
616
|
+
function isMatchOrParent(pattern: string, path: string): boolean;
|
|
617
|
+
/**
|
|
618
|
+
* 从 path 中提取出匹配pattern 的 parent path,包括是 path 自身
|
|
619
|
+
* 该方法默认 isMatchOrParent(pattern, path) 为 true, 不做为false 的错误处理。
|
|
620
|
+
* @param pattern
|
|
621
|
+
* @param path
|
|
622
|
+
*/
|
|
623
|
+
function getParentPathByPattern(pattern: string, path: string): string;
|
|
624
|
+
/**
|
|
625
|
+
* 找到 obj 在给与 paths 下所有子path
|
|
626
|
+
* @param paths
|
|
627
|
+
* @param obj
|
|
628
|
+
* @private
|
|
629
|
+
*/
|
|
630
|
+
function getSubPaths(paths: string[], obj: any): string[];
|
|
631
|
+
/**
|
|
632
|
+
* 将带有通配符的 path pattern 分割。如 a.b.*.c.*.d, 会被分割成['a.b','*','c','*','d']
|
|
633
|
+
* @param pattern
|
|
634
|
+
* @private
|
|
635
|
+
*/
|
|
636
|
+
function splitPattern(pattern: string): string[];
|
|
637
|
+
function findMatchPaths(obj: any, pattern: string): string[];
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
declare function toField<TValue>(model: FieldModel): Field$1<TValue>;
|
|
641
|
+
declare function toFieldState(modelState: FieldModelState): {
|
|
642
|
+
readonly isTouched: boolean;
|
|
643
|
+
readonly invalid: boolean;
|
|
644
|
+
readonly isDirty: boolean;
|
|
645
|
+
readonly isValidating: boolean;
|
|
646
|
+
readonly errors: FieldError[] | undefined;
|
|
647
|
+
readonly warnings: FieldWarning[] | undefined;
|
|
648
|
+
};
|
|
649
|
+
|
|
650
|
+
declare function toFieldArray<TValue>(model: FieldArrayModel<TValue>): FieldArray$1<TValue>;
|
|
651
|
+
|
|
652
|
+
declare function toForm<TValue>(model: FormModel): Form$1<TValue>;
|
|
653
|
+
declare function toFormState(modelState: FormModelState): FormState;
|
|
654
|
+
|
|
655
|
+
export { Field, FieldArray, FieldArrayModel, type FieldArrayProps, type FieldArrayRenderProps, type FieldError, FieldModel, type FieldName, type FieldProps, type FieldRenderProps, type FieldState, type FieldValue, type FieldWarning, Form, type FormControl, FormModel, type FormProps, type FormRenderProps, type FormState, type FormValidateReturn, Glob, type Field$1 as IField, type FieldArray$1 as IFieldArray, Path, type Validate, ValidateTrigger, createForm, toField, toFieldArray, toFieldState, toForm, toFormState, useCurrentField, useCurrentFieldState, useField, useFieldValidate, useForm, useFormErrors, useFormState, useFormWarnings, useWatch };
|