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] Add documentation about dispatching actions within FormDataConsumer #2775

Merged
merged 1 commit into from
Jan 17, 2019
Merged
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
40 changes: 39 additions & 1 deletion docs/Inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1448,10 +1448,48 @@ const OrderEdit = (props) => (
);
```

**Tip**: When using a `FormDataConsumer` inside an `ArrayInput`, the `FormDataConsumer` will provide two additional properties to its children function:
**Tip**: When using a `FormDataConsumer` inside an `ArrayInput`, the `FormDataConsumer` will provide three additional properties to its children function:

- `scopedFormData`: an object containing the current values of the currently rendered item from the `ArrayInput`
- `getSource`: a function which will translate the source into a valid one for the `ArrayInput`
- `dispatch`: Redux' function to dispatch an action. Useful to update another input value.

Here is an example usage for `dispatch`: A country input that resets a city input on change.

```jsx

import React, { Fragment } from 'react'
import { change } from 'redux-form'
import { FormDataConsumer, REDUX_FORM_NAME } from 'react-admin';

const OrderEdit = (props) => (
<Edit {...props}>
<SimpleForm>
<FormDataConsumer>
{({ formData, dispatch, ...rest }) => (
<Fragment>
<SelectInput
source="country"
choices={countries}
onChange={value => dispatch(
change(REDUX_FORM_NAME, 'city', null)
)}
{...rest}
/>
<SelectInput
source="city"
choices={getCitiesFor(formData.country)}
{...rest}
/>
</Fragment>
)}
</FormDataConsumer>
</SimpleForm>
</Edit>
);
```

And here is an example usage for `getSource` inside `<ArrayInput>`:

```jsx
import { FormDataConsumer } from 'react-admin';
Expand Down