Skip to content

Commit 99d20a6

Browse files
committed
[#99] ♻️ separate resuable interface (UsePagination)
1 parent 480b956 commit 99d20a6

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

src/components/shared/pagination/Pagination.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import { IcChevronLeft, IcChevronRight } from '@/assets/IconList'
2+
import type { UsePaginationReturn } from '@/types/hooks'
23
import clsx from 'clsx'
34

45
import { Button } from '@/components/common/button'
56

6-
interface PaginationProps {
7-
currentPage: number // 현재 페이지
8-
pageButtons: number[] // 현재 그룹의 버튼 목록
9-
hasNextPageGroup: boolean // 다음 페이지 그룹 존재 여부
10-
hasPreviousPageGroup: boolean // 이전 페이지 그룹 존재 여부
11-
goToPage: (page: number) => void // 특정 페이지로 이동
12-
goToNextPageGroup: () => void // 다음 페이지 그룹으로 이동
13-
goToPreviousPageGroup: () => void // 이전 페이지 그룹으로 이동
14-
}
15-
167
const baseStyle =
178
'flex h-24 w-24 items-center justify-center bg-common-white p-0 text-body3 text-gray-600'
189
const defaultPageButtonClass = clsx(baseStyle, 'hover:bg-gray-100')
@@ -28,7 +19,7 @@ export const Pagination = ({
2819
goToPage,
2920
goToNextPageGroup,
3021
goToPreviousPageGroup,
31-
}: PaginationProps): JSX.Element => {
22+
}: UsePaginationReturn): JSX.Element => {
3223
return (
3324
<div className='flex items-center gap-20'>
3425
<Button

src/hooks/usePagination.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
import { useState } from 'react'
22

3+
import type { UsePaginationReturn } from '@/types/hooks'
4+
35
interface UsePaginationProps {
46
totalItems: number // 전체 아이템 수
57
itemsPerPage: number // 페이지당 아이템 수
68
buttonsPerPage?: number // 한 번에 보여줄 페이지네이션 버튼 수 (기본값: 10)
79
}
810

9-
interface UsePaginationReturn {
10-
currentPage: number // 현재 페이지
11-
pageButtons: number[] // 현재 페이지 그룹의 버튼 목록
12-
hasNextPageGroup: boolean // 다음 페이지 그룹 존재 여부
13-
hasPreviousPageGroup: boolean // 이전 페이지 그룹 존재 여부
14-
goToPage: (page: number) => void // 특정 페이지로 이동
15-
goToNextPageGroup: () => void // 다음 페이지 그룹으로 이동
16-
goToPreviousPageGroup: () => void // 이전 페이지 그룹으로 이동
17-
}
18-
1911
export function usePagination({
2012
totalItems,
2113
itemsPerPage,
2214
buttonsPerPage = 10,
2315
}: UsePaginationProps): UsePaginationReturn {
2416
if (totalItems <= 0 || itemsPerPage <= 0 || buttonsPerPage <= 0) {
25-
throw new Error('All parameters must be greater than zero')
17+
throw new Error('0보다 같거나 작은 페이지를 인자로 전달할 수 없습니다.')
2618
}
2719

2820
const totalPages = Math.ceil(totalItems / itemsPerPage) // 총 페이지 수

src/types/hooks/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { UsePaginationReturn } from './usePagination.types'
2+
3+
export type { UsePaginationReturn }
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface UsePaginationReturn {
2+
currentPage: number // 현재 페이지
3+
pageButtons: number[] // 현재 페이지 그룹의 버튼 목록
4+
hasNextPageGroup: boolean // 다음 페이지 그룹 존재 여부
5+
hasPreviousPageGroup: boolean // 이전 페이지 그룹 존재 여부
6+
goToPage: (page: number) => void // 특정 페이지로 이동
7+
goToNextPageGroup: () => void // 다음 페이지 그룹으로 이동
8+
goToPreviousPageGroup: () => void // 이전 페이지 그룹으로 이동
9+
}

0 commit comments

Comments
 (0)