From 14753acab16d5e5db72579f825b37a0e3302419b Mon Sep 17 00:00:00 2001 From: Neto Chaves Date: Sun, 15 Dec 2019 15:35:15 -0300 Subject: [PATCH 1/3] bug: fix bug on controlled select focus --- .../material-ui/src/Select/Select.test.js | 27 +++++++++++++++++++ .../material-ui/src/Select/SelectInput.js | 7 ++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/material-ui/src/Select/Select.test.js b/packages/material-ui/src/Select/Select.test.js index 8015bba9b84464..21c296a88ec9b1 100644 --- a/packages/material-ui/src/Select/Select.test.js +++ b/packages/material-ui/src/Select/Select.test.js @@ -593,6 +593,33 @@ describe(' setOpen(false)} + onOpen={() => setOpen(true)} + value="" + > + setOpen(false)}>close + + ); + } + const { getByRole } = render(); + fireEvent.mouseDown(getByRole('button')); + expect(getByRole('option')).to.have.focus; + + act(() => { + getByRole('option').click(); + }); + + expect(getByRole('option')).to.not.have.focus; + }); + it('should allow to control closing by passing onClose props', () => { function ControlledWrapper() { const [open, setOpen] = React.useState(false); diff --git a/packages/material-ui/src/Select/SelectInput.js b/packages/material-ui/src/Select/SelectInput.js index 86d23c13a5a6b8..79c8f921940ab6 100644 --- a/packages/material-ui/src/Select/SelectInput.js +++ b/packages/material-ui/src/Select/SelectInput.js @@ -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) { From 8004963c548de3173ebd168c13f3221222797434 Mon Sep 17 00:00:00 2001 From: Neto Chaves Date: Mon, 16 Dec 2019 10:08:04 -0300 Subject: [PATCH 2/3] fix: fix tests --- packages/material-ui/src/Select/Select.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/material-ui/src/Select/Select.test.js b/packages/material-ui/src/Select/Select.test.js index 21c296a88ec9b1..5bb189eecf8a54 100644 --- a/packages/material-ui/src/Select/Select.test.js +++ b/packages/material-ui/src/Select/Select.test.js @@ -611,10 +611,10 @@ describe('', () => { const [open, setOpen] = React.useState(false); return ( - +
+ + +
); } - const { getByRole } = render(); - fireEvent.mouseDown(getByRole('button')); - expect(getByRole('option', { hidden: true })).to.have.focus; + const { container, getByRole } = render(); + const openSelect = container.querySelector('#open-select'); + openSelect.focus(); + fireEvent.click(openSelect); - act(() => { - getByRole('option', { hidden: true }).click(); - }); + const option = getByRole('option'); + expect(option).to.have.focus; + fireEvent.click(option); - expect(getByRole('option')).to.not.have.focus; + expect(container.querySelectorAll('.Mui-focused').length).to.equal(0); + expect(openSelect).to.have.focus; }); it('should allow to control closing by passing onClose props', () => {