페이지네이션(Pagination)
범위 내에서 특정 페이지를 선택할 수 있게 해주는 컴포넌트예요.
출처: 문서
본문
Import
import { Pagination } from "@kobalte/core/pagination";
// or
import { Root, Item, ... } from "@kobalte/core/pagination";
// or (deprecated)
import { Pagination } from "@kobalte/core";
Features
- 접근성을 위한 라벨링을 지원해요.
- 탭 포커스 관리를 제공해요.
- 제어(controlled) 또는 비제어(uncontrolled) 방식으로 사용할 수 있어요.
- 모양을 자유롭게 커스터마이즈할 수 있어요.
Anatomy
페이지네이션은 다음 요소들로 구성돼요:
- Pagination: 페이지네이션 컴포넌트의 루트 컨테이너예요.
- Pagination.Item: 페이지네이션의 항목이에요.
- Pagination.Ellipsis: 페이지네이션의 생략(말줄임) 항목이에요.
- Pagination.Previous: 페이지네이션의 이전 버튼이에요.
- Pagination.Next: 페이지네이션의 다음 버튼이에요.
- Pagination.Items: 항목 목록을 담고 있으며 사용자가 그중 하나를 선택할 수 있게 해줘요.
<Pagination>
<Pagination.Previous/>
<Pagination.Items/>
<Pagination.Next/>
</Select>
Example
Usage
Default value
defaultPage prop으로 기본으로 선택될 초기 비제어(uncontrolled) 페이지를 지정할 수 있어요. 이 값은 count보다 작거나 같은 숫자를 받으며 1부터 시작해요.
<Pagination
count={10}
defaultPage={4}
itemComponent={props => <Pagination.Item page={props.page}>{props.page}</Pagination.Item>}
ellipsisComponent={() => <Pagination.Ellipsis>...</Pagination.Ellipsis>}
>
<Pagination.Previous>Previous</Pagination.Previous>
<Pagination.Items />
<Pagination.Next>Next</Pagination.Next>
</Pagination>
Controlled value
페이지 번호를 받는 page prop을 사용해 값을 제어(controlled) 방식으로 만들 수 있어요. 사용자가 항목을 선택하면 onPageChange 이벤트가 발생하며 새 페이지 번호를 받아요.
import { createSignal } from "solid-js";
export function ControlledExample() {
const [page, setPage] = createSignal(4);
return (
<Pagination
page={page()}
onPageChange={setPage}
count={10}
itemComponent={props => <Pagination.Item page={props.page}>{props.page}</Pagination.Item>}
ellipsisComponent={() => <Pagination.Ellipsis>...</Pagination.Ellipsis>}
>
<Pagination.Previous>Previous</Pagination.Previous>
<Pagination.Items />
<Pagination.Next>Next</Pagination.Next>
</Pagination>
);
}
Next / Previous buttons example
Next와 Previous 버튼을 생략해 모양을 커스터마이즈할 수 있어요.
<Pagination
count={10}
itemComponent={props => <Pagination.Item page={props.page}>{props.page}</Pagination.Item>}
ellipsisComponent={() => <Pagination.Ellipsis>...</Pagination.Ellipsis>}
>
<Pagination.Items />
</Pagination>
First / Last item example
항상 표시하는 대신 First와 Last 항목을 숨길 수 있어요.
<Pagination
count={10}
showFirst={false}
showLast={false}
itemComponent={props => <Pagination.Item page={props.page}>{props.page}</Pagination.Item>}
ellipsisComponent={() => <Pagination.Ellipsis>...</Pagination.Ellipsis>}
>
<Pagination.Previous>Previous</Pagination.Previous>
<Pagination.Items />
<Pagination.Next>Next</Pagination.Next>
</Pagination>
Siblings example
현재 페이지 주변에 표시할 항목 개수를 커스터마이즈할 수 있어요.
<Pagination
count={10}
siblingCount={2}
itemComponent={props => <Pagination.Item page={props.page}>{props.page}</Pagination.Item>}
ellipsisComponent={() => <Pagination.Ellipsis>...</Pagination.Ellipsis>}
>
<Pagination.Previous>Previous</Pagination.Previous>
<Pagination.Items />
<Pagination.Next>Next</Pagination.Next>
</Pagination>
Fixed Items example
콘텐츠 이동을 막기 위해 항목의 총 개수를 고정할 수 있어요. 말줄임이 비활성화되어 있으면(빈 컴포넌트를 반환하면) fixedItems="no-ellipsis"를 대신 사용해요.
<Pagination
count={10}
fixedItems
itemComponent={props => <Pagination.Item page={props.page}>{props.page}</Pagination.Item>}
ellipsisComponent={() => <Pagination.Ellipsis>...</Pagination.Ellipsis>}
>
<Pagination.Previous>Previous</Pagination.Previous>
<Pagination.Items />
<Pagination.Next>Next</Pagination.Next>
</Pagination>
API Reference
Pagination
Pagination은 @kobalte/core/pagination의 Root import와 동일해요(그리고 폐기(deprecated)된 Pagination.Root).
| Prop | Description |
|---|---|
| page | number The controlled page number of the pagination. (1-indexed) |
| defaultPage | string The default page number when initially rendered. (1-indexed) |
| onPageChange | (page: number) => void Event handler called when the page number changes. |
| count | number The number of pages for the pagination. |
| siblingCount | number The number of siblings to show around the current page item. |
| showFirst | boolean Whether to always show the first page item. |
| showLast | boolean Whether to always show the last page item. |
| fixedItems | boolean | "no-ellipsis" Whether to always show the same number of items (to avoid content shift). Special value: "no-ellipsis" does not count the ellipsis as an item (used when ellipsis are disabled). |
| itemComponent | Component<{page: number}> The component to render as an item in the Pagination.Items. |
| ellipsisComponent | Component The component to render as an ellipsis item in the Pagination.Items. |
| disabled | boolean Whether the pagination is disabled. |
| Data attribute | Description |
|---|---|
| data-disabled | Present when the pagination is disabled. |
Pagination.Item
| Prop | Description |
|---|---|
| page | number The page number to render. |
| Data attribute | Description |
|---|---|
| data-current | Present when the item is the current page. |
Rendered elements
| Component | Default rendered element |
|---|---|
Pagination |
div |
Pagination.Item |
button |
Pagination.Ellipsis |
div |
Pagination.Previous |
button |
Pagination.Next |
button |
Pagination.Items |
none |