feat: 페이지네이션 공용 컴포넌트 추가
Co-Authored-By: Claude Opus 5
@d5faff1beee8b9198df625202eb0864486d9faf1
+++ components/ui/pagination.tsx
... | ... | @@ -0,0 +1,109 @@ |
| 1 | +import Link from "next/link"; | |
| 2 | +import type { ReactNode } from "react"; | |
| 3 | + | |
| 4 | +interface PaginationProps { | |
| 5 | + /** 1-based. */ | |
| 6 | + currentPage: number; | |
| 7 | + totalPages: number; | |
| 8 | + /** 각 페이지 번호로 이동할 href를 만들어 준다 — 라우팅은 소비처 책임. */ | |
| 9 | + buildHref: (page: number) => string; | |
| 10 | + /** 한 번에 보여줄 번호 개수. 기본 5. */ | |
| 11 | + windowSize?: number; | |
| 12 | +} | |
| 13 | + | |
| 14 | +const ITEM_CLASSES = | |
| 15 | + "inline-flex size-9 items-center justify-center rounded-md text-body-sm transition-colors duration-200"; | |
| 16 | +const ENABLED_CLASSES = "text-foreground-muted hover:bg-surface-muted hover:text-foreground"; | |
| 17 | +const DISABLED_CLASSES = "text-foreground-subtle opacity-50"; | |
| 18 | +const CURRENT_CLASSES = "bg-primary text-on-primary"; | |
| 19 | + | |
| 20 | +/** 현재 페이지를 중심으로 windowSize개 번호를 [1, totalPages] 범위 안에서 슬라이딩한다. */ | |
| 21 | +function getPageWindow(currentPage: number, totalPages: number, windowSize: number): number[] { | |
| 22 | + if (totalPages < 1) { | |
| 23 | + return []; | |
| 24 | + } | |
| 25 | + const half = Math.floor(windowSize / 2); | |
| 26 | + let start = Math.max(1, currentPage - half); | |
| 27 | + const end = Math.min(totalPages, start + windowSize - 1); | |
| 28 | + start = Math.max(1, end - windowSize + 1); | |
| 29 | + | |
| 30 | + const pages: number[] = []; | |
| 31 | + for (let page = start; page <= end; page += 1) { | |
| 32 | + pages.push(page); | |
| 33 | + } | |
| 34 | + return pages; | |
| 35 | +} | |
| 36 | + | |
| 37 | +interface PaginationItemProps { | |
| 38 | + disabled: boolean; | |
| 39 | + href: string; | |
| 40 | + label: string; | |
| 41 | + children: ReactNode; | |
| 42 | +} | |
| 43 | + | |
| 44 | +/** 처음·이전·다음·끝 이동 항목 — 비활성일 때는 링크 대신 aria-disabled span으로 렌더한다. */ | |
| 45 | +function PaginationItem({ disabled, href, label, children }: PaginationItemProps) { | |
| 46 | + if (disabled) { | |
| 47 | + return ( | |
| 48 | + <span aria-disabled="true" aria-label={label} className={`${ITEM_CLASSES} ${DISABLED_CLASSES}`}> | |
| 49 | + {children} | |
| 50 | + </span> | |
| 51 | + ); | |
| 52 | + } | |
| 53 | + return ( | |
| 54 | + <Link href={href} aria-label={label} className={`${ITEM_CLASSES} ${ENABLED_CLASSES}`}> | |
| 55 | + {children} | |
| 56 | + </Link> | |
| 57 | + ); | |
| 58 | +} | |
| 59 | + | |
| 60 | +/** | |
| 61 | + * 페이지네이션 — « ‹ [번호들] › ». 페이지 이동은 `next/link` href 기반이며 실제 라우팅(쿼리 | |
| 62 | + * 구성 등)은 `buildHref`를 통해 호출부(feature)가 책임진다. 첫/마지막 페이지에서는 처음·이전· | |
| 63 | + * 다음·끝 이동이 비활성 처리된다. | |
| 64 | + */ | |
| 65 | +export function Pagination({ | |
| 66 | + currentPage, | |
| 67 | + totalPages, | |
| 68 | + buildHref, | |
| 69 | + windowSize = 5, | |
| 70 | +}: PaginationProps) { | |
| 71 | + const pages = getPageWindow(currentPage, totalPages, windowSize); | |
| 72 | + const isFirst = currentPage <= 1; | |
| 73 | + const isLast = currentPage >= totalPages; | |
| 74 | + | |
| 75 | + return ( | |
| 76 | + <nav aria-label="페이지네이션" className="flex items-center justify-center gap-1"> | |
| 77 | + <PaginationItem disabled={isFirst} href={buildHref(1)} label="처음 페이지"> | |
| 78 | + « | |
| 79 | + </PaginationItem> | |
| 80 | + <PaginationItem | |
| 81 | + disabled={isFirst} | |
| 82 | + href={buildHref(Math.max(1, currentPage - 1))} | |
| 83 | + label="이전 페이지" | |
| 84 | + > | |
| 85 | + ‹ | |
| 86 | + </PaginationItem> | |
| 87 | + {pages.map((page) => ( | |
| 88 | + <Link | |
| 89 | + key={page} | |
| 90 | + href={buildHref(page)} | |
| 91 | + aria-current={page === currentPage ? "page" : undefined} | |
| 92 | + className={`${ITEM_CLASSES} ${page === currentPage ? CURRENT_CLASSES : ENABLED_CLASSES}`} | |
| 93 | + > | |
| 94 | + {page} | |
| 95 | + </Link> | |
| 96 | + ))} | |
| 97 | + <PaginationItem | |
| 98 | + disabled={isLast} | |
| 99 | + href={buildHref(Math.min(totalPages, currentPage + 1))} | |
| 100 | + label="다음 페이지" | |
| 101 | + > | |
| 102 | + › | |
| 103 | + </PaginationItem> | |
| 104 | + <PaginationItem disabled={isLast} href={buildHref(totalPages)} label="마지막 페이지"> | |
| 105 | + » | |
| 106 | + </PaginationItem> | |
| 107 | + </nav> | |
| 108 | + ); | |
| 109 | +} |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?