From 931966beeadcbf8c59745e50596fc72bd1f4c681 Mon Sep 17 00:00:00 2001 From: Natrim Date: Wed, 11 Sep 2019 09:53:32 +0200 Subject: [PATCH] FormDataConsumer - add subscription prop to pass to the final-form useFormState --- docs/Inputs.md | 19 +++++++++++++++++++ .../ra-core/src/form/FormDataConsumer.tsx | 6 ++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/docs/Inputs.md b/docs/Inputs.md index c0c643a3916..6bd2b52c5f7 100644 --- a/docs/Inputs.md +++ b/docs/Inputs.md @@ -1511,3 +1511,22 @@ import { FormDataConsumer } from 'react-admin'; ); ``` + +**Tip**: When using a `FormDataConsumer` you can define `subscription` prop to pass to the `react-final-form` + +```jsx +import { FormDataConsumer } from 'react-admin'; + + const PostEdit = (props) => ( + + + + + {({ formData, ...rest }) => formData.hasEmail && + + } + + + + ); +``` diff --git a/packages/ra-core/src/form/FormDataConsumer.tsx b/packages/ra-core/src/form/FormDataConsumer.tsx index 65516d774c5..a0938227052 100644 --- a/packages/ra-core/src/form/FormDataConsumer.tsx +++ b/packages/ra-core/src/form/FormDataConsumer.tsx @@ -1,5 +1,6 @@ import React, { ReactNode, SFC } from 'react'; import { useFormState } from 'react-final-form'; +import { FormSubscription } from 'final-form'; import get from 'lodash/get'; import warning from '../util/warning'; @@ -15,6 +16,7 @@ interface ConnectedProps { form?: string; record?: any; source?: string; + subscription?: FormSubscription; [key: string]: any; } @@ -117,8 +119,8 @@ export const FormDataConsumerView: SFC = ({ return ret === undefined ? null : ret; }; -const FormDataConsumer = (props: ConnectedProps) => { - const formState = useFormState(); +const FormDataConsumer = ({ subscription, ...props }: ConnectedProps) => { + const formState = useFormState({ subscription }); return ; };