[英]Scrollable content inside a React.Box component
I have a following view.我有以下看法。 The box (columnBox) can contain a lot of items.
框(columnBox)可以包含很多项。 However in the view the box doesn't scroll.
但是在视图中该框不会滚动。 I have set overFlowY as auto, I can not set height as I don't know the exact height it will exand to.
我已将 overFlowY 设置为自动,我无法设置高度,因为我不知道它将扩展到的确切高度。 However my parent has fixed height, only that box contents need to be scrollable.
但是我的父母有固定的高度,只有那个盒子的内容需要是可滚动的。
What can I do to fix this我能做些什么来解决这个问题
columnBox: {
overflowY:'auto',
display: "flex",
flexGrow: 1,
flexDirection:"column"
}
<Card
height="calc(100vh - 200px)"
>
<Box>
<Divider />
<Box height={88} p={4} display="flex" alignItems="center">
{children}
</Box>
<Divider />
<Box className={classes.columnBox} bgcolor="green">
{products &&
products.length > 0 &&
testProducts.map(item => {
return (
<Fragment>
<Box
display="flex"
justifyContent="space-between"
alignItems="center"
>
{someChildren}
</Box>
<Divider>
<Fragment>
})
You need to add max Height to columnBox then only it will get a scroll element and there is a mistake in your code you have added Card prop height="calc(100vh - 200px)" but it won't take calc values you need to pass it directly in styles like <Card style={{height:"calc(100vh-200px)"}}您需要将 max Height 添加到 columnBox 然后只有它会获得一个滚动元素,并且您添加 Card prop height="calc(100vh - 200px)" 的代码中有错误,但它不会采用您需要的 calc 值像 <Card style={{height:"calc(100vh-200px)"}}
columnBox: {
overflowY:'auto',
display: "flex",
flexGrow: 1,
flexDirection:"column",
maxHeight:"200px"
}
I have put the code here for it我已经把代码放在这里了
import "./styles.css";
import { Card, Box, Divider } from "@material-ui/core";
const products = [
"apple",
"ball",
"cat",
"dog",
"elephant",
"apple",
"ball",
"cat",
"dog",
"elephant"
];
export default function App() {
return (
<div className="App">
<Card style={{ height: "calc(100vh - 200px)" }}>
<Box>
<Divider />
<Box height={88} p={4} display="flex" alignItems="center">
<h1>Box</h1>
</Box>
<Divider />
<Box
bgcolor="green"
style={{
overflowY: "auto",
maxHeight: "180px",
display: "flex",
flexGrow: 1,
flexDirection: "column"
}}
>
{products?.map((data) => {
return (
<>
<Box
display="flex"
justifyContent="space-between"
alignItems="center"
>
<p>{data}</p>
</Box>
<Divider />
</>
);
})}
</Box>
</Box>
</Card>
</div>
);
}
You can refer the CodeSandbox if you want如果需要,您可以参考 CodeSandbox
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.