Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Pagination 컴포넌트, 스토리북 구현 #22

Merged
merged 6 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/shared/ui/Pagination/Pagination.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useState } from 'react';
import type { Meta, StoryObj } from '@storybook/react';

import { Pagination } from './Pagination';

const meta = {
title: 'components/common/Pagination',
component: Pagination,
tags: ['autodocs'],
} satisfies Meta<typeof Pagination>;

export default meta;
type Story = StoryObj<typeof Pagination>;

export const Basic: Story = {
args: {
currentPage: 1,
totalPages: 2,
className: '',
},
argTypes: {
currentPage: { control: { type: 'number' } },
totalPages: { control: { type: 'number' } },
className: { control: { type: 'text' } },
},
render: (args) => {
const [currentPage, setCurrentPage] = useState<number>(1);

const handlePageChange = (page: number) => {
setCurrentPage(page);
};
return (
<Pagination
currentPage={currentPage}
totalPages={args.totalPages}
onPageChange={handlePageChange}
/>
);
},
};
109 changes: 109 additions & 0 deletions src/shared/ui/Pagination/Pagination.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저번 pagination구현 시 남긴 리뷰와 동일하지만, 현재는 최대로 커버할 수 있는 page가 5개이네용. 피드에서도 무한스크롤을 적용하셔서 현재는 우선순위가 떨어지는 것 같고, 저희 sprint 일정이 가능해지면 챙기면 좋아보여요

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { cn } from '@/shared/lib/core';

import { Icon } from '../Icon';

type WrapperProps = {
/**
* additional className.
*/
className: string;
} & React.HTMLAttributes<HTMLElement>;

function PaginationWrapper({ className, ...props }: WrapperProps) {
return (
<nav
role="navigation"
aria-label="pagination"
className={cn('flex cursor-pointer items-center justify-center gap-1', className)}
{...props}
/>
);
}

type PaginationItemProps = {
/**
* page number.
*/
page: number;
/**
* whether the page is active.
*/
isActive: boolean;
/**
* function to be called when the page is clicked
*/
onClick: () => void;
};

function PaginationItem({ page, isActive, onClick }: PaginationItemProps) {
return (
<button
onClickCapture={onClick}
className={`size-9 rounded px-3 py-1 font-semibold ${
isActive ? 'bg-primary-100 text-primary-300 shadow-sm' : 'hover:bg-gray-100'
}`}
>
{page}
</button>
);
}

type Props = {
/**
* current page number.
*/
currentPage: number;
/**
* total page number.
*/
totalPages: number;
/**
* function to be called when page is changed.
*/
onPageChange: (page: number) => void;
/**
* additional className.
*/
className?: string;
};

export function Pagination({ currentPage, totalPages, onPageChange, className = '' }: Props) {
const handlePrevious = () => {
if (currentPage > 1) onPageChange(currentPage - 1);
};

const handleNext = () => {
if (currentPage < totalPages) onPageChange(currentPage + 1);
};

return (
<PaginationWrapper className={className}>
{totalPages >= 3 && currentPage > 1 && (
<Icon
name="arrowLeft"
size={35}
className="size-9 rounded-md px-2 py-1 hover:bg-gray-100"
onClickCapture={handlePrevious}
/>
)}

{Array.from({ length: totalPages }, (_, index) => (
<PaginationItem
key={index}
page={index + 1}
isActive={index + 1 === currentPage}
onClick={() => onPageChange(index + 1)}
/>
))}

{totalPages >= 3 && currentPage < totalPages && (
<Icon
name="arrowRight"
size={35}
className="size-9 rounded-md px-2 py-1 hover:bg-gray-100"
onClickCapture={handleNext}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

캡쳐링 단계에서 실행하는 이유는 무엇인가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네비게이션 아이콘 클릭 시 의도하지 않은 부모 요소의 클릭 이벤트를 방지하기 위해 사용했어요.

/>
)}
</PaginationWrapper>
);
}
1 change: 1 addition & 0 deletions src/shared/ui/Pagination/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Pagination } from './Pagination';
3 changes: 3 additions & 0 deletions src/shared/ui/assets/arrow-left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/shared/ui/assets/arrow-right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/shared/ui/assets/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Add from './add.svg';
import ArrowDown from './arrow-down.svg';
import ArrowLeft from './arrow-left.svg';
import ArrowRight from './arrow-right.svg';
import ArrowUp from './arrow-up.svg';
import Check from './check.svg';
import Close from './close.svg';
Expand All @@ -17,6 +19,8 @@ export const Icons = {
add: Add,
arrowDown: ArrowDown,
arrowUp: ArrowUp,
arrowLeft: ArrowLeft,
arrowRight: ArrowRight,
check: Check,
close: Close,
download: DownLoad,
Expand Down
Loading