【问题标题】:React Material-UI GridList cascading layoutReact Material-UI GridList 级联布局
【发布时间】:2017-03-13 11:33:22
【问题描述】:

是否可以在类似层叠布局的 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>
    );

结果如下:

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

【问题讨论】:

    标签: javascript css reactjs material-ui


    【解决方案1】:

    您想要的 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 样式布局在确定下一个要渲染的元素应该去哪里时需要一些更复杂的逻辑。

    【讨论】:

      【解决方案2】:

      这个布局有一个名字: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>
        );
      }
      
      

      【讨论】:

        猜你喜欢
        • 2020-12-26
        • 2017-12-17
        • 1970-01-01
        • 2021-01-25
        • 1970-01-01
        • 2020-06-17
        • 2018-08-01
        • 2019-10-17
        • 2020-05-04
        相关资源
        最近更新 更多