What you were expecting:
FormDataConsumer to be used in Filters, so that we can have a filter with subcat values filtered based on another filter with cat values.
What happened instead:
The filter does not appear correctly:

Other information:
<FormDataConsumer>
{({ formData, ...rest }) =>
formData.cat_id && (
<ReferenceInput
// key={formData.cat_id}
label="违螤螣螝螒韦螚螕螣巍螜螒"
source="subCat_id"
reference="subCategories"
filter={{ cat_id: formData.cat_id }}
{...rest}
>
<SelectInput optionText="name" />
</ReferenceInput>
)
}
</FormDataConsumer>
Environment
UPDATE: I removed the condition and also added a label to filter prop has no effect, it just fetches all subcats regardless of the cat.id value.
<FormDataConsumer>
{({ formData, ...rest }) =>
formData.cat_id && (
<ReferenceInput
// key={formData.cat_id}
label="违螤螣螝螒韦螚螕螣巍螜螒"
source="subCat_id"
reference="subCategories"
filter={{ cat_id: formData.cat_id }}
{...rest}
>
<SelectInput optionText="name" />
</ReferenceInput>
)
}
</FormDataConsumer>
FormDataConsumer can only work in TabbedForm and SimpleForm currently as it specifically targets the redux-form named record-form. Marking this as an enhancement
@djhi That would be great to have as a feature, thanks a lot!
P.S. This can help a lot in a "wizard-like" filtering.
I don't know what you mean by "wizard-like" filtering, but we've implemented a filter as tabs in the demo, see
@fzaninotto, I read the code there (thanks for the suggestion), I believe this is something different.
I would like a filter in filters whose appearance and values depend on another filter.
For example, I have categories and subCategories as filters:
If the categories filter is not selected, the subCategories filter should disappear (it should not have any effect in the query, etc.).
If the categories filter is selected, then the subCategories filter should be available for selection and its values should be filtered by the chosen category.
In other words, exactly like what formDataConsumer does (for appearance and filtered values of an input field), but inside the Filters logic.
So this is what I mean wizard-like, we can choose filters based on other filters.
Another example: Car brand and model (do not show all available models in the filter, but only show those related with the selected brand filter value).
There are many other such use cases (Date From/To, etc.).
Thanks!
Gotcha.
You can probably do it using a custom wrapper for your input connected to the filter form in redux-form.
I'm not sure we'll implement that in core, because there are tons of corner cases. I believe this should stay in userland.
Probably fixed by #2656
Found solution to filter based on another filter without FormDataConsumer
<ReferenceInput source="filialid" reference="filials">
<SelectInput optionText="filialname"/>
</ReferenceInput>
{props.filterValues.filialid &&
<ReferenceInput
key={props.filterValues.filialid}
source="cabinetid"
reference="cabinets"
perPage={200}
filter={{ filialid: props.filterValues.filialid }}
>
<SelectInput optionText="cabinetname"/>
</ReferenceInput>}
Works like a charm
In case anyone cames in here wondering how to reset the dependent filter value, here is a solution:
<FormDataConsumer form={'filterForm'} alwaysOn source='stateId'>
{({ formData, dispatch, ...rest }) => (
<ReferenceInput
resource={props.resource}
source='stateId'
reference='states'
onChange={value => {
dispatch(change('filterForm', 'districtId', null));
}}
allowEmpty>
<SelectInput optionText='name' />
</ReferenceInput>
)}
</FormDataConsumer>
{(props.filterValues.stateId || props.filterValues.districtId) && (
<ReferenceInput
key={props.filterValues.stateId}
source='districtId'
reference='districts'
filter={{ stateId: props.filterValues.stateId }}
alwaysOn>
<SelectInput optionText='name' />
</ReferenceInput>
)}
Most helpful comment
Found solution to filter based on another filter without FormDataConsumer
Works like a charm