Skip to content

Commit

Permalink
[Select] Fix bug on focus in controlled open (#18857)
Browse files Browse the repository at this point in the history
  • Loading branch information
netochaves authored and oliviertassinari committed Dec 17, 2019
1 parent b6ff4f5 commit 192b426
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
33 changes: 33 additions & 0 deletions packages/material-ui/src/Select/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,39 @@ describe('<Select />', () => {
clock.restore();
});

it('should not focus on close controlled select', () => {
function ControlledWrapper() {
const [open, setOpen] = React.useState(false);

return (
<div>
<button type="button" id="open-select" onClick={() => setOpen(true)}>
Open select
</button>
<Select
MenuProps={{ transitionDuration: 0 }}
open={open}
onClose={() => setOpen(false)}
value=""
>
<MenuItem onClick={() => setOpen(false)}>close</MenuItem>
</Select>
</div>
);
}
const { container, getByRole } = render(<ControlledWrapper />);
const openSelect = container.querySelector('#open-select');
openSelect.focus();
fireEvent.click(openSelect);

const option = getByRole('option');
expect(option).to.have.focus;
fireEvent.click(option);

expect(container.querySelectorAll('.Mui-focused').length).to.equal(0);
expect(openSelect).to.have.focus;
});

it('should allow to control closing by passing onClose props', () => {
function ControlledWrapper() {
const [open, setOpen] = React.useState(false);
Expand Down
7 changes: 2 additions & 5 deletions packages/material-ui/src/Select/SelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,8 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
if (onOpen) {
onOpen(event);
}
} else {
displayNode.focus();
if (onClose) {
onClose(event);
}
} else if (onClose) {
onClose(event);
}

if (!isOpenControlled) {
Expand Down

0 comments on commit 192b426

Please sign in to comment.