Skip to content

Commit 4baea0e

Browse files
enhancement: add generic type
1 parent e6b9cec commit 4baea0e

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

packages/ra-core/src/form/FormDataConsumer.tsx

+24-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as React from 'react';
22
import { ReactNode } from 'react';
33
import { useWatch, useFormContext, FieldValues } from 'react-hook-form';
44
import get from 'lodash/get';
5-
65
import warning from '../util/warning';
76
/**
87
* Get the current (edited) value of the record from the form and pass it
@@ -12,7 +11,7 @@ import warning from '../util/warning';
1211
*
1312
* const PostEdit = (props) => (
1413
* <Edit {...props}>
15-
* <SimpleForm>
14+
* <SimpleForm<FieldValues>>
1615
* <BooleanInput source="hasEmail" />
1716
* <FormDataConsumer>
1817
* {({ formData, ...rest }) => formData.hasEmail &&
@@ -29,7 +28,7 @@ import warning from '../util/warning';
2928
* <Edit {...props}>
3029
* <SimpleForm>
3130
* <SelectInput source="country" choices={countries} />
32-
* <FormDataConsumer>
31+
* <FormDataConsumer<FieldValues>>
3332
* {({ formData, ...rest }) =>
3433
* <SelectInput
3534
* source="city"
@@ -42,20 +41,26 @@ import warning from '../util/warning';
4241
* </Edit>
4342
* );
4443
*/
45-
const FormDataConsumer = (props: ConnectedProps) => {
44+
const FormDataConsumer = <TFieldValues extends FieldValues = FieldValues>(
45+
props: ConnectedProps<TFieldValues>
46+
) => {
4647
const { getValues } = useFormContext();
47-
let formData = useWatch();
48+
const formData = useWatch<TFieldValues>();
4849

4950
//useWatch will initially return the provided defaultValues of the form.
5051
//We must get the initial formData from getValues
5152
if (Object.keys(formData).length === 0) {
5253
(formData as FieldValues) = getValues();
5354
}
5455

55-
return <FormDataConsumerView formData={formData} {...props} />;
56+
return <FormDataConsumerView<TFieldValues> formData={formData} {...props} />
5657
};
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+
) => {
5964
const { children, form, formData, source, index, ...rest } = props;
6065
let getSourceHasBeenCalled = false;
6166
let ret;
@@ -104,26 +109,24 @@ export const FormDataConsumerView = (props: Props) => {
104109
};
105110

106111
export default FormDataConsumer;
107-
108-
export interface FormDataConsumerRenderParams {
109-
formData: any;
112+
export interface FormDataConsumerRenderParams<
113+
TFieldValues extends FieldValues = FieldValues
114+
> {
115+
formData: TFieldValues;
110116
scopedFormData?: any;
111117
getSource?: (source: string) => string;
112118
}
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>;
120124
form?: string;
121125
record?: any;
122126
source?: string;
123127
[key: string]: any;
124128
}
125-
126-
interface Props extends ConnectedProps {
127-
formData: any;
129+
interface Props<TFieldValues extends FieldValues> extends ConnectedProps {
130+
formData: TFieldValues;
128131
index?: number;
129132
}

0 commit comments

Comments
 (0)