Skip to content

Commit

Permalink
[SelectInput] Only attach click handler to label if a labelId is pass…
Browse files Browse the repository at this point in the history
…ed (#30239)
  • Loading branch information
johsunds authored Feb 7, 2022
1 parent c3beda9 commit 40885ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/mui-material/src/Select/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1304,4 +1304,17 @@ describe('<Select />', () => {
expect(getByTestId('root')).to.have.class('bar');
});
});

it('should not focus select when clicking an arbitrary element with id="undefined"', () => {
const { getByRole, getByTestId } = render(
<React.Fragment>
<div id="undefined" data-testid="test-element" />
<Select value="" />
</React.Fragment>,
);

fireEvent.click(getByTestId('test-element'));

expect(getByRole('button')).not.toHaveFocus();
});
});
3 changes: 3 additions & 0 deletions packages/mui-material/src/Select/SelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
}, [autoFocus]);

React.useEffect(() => {
if (!labelId) {
return undefined;
}
const label = ownerDocument(displayRef.current).getElementById(labelId);
if (label) {
const handler = () => {
Expand Down

0 comments on commit 40885ed

Please sign in to comment.