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

Feat: move remove filter button and display it on hover #8217

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 4 additions & 2 deletions docs/FilteringTutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ import { Chip } from '@mui/material';

const QuickFilter = ({ label }) => {
const translate = useTranslate();
return <Chip sx={{ marginBottom: 1 }} label={translate(label)} />;
return <Chip sx={{ marginBottom: 1 }} label={translate(label)} clickable />;
};

const postFilters = [
Expand All @@ -153,7 +153,9 @@ const postFilters = [
```
{% endraw %}

**Tip**: It's currently not possible to use two quick filters for the same source.
**Tip**: Notice we have set the `clickable` prop on the `<Chip>`. This is an accessibility good practice. Indeed this allows the element to be focusable, and hence allows to remove the filter using only the keyboard.

**Tip**: It's currently not possible to use two quick filters for the same source.

## The `<FilterList>` Sidebar

Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/products/ProductList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const ProductList = () => {

const QuickFilter = ({ label }: InputProps) => {
const translate = useTranslate();
return <Chip sx={{ mb: 1 }} label={translate(label as string)} />;
return <Chip sx={{ mb: 1 }} label={translate(label as string)} clickable />;
};

export const productFilters = [
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/src/posts/PostList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const PostIcon = BookIcon;

const QuickFilter = ({ label, source, defaultValue }) => {
const translate = useTranslate();
return <Chip sx={{ marginBottom: 1 }} label={translate(label)} />;
return <Chip sx={{ marginBottom: 1 }} label={translate(label)} clickable />;
};

const postFilter = [
Expand Down
29 changes: 21 additions & 8 deletions packages/ra-ui-materialui/src/list/filter/FilterFormInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export const FilterFormInput = props => {
data-source={filterElement.props.source}
className={clsx('filter-field', className)}
>
{React.cloneElement(filterElement, {
resource,
record: emptyRecord,
size: 'small',
helperText: false,
// ignore defaultValue in Field because it was already set in Form (via mergedInitialValuesWithDefaultValues)
defaultValue: undefined,
})}
{!filterElement.props.alwaysOn && (
<IconButton
className={clsx(
Expand All @@ -30,14 +38,6 @@ export const FilterFormInput = props => {
<ActionHide />
</IconButton>
)}
{React.cloneElement(filterElement, {
resource,
record: emptyRecord,
size: 'small',
helperText: false,
// ignore defaultValue in Field because it was already set in Form (via mergedInitialValuesWithDefaultValues)
defaultValue: undefined,
})}
<div className={FilterFormInputClasses.spacer}>&nbsp;</div>
</Root>
);
Expand All @@ -64,10 +64,23 @@ const Root = styled('div', {
display: 'flex',
alignItems: 'flex-end',
pointerEvents: 'auto',
position: 'relative',
'@media (hover: hover)': {
'&:not(:hover, :focus, :active, :focus-visible, :focus-within)': {
[`& .${FilterFormInputClasses.hideButton}`]: {
visibility: 'hidden',
},
},
},

[`& .${FilterFormInputClasses.spacer}`]: { width: theme.spacing(2) },
[`& .${FilterFormInputClasses.hideButton}`]: {
marginBottom: theme.spacing(1),
position: 'absolute',
right: 0,
top: 0,
zIndex: 10,
display: 'flex',
},
}));

Expand Down