forked from davidkpiano/react-redux-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-redux-form.d.ts
1191 lines (1101 loc) · 48.7 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 FormValidatorsFn {
(val: any): FormValidationErrors
}
interface ValidationErrors {
[key: string]: any;
}
interface FormValidationErrors {
[key: string]: ValidationErrors
}
/**
* 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;
onKeyPress: (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;
type UpdateOn = 'change' | 'blur' | 'focus';
type ValidateOn = 'change' | 'blur' | 'focus';
export interface ControlProps<T> extends React.HTMLProps<T> {
/**
* Wrap field into custom component
*/
component?: React.ReactType;
/**
* Function that receives a field props and returns a custom component
*/
render?: (props: MapPropsProps) => React.ReactNode;
/**
* 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 string/array of strings specifying when the component should dispatch a change(...) action, with one of these values:
*
* change - will dispatch in onChange
* blur - will dispatch in onBlur
* focus - will dispatch in onFocus
*
* @default change
*/
updateOn?: UpdateOn | UpdateOn[];
/**
* The time in milliseconds, by which the change action will be debounced.
*/
debounce?: number;
/**
* 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?: ValidateOn | ValidateOn[];
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) => any;
/**
* A function that formattes the model value of the field before it is rendered.
* @param value The model value that represents the next model value
* @returns {typeof value} The formatted value
*/
formatter?: (value: 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?: (node: T) => void;
/**
* Indicates that the model's fieldValue should be passed in
* to event handlers as the second prop.
*/
withField?: boolean;
/**
* Signifies that the field state (validation, etc.) should not persist when the component is unmounted. Default: false
*/
persist?: boolean;
/**
* The disabled prop works just like you'd expect for controls that support the HTML5 disabled attribute.
*/
disabled?: any;
/**
* The event(s) that you want the <Control> to ignore. This can be good for performance and/or for de-cluttering the console log.
*/
ignore?: string|string[];
/**
* Determines the value given the event (from onChange) and optionally the control component's props.
*/
getValue?: (e: Event, props: any) => any;
/**
* Signifies that the control is a toggle (e.g., a checkbox or a radio). If true, then some optimizations are made.
*/
isToggle?: boolean;
}
export class Control<T> extends React.Component<ControlProps<T>, {}> {
static custom: React.ComponentClass<ControlProps<HTMLInputElement>>;
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>>;
static button: 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 | FormValidatorsFn;
/**
* 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';
/**
* This handler function is called regardless of the validity of the form, and allows you to prevent the default action
* if using a form action url.
*/
onBeforeSubmit?: (event?: Event) => void;
/**
* 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 as the first argument, and the event as the second argument.
*
* 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, event?: Event) => void;
/**
* The handler function called when the form fails to submit. This happens when:
* * attempting to submit an invalid form
* * submitting a valid form that later becomes invalid (due to async server/API validation, etc.)
*
* The callback function provided to onSubmitFailed will be called with one argument: the entire formState for the form's model.
*/
onSubmitFailed?: (formState: 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;
/**
* Calls the callback provided with the node instance. Similar to the "ref" prop.
*/
getRef?: (ref: any) => void;
/**
* Provides the dispatcher for the form's Redux store.
*/
getDispatch?: (dispatch: Function) => void;
/**
* Specify the action if you want the form to let the default happen on submit.
*/
action?: string;
/**
* HTTP method
*/
method?: string;
/**
* Indicates whether native HTML5 constraint validation error messages should be shown. This does not preclude the form from failing to submit if native validation fails.
*/
hideNativeErrors?: boolean;
}
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, {}> { }
interface BaseFieldsetProps {
/**
* CSS Class Name(s)
*/
className?: string;
/**
* The component that the <Fieldset> should be rendered to (default: "div").
*
* * For React Native, the View component is used to render the fieldset, if you import { Fieldset } from 'react-redux-form/native'.
*/
component?: React.ComponentClass<any> | string;
}
export interface FieldsetProps extends BaseFieldsetProps {
/**
* The string or tracker representing the model value of the entire form in the store.
*
* You can also use partial models for <Control>, <Field>, and <Errors> components inside of <Fieldset> - they will be resolved to the fieldset's model.
*
* In addition, you can use a partial model for <Fieldset> itself - it will resolve to the parent <Fieldset> (yes, you can nest fieldsets) or <Form> models.
*/
model: string | ModelGetterFn;
}
export class Fieldset extends React.Component<FieldsetProps, {}> { }
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>;
/**
* CSS Class Name(s)
*/
className?: string;
}
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
*/
submitFailed?: 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, options?: object): 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 SetErrorsOptions {
async?: boolean;
}
interface Actions {
/* ------ ------ ------ ------ */
/* ------ Model Actions ------ */
/* ------ ------ ------ ------ */
/**
* Returns an action that, when handled by a formReducer, adds the specified intent to this fields list of intents
*
* @param model
* @param intent
*/
addIntent: (model: string, intent: any) => ModelAction;
/**
* Returns an action that, when handled by a formReducer, adds the specified intent to this fields list of intents
*
* @param model
* @param intents
* @param options
*/
clearIntents: (model: string, intents: any[], options?: any) => ModelAction;
/**
* 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 | ModelGetterFn, 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 | ModelGetterFn, 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 | ModelGetterFn) => 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 | ModelGetterFn, 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 | ModelGetterFn, 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 | ModelGetterFn, 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 | ModelGetterFn) => 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 | ModelGetterFn, 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 | ModelGetterFn, 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 | ModelGetterFn, 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 | ModelGetterFn, 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 | ModelGetterFn, 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 | ModelGetterFn) => 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 | ModelGetterFn) => 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 | ModelGetterFn) => 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 | ModelGetterFn) => 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 | ModelGetterFn, 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 | ModelGetterFn) => 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 | ModelGetterFn, 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 | ModelGetterFn) => 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 | ModelGetterFn) => 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 | ModelGetterFn, promise?: Promise<any> | undefined, options?: 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 | ModelGetterFn, 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 | ModelGetterFn, validity: boolean | string | ValidityObject | ErrorsObject, options?: ValidityOptions) => FieldAction;