forked from davidkpiano/react-redux-form
-
Notifications
You must be signed in to change notification settings - Fork 1
/
react-redux-form.d.ts
1035 lines (953 loc) · 42 KB
/
react-redux-form.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Type definitions for react-redux-form
// Project: https://github.com/davidkpiano/react-redux-form
// Definitions by: Robert Parker (Flavorus) <https://github.com/hsrobflavorus>, Flavorus <http://www.flavorus.com>, Alexey Svetliakov (@asvetliakov), Zach Waggoner <https://github.com/zach-waggoner>
import * as React from 'react';
import * as Redux from 'redux';
interface Action {
type: any;
}
type Reducer<S> = <A extends Action>(state: S, action: A) => S;
interface AsyncValidatorFn {
(val: any, done: Function): void;
}
interface AsyncValidators {
[key: string]: AsyncValidatorFn;
}
interface ValidatorFn {
(val: any): boolean;
}
interface Validators {
[key: string]: ValidatorFn;
}
interface ValidityObject {
[key: string]: boolean;
}
interface ErrorFn {
(val: any): boolean | string;
}
interface ErrorValidators {
[key: string]: ErrorFn;
}
interface ErrorsObject {
[key: string]: boolean | string;
}
interface ErrorsMessageSelector {
(val: any): string;
}
interface ErrorsComponentMessages {
[key: string]: string | ErrorsMessageSelector;
}
interface FormValidators {
[key: string]: Validators;
}
interface ValidationErrors {
[key: string]: any;
}
/**
* Internal interface
*/
interface FieldsObject<T> {
[key: string]: T;
}
/**
* Used for the <Errors> `show` prop
*/
interface FieldStatePredicate {
(field: FieldState): boolean;
}
/**
* A function that, when called with state, will return the first full model string (with the sub-model) that matches the predicate iteratee.
*/
interface ModelGetterFn {
(state: any): string;
}
/**
* Additional props when specifying a SFC Wrapper (ex. in <Errors>)
*/
interface WrapperProps {
modelValue: any;
fieldValue: FieldState;
}
/**
* Additional props when specifying a SFC Custom Component (ex. in <Errors>)
*/
interface CustomComponentProps extends WrapperProps {
children: any;
}
interface MapPropsProps {
onChange: (event: any) => void;
onBlur: (event: any) => void;
onFocus: (event: any) => void;
fieldValue: FieldState;
modelValue: any;
viewValue: any;
}
type MapPropsFunc = (props: MapPropsProps) => any;
type MapPropsObject = { [key: string]: (props: MapPropsProps) => any };
type MapProps = MapPropsFunc | MapPropsObject;
export interface ControlProps<T> extends React.HTMLProps<T> {
/**
* Wrap field into custom component
*/
component?: React.ReactType;
/**
* The string representing the store model value
* OR
* A function that, when called with state, will return the first full model string (with the sub-model) that matches the predicate iteratee.
*/
model: string | ModelGetterFn;
/**
* If updateOn is a function, the function given will be called with the change action creator.
* The function given will be called in onChange.
*
* If a string, updateOn can be one of these values:
* * change - will dispatch in onChange
* * blur - will dispatch in onBlur
* * focus - will dispatch in onFocus
* @example
* import debounce from 'lodash/debounce';
* <Field model="test.bounce" updateOn={(change) => debounce(change, 1000)}>
* <input type="text" />
* </Field>
*/
updateOn?: 'change' | 'blur' | 'focus';
/**
* A map where the keys are validation keys, and the values are the corresponding functions that determine the validity of each key, given the model's value.
* Validator functions accept the value and return true if the field is valid.
*/
validators?: Validators;
/**
* Indicates when to validate the field
*
* change - will dispatch in onChange
* blur - will dispatch in onBlur
* focus - will dispatch in onFocus
*
* @default change
*/
validateOn?: 'change' | 'blur' | 'focus';
asyncValidators?: AsyncValidators;
/**
* Indicates when to validate the field asynchronously
*
* change - will dispatch in onChange
* blur - will dispatch in onBlur
* focus - will dispatch in onFocus
*
* @default change
*/
asyncValidateOn?: 'change' | 'blur' | 'focus';
/**
* An error validation object with key-errorValidator pairs.
* The inverse of `validators`.
*/
errors?: ValidationErrors | ErrorValidators;
/**
* A function that parses the view value of the field before it is changed.
* @param value The view value that represents the next model value
* @param previous The current model value before it is changed
* @returns {typeof value} The parsed/processed value
*/
parser?: (value: any, previous?: any) => any;
/**
* An action creator (function) that specifies which action the <Field> component should use when dispatching a change to the model.
*
* By default, this action is:
* * actions.change(model, value) for text input controls
* * actions.toggle(model, value) for checkboxes (single-value models)
* * actions.xor(model, value) for checkboxes (multi-value models)
*
* Notes:
* * Use changeAction to do any other custom actions whenever your value is to change.
* * Since changeAction expects an action creator and redux-thunk is used, you can asynchronously dispatch actions by returning a thunk.
*
* @param model The model that is being changed
* @param value The value that the model is being changed to
*/
changeAction?: (model: string, value: any) => void;
/**
* A mapping of control-specific property keys to prop-getter functions that taken in the original props and return the result prop.
* See {@link https://davidkpiano.github.io/react-redux-form/docs/guides/custom-controls.html the documentation on custom controls} for more information.
*/
mapProps?: MapProps;
controlProps?: any;
/**
* Calls the callback provided to the getRef prop with the node instance. Similar to ref.
*/
getRef?: () => void;
}
export class Control<T> extends React.Component<ControlProps<T>, {}> {
static input: React.ComponentClass<ControlProps<HTMLInputElement>>;
static text: React.ComponentClass<ControlProps<HTMLInputElement>>;
static textarea: React.ComponentClass<ControlProps<HTMLTextAreaElement>>;
static radio: React.ComponentClass<ControlProps<HTMLInputElement>>;
static checkbox: React.ComponentClass<ControlProps<HTMLInputElement>>;
static file: React.ComponentClass<ControlProps<HTMLInputElement>>;
static select: React.ComponentClass<ControlProps<HTMLSelectElement>>;
static reset: React.ComponentClass<ControlProps<HTMLButtonElement>>;
}
export interface FieldProps<T> extends ControlProps<T> {
/**
* Specifies whether the children inside <Field> are dynamic; that is, whether they are subject to change based on outside values.
* Default value: true. To optimize for performance, set dynamic={false} for any <Field> that does not have dynamic children.
* @example
* // Does NOT have dynamic children
* <Field model="user.favoriteColors" dynamic={false}>
* <select>
* <option value="red">red</option>
* <option value="green">green</option>
* <option value="blue">blue</option>
* </select>
* </Field>
*
* // DOES have dynamic children
* <Field model="user.favoriteColors">
* <select>
* {showWhite && <option value="white">white</option>}
* <option value="red">red</option>
* <option value="green">green</option>
* <option value="blue">blue</option>
* </select>
* </Field>
*
* // Does NOT have dynamic children
* <Field model="user.state" mapProps={...} dynamic={false}>
* <StatePicker />
* </Field>
*
* // DOES have dynamic children
* const { showTerritories } = this.props;
*
* <Field model="user.state" mapProps={...}>
* <StatePicker territories={showTerritories} />
* </Field>
*/
dynamic?: boolean;
}
export class Field<T> extends React.Component<FieldProps<T>, {}> {
}
interface BaseFormProps {
/**
* CSS Class Name(s)
*/
className?: string;
/**
* An object representing the validators for the fields inside the form, where:
* * The keys are the field model names (e.g. 'email' for user.email)
* * The values are validator(s) for the field model. They can be:
* ** A validator function, which receives the field model value, or
* ** A validator object, with validation keys and validator functions as values, also receiving the field model value.
*
* If the key is the empty string ('': {...}), then the validator will belong to the form model itself.
* Validation will occur on any field model change by default, and only the validators for the fields that have changed will be run (as a performance enhancement)!
*
* Tips
* * Specifying validators on the form is usually sufficient - you don't need to put validators on the <Field> for most use cases.
* * If you need validators to run on submit, this is the place to put them.
*/
validators?: Validators | FormValidators;
/**
* An object representing the error validators for the fields inside the form, where:
* * the keys are the field model names (e.g. 'email' for user.email)
* * the values are error validator(s) for the field model. They can be:
* * an error validator function, which receives the field model value, or
* * an error validator object, with validation keys and error validator functions as values, also receiving the field model value.
*
* Its behavior is identical to the validators={{...}} prop, with the exception that an error validator that returns anything truthy is interpreted as an error. See the validation guide for more info.
*/
errors?: ValidationErrors | ErrorValidators;
/**
* A string that indicates when validators or errors (for error validators) should run.
* By default, validators will only run whenever a field changes, and
* * Only for the field that has changed, and
* * Always for any form-wide validators.
*
* The possible values are:
* * change (Default): run validation whenever a field model value changes
* * submit: run validation only when submitting the form.
*
* Tips
* * Keep in mind, validation will always run initially, when the form is loaded.
* * If you want better performance, you can use validateOn="submit", depending on your use-case.
*/
validateOn?: 'change' | 'submit';
/**
* The handler function called when the form is submitted. This works almost exactly like a normal <form onSubmit={...}> handler, with a few differences:
* * The submit event's default action is prevented by default, using event.preventDefault().
* * The onSubmit handler will not execute if the form is invalid.
* * The onSubmit handler receives the form model data, not the event.
*
* Tips:
* * You can do anything in onSubmit; including firing off custom actions or handling (async) validation yourself.
* @param formModelData The form's model data
*/
onSubmit?: (formModelData: any) => void;
/**
* The handler function that is called with the form state whenever the form state is updated.
*
* Notes
* * This is an optional but useful property, especially if you are using local forms.
* * Remember: the form state is the state related to the form and its fields, such as whether it's valid, focused, pristine, etc.
* @param formValue
*/
onUpdate?: (formValue: any) => void;
/**
* The handler function that is called with the form's model value whenever the model value is updated.
*
* Notes
* * This is also an optional but useful property, especially if you are using local forms.
* * Remember: the model value is the value of the form's model, specified by the model="..." prop. The entire model value will be passed in.
* @param modelValue
*/
onChange?: (modelValue: any) => void;
/**
* The component that the <Form> should be rendered to (default: "form".)
*
* * For React Native, it is important that you specify the component to avoid any rendering errors. For most use cases, component={View} will work.
*/
component?: React.ComponentClass<any> | string;
}
export interface FormProps extends BaseFormProps {
/**
* The string representing the model value of the entire form in the store.
* OR
* A function that, when called with state, will return the first full model string (with the sub-model) that matches the predicate iteratee.
*
* Typically, the <Control> (and/or <Field>) components nested inside <Form> would be members of the form model;
* e.g. user.email and user.password are members of the user model.
*
* You can also use partial models for <Control>, <Field>, and <Errors> components inside of <Form> - they will be resolved to the form's model.
*/
model: string | ModelGetterFn;
}
export class Form extends React.Component<FormProps, {}> { }
interface LocalFormProps extends BaseFormProps {
store?: Redux.Store<any>;
/**
* The initial state of the model (default: {})
*/
initialState?: any;
/**
* The name of the model in the internal state. This is completely optional, as the model is not related to any external Redux store (default: "local")
*/
model?: string;
}
export class LocalForm extends React.Component<LocalFormProps, void> { }
export interface ErrorsProps {
/**
* The string representing the store model value
* OR
* A function that, when called with state, will return the first full model string (with the sub-model) that matches the predicate iteratee.
*/
model: string | ModelGetterFn;
/**
* A plain object mapping where:
* * the keys are error keys (such as "required")
* * the values are either strings or functions.
* If the message value is a function, it will be called with the model value.
*
* Tips:
* * The messages prop is a great place to keep custom error messages that can vary based on the location in the UI, instead of hardcoding error messages in validation fuctions.
* * If a message is not provided for an error key, the message will default to the key value in the field .errors property.
* * This means if you're using setErrors or the errors prop in <Field> to set error messages, they will automatically be shown in <Errors />.
*/
messages?: ErrorsComponentMessages;
/**
* The show prop determines when error messages should be shown, based on the model's field state (determined by the form reducer).
* It can be a boolean, or a function, string, or object as a Lodash iteratee (https://lodash.com/docs#iteratee)
*
* Examples
* * show={true} will always show the errors if they exist
* * show={(field) => field.touched && !field.focus} will show errors if the model's field is touched and not focused
* * show={ {touched: true, focus: false} } same as above
* * show="touched" will show errors if the model's field is touched
*
* Tips
* * For the greatest amount of control, use show as a function.
* * Use show as a boolean if you want to calculate when an error should be shown based on external factors, such as form state.
* @see https://lodash.com/docs#iteratee
*/
show?: FieldState | FieldStatePredicate | boolean | string;
/**
* The `wrapper` component, which is the component that wraps all errors, can be configured using this prop. Default: "div".
*
* Examples
* * wrapper="ul" will wrap all errors in an <ul>
* * wrapper={(props) => <div className="errors">{props.children}</div>} will render the specified functional component, with all the props from <Errors> and some computed props:
* * modelValue - the current value of the model
* * fieldValue - the current field state of the model
* * wrapper={CustomErrors} will wrap all errors in a <CustomErrors> component, which will receive the same props as above.
*/
wrapper?: string | React.StatelessComponent<ErrorsProps & WrapperProps> | React.ComponentClass<ErrorsProps & WrapperProps>;
/**
* The `component`, which is the component for each error message, can be configured using this prop. Default: "span".
*
* Examples
* * component="li" will wrap all errors in a <li>
* * component={(props) => <div className="error">{props.children}</div>} will render the error message in the specified functional component, with these props:
* * modelValue - the current value of the model
* * fieldValue - the current field state of the model
* * children - the error message (text).
* * component={CustomError} will wrap the error in a <CustomError> component, which will receive the same props as above.
*/
component?: string | React.StatelessComponent<ErrorsProps & CustomComponentProps> | React.ComponentClass<ErrorsProps & CustomComponentProps>;
}
export class Errors extends React.Component<ErrorsProps, {}> {
}
/**
* Model state returned by model reducer
*/
export interface ModelState {
[field: string]: any;
}
/**
* Represents the state of a field returned by the Form Reducer
*/
interface _FieldState {
/**
* @default true
*/
blur: boolean;
/**
* @default false
*/
dirty: boolean;
/**
* @default false
*/
focus: boolean;
/**
* @default any
*/
initialValue: any;
/**
* @default false
*/
pending: boolean;
/**
* @default true
*/
pristine: boolean;
/**
* @default false
*/
retouched: boolean;
/**
* @default false
*/
submitted: boolean;
/**
* @default false
*/
touched: boolean;
/**
* @default true
*/
untouched: boolean;
/**
* @default true
*/
valid: boolean;
/**
* @default false
*/
validating: boolean;
/**
* @default null
*/
viewValue: boolean;
/**
* @default {}
*/
validity: any;
}
export interface FieldState extends _FieldState {
/**
* Error object containing (key) validator name -> (value) boolean if is error (meaning INVALID is true)
* @default {}
*/
errors: any;
}
interface FieldStateT<TErrors extends ErrorsObject> extends _FieldState {
errors: TErrors;
}
/**
* Form state returned by form reducer
*/
export interface FormState {
/**
* @deprecated Use the inverse of focus instead
*/
blur: boolean;
/**
* @deprecated Use the inverse of pristine instead
*/
dirty: boolean;
errors: ErrorsObject;
fields: { [name: string]: FieldState };
focus: boolean;
model: string;
pending: boolean;
pristine: boolean;
retouched: boolean;
submitFailed: boolean;
submitted: boolean;
touched: boolean;
valid: boolean;
validating: boolean;
validity: ValidityObject;
viewValue: boolean;
}
/**
* Returns a form reducer that only responds to any actions on the model or model's child values.
* The reducer's state has the shape of initialFormState, with a fields property where each field has the shape of initialFieldState.
* If provided an initialState, the form reducer will initialize its fields based on the initialState.
*
* Tips
* * You might not need a form reducer if you don't care about field state (such as blur, focus, pristine, touched, etc.) or validation. If this is the case, don't create a form reducer. You'll save on performance costs.
* * Fields in the form state can be accessed from <form>.fields, which is a flat object.
* * E.g., the user.email model is retrieved from userForm.fields.email, whereas the user.phones[3].type model is retrieved from userForm.fields['phones.3.type'], and not userForms.fields.phones[3].type.
* ** Why? For performance, simplicity, and integrity. If you have a model named user.focus or user.valid, you won't have any collisions with the flat <form>.fields[<field>] shape.
* @param model The model whose form state (and child field states) the reducer will update
* @param initialState The initial state of the model
*/
export function formReducer(model: string, initialState?: any): Reducer<FormState>;
/**
* Returns the field from the formState calculated by the formReducer(s), if the field exists (has been initialized).
* If the field doesn't exist, the initialFieldState is returned.
* @param formState The form state as returned by the form reducer.
* @param model The string model path to the field inside the form model, if it exists.
*/
export function getField(formState: FormState, model: string): FieldState;
interface PropMapping {
[key: string]: Function;
}
interface FieldClassPropsMappings<P> {
(props: P): PropMapping;
}
interface ComponentFieldClassPropsMappings<P> {
[componentDisplayName: string]: FieldClassPropsMappings<P>;
}
/**
* Create a custom <{SomeCustomControl}Field> wrapper component, given a dictionary of props mappings, where the key is the custom Component's `displayName` and the value is a props object mapping function.
* @param propsMapping
* @param defaultProps
*/
export function createFieldClass<P, T>(propsMapping: ComponentFieldClassPropsMappings<P>, defaultProps?: any): React.ComponentClass<FieldProps<T>>;
interface ControlPropsMap {
default: FieldClassPropsMappings<any>;
checkbox: FieldClassPropsMappings<any>;
radio: FieldClassPropsMappings<any>;
select: FieldClassPropsMappings<any>;
text: FieldClassPropsMappings<any>;
textArea: FieldClassPropsMappings<any>;
}
export const controls: ControlPropsMap;
export function getModel<TObject, TResult>(object: TObject, path: string | number | Array<string>, defaultValue?: TResult): TResult;
/**
* Returns a model reducer that only responds to change() and reset() actions on the model or model's child values.
*
* Note: the model must be the same as the key given to the reducer in combineReducers({...}).
* @param model The model that the reducer will update
* @param initialState The initial state of the model
*/
export function modelReducer(model: string, initialState?: any): Reducer<any>;
/**
* Decorates your existing reducer to respond to model actions
*/
export function modeled<TState>(reducer: Reducer<TState>, model: string): Reducer<TState>;
interface ActionThunk {
(dispatch: (action: any) => void, getState: () => any): any;
}
interface BaseFormAction {
type: string;
model: string;
}
interface FieldAction extends BaseFormAction {
errors?: any;
validity?: any;
pending?: boolean;
}
interface ModelAction extends BaseFormAction {
multi?: boolean;
value?: any;
}
interface ChangeOptions {
/**
* If true, the CHANGE action will not trigger change-related operations in the form reducer, such as setting .pristine = false.
* @default false
*/
silent?: boolean;
}
interface ValidityOptions {
/**
* If true, the validity will be set for .errors instead of .validity on the field. This is equivalent to actions.setErrors().
*/
errors?: boolean;
}
interface Actions {
/* ------ ------ ------ ------ */
/* ------ Model Actions ------ */
/* ------ ------ ------ ------ */
/**
* Returns an action that, when handled by a modelReducer, changes the value of the model to the value.
* When the change action is handled by a formReducer, the field model's .dirty state is set to true and its corresponding .pristine state is set to false.
*
* The model path can be as deep as you want. E.g. actions.change('user.phones[0].type', 'home')
*
* @param model
* @param value
*/
change: (model: string, value: any, options?: ChangeOptions) => ModelAction;
/**
* Returns an action that, when handled by a modelReducer, changes the value of the model to the value.
* The load action is NOT handled by any formReducer. The field model's .dirty and .pristine states are NOT affected.
*
* This action is useful for loading the initial data set into the model store state, without affecting the form store state (i.e. not marking the form or any fields as touched/dirty).
*
* The model path can be as deep as you want. E.g. actions.change('user.phones[0].type', 'home')
*
* @param model
* @param value
*/
load: (model: string, value: any) => ModelAction;
/**
* Returns an action that, when handled by a modelReducer, changes the value of the respective model to its initial value.
*
* This action will reset both the model value in the model reducer, and the model field state in the form reducer (if it exists).
* To reset just the field state (in the form reducer), use actions.setInitial(model).
*/
reset: (model: string) => ModelAction;
/**
* Dispatches an actions.change(...) action that merges the values into the value specified by the model.
*
* Use this action to update multiple and/or deep properties into a model, if the model represents an object.
* This uses icepick.merge(modelValue, values) internally.
*/
merge: (model: string, values: any) => ActionThunk;
/**
* Dispatches an actions.change(...) action that applies an "xor" operation to the array represented by the model; that is, it "toggles" an item in an array.
* If the model value contains item, it will be removed. If the model value doesn't contain item, it will be added.
*
* This action is most useful for toggling a checkboxes whose values represent items in a model's array.
* @param item the item to be "toggled" in the model value.
*/
xor: (model: string, item: any) => ActionThunk;
/**
* Dispatches an actions.change(...) action that "pushes" the item to the array represented by the model.
*
* This action does not mutate the model. It only simulates the mutable .push() method.
*/
push: (model: string, item: any) => ActionThunk;
/**
* Dispatches an actions.change(...) action that sets the model to true if it is falsey, and false if it is truthy.
*
* This action is most useful for single checkboxes.
*/
toggle: (model: string) => ActionThunk;
/**
* Dispatches an actions.change(...) action that filters the array represented by the model through the iteratee function.
* If no iteratee is specified, the identity function is used by default.
* @param iteratee Filter iteratee function that filters the array represented by the model.
*/
filter: (model: string, iteratee: (value: any) => boolean) => ActionThunk;
/**
* Dispatches an actions.change(...) action that maps the array represented by the model through the iteratee function.
* If no iteratee is specified, the identity function is used by default.
*/
map: (model: string, iteratee: Function) => ActionThunk;
/**
* Dispatches an actions.change(...) action that removes the item at the specified index of the array represented by the model.
*/
remove: (model: string, index: number) => ActionThunk;
/**
* Dispatches an actions.change(...) action that moves the item at the specified fromIndex of the array to the toIndex of the array represented by the model.
* If `fromIndex` or `toIndex` are out of bounds, an error will be thrown.
* @param model The array model to be updated.
* @param fromIndex The index of the item that should be moved in the array.
* @param toIndex The index to move the item to in the array.
*/
move: (model: string, fromIndex: number, toIndex: number) => ActionThunk;
/**
* Dispatches an actions.change(...) action with the model value updated to not include any of the omitted props.
* @param model The model to be modified with the omitted props
* @param props The props to omit from the model value
*/
omit: (model: string, props: string | string[]) => ActionThunk;
/* ------ ------ ------ ------ */
/* ------ Field Actions ------ */
/* ------ ------ ------ ------ */
/**
* Returns an action that, when handled by a formReducer, changes the .focus state of the field model in the form to true, as well as the corresponding .blur state to false.
*
* The "focus" state indicates that the field model is the currently focused field in the form.
*/
focus: (model: string) => FieldAction;
/**
* Returns an action that, when handled by a formReducer, changes the .blur state of the field model in the form to true, as well as the corresponding .focus state to false.
* It also indicates that the field model has been .touched, and will set that state to true and the untouched state to false.
*
* The "blur" state indicates that the field model is not focused.
*/
blur: (model: string) => FieldAction;
/**
* Returns an action that, when handled by a formReducer, changes the .pristine state of the field model in the form to true, as well as the corresponding .dirty state to false.
*
* The "pristine" state indicates that the user has not interacted with this field model yet.
*
* Whenever a field is set to pristine, the entire form is set to:
* * Pristine if all other fields are pristine
* * Otherwise, dirty.
*/
setPristine: (model: string) => FieldAction;
/**
* Returns an action that, when handled by a formReducer, changes the .dirty state of the field model in the form to true, as well as the corresponding .pristine state to false.
* The "dirty" state indicates that the model value has been changed.
*
* Whenever a field is set to dirty, the entire form is set to dirty.
*/
setDirty: (model: string) => FieldAction;
/**
* Returns an action that, when handled by a formReducer, changes the .pending state of the field model in the form to true. It simultaneously sets the .submitted state to false.
*
* Tips:
* * This action is useful when asynchronously validating or submitting a model. It represents the state between the initial and final state of a field model's validation/submission.
*/
setPending: (model: string, pending?: boolean) => FieldAction;
/**
* Returns an action that, when handled by a formReducer, changes the .touched state of the field model in the form to true. It simultaneously sets the .untouched state to false.
*
* The "touched" state indicates that this model has been interacted with.
*
* Tips:
* * Setting a model to touched also sets the entire form to touched.
* * Touched also sets the model to blurred.
*/
setTouched: (model: string) => FieldAction;
/**
* Returns an action that, when handled by a formReducer, changes the .untouched state of the field model in the form to true. It simultaneously sets the .touched state to true.
*
* The "untouched" state indicates that this model has not been interacted with yet.
*
* Tips:
* * This action is useful for conditionally displaying error messages based on whether the field has been touched.
*/
setUntouched: (model: string) => FieldAction;
/**
* Returns an action that, when handled by a formReducer, changes the .submitted state of the field model in the form to submitted (true or false).
* It simultaneously sets the .pending state to the inverse of submitted.
*
* The "submitted" state indicates that this model has been "sent off," or an action has been completed for the model.
*
* Tips:
* * Use the setPending() and setSubmitted() actions together to update the state of the field model during some async action.
*/
setSubmitted: (model: string, submitted?: boolean) => FieldAction;
/**
* Returns an action that, when handled by a formReducer, changes the .submitFailed state of the field model in the form to true.
* It simultaneously sets the .pending state to false, and the .retouched state to false.
* Tips:
* * If the form has not been submitted yet, .submitFailed = false
* * If submitting (pending), .submitFailed = false
* * If submit failed, .submitFailed = true
* * If resubmitting, .submitFailed = false again.
*/
setSubmitFailed: (model: string) => FieldAction;
/**
* Returns an action that, when handled by a formReducer, changes the state of the field model in the form to its initial state.
*
* Tips:
* * This action will reset the field state, but will not reset the model value in the model reducer. To reset both the field and model, use actions.reset(model).
*/
setInitial: (model: string) => FieldAction;
/**
* Waits for a submission promise to be completed, then, if successful:
* * Sets .submitted property of form for model to true
* * Sets .validity property of form for model to the response (or true if the response is undefined).
*
* If the promise fails, the action will:
* * set .submitFailed property of form for model to true
* * set .errors property of form for model to the response
*
* @param model The top-level model form key of the data to submit.
* @param promise The promise that the submit action will wait to be resolved or rejected
*/
submit: (model: string, promise: Promise<any>) => ActionThunk;
/**
* Waits for a submission promise to be completed, then, if successful:
* * Sets .submitted property of form for model to true
* * Each key in the response, which represents a sub-model (e.g., "name" for users.name) will have its .validity set to its value.
*
* If the promise fails, the action will:
*
* * set .submitFailed property of form for model to true
* * Each key in the response, which represents a sub-model (e.g., "name" for users.name) will have its .errors set to its value. (See example)
*
* @param model (String | Function): the model to be submitted
* @param promise (Promise): the promise that the submit action will wait to be resolved or rejected
*/
submitFields: (model: string, promise: Promise<any>) => ActionThunk;
/* -------------------------------- */
/* ------ Validation Actions ------ */
/* -------------------------------- */
/**
* Returns an action that, when handled by a formReducer, changes the .valid state of the field model in the form to true or false, based on the validity.
* It will also set the .validity state of the field model to the validity.
* It simultaneously sets the .errors on the field model to the inverse of the validity.
*
* Tips:
* * If you really want to set error messages instead, use actions.setErrors(model, errors).
* * Since arrays are objects, the validity argument can be an array. Only do this if your use case requires it.
*
* @param model The model whose validity will be set.
* @param validity Boolean value or an object indicating which validation keys of the field model are valid.
*/
setValidity: (model: string, validity: boolean | string | ValidityObject | ErrorsObject, options?: ValidityOptions) => FieldAction;
/**
* Returns an action thunk that calculates the validity of the model based on the function/object validators. Then, the thunk dispatches actions.setValidity(model, validity).
*
* A validator is a function that returns true if valid, and false if invalid.
* @param model The model whose validity will be calculated.
* @param validators A validator function or an object whose keys are validation keys (such as 'required') and values are validators.
*/
validate: (model: string, validators: Function | Validators) => ActionThunk;
/**
* Explicit action created for validating fields of a form.
* @param model The model to validate
* @param fieldValidators an object where the keys are the field paths (such as "email" for user.email), and the values are validators (either functions or a validation object)
* @param options Options
*/
validateFields: (model: string, fieldValidators: FieldsObject<ValidatorFn | Validators>, options?: { onValid?: Function; onInvalid?: Function; errors?: any }) => ActionThunk;
/**
* Runs error validation on each of the error validators for each submodel of model
* @param model The model to validate
* @param fieldErrorsValidators An object where the keys are the field paths (such as "email" for user.email) and the values are errors
* @param options Options
*/
validateFieldsErrors: (model: string, fieldErrorsValidators: FieldsObject<ErrorFn | ErrorsObject>, options?: any) => ActionThunk;
/**
* This action allows you to set the validity for multiple submodels of a model at the same time.
* @param model The top level form model
* @param fieldsValidity An object where the keys are field paths and the value is validity object
*/
setFieldsValidity: (model: string, fieldsValidity: FieldsObject<ValidityObject | boolean>) => FieldAction;
/**
* This action allows you to set the errors for multiple submodels of a model at the same time. Similar to setFieldsValidity but for errors
* @param model The top level form model
* @param fieldsErrors An object where the keys are field paths and the value is error object
*/
setFieldsErrors: (model: string, fieldsErrors: FieldsObject<ErrorsObject | boolean | string>) => FieldAction;
/**
* Returns an action thunk that calculates the validity of the model based on the async function asyncValidator.
* That function dispatches actions.setValidity(model, validity) by calling done(validity).
*
* Tips:
* * This action is useful for general-purpose asynchronous validation using callbacks. If you are using promises, using actions.submit(model, promise) is a cleaner pattern.
*
* @param model The model whose validity will be set
* @param asyncValidator A function that is given two arguments: value - the value of the model, done - the callback where the calculated validity is passed in as the argument.
*/
asyncSetValidity: (model: string, asyncValidator: AsyncValidatorFn) => ActionThunk;
/**
* Returns an action that, when handled by a formReducer, changes the .valid state of the field model in the form to true or false, based on the errors.
* It will also set the .errors state of the field model to the errors.
*
* It simultaneously sets the .validity on the field model to the inverse of the errors.
*
* Tips:
* * If you aren't hard-coding error messages, use actions.setValidity(model, validity) instead. It's a cleaner pattern.
* * You can set errors to a boolean, object, array, string, etc. Remember: truthy values indicate errors.
*
* @param model
* @param errors A truthy/falsey value, a string error message, or an object indicating which error keys of the field model are invalid via booleans (where true is invalid) or strings (set specific error messages, not advised).
*/
setErrors: (model: string, errors: boolean | string | ErrorsObject) => FieldAction;
/**
* Returns an action thunk that calculates the errors of the model based on the function/object errorValidators. Then, the thunk dispatches actions.setErrors(model, errors).
*
* An error validator is a function that returns true or a truthy value (such as a string) if invalid, and false if valid.
*
* Tips:
* * As previously stated, if you aren't using error messages, use actions.validate(model, validators) as a cleaner pattern.
* @param model
* @param errorValidators An error validator or an object whose keys are error keys (such as 'incorrect') and values are error validators.
*/
validateErrors: (model: string, errorValidators: ValidatorFn | Validators) => ActionThunk;
/**
* Can be dispatched to reset the validity and errors of any model at any time.
* @param model The model
*/
resetValidity: (model: string) => ActionThunk;
/**
* Can be dispatched to reset the validity and errors of any model at any time.
* @param model The model
*/
resetErrors: (model: string) => ActionThunk;
/**
* Process multiple actions, which can be standard ModelAction/FieldAction or ActionThunks.
*
* If all actions are NOT thunks, will return a standard action of type `actionTypes.BATCH` which can then be dispatched.
*
* If some actions ARE thunks, will return a thunk which can then be dispatched.
*
* @param model
* @param actions An array of standard actions or thunk actions to combine into a new standard action or thunk, respectively.
*/
batch: (model: string, actions: any[]) => ActionThunk | ModelAction | FieldAction;
}
export const actions: Actions;
interface ActionTypes {
BLUR: string;
CHANGE: string;
FOCUS: string;
RESET: string;
VALIDATE: string;
SET_DIRTY: string;
SET_ERRORS: string;
SET_INITIAL: string;
SET_PENDING: string;
SET_PRISTINE: string;
SET_SUBMITTED: string;
SET_SUBMIT_FAILED: string;
SET_TOUCHED: string;
SET_UNTOUCHED: string;
SET_VALIDITY: string;
SET_FIELDS_VALIDITY: string;
SET_VIEW_VALUE: string;
RESET_VALIDITY: string;
BATCH: string;
}
export var actionTypes: ActionTypes;
/**