-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Options in Autocomplete lose the last focus after component is rerendered #48093
Copy link
Copy link
Open
Labels
scope: autocompleteChanges related to the autocomplete. This includes ComboBox.Changes related to the autocomplete. This includes ComboBox.
Description
Steps to reproduce
Steps:
- Open this link to live example: https://codesandbox.io/p/sandbox/5gc5pj
- Set focus to the text input
- Press "ArrowDown" key so the first option is highlighted
- Press "Enter" key
- Press "ArrowDown" three times, so the
"Focus on me now, and try to select too"option is highlighted - Press "Enter" key
- Press "ArrowDown"
Current behavior
The focus goes to the first option
Expected behavior
The next "Then try to focus on me" option is highlighted
Context
The example code looks strange, because we manually trigger rerenders.
However, this rerender could be triggered by an async action at the parent component.
I think the problem is easier to understand if we look at its test case 👇
it('should keep focus when component is re-rendered', () => {
const onChange = spy();
function TestComponent(props) {
return (
<Autocomplete
open
multiple
value={[]}
options={props.options}
renderInput={(params) => <TextField {...params} autoFocus />}
onChange={onChange}
/>
);
}
const view = render(<TestComponent options={['one', 'two']} />);
const textbox = screen.getByRole('combobox');
const listbox = screen.getByRole('listbox');
// highlight 'two'
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(listbox, 'two');
// the highlighted option should be the same even if the options are updated
view.rerender(<TestComponent options={['one', 'two', 'three', 'four', 'five']} />);
checkHighlightIs(listbox, 'two');
// highlight 'four'
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(listbox, 'four');
// re-rendering the component should not change the highlighted option
view.rerender(<TestComponent options={['one', 'two', 'three', 'four', 'five']} />);
// highlight 'five'
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
checkHighlightIs(listbox, 'five'); // Test fails here, the focus goes to the first option
});Your environment
We only need the latest @mui/material package
Search keywords: focus, Autocomplete, highlight
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
scope: autocompleteChanges related to the autocomplete. This includes ComboBox.Changes related to the autocomplete. This includes ComboBox.