-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CE): added new pagination format
- Loading branch information
1 parent
c54babe
commit 7084f65
Showing
8 changed files
with
303 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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,56 @@ | ||
import { Button, Icon } from '@chakra-ui/react'; | ||
import { FiChevronLeft, FiChevronRight, FiChevronsLeft, FiChevronsRight } from 'react-icons/fi'; | ||
|
||
export enum PAGE_CHANGE_BUTTON_TYPE { | ||
PREVIOUS = 'previous', | ||
NEXT = 'next', | ||
FIRST = 'first', | ||
LAST = 'last', | ||
} | ||
|
||
type PageButtonProps = { | ||
type: PAGE_CHANGE_BUTTON_TYPE; | ||
onClick: () => void; | ||
isEnabled?: boolean; | ||
}; | ||
|
||
const PageChangeButton = ({ type, onClick, isEnabled = false }: PageButtonProps) => { | ||
const iconMap = { | ||
[PAGE_CHANGE_BUTTON_TYPE.PREVIOUS]: { icon: FiChevronLeft, value: 'previous' }, | ||
[PAGE_CHANGE_BUTTON_TYPE.FIRST]: { icon: FiChevronsLeft, value: 'first' }, | ||
[PAGE_CHANGE_BUTTON_TYPE.LAST]: { icon: FiChevronsRight, value: 'last' }, | ||
[PAGE_CHANGE_BUTTON_TYPE.NEXT]: { icon: FiChevronRight, value: 'next' }, | ||
}; | ||
|
||
const icon = iconMap[type] || iconMap[PAGE_CHANGE_BUTTON_TYPE.NEXT]; | ||
|
||
return ( | ||
<Button | ||
height='32px' | ||
width='32px' | ||
borderRadius='6px' | ||
borderStyle='solid' | ||
borderWidth='1px' | ||
borderColor='gray.500' | ||
display='flex' | ||
justifyContent='center' | ||
alignItems='center' | ||
color='black.200' | ||
backgroundColor='gray.300' | ||
minWidth='0' | ||
padding={0} | ||
onClick={onClick} | ||
_hover={{ backgroundColor: 'gray.400' }} | ||
_disabled={{ | ||
_hover: { cursor: 'not-allowed' }, | ||
backgroundColor: 'gray.400', | ||
}} | ||
isDisabled={!isEnabled} | ||
data-testid={`page-change-${icon.value}`} | ||
> | ||
<Icon as={icon.icon} h='14px' w='14px' color='black.500' /> | ||
</Button> | ||
); | ||
}; | ||
|
||
export default PageChangeButton; |
This file contains 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,42 @@ | ||
import { Box, Text } from '@chakra-ui/react'; | ||
|
||
const PageNumberItem = ({ | ||
isActive = false, | ||
onClick, | ||
value, | ||
isEllipsis = false, | ||
}: { | ||
isActive?: boolean; | ||
onClick?: () => void; | ||
value?: number; | ||
isEllipsis?: boolean; | ||
}) => ( | ||
<Box | ||
height='32px' | ||
width='32px' | ||
borderRadius='6px' | ||
borderStyle='solid' | ||
borderWidth={isActive ? '1px' : '0px'} | ||
borderColor={isActive ? 'gray.500' : 'gray.400'} | ||
display='flex' | ||
justifyContent='center' | ||
alignItems='center' | ||
color='black.200' | ||
backgroundColor={isActive ? 'gray.100' : 'gray.300'} | ||
minWidth='0' | ||
padding={0} | ||
onClick={isEllipsis ? () => {} : onClick} | ||
_hover={{ backgroundColor: 'gray.400', cursor: 'pointer' }} | ||
_disabled={{ | ||
_hover: { cursor: 'not-allowed' }, | ||
backgroundColor: 'gray.400', | ||
}} | ||
data-testid={isEllipsis ? 'ellipsis' : `page-number-${value}`} | ||
> | ||
<Text size='xs' fontWeight='semibold'> | ||
{isEllipsis ? '...' : value} | ||
</Text> | ||
</Box> | ||
); | ||
|
||
export default PageNumberItem; |
This file contains 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,101 @@ | ||
import { LinksType } from '@/services/common'; | ||
import { Stack, Box } from '@chakra-ui/react'; | ||
import PageNumberItem from './PageNumberItem'; | ||
import PageChangeButton, { PAGE_CHANGE_BUTTON_TYPE } from './PageChangeButton'; | ||
|
||
const getPage = (url: string): number => { | ||
const parser = new URLSearchParams(new URL(url).search); | ||
return parseInt(parser.get('page') || '0'); | ||
}; | ||
|
||
export type PaginationProps = { | ||
links: LinksType; | ||
currentPage: number; | ||
handlePageChange: (pageNumber: number) => void; | ||
}; | ||
|
||
const Pagination = ({ links, currentPage, handlePageChange }: PaginationProps) => { | ||
const firstPage = getPage(links.first); | ||
const lastPage = getPage(links.last); | ||
|
||
const renderPageNumbers = () => { | ||
if (currentPage === firstPage) { | ||
return ( | ||
<> | ||
<PageNumberItem | ||
isActive | ||
value={currentPage} | ||
onClick={() => handlePageChange(currentPage)} | ||
/> | ||
{lastPage > firstPage + 1 && <PageNumberItem isEllipsis />} | ||
{lastPage !== firstPage && ( | ||
<PageNumberItem value={lastPage} onClick={() => handlePageChange(lastPage)} /> | ||
)} | ||
</> | ||
); | ||
} else if (currentPage === lastPage) { | ||
return ( | ||
<> | ||
<PageNumberItem value={firstPage} onClick={() => handlePageChange(firstPage)} /> | ||
{lastPage > firstPage + 1 && <PageNumberItem isEllipsis />} | ||
<PageNumberItem | ||
isActive | ||
value={currentPage} | ||
onClick={() => handlePageChange(currentPage)} | ||
/> | ||
</> | ||
); | ||
} else { | ||
return ( | ||
<> | ||
<PageNumberItem value={firstPage} onClick={() => handlePageChange(firstPage)} /> | ||
{currentPage > firstPage + 1 && <PageNumberItem isEllipsis />} | ||
<PageNumberItem | ||
isActive | ||
value={currentPage} | ||
onClick={() => handlePageChange(currentPage)} | ||
/> | ||
{currentPage < lastPage - 1 && <PageNumberItem isEllipsis />} | ||
<PageNumberItem value={lastPage} onClick={() => handlePageChange(lastPage)} /> | ||
</> | ||
); | ||
} | ||
}; | ||
|
||
return ( | ||
<Box | ||
width='fit-content' | ||
backgroundColor='gray.300' | ||
borderColor='gray.400' | ||
borderRadius='8px' | ||
borderWidth='1px' | ||
padding='4px' | ||
> | ||
<Stack direction='row' justify='end' spacing={4} alignItems='center' gap='6px'> | ||
<PageChangeButton | ||
isEnabled={currentPage !== firstPage} | ||
type={PAGE_CHANGE_BUTTON_TYPE.FIRST} | ||
onClick={() => handlePageChange(firstPage)} | ||
/> | ||
<PageChangeButton | ||
isEnabled={currentPage !== firstPage} | ||
type={PAGE_CHANGE_BUTTON_TYPE.PREVIOUS} | ||
onClick={() => handlePageChange(currentPage - 1)} | ||
/> | ||
{renderPageNumbers()} | ||
<PageChangeButton | ||
isEnabled={currentPage !== lastPage} | ||
type={PAGE_CHANGE_BUTTON_TYPE.NEXT} | ||
onClick={() => handlePageChange(currentPage + 1)} | ||
/> | ||
<PageChangeButton | ||
isEnabled={currentPage !== lastPage} | ||
type={PAGE_CHANGE_BUTTON_TYPE.LAST} | ||
onClick={() => handlePageChange(lastPage)} | ||
/> | ||
</Stack> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default Pagination; |
86 changes: 86 additions & 0 deletions
86
ui/src/components/EnhancedPagination/__tests__/Pagination.test.tsx
This file contains 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,86 @@ | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import { expect } from '@jest/globals'; | ||
import '@testing-library/jest-dom'; | ||
|
||
import Pagination, { PaginationProps } from '../Pagination'; | ||
|
||
describe('Pagination', () => { | ||
const mockHandlePageChange = jest.fn(); | ||
const defaultProps: PaginationProps = { | ||
links: { | ||
self: 'http://localhost:3000/api/v1/connectors?', | ||
first: 'http://localhost:3000/api/v1/connectors?page=1&per_page=10', | ||
prev: null, | ||
next: 'http://localhost:3000/api/v1/connectors?page=2&per_page=10', | ||
last: 'http://localhost:3000/api/v1/connectors?page=10&per_page=10', | ||
}, | ||
currentPage: 5, | ||
handlePageChange: mockHandlePageChange, | ||
}; | ||
|
||
beforeEach(() => { | ||
mockHandlePageChange.mockClear(); | ||
}); | ||
|
||
it('should render correct page numbers for middle page', () => { | ||
render(<Pagination {...defaultProps} />); | ||
expect(screen.getByTestId('page-number-1')).toBeTruthy(); | ||
expect(screen.getByTestId('page-number-5')).toBeTruthy(); | ||
expect(screen.getByTestId('page-number-10')).toBeTruthy(); | ||
}); | ||
|
||
it('should render correct page numbers for first page', () => { | ||
render(<Pagination {...defaultProps} currentPage={1} />); | ||
expect(screen.getByTestId('page-number-1')).toBeTruthy(); | ||
expect(screen.getByTestId('page-number-10')).toBeTruthy(); | ||
expect(screen.queryByTestId('page-number-5')).not.toBeTruthy(); | ||
}); | ||
|
||
it('should render correct page numbers for last page', () => { | ||
render(<Pagination {...defaultProps} currentPage={10} />); | ||
expect(screen.getByTestId('page-number-1')).toBeTruthy(); | ||
expect(screen.getByTestId('page-number-10')).toBeTruthy(); | ||
}); | ||
|
||
it('should call handlePageChange with correct page number when a page number is clicked', () => { | ||
render(<Pagination {...defaultProps} />); | ||
const pageButton = screen.getByTestId('page-number-1'); | ||
fireEvent.click(pageButton); | ||
expect(mockHandlePageChange).toHaveBeenCalledWith(1); | ||
}); | ||
|
||
it('should disable first and previous buttons on first page', () => { | ||
render(<Pagination {...defaultProps} currentPage={1} />); | ||
|
||
expect(screen.getByTestId('page-change-first')).toHaveProperty('disabled', true); | ||
expect(screen.getByTestId('page-change-previous')).toHaveProperty('disabled', true); | ||
}); | ||
|
||
it('should disable next and last buttons on last page', () => { | ||
render(<Pagination {...defaultProps} currentPage={10} />); | ||
|
||
expect(screen.getByTestId('page-change-next')).toHaveProperty('disabled', true); | ||
expect(screen.getByTestId('page-change-last')).toHaveProperty('disabled', true); | ||
}); | ||
|
||
it('should call handlePageChange with correct page number when navigation buttons are clicked', () => { | ||
render(<Pagination {...defaultProps} />); | ||
fireEvent.click(screen.getByTestId('page-change-first')); | ||
expect(mockHandlePageChange).toHaveBeenCalledWith(1); | ||
|
||
fireEvent.click(screen.getByTestId('page-change-previous')); | ||
expect(mockHandlePageChange).toHaveBeenCalledWith(4); | ||
|
||
fireEvent.click(screen.getByTestId('page-change-next')); | ||
expect(mockHandlePageChange).toHaveBeenCalledWith(6); | ||
|
||
fireEvent.click(screen.getByTestId('page-change-last')); | ||
expect(mockHandlePageChange).toHaveBeenCalledWith(10); | ||
}); | ||
|
||
it('should render ellipsis correctly', () => { | ||
render(<Pagination {...defaultProps} />); | ||
const ellipses = screen.getAllByText('...'); | ||
expect(ellipses).toHaveLength(2); | ||
}); | ||
}); |
This file contains 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 @@ | ||
export { default } from './Pagination'; |
This file contains 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 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 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