Skip to content

Commit

Permalink
[Autocomplete] Fix keyboard navigation when using custom popover (mui…
Browse files Browse the repository at this point in the history
  • Loading branch information
sai6855 authored and Daniel Rabe committed Nov 29, 2022
1 parent 5d95812 commit b75ca49
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ const AutocompleteListbox = styled('div', {
padding: '8px 0',
maxHeight: '40vh',
overflow: 'auto',
position: 'relative',
[`& .${autocompleteClasses.option}`]: {
minHeight: 48,
display: 'flex',
Expand Down
34 changes: 34 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Autocomplete, {
} from '@mui/material/Autocomplete';
import { paperClasses } from '@mui/material/Paper';
import { iconButtonClasses } from '@mui/material/IconButton';
import Box from '@mui/system/Box';

function checkHighlightIs(listbox, expected) {
const focused = listbox.querySelector(`.${classes.focused}`);
Expand Down Expand Up @@ -208,6 +209,39 @@ describe('<Autocomplete />', () => {
checkHighlightIs(getByRole('listbox'), 'two');
});

// https://github.com/mui/material-ui/issues/34998
it('should scroll the listbox to the top when keyboard highlight wraps around after the last item is highlighted', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}

const { getByRole } = render(
<Autocomplete
open
options={['one', 'two', 'three', 'four', 'five']}
renderInput={(params) => <TextField {...params} />}
ListboxProps={{ style: { padding: 0, maxHeight: '100px' } }}
PopperComponent={(props) => {
const { disablePortal, anchorEl, open, ...other } = props;
return <Box {...other} />;
}}
/>,
);
const textbox = getByRole('combobox');
act(() => {
textbox.focus();
});
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });

checkHighlightIs(getByRole('listbox'), 'one');
expect(getByRole('listbox')).to.have.property('scrollTop', 0);
});

it('should keep the current highlight if possible', () => {
const { getByRole } = render(
<Autocomplete
Expand Down

0 comments on commit b75ca49

Please sign in to comment.