From d7463556fe2211b8520b94a854804c1ae6abd9a4 Mon Sep 17 00:00:00 2001 From: yoosangwook Date: Wed, 16 Oct 2024 18:18:40 +0900 Subject: [PATCH] feat: Add QPagination Component --- src/components/Playground.jsx | 12 ++++++ .../common/pagination/QPagination.jsx | 37 +++++++++++++++++++ src/hooks/usePagination.js | 30 +++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 src/components/common/pagination/QPagination.jsx create mode 100644 src/hooks/usePagination.js diff --git a/src/components/Playground.jsx b/src/components/Playground.jsx index 5d54c858..a882bdd9 100644 --- a/src/components/Playground.jsx +++ b/src/components/Playground.jsx @@ -20,6 +20,7 @@ import Image from 'next/image' import QInput from './common/input/Qinput' import QSelect from './common/select/QSelect' +import QPagination from './common/pagination/QPagination' export default function Playground() { const [useCadFile, setUseCadFile] = useRecoilState(useCadFileState) @@ -41,6 +42,7 @@ export default function Playground() { const [radioInput, setRadioInput] = useState('') const [checkboxInput, setCheckboxInput] = useState([]) const [selectedValue, setSelectedValue] = useState('') + useEffect(() => { console.log('textInput:', textInput) }, [textInput]) @@ -130,6 +132,13 @@ export default function Playground() { }) } + const paginationProps = { + pageNo: 1, + pageSize: 10, + pagePerBlock: 10, + totalCount: 501, + } + return ( <>
@@ -290,6 +299,9 @@ export default function Playground() { Sweetalert - confirm
+
+ +
) diff --git a/src/components/common/pagination/QPagination.jsx b/src/components/common/pagination/QPagination.jsx new file mode 100644 index 00000000..4175036b --- /dev/null +++ b/src/components/common/pagination/QPagination.jsx @@ -0,0 +1,37 @@ +import usePagination from '@/hooks/usePagination' + +export default function QPagination(props) { + const { currentPage, pageRange, changePage, totalPages } = usePagination(props) + + return ( +
    +
  1. + +
  2. +
  3. + +
  4. + {pageRange.map((page) => ( +
  5. + +
  6. + ))} +
  7. + +
  8. +
  9. + +
  10. +
+ ) +} diff --git a/src/hooks/usePagination.js b/src/hooks/usePagination.js new file mode 100644 index 00000000..818275e1 --- /dev/null +++ b/src/hooks/usePagination.js @@ -0,0 +1,30 @@ +import { useState } from 'react' + +/** + * 페이지네이션 훅 + * @param {number} pageNo 현재 페이지 {optional} + * @param {number} pageSize 페이지당 아이템 수 {optional} + * @param {number} pagePerBlock 페이지 그룹 수 {optional} + * @param {number} totalCount 전체 아이템 수 {required} + * @returns + */ +const usePagination = ({ pageNo = 1, pageSize = 10, pagePerBlock = 10, totalCount = 0 }) => { + const [currentPage, setCurrentPage] = useState(pageNo) + const changePage = (page) => { + setCurrentPage(page) + } + + const pageGroup = Math.floor((currentPage - 1) / pagePerBlock) + 1 + const totalPages = Math.ceil(totalCount / pageSize) + const pages = Array.from({ length: totalPages }, (_, i) => i + 1) + const startPage = Math.max(1, (pageGroup - 1) * pagePerBlock + 1) + const endPage = Math.min(totalPages, pageGroup * pagePerBlock) + const pageRange = Array.from({ length: endPage !== totalPages ? pagePerBlock : endPage - startPage + 1 }, (_, i) => { + if (i + startPage > endPage) return + return i + startPage + }) + + return { currentPage, pageRange, changePage, totalPages } +} + +export default usePagination