-
Notifications
You must be signed in to change notification settings - Fork 2
feat(Paging): extract paging into standalone reusable component #128
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
Merged
+186
−58
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite' | ||
| import { fn } from 'storybook/test' | ||
| import { useState } from 'react' | ||
| import { Paging } from './Paging' | ||
|
|
||
| const meta = { | ||
| title: 'Components/Paging', | ||
| component: Paging, | ||
| tags: ['autodocs'], | ||
| parameters: { layout: 'padded' }, | ||
| argTypes: { | ||
| pagingControls: { | ||
| control: 'select', | ||
| options: ['STANDARD', 'ROW_COUNT'], | ||
| }, | ||
| }, | ||
| } satisfies Meta<typeof Paging> | ||
|
|
||
| export default meta | ||
| type Story = StoryObj<typeof meta> | ||
|
|
||
| export const Standard: Story = { | ||
| args: { | ||
| totalCount: 50, | ||
| pageSize: 10, | ||
| currentPage: 1, | ||
| pagingControls: 'STANDARD', | ||
| onPageChange: fn(), | ||
| }, | ||
| render: (args) => { | ||
| const [currentPage, setCurrentPage] = useState(args.currentPage) | ||
| return ( | ||
| <Paging | ||
| {...args} | ||
| currentPage={currentPage} | ||
| onPageChange={setCurrentPage} | ||
| /> | ||
| ) | ||
| }, | ||
| } | ||
|
|
||
| export const RowCount: Story = { | ||
| args: { | ||
| totalCount: 50, | ||
| pageSize: 10, | ||
| currentPage: 1, | ||
| pagingControls: 'ROW_COUNT', | ||
| onPageChange: fn(), | ||
| }, | ||
| render: (args) => { | ||
| const [currentPage, setCurrentPage] = useState(args.currentPage) | ||
| return ( | ||
| <Paging | ||
| {...args} | ||
| currentPage={currentPage} | ||
| onPageChange={setCurrentPage} | ||
| /> | ||
| ) | ||
| }, | ||
| } | ||
|
|
||
| export const TwoPages: Story = { | ||
| args: { | ||
| totalCount: 15, | ||
| pageSize: 10, | ||
| currentPage: 1, | ||
| pagingControls: 'ROW_COUNT', | ||
| onPageChange: fn(), | ||
| }, | ||
| render: (args) => { | ||
| const [currentPage, setCurrentPage] = useState(args.currentPage) | ||
| return ( | ||
| <Paging | ||
| {...args} | ||
| currentPage={currentPage} | ||
| onPageChange={setCurrentPage} | ||
| /> | ||
| ) | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import * as React from 'react' | ||
| import { ChevronLeft, ChevronRight, ChevronsLeft, ChevronsRight } from 'lucide-react' | ||
|
|
||
| export interface PagingProps { | ||
| /** Total number of items */ | ||
| totalCount: number | ||
| /** Number of items per page */ | ||
| pageSize: number | ||
| /** Current page number (1-based) */ | ||
| currentPage: number | ||
| /** Callback when page changes */ | ||
| onPageChange: (page: number) => void | ||
| /** Determines if the paging includes the total row count. "STANDARD" hides total count; "ROW_COUNT" shows total count and first/last controls. */ | ||
| pagingControls?: 'STANDARD' | 'ROW_COUNT' | ||
| /** Whether the component is displayed */ | ||
| showWhen?: boolean | ||
| } | ||
|
|
||
| export const Paging: React.FC<PagingProps> = ({ | ||
| totalCount, | ||
| pageSize, | ||
| currentPage, | ||
| onPageChange, | ||
| pagingControls = 'STANDARD', | ||
| showWhen = true, | ||
| }) => { | ||
| if (!showWhen) return null | ||
|
|
||
| const totalPages = Math.ceil(totalCount / pageSize) | ||
| if (totalPages <= 1) return null | ||
|
|
||
| const startIndex = (currentPage - 1) * pageSize + 1 | ||
| const endIndex = Math.min(currentPage * pageSize, totalCount) | ||
| const hasPreviousPage = currentPage > 1 | ||
| const hasNextPage = currentPage < totalPages | ||
|
|
||
| return ( | ||
| <div className="flex items-center justify-end gap-2 px-3 py-2 text-sm text-gray-700"> | ||
| {pagingControls === 'ROW_COUNT' && totalPages >= 3 && ( | ||
| <button | ||
| onClick={() => onPageChange(1)} | ||
| disabled={!hasPreviousPage} | ||
| aria-label="First page" | ||
| title="First page" | ||
| className="px-1 py-1 disabled:text-gray-400 disabled:cursor-not-allowed text-blue-700 hover:text-blue-900 cursor-pointer" | ||
| > | ||
| <ChevronsLeft size={18} /> | ||
| </button> | ||
| )} | ||
| <button | ||
| onClick={() => onPageChange(currentPage - 1)} | ||
| disabled={!hasPreviousPage} | ||
| aria-label="Previous page" | ||
| title="Previous page" | ||
| className="px-1 py-1 disabled:text-gray-400 disabled:cursor-not-allowed text-blue-700 hover:text-blue-900 cursor-pointer" | ||
| > | ||
| <ChevronLeft size={18} /> | ||
| </button> | ||
| {pagingControls === 'ROW_COUNT' ? ( | ||
| <span> | ||
| <span className="font-bold">{startIndex} – {endIndex}</span> | ||
| {' '}of {totalCount} | ||
| </span> | ||
| ) : ( | ||
| <span> | ||
| <span className="font-bold">{startIndex} – {endIndex}</span> | ||
| {' '}of many | ||
| </span> | ||
| )} | ||
| <button | ||
| onClick={() => onPageChange(currentPage + 1)} | ||
| disabled={!hasNextPage} | ||
| aria-label="Next page" | ||
| title="Next page" | ||
| className="px-1 py-1 disabled:text-gray-400 disabled:cursor-not-allowed text-blue-700 hover:text-blue-900 cursor-pointer" | ||
| > | ||
| <ChevronRight size={18} /> | ||
| </button> | ||
| {pagingControls === 'ROW_COUNT' && totalPages >= 3 && ( | ||
| <button | ||
| onClick={() => onPageChange(totalPages)} | ||
| disabled={!hasNextPage} | ||
| aria-label="Last page" | ||
| title="Last page" | ||
| className="px-1 py-1 disabled:text-gray-400 disabled:cursor-not-allowed text-blue-700 hover:text-blue-900 cursor-pointer" | ||
| > | ||
| <ChevronsRight size={18} /> | ||
| </button> | ||
| )} | ||
| </div> | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { Paging } from './Paging' | ||
| export type { PagingProps } from './Paging' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.