React Material-UI GridList层叠布局

React Material-UI GridList cascading layout

是否可以在类似层叠布局的 pinterest 中使用 React Material-UI 库的 GridList 组件?

我使用 Material-UI Card 组件作为 GridList:

的子组件
    const cards = props.items.map((item) => <Card key={item.id} {...item} />);
    return (
      <GridList cols={3} cellHeight={'auto'}>
        {React.Children.toArray(cards)}
      </GridList>
    );

结果如下:

我想删除红色圆圈中的间隙。任何帮助都感激不尽。

您想要的 CSS 等效项是将 flex-direction 设置为 column 而不是 row,但是 it doesn't look like the material-ui documentation gives you the option to change the order in which things render.

我会尝试使用不同的容器对象或自己构建一些东西,因为 Pinterest 样式布局在确定下一个要呈现的元素应该放在哪里时需要一些更复杂的逻辑。

这个布局有一个名字:masonry,它来自建筑结构,因为砖块是如何组合在一起的:) 有 mui 组件:

https://mui.com/components/masonry/#basic-masonry

这里也复制示例:

import * as React from 'react';
import Box from '@mui/material/Box';
import { styled } from '@mui/material/styles';
import Paper from '@mui/material/Paper';
import Masonry from '@mui/lab/Masonry';

const heights = [150, 30, 90, 70, 110, 150, 130, 80, 50, 90, 100, 150, 30, 50, 80];

const Item = styled(Paper)(({ theme }) => ({
  ...theme.typography.body2,
  color: theme.palette.text.secondary,
  border: '1px solid black',
  display: 'flex',
  alignItems: 'center',
  justifyContent: 'center',
}));

export default function BasicMasonry() {
  return (
    <Box sx={{ width: 500, minHeight: 393 }}>
      <Masonry columns={4} spacing={1}>
        {heights.map((height, index) => (
          <Item key={index} sx={{ height }}>
            {index + 1}
          </Item>
        ))}
      </Masonry>
    </Box>
  );
}