generated from muratkeremozcan/react-cypress-ts-vite-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from cesarhenrq/qa
Feat/pagination
- Loading branch information
Showing
8 changed files
with
291 additions
and
24 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
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 React from 'react'; | ||
|
||
import * as S from './styles'; | ||
|
||
import * as I from '@assets/db.icons'; | ||
|
||
type IProps = { | ||
quantity: number; | ||
currentPage: number; | ||
setCurrentPage: React.Dispatch<React.SetStateAction<number>>; | ||
}; | ||
|
||
const Pagination = ({ currentPage, setCurrentPage, quantity }: IProps) => { | ||
const pagesToShow = 5; // Número de botões de página a serem exibidos | ||
|
||
const previousPage = () => { | ||
if (currentPage > 1) setCurrentPage(currentPage - 1); | ||
}; | ||
|
||
const nextPage = () => { | ||
if (currentPage < Math.ceil(quantity / pagesToShow)) { | ||
setCurrentPage(currentPage + 1); | ||
} | ||
}; | ||
|
||
const firstPage = () => { | ||
setCurrentPage(1); | ||
}; | ||
|
||
const lastPage = () => { | ||
setCurrentPage(Math.ceil(quantity / pagesToShow)); | ||
}; | ||
|
||
const getPageNumbers = () => { | ||
const totalPages = Math.ceil(quantity / pagesToShow); | ||
const pageNumbers = []; | ||
for (let i = 1; i <= totalPages; i++) { | ||
pageNumbers.push(i); | ||
} | ||
return pageNumbers; | ||
}; | ||
|
||
const pageNumbers = getPageNumbers(); | ||
|
||
// Calcula o índice inicial para renderizar os botões da página | ||
const startIndex = Math.max(0, currentPage - 3); | ||
const endIndex = Math.min(startIndex + 5, pageNumbers.length); | ||
|
||
const visiblePageNumbers = pageNumbers.slice(startIndex, endIndex); | ||
|
||
return ( | ||
<S.WrapperPagination> | ||
<S.TextPagination> | ||
{`${Math.min(currentPage * 5, quantity) | ||
.toString() | ||
.padStart(2, '0')} de ${quantity.toString().padStart(2, '0')} itens`} | ||
</S.TextPagination> | ||
<S.WrapperPaginationButtons> | ||
<S.IconWrapper onClick={firstPage}> | ||
<I.ArrowDoubleLeft /> | ||
</S.IconWrapper> | ||
<S.IconWrapper onClick={previousPage}> | ||
<I.ArrowLeft /> | ||
</S.IconWrapper> | ||
|
||
{visiblePageNumbers.map(page => ( | ||
<S.ButtonPagination | ||
key={`button-pagination-${page}`} | ||
onClick={() => setCurrentPage(page)} | ||
isActive={currentPage === page} | ||
> | ||
{page} | ||
</S.ButtonPagination> | ||
))} | ||
<S.IconWrapper onClick={nextPage}> | ||
<I.ArrowRight /> | ||
</S.IconWrapper> | ||
<S.IconWrapper onClick={lastPage}> | ||
<I.ArrowDoubleRight /> | ||
</S.IconWrapper> | ||
</S.WrapperPaginationButtons> | ||
</S.WrapperPagination> | ||
); | ||
}; | ||
|
||
export default 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const WrapperPagination = styled.div` | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
`; | ||
|
||
export const TextPagination = styled.span` | ||
font-size: var(--font-size-xsmall); | ||
font-weight: 500; | ||
color: 'var(--dark-gray)'; | ||
`; | ||
|
||
export const WrapperPaginationButtons = styled.div` | ||
display: flex; | ||
align-items: center; | ||
gap: 5px; | ||
`; | ||
|
||
type IButtonPaginationProps = { | ||
isActive: boolean; | ||
}; | ||
|
||
export const ButtonPagination = styled.button<IButtonPaginationProps>` | ||
border: none; | ||
background-color: ${({ isActive }) => | ||
isActive ? 'var(--purple-dark-secondary)' : 'transparent'}; | ||
cursor: pointer; | ||
font-size: var(--font-size-xsmall); | ||
font-weight: ${({ isActive }) => (isActive ? '500' : '400')}; | ||
color: ${({ isActive }) => (isActive ? 'var(--white)' : 'var(--dark-gray)')}; | ||
width: 24px; | ||
height: 24px; | ||
border-radius: 8px; | ||
`; | ||
export const IconWrapper = styled.div` | ||
width: 24px; | ||
height: 24px; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
cursor: pointer; | ||
`; |
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
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