React Modal中ExpansionPanelDetails内Checkbox溢出布局问题求助
Let's work through this step by step to fix the overflow problem:
Wrap Your Grid Items in a Grid Container
The biggest issue here is that you're renderingGrid itemcomponents directly insideExpansionPanelDetailswithout a parentGrid container. Material-UI's Grid system relies on the container to manage spacing, wrapping, and alignment of its items. Update yourchecklistmethod to add the container:checklist = () => { const { scopesList } = this.props; return ( <Grid container spacing={2}> {/* This container was missing */} {scopesList.map((scope, i) => { // Keep your existing Grid item code here })} </Grid> ); };Fix ExpansionPanelDetails Overflow Behavior
By default,ExpansionPanelDetailsdoesn't handle overflow content well. Add some inline styles to make it wrap content and enable scrolling when needed:<ExpansionPanelDetails style={{ display: 'flex', flexWrap: 'wrap', overflow: 'auto', padding: '16px' }}> {this.checklist()} </ExpansionPanelDetails>The
flexWrap: 'wrap'ensures items don't overflow horizontally, andoverflow: 'auto'adds a scrollbar if content exceeds the panel's height.Tame the Modal Body's Content
Even with the panel fixed, the modal itself might not handle overflow correctly. Add a max height and overflow rule to theModal.Bodyto prevent content from spilling outside the modal:<Modal.Body style={{ maxHeight: '70vh', overflow: 'auto' }}> {/* Your ExpansionPanel goes here */} </Modal.Body>maxHeight: '70vh'limits the body to 70% of the viewport height, andoverflow: 'auto'adds a scrollbar when content is too tall.Resolve Bootstrap + Material-UI Style Conflicts
Mixing Bootstrap and Material-UI can cause unexpected layout issues because both libraries have their own reset styles and box-sizing rules. To fix this, add a global style to normalize box-sizing:* { box-sizing: border-box; }This ensures that padding and borders are included in an element's total width/height, preventing unexpected overflow from conflicting styles.
Tweak Grid Breakpoints (Optional)
Your current grid items usexs={12} sm={12} xl={6} md={6}—this means they'll stack on small screens but split into two columns on medium and larger sizes. If you want more responsive behavior, adjust thesmbreakpoint to6so items split into two columns on smaller screens:<Grid item xs={12} sm={6} xl={6} md={6} key={i}>
After applying these changes, your checkboxes should stay within the ExpansionPanelDetails and modal boundaries, even when resizing the window or adding more scope items.
内容的提问来源于stack exchange,提问作者Paul

