Skip to content

Commit

Permalink
[DataGrid] Allow to disable autofocusing the search field in the colu…
Browse files Browse the repository at this point in the history
…mns panel (mui#6630)

Co-authored-by: Scott <saintscott119@gmail.com>
  • Loading branch information
cherniavskii and e-cloud committed Oct 25, 2022
1 parent 503988d commit 5727f43
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { GridPanelWrapper, GridPanelWrapperProps } from './GridPanelWrapper';
import { GRID_EXPERIMENTAL_ENABLED } from '../../constants/envConstants';
import { useGridRootProps } from '../../hooks/utils/useGridRootProps';
import { DataGridProcessedProps } from '../../models/props/DataGridProps';
import type { GridStateColDef } from '../../models/colDef/gridColDef';
import { getDataGridUtilityClass } from '../../constants/gridClasses';
import { GridStateColDef } from '../../models/colDef/gridColDef';

type OwnerState = { classes: DataGridProcessedProps['classes'] };

Expand Down Expand Up @@ -67,6 +67,13 @@ export interface GridColumnsPanelProps extends GridPanelWrapperProps {
*/
sort?: 'asc' | 'desc';
searchPredicate?: (column: GridStateColDef, searchValue: string) => boolean;
/*
* If `true`, the column search field will be focused automatically.
* If `false`, the first column switch input will be focused automatically.
* This helps to avoid input keyboard panel to popup automatically on touch devices.
* @default true
*/
autoFocusSearchField?: boolean;
}

const collator = new Intl.Collator();
Expand All @@ -88,7 +95,12 @@ function GridColumnsPanel(props: GridColumnsPanelProps) {
const ownerState = { classes: rootProps.classes };
const classes = useUtilityClasses(ownerState);

const { sort, searchPredicate = defaultSearchPredicate, ...other } = props;
const {
sort,
searchPredicate = defaultSearchPredicate,
autoFocusSearchField = true,
...other
} = props;

const sortedColumns = React.useMemo(() => {
switch (sort) {
Expand Down Expand Up @@ -155,9 +167,24 @@ function GridColumnsPanel(props: GridColumnsPanelProps) {
return sortedColumns.filter((column) => searchPredicate(column, searchValueToCheck));
}, [sortedColumns, searchValue, searchPredicate]);

const firstSwitchRef = React.useRef<HTMLInputElement>(null);

React.useEffect(() => {
searchInputRef.current!.focus();
}, []);
if (autoFocusSearchField) {
searchInputRef.current!.focus();
} else if (firstSwitchRef.current && typeof firstSwitchRef.current.focus === 'function') {
firstSwitchRef.current.focus();
}
}, [autoFocusSearchField]);

let firstHideableColumnFound = false;
const isFirstHideableColumn = (column: GridStateColDef) => {
if (firstHideableColumnFound === false && column.hideable !== false) {
firstHideableColumnFound = true;
return true;
}
return false;
};

return (
<GridPanelWrapper {...other}>
Expand Down Expand Up @@ -185,6 +212,7 @@ function GridColumnsPanel(props: GridColumnsPanelProps) {
onClick={toggleColumn}
name={column.field}
size="small"
inputRef={isFirstHideableColumn(column) ? firstSwitchRef : undefined}
{...rootProps.componentsProps?.baseSwitch}
/>
}
Expand Down Expand Up @@ -228,6 +256,7 @@ GridColumnsPanel.propTypes = {
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
autoFocusSearchField: PropTypes.bool,
searchPredicate: PropTypes.func,
sort: PropTypes.oneOf(['asc', 'desc']),
} as any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,23 @@ describe('<DataGridPro /> - Columns Visibility', () => {
expect(getColumnHeadersTextContent()).to.deep.equal(['id', 'idBis']);
});
});

it('should autofocus the first switch element in columns panel when `autoFocusSearchField` disabled', () => {
render(
<TestDataGrid
components={{
Toolbar: GridToolbar,
}}
componentsProps={{
columnsPanel: {
autoFocusSearchField: false,
},
}}
/>,
);

fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));

expect(screen.getByRole('checkbox', { name: columns[0].field })).toHaveFocus();
});
});

0 comments on commit 5727f43

Please sign in to comment.