Skip to Content
文档

分页

用于在一系列页面之间导航。

SourceStorybookArk

用法

¥Usage

import { Pagination } from "@chakra-ui/react"
<Pagination.Root>
  <Pagination.PrevTrigger />
  <Pagination.Ellipsis />
  <Pagination.Item />
  <Pagination.PageText />
  <Pagination.NextTrigger />
</Pagination.Root>

快捷键

¥Shortcuts

Pagination 组件还提供了一组用于常见用例的快捷方式。

¥The Pagination component also provides a set of shortcuts for common use cases.

PaginationItems

此组件根据 countpageSize 属性渲染页面数量。

¥This component renders the number of pages based on the count and pageSize props.

渲染此内容:

¥Rendering this:

<Pagination.Items />

是以下代码的简写:

¥is shorthand for this:

<Pagination.Context>
  {({ pages }) =>
    pages.map((page, index) =>
      page.type === "page" ? (
        <Pagination.Item key={index} {...page} />
      ) : (
        <Pagination.Ellipsis key={index} index={index} />
      ),
    )
  }
</Pagination.Context>

示例

¥Examples

尺寸

¥Sizes

使用 size 属性更改分页的大小。

¥Use the size prop to change the size of the pagination.

info

分页大小映射到 Button 组件大小。

¥The pagination sizes are mapped to the Button component sizes.

变量

¥Variants

使用 variant 属性控制分页项和省略号的变体。

¥Use the variant prop to control the variant of the pagination items and ellipsis.

该变体与 Button 组件变体匹配。

受控

¥Controlled

使用 pageonPageChange 属性控制当前页面。

¥Use the page and onPageChange props to control the current page.

兄弟元素数量

¥Sibling Count

使用 siblingCount 控制在当前页面前后显示的同级页面数量。

¥Use siblingCount to control the number of sibling pages to show before and after the current page.

紧凑

¥Compact

使用 Pagination.PageText 创建紧凑的分页。这对于移动视图非常有用。

¥Use the Pagination.PageText to create a compact pagination. This can be useful for mobile views.

作为链接

¥As Link

以下是将分页渲染为链接的示例。

¥Here's an example of rendering the pagination as links.

已附加

¥Attached

以下是组合分页组件的示例,使用 Group 组件附加分页项目和触发器。

¥Here's an example of composing the pagination with the Group component to attach the pagination items and triggers.

计数文本

¥Count Text

format="long" 传递给 Pagination.PageText 组件以显示计数文本。

¥Pass format="long" to the Pagination.PageText component to show the count text.

数据驱动

¥Data Driven

以下是控制分页状态并使用状态对数据进行分块的示例。

¥Here's an example of controlling the pagination state and using the state to chunk the data.

Lorem ipsum dolor sit amet 1

Lorem ipsum dolor sit amet 2

Lorem ipsum dolor sit amet 3

Lorem ipsum dolor sit amet 4

Lorem ipsum dolor sit amet 5

属性

¥Props

根元素

¥Root

PropDefaultType
defaultPage '1'
number

The initial active page when rendered. Use when you don't need to control the active page of the pagination.

defaultPageSize '10'
number

The initial number of data items per page when rendered. Use when you don't need to control the page size of the pagination.

siblingCount '1'
number

Number of pages to show beside active page

type '\'button\''
'button' | 'link'

The type of the trigger element

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
count
number

Total number of data items

ids
Partial<{ root: string ellipsis: (index: number) => string prevTrigger: string nextTrigger: string item: (page: number) => string }>

The ids of the elements in the accordion. Useful for composition.

onPageChange
(details: PageChangeDetails) => void

Called when the page number is changed

onPageSizeChange
(details: PageSizeChangeDetails) => void

Called when the page size is changed

page
number

The controlled active page

pageSize
number

The controlled number of data items per page

translations
IntlTranslations

Specifies the localized strings that identifies the accessibility elements and their states

Previous

可折叠

Next

步骤