Skip to content

Commit fb2529b

Browse files
[mui-material][Select] Fixed dropdown resizing bug
1 parent 3791242 commit fb2529b

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,4 +1866,51 @@ describe('<Select />', () => {
18661866
expect(container.querySelector('.MuiSelect-iconFilled')).not.to.equal(null);
18671867
expect(container.querySelector('.MuiSelect-filled ~ .MuiSelect-icon')).not.to.equal(null);
18681868
});
1869+
1870+
describe('window resize', () => {
1871+
it('should update menu width when window resizes while menu is open', () => {
1872+
const { container } = render(
1873+
<div style={{ width: '200px' }}>
1874+
<Select open value="">
1875+
<MenuItem value="option1">Option 1</MenuItem>
1876+
<MenuItem value="option2">Option 2</MenuItem>
1877+
</Select>
1878+
</div>,
1879+
);
1880+
1881+
// Get the menu paper element
1882+
const menuPaper = container.querySelector('[role="presentation"]');
1883+
expect(menuPaper).not.to.equal(null);
1884+
const initialMinWidth = menuPaper.style.minWidth;
1885+
1886+
// Simulate window resize
1887+
const resizeEvent = new Event('resize', { bubbles: true });
1888+
window.dispatchEvent(resizeEvent);
1889+
1890+
// Menu width should be updated (menu paper should have minWidth)
1891+
expect(menuPaper.style.minWidth).to.equal(initialMinWidth);
1892+
});
1893+
1894+
it('should not update menu width on resize if autoWidth is true', () => {
1895+
const { container } = render(
1896+
<div style={{ width: '200px' }}>
1897+
<Select open value="" autoWidth>
1898+
<MenuItem value="option1">Option 1</MenuItem>
1899+
<MenuItem value="option2">Option 2</MenuItem>
1900+
</Select>
1901+
</div>,
1902+
);
1903+
1904+
const menuPaper = container.querySelector('[role="presentation"]');
1905+
expect(menuPaper).not.to.equal(null);
1906+
const initialMinWidth = menuPaper.style.minWidth;
1907+
1908+
// Simulate window resize
1909+
const resizeEvent = new Event('resize', { bubbles: true });
1910+
window.dispatchEvent(resizeEvent);
1911+
1912+
// Menu width should not have minWidth set when autoWidth is true
1913+
expect(menuPaper.style.minWidth).to.equal(initialMinWidth);
1914+
});
1915+
});
18691916
});

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
161161
}, []);
162162

163163
const anchorElement = displayNode?.parentNode;
164+
const anchorElementRef = React.useRef(anchorElement);
165+
166+
// Update the ref whenever anchorElement changes
167+
React.useEffect(() => {
168+
anchorElementRef.current = anchorElement;
169+
}, [anchorElement]);
164170

165171
React.useImperativeHandle(
166172
handleRef,
@@ -210,6 +216,29 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
210216
return undefined;
211217
}, [labelId]);
212218

219+
// Update menu width on window resize when menu is open
220+
React.useEffect(() => {
221+
if (!openState || !displayNode || autoWidth) {
222+
return undefined;
223+
}
224+
225+
const handleResize = () => {
226+
const currentAnchorElement = anchorElementRef.current;
227+
if (currentAnchorElement) {
228+
setMenuMinWidthState(currentAnchorElement.clientWidth);
229+
}
230+
};
231+
232+
const win = ownerDocument(displayNode).defaultView;
233+
if (win) {
234+
win.addEventListener('resize', handleResize);
235+
return () => {
236+
win.removeEventListener('resize', handleResize);
237+
};
238+
}
239+
return undefined;
240+
}, [openState, displayNode, autoWidth]);
241+
213242
const update = (open, event) => {
214243
if (open) {
215244
if (onOpen) {

0 commit comments

Comments
 (0)