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

Update Filter Form to show remove button after the input #9224

Merged
merged 1 commit into from
Aug 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/ra-ui-materialui/src/list/filter/FilterButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ export const FilterButton = (props: FilterButtonProps): JSX.Element => {
const handleShow = useCallback(
({ source, defaultValue }) => {
showFilter(source, defaultValue === '' ? undefined : defaultValue);
// We have to fallback to imperative code because the new FilterFormInput
// has no way of knowing it has just been displayed (and thus that it should focus its input)
setTimeout(() => {
const inputElement = document.querySelector(
`input[name='${source}']`
) as HTMLInputElement;
if (inputElement) {
inputElement.focus();
}
}, 50);
setOpen(false);
},
[showFilter, setOpen]
Expand Down
19 changes: 10 additions & 9 deletions packages/ra-ui-materialui/src/list/filter/FilterFormInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { styled } from '@mui/material/styles';
import PropTypes from 'prop-types';
import { IconButton } from '@mui/material';
import ActionHide from '@mui/icons-material/HighlightOff';
import ActionHide from '@mui/icons-material/RemoveCircleOutline';
import clsx from 'clsx';
import { useResourceContext, useTranslate } from 'ra-core';

Expand All @@ -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: filterElement.props.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,7 @@ export const FilterFormInput = props => {
<ActionHide />
</IconButton>
)}
{React.cloneElement(filterElement, {
resource,
record: emptyRecord,
size: filterElement.props.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 Down