Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR][v3] FormDataConsumer - add subscription prop to the FormDataConsumer #3670

Merged
merged 1 commit into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/Inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1511,3 +1511,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