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

Fix FilterButton throws error if no filters are present #7227

Merged
merged 3 commits into from
Feb 11, 2022
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
10 changes: 5 additions & 5 deletions docs/List.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ The title can be either a string or an element of your own.

![Actions Toolbar](./img/actions-toolbar.png)

You can replace the list of default actions by your own element using the `actions` prop:
You can replace the list of default actions by your own elements using the `actions` prop:

```jsx
import * as React from 'react';
Expand Down Expand Up @@ -119,6 +119,7 @@ export const PostList = (props) => (
</List>
);
```
Note that in order to add the `<FilterButton>` component among your custom actions, either the parent `List` must have its `filters` prop set up or you must pass the filters to the button itself.

This allows you to further control how the default actions behave. For example, you could disable the `<ExportButton>` when the list is empty:

Expand All @@ -134,7 +135,7 @@ import {
ExportButton,
FilterButton,
Button,
sanitizeListRestProps
sanitizeListRestProps
} from 'react-admin';
import IconEvent from '@material-ui/icons/Event';

Expand All @@ -144,9 +145,8 @@ const ListActions = (props) => {
maxResults,
...rest
} = props;
const {
total,
} = useListContext();
const { total } = useListContext();

return (
<TopToolbar className={className} {...sanitizeListRestProps(rest)}>
<FilterButton />
Expand Down
4 changes: 4 additions & 0 deletions packages/ra-ui-materialui/src/list/filter/FilterButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const FilterButton = (props: FilterButtonProps): JSX.Element => {
const anchorEl = useRef();
const classes = useStyles(props);

if (filters === undefined) {
throw new Error('FilterButton requires filters prop to be set');
}

const hiddenFilters = filters.filter(
(filterElement: JSX.Element) =>
!filterElement.props.alwaysOn &&
Expand Down