Skip to content

Commit 94f8c9f

Browse files
[SelectInput] Update menu to use select wrapper as anchor (#34229)
Co-authored-by: Michał Dudak <[email protected]>
1 parent b8c0d39 commit 94f8c9f

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

packages/mui-material/src/Select/Select.test.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -851,27 +851,29 @@ describe('<Select />', () => {
851851
});
852852

853853
describe('prop: autoWidth', () => {
854-
it('should take the trigger width into account by default', () => {
855-
const { getByRole, getByTestId } = render(
854+
it('should take the trigger parent element width into account by default', () => {
855+
const { container, getByRole, getByTestId } = render(
856856
<Select MenuProps={{ PaperProps: { 'data-testid': 'paper' } }} value="">
857857
<MenuItem>Only</MenuItem>
858858
</Select>,
859859
);
860+
const parentEl = container.querySelector('.MuiInputBase-root');
860861
const button = getByRole('button');
861-
stub(button, 'clientWidth').get(() => 14);
862+
stub(parentEl, 'clientWidth').get(() => 14);
862863

863864
fireEvent.mouseDown(button);
864865
expect(getByTestId('paper').style).to.have.property('minWidth', '14px');
865866
});
866867

867-
it('should not take the triger width into account when autoWidth is true', () => {
868-
const { getByRole, getByTestId } = render(
868+
it('should not take the trigger parent element width into account when autoWidth is true', () => {
869+
const { container, getByRole, getByTestId } = render(
869870
<Select autoWidth MenuProps={{ PaperProps: { 'data-testid': 'paper' } }} value="">
870871
<MenuItem>Only</MenuItem>
871872
</Select>,
872873
);
874+
const parentEl = container.querySelector('.MuiInputBase-root');
873875
const button = getByRole('button');
874-
stub(button, 'clientWidth').get(() => 14);
876+
stub(parentEl, 'clientWidth').get(() => 14);
875877

876878
fireEvent.mouseDown(button);
877879
expect(getByTestId('paper').style).to.have.property('minWidth', '');

packages/mui-material/src/Select/SelectInput.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
158158
}
159159
}, []);
160160

161+
const anchorElement = displayNode?.parentNode;
162+
161163
React.useImperativeHandle(
162164
handleRef,
163165
() => ({
@@ -173,7 +175,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
173175
// Resize menu on `defaultOpen` automatic toggle.
174176
React.useEffect(() => {
175177
if (defaultOpen && openState && displayNode && !isOpenControlled) {
176-
setMenuMinWidthState(autoWidth ? null : displayNode.clientWidth);
178+
setMenuMinWidthState(autoWidth ? null : anchorElement.clientWidth);
177179
displayRef.current.focus();
178180
}
179181
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -215,7 +217,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
215217
}
216218

217219
if (!isOpenControlled) {
218-
setMenuMinWidthState(autoWidth ? null : displayNode.clientWidth);
220+
setMenuMinWidthState(autoWidth ? null : anchorElement.clientWidth);
219221
setOpenState(open);
220222
}
221223
};
@@ -479,7 +481,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
479481
let menuMinWidth = menuMinWidthState;
480482

481483
if (!autoWidth && isOpenControlled && displayNode) {
482-
menuMinWidth = displayNode.clientWidth;
484+
menuMinWidth = anchorElement.clientWidth;
483485
}
484486

485487
let tabIndex;
@@ -546,7 +548,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
546548
<SelectIcon as={IconComponent} className={classes.icon} ownerState={ownerState} />
547549
<Menu
548550
id={`menu-${name || ''}`}
549-
anchorEl={displayNode}
551+
anchorEl={anchorElement}
550552
open={open}
551553
onClose={handleClose}
552554
anchorOrigin={{

0 commit comments

Comments
 (0)