I have a filter component:
export const PostsFilter = (props) => (
<Filter {...props}>
<TextInput label='Post ID' source='id' alwaysOn />
<TextInput label='User ID' source='user_id' alwaysOn />
</Filter>
);
I need to add a reset button that will clear input values. I understand that in should be done via dispatching smth to redux. So maybe somebody already solved this problem? Thanks!
There is a setFilters
prop in your filter component, you can use it:
export const PostsFilter = (props) => (
<div>
<Filter {...props}>
<TextInput label='Post ID' source='id' alwaysOn />
<TextInput label='User ID' source='user_id' alwaysOn />
</Filter>
<Button onClick={() => props.setFilters({
id: '',
user_id: ''
})}>Clear fields</Button>
<div>
);
Unfortunately, setFilters does nothing for me. Here is the solution to the problem:
<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>
)}
Here's how I created a "Clear Filters" button ("react-admin": "3.10.2"
).
Extending upon the custom ListActions
toolbar example...
import { FilterList } from "@material-ui/icons";
import React from "react";
import {
Button,
sanitizeListRestProps,
TopToolbar
} from "react-admin";
const ListActionsTopToolbar = (props) => {
const {
className,
exporter,
filters,
maxResults,
setFilters,
...rest
} = props;
return (
<TopToolbar className={className} {...sanitizeListRestProps(rest)}>
{/* Clear Filters Button */}
<Button onClick={() => setFilters({})} label="Clear Filters">
<FilterList />
</Button>
</TopToolbar>
);
};
Traceback: https://github.com/marmelab/react-admin/issues/816#issuecomment-734030723
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With