hin*_*ner 6 grid reactjs material-ui
我正在学习使用 Material-ui 和 React 创建一个网站。
我想创建一个可折叠网格,其中一些行根据页面状态折叠/展开。
当我在网格布局中添加折叠组件时,网格布局被破坏。
我在这里创建了一个示例代码(https://codesandbox.io/embed/jolly-golick-3lwt5)进行演示。
在这里,您会看到折叠部分(当前条件为“展开”)未按预期显示。
我在这里做错了什么吗?
这对我有用:
import MuiCollapse from '@material-ui/core/Collapse'
import Grid from '@material-ui/core/Grid'
const GridItemXs12 = (props) => <Grid item xs={12} {...props} />
const Collapse = (props) => {
const classes = useCollapseStyles()
return (
<MuiCollapse
component={GridItemXs12}
classes={{
hidden: classes.hidden,
}}
{...props}
>
{/* This spacing has to match with the one in the container
outside the collapse */}
<Grid container spacing={2}>
{props.children}
</Grid>
</MuiCollapse>
)
}
const useCollapseStyles = makeStyles({
hidden: {
// The grid item's padding normally balances with the negative padding
// of the container outside the Collapse.
// But when the collapse hides its content, the padding of the item
// is still taking up space, creating an unwanted space.
// The 'hidden' style rule is only applied when the collapse finishes
// hiding its content
padding: '0 !important',
},
})
Run Code Online (Sandbox Code Playgroud)
然后将此习俗Collapse用作Grid container
const MyComponent = () => {
return (
<Grid container spacing={2}>
<Collapse in={/* your controlled variable */}>
<Grid item xs={12}>
The content inside this grid item looks as if the item were
directly inside the container
</Grid>
</Collapse>
</Grid>
)
}
Run Code Online (Sandbox Code Playgroud)
这是使用此方法https://codesandbox.io/s/exciting-hodgkin-rk2wv的示例代码的修改版本,我向主 div 添加了一个 onClick 处理程序,以便单击其中的任意位置可以切换折叠。
此解决方案假设您想要折叠占据容器整个宽度的内容。它还假设您希望在该容器中留出一些间距(实际上,如果没有间距,网格根本不会破裂)。此外,当应用填充样式时,动画中会出现一些卡顿,间距越大,越明显。
| 归档时间: |
|
| 查看次数: |
9284 次 |
| 最近记录: |