Skip to content

Commit d5ec89a

Browse files
Jarosław Wasiakdziraf
authored andcommitted
feat: added first and last button to pagination component
1 parent 7e2707b commit d5ec89a

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/molecules/pagination/pagination.tsx

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { Button, ButtonProps } from '../../atoms/button'
77
import { Icon } from '../../atoms/icon'
88
import { cssClass } from '../../utils/css-class'
99

10+
const MIN_PAGES_FOR_FIRST_PAGE_BUTTON = 3
11+
const FIRST_PAGE = 1
12+
1013
/**
1114
* @alias PaginationProps
1215
* @memberof Pagination
@@ -59,16 +62,10 @@ const PaginationWrapper = styled(Box)`
5962
width: 56px;
6063
border-right: 1px solid ${({ theme }): string => theme.colors.grey20};
6164
}
62-
& > :nth-child(2) {
63-
padding-left: 16px;
64-
}
6565
& > :last-child {
6666
width: 56px;
6767
border-left: 1px solid ${({ theme }): string => theme.colors.grey20};
6868
}
69-
& > :nth-last-child(2) {
70-
padding-right: 16px;
71-
}
7269
`
7370

7471
/**
@@ -106,7 +103,7 @@ const PaginationWrapper = styled(Box)`
106103
*/
107104
const Pagination: React.FC<PaginationProps> = (props) => {
108105
const { total, page, perPage, onChange, ...rest } = props
109-
const currentPage = page || 1
106+
const currentPage = page || FIRST_PAGE
110107
const paginate = JWPaginate(total, currentPage, perPage)
111108

112109
const isFirstPage = currentPage === paginate.startPage
@@ -115,16 +112,29 @@ const Pagination: React.FC<PaginationProps> = (props) => {
115112
const prevPage = isFirstPage ? currentPage : currentPage - 1
116113
const nextPage = isLastPage ? currentPage : currentPage + 1
117114

118-
if (paginate.totalPages === 1 || total === 0) {
115+
if (paginate.totalPages === FIRST_PAGE || total === 0) {
119116
return null
120117
}
121118

122119
return (
123120
<PaginationWrapper className={cssClass('Pagination')} {...rest}>
121+
{
122+
total >= MIN_PAGES_FOR_FIRST_PAGE_BUTTON
123+
? (
124+
<PaginationLink
125+
data-testid="first"
126+
disabled={isFirstPage}
127+
onClick={() => (!isFirstPage ? onChange(FIRST_PAGE) : undefined)}
128+
>
129+
<Icon icon="PageFirst" />
130+
</PaginationLink>
131+
)
132+
: null
133+
}
124134
<PaginationLink
125135
data-testid="previous"
126136
disabled={isFirstPage}
127-
onClick={(): void => (!isFirstPage ? onChange(prevPage) : undefined)}
137+
onClick={() => (!isFirstPage ? onChange(prevPage) : undefined)}
128138
>
129139
<Icon icon="ChevronLeft" />
130140
</PaginationLink>
@@ -141,11 +151,25 @@ const Pagination: React.FC<PaginationProps> = (props) => {
141151
))}
142152
<PaginationLink
143153
data-testid="next"
144-
onClick={(): void => (!isLastPage ? onChange(nextPage) : undefined)}
154+
onClick={() => (!isLastPage ? onChange(nextPage) : undefined)}
145155
disabled={isLastPage}
146156
>
147157
<Icon icon="ChevronRight" />
148158
</PaginationLink>
159+
{
160+
total >= MIN_PAGES_FOR_FIRST_PAGE_BUTTON
161+
? (
162+
<PaginationLink
163+
data-testid="last"
164+
onClick={() => (!isLastPage ? onChange(paginate.endPage) : undefined)}
165+
disabled={isLastPage}
166+
>
167+
<Icon icon="PageLast" />
168+
</PaginationLink>
169+
)
170+
: null
171+
}
172+
149173
</PaginationWrapper>
150174
)
151175
}

0 commit comments

Comments
 (0)