Back to Ant Design

Masonry

components/masonry/index.zh-CN.md

6.3.72.8 KB
Original Source

瀑布流布局组件,用于展示不同高度的内容。

何时使用

  • 展示不规则高度的图片或卡片时
  • 需要按照列数均匀分布内容时
  • 需要响应式调整列数时

代码演示 {#examples}

<!-- prettier-ignore -->

<code src="./demo/basic.tsx">基础用法</code> <code src="./demo/responsive.tsx">响应式</code> <code src="./demo/image.tsx">图片</code> <code src="./demo/dynamic.tsx">动态更新</code> <code src="./demo/style-class.tsx">自定义语义结构的样式和类</code> <code src="./demo/fresh.tsx" debug>持续更新</code>

API

通用属性参考:通用属性

Masonry

参数说明类型默认值版本
classNames用于自定义组件内部各语义化结构的 class,支持对象或函数Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
columns列数,可以是固定值或响应式配置number | { xs?: number; sm?: number; md?: number }3
fresh是否持续监听子项尺寸变化booleanfalse
gutter间距,可以是固定值、响应式配置或水平垂直间距配置Gap | [Gap, Gap]0
items瀑布流项MasonryItem[]-
itemRender自定义项渲染(item: MasonryItem) => React.ReactNode-
styles语义化结构 style,支持对象和函数形式Record<SemanticDOM, CSSProperties> | ((info: { props }) => Record<SemanticDOM, CSSProperties>)-
onLayoutChange列排序回调({ key: React.Key; column: number }[]) => void-

MasonryItem

参数说明类型默认值
children自定义展示内容,相对 itemRender 具有更高优先级React.ReactNode-
column自定义所在列number-
data自定义存储数据T-
height高度number-
key唯一标识string | number-

Gap

Gap 是项之间的间距,可以是固定值,也可以是响应式配置。

ts
type Gap = undefined | number | Partial<Record<'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl', number>>;

Semantic DOM

<code src="./demo/_semantic.tsx" simplify="true"></code>

Design Token

<ComponentTokenTable component="Masonry"></ComponentTokenTable>