Skip to content

Commit

Permalink
Merge pull request #3670 from natrim/patch-13
Browse files Browse the repository at this point in the history
[RFR][v3] FormDataConsumer - add subscription prop to the FormDataConsumer
  • Loading branch information
fzaninotto authored Sep 13, 2019
2 parents 2f347d8 + 931966b commit e5ddaca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
19 changes: 19 additions & 0 deletions docs/Inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1486,3 +1486,22 @@ import { FormDataConsumer } from 'react-admin';
</Edit>
);
```
**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) => (
<Edit {...props}>
<SimpleForm>
<BooleanInput source="hasEmail" />
<FormDataConsume subscription={{ values: true }}>
{({ formData, ...rest }) => formData.hasEmail &&
<TextInput source="email" {...rest} />
}
</FormDataConsumer>
</SimpleForm>
</Edit>
);
```
6 changes: 4 additions & 2 deletions packages/ra-core/src/form/FormDataConsumer.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,6 +16,7 @@ interface ConnectedProps {
form?: string;
record?: any;
source?: string;
subscription?: FormSubscription;
[key: string]: any;
}

Expand Down Expand Up @@ -117,8 +119,8 @@ export const FormDataConsumerView: SFC<Props> = ({
return ret === undefined ? null : ret;
};

const FormDataConsumer = (props: ConnectedProps) => {
const formState = useFormState();
const FormDataConsumer = ({ subscription, ...props }: ConnectedProps) => {
const formState = useFormState({ subscription });

return <FormDataConsumerView formData={formState.values} {...props} />;
};
Expand Down

0 comments on commit e5ddaca

Please sign in to comment.