Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/mui-material/src/Select/Select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1866,4 +1866,52 @@ describe('<Select />', () => {
expect(container.querySelector('.MuiSelect-iconFilled')).not.to.equal(null);
expect(container.querySelector('.MuiSelect-filled ~ .MuiSelect-icon')).not.to.equal(null);
});

describe('window resize', () => {
it('should update menu width when window resizes while menu is open', () => {
render(
<div style={{ width: '200px' }}>
<Select open value="">
<MenuItem value="option1">Option 1</MenuItem>
<MenuItem value="option2">Option 2</MenuItem>
</Select>
</div>,
);

// Get the menu paper element (Menu is rendered in a portal on the document)
const menuPaper = document.querySelector('[role="presentation"]');
expect(menuPaper).not.to.equal(null);
const initialMinWidth = menuPaper.style.minWidth;

// Simulate window resize
const resizeEvent = new Event('resize', { bubbles: true });
window.dispatchEvent(resizeEvent);

// Menu width should be updated (menu paper should have minWidth)
expect(menuPaper.style.minWidth).to.equal(initialMinWidth);
});

it('should not update menu width on resize if autoWidth is true', () => {
render(
<div style={{ width: '200px' }}>
<Select open value="" autoWidth>
<MenuItem value="option1">Option 1</MenuItem>
<MenuItem value="option2">Option 2</MenuItem>
</Select>
</div>,
);

// Menu is rendered in a portal on the document
const menuPaper = document.querySelector('[role="presentation"]');
expect(menuPaper).not.to.equal(null);
const initialMinWidth = menuPaper.style.minWidth;

// Simulate window resize
const resizeEvent = new Event('resize', { bubbles: true });
window.dispatchEvent(resizeEvent);

// Menu width should not have minWidth set when autoWidth is true
expect(menuPaper.style.minWidth).to.equal(initialMinWidth);
});
});
});
29 changes: 29 additions & 0 deletions packages/mui-material/src/Select/SelectInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
}, []);

const anchorElement = displayNode?.parentNode;
const anchorElementRef = React.useRef(anchorElement);

// Update the ref whenever anchorElement changes
React.useEffect(() => {
anchorElementRef.current = anchorElement;
}, [anchorElement]);

React.useImperativeHandle(
handleRef,
Expand Down Expand Up @@ -210,6 +216,29 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {
return undefined;
}, [labelId]);

// Update menu width on window resize when menu is open
React.useEffect(() => {
if (!openState || !displayNode || autoWidth) {
return undefined;
}

const handleResize = () => {
const currentAnchorElement = anchorElementRef.current;
if (currentAnchorElement) {
setMenuMinWidthState(currentAnchorElement.clientWidth);
}
};

const win = ownerDocument(displayNode).defaultView;
if (win) {
win.addEventListener('resize', handleResize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siddarthan007 Thanks for the PR ~ here it would be good to use ResizeObserver if supported, see:

return () => {
win.removeEventListener('resize', handleResize);
};
}
return undefined;
}, [openState, displayNode, autoWidth]);

const update = (open, event) => {
if (open) {
if (onOpen) {
Expand Down
Loading