@@ -2,7 +2,6 @@ import * as React from 'react';
2
2
import { ReactNode } from 'react' ;
3
3
import { useWatch , useFormContext , FieldValues } from 'react-hook-form' ;
4
4
import get from 'lodash/get' ;
5
-
6
5
import warning from '../util/warning' ;
7
6
/**
8
7
* Get the current (edited) value of the record from the form and pass it
@@ -12,7 +11,7 @@ import warning from '../util/warning';
12
11
*
13
12
* const PostEdit = (props) => (
14
13
* <Edit {...props}>
15
- * <SimpleForm>
14
+ * <SimpleForm<FieldValues> >
16
15
* <BooleanInput source="hasEmail" />
17
16
* <FormDataConsumer>
18
17
* {({ formData, ...rest }) => formData.hasEmail &&
@@ -29,7 +28,7 @@ import warning from '../util/warning';
29
28
* <Edit {...props}>
30
29
* <SimpleForm>
31
30
* <SelectInput source="country" choices={countries} />
32
- * <FormDataConsumer>
31
+ * <FormDataConsumer<FieldValues> >
33
32
* {({ formData, ...rest }) =>
34
33
* <SelectInput
35
34
* source="city"
@@ -42,20 +41,26 @@ import warning from '../util/warning';
42
41
* </Edit>
43
42
* );
44
43
*/
45
- const FormDataConsumer = ( props : ConnectedProps ) => {
44
+ const FormDataConsumer = < TFieldValues extends FieldValues = FieldValues > (
45
+ props : ConnectedProps < TFieldValues >
46
+ ) => {
46
47
const { getValues } = useFormContext ( ) ;
47
- let formData = useWatch ( ) ;
48
+ const formData = useWatch < TFieldValues > ( ) ;
48
49
49
50
//useWatch will initially return the provided defaultValues of the form.
50
51
//We must get the initial formData from getValues
51
52
if ( Object . keys ( formData ) . length === 0 ) {
52
53
( formData as FieldValues ) = getValues ( ) ;
53
54
}
54
55
55
- return < FormDataConsumerView formData = { formData } { ...props } /> ;
56
+ return < FormDataConsumerView < TFieldValues > formData = { formData } { ...props } />
56
57
} ;
57
-
58
- export const FormDataConsumerView = ( props : Props ) => {
58
+ export const FormDataConsumerView = <
59
+ TFieldValues extends FieldValues = FieldValues ,
60
+ TPathValue = PathValue < TFieldValues , Path < TFieldValues > > [ ]
61
+ > (
62
+ props : Props < TPathValue >
63
+ ) => {
59
64
const { children, form, formData, source, index, ...rest } = props ;
60
65
let getSourceHasBeenCalled = false ;
61
66
let ret ;
@@ -104,26 +109,24 @@ export const FormDataConsumerView = (props: Props) => {
104
109
} ;
105
110
106
111
export default FormDataConsumer ;
107
-
108
- export interface FormDataConsumerRenderParams {
109
- formData : any ;
112
+ export interface FormDataConsumerRenderParams <
113
+ TFieldValues extends FieldValues = FieldValues
114
+ > {
115
+ formData : TFieldValues ;
110
116
scopedFormData ?: any ;
111
117
getSource ?: ( source : string ) => string ;
112
118
}
113
-
114
- export type FormDataConsumerRender = (
115
- params : FormDataConsumerRenderParams
116
- ) => ReactNode ;
117
-
118
- interface ConnectedProps {
119
- children : FormDataConsumerRender ;
119
+ export type FormDataConsumerRender <
120
+ TFieldValues extends FieldValues = FieldValues
121
+ > = ( params : FormDataConsumerRenderParams < TFieldValues > ) => ReactNode ;
122
+ interface ConnectedProps < TFieldValues extends FieldValues = FieldValues > {
123
+ children : FormDataConsumerRender < TFieldValues > ;
120
124
form ?: string ;
121
125
record ?: any ;
122
126
source ?: string ;
123
127
[ key : string ] : any ;
124
128
}
125
-
126
- interface Props extends ConnectedProps {
127
- formData : any ;
129
+ interface Props < TFieldValues extends FieldValues > extends ConnectedProps {
130
+ formData : TFieldValues ;
128
131
index ?: number ;
129
132
}
0 commit comments