-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
183 additions
and
38 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React from 'react' | ||
import { Icon } from '@blueprintjs/core' | ||
import cn from 'classnames' | ||
|
||
import { noop } from 'helpers/fn' | ||
import { PAGE, PAGE_SIZE, TOTAL } from 'constants/pagination' | ||
import styles from './style.module.css' | ||
|
||
interface IProps { | ||
total: number, | ||
page?: number, | ||
pageSize?: number, | ||
onPageChange: (page: number) => void | ||
} | ||
|
||
const { useState } = React | ||
|
||
const Pagination: React.FC<IProps> = ({ total = TOTAL, page = PAGE, pageSize = PAGE_SIZE, onPageChange = noop }) => { | ||
const [currentPage, setCurrentPage] = useState(page) | ||
const pageCount = Math.ceil(total / pageSize) | ||
|
||
const isFirstPage = currentPage === 1 | ||
const isLastPage = currentPage === pageCount | ||
|
||
const handleItemClick = (page: number) => { | ||
setCurrentPage(page) | ||
onPageChange(page) | ||
} | ||
|
||
const handlePrev = () => { | ||
if (isFirstPage) { | ||
return | ||
} | ||
|
||
handleItemClick(currentPage - 1) | ||
} | ||
|
||
const handleNext = () => { | ||
if (isLastPage) { | ||
return | ||
} | ||
|
||
handleItemClick(currentPage + 1) | ||
} | ||
|
||
const renderPages = () => { | ||
const result = [] | ||
for (let i = 0; i < pageCount; i++) { | ||
const page = i + 1 | ||
|
||
result.push( | ||
<div | ||
key={i} | ||
className={cn(styles.item, currentPage === page && styles.active)} | ||
onClick={() => handleItemClick(page)} | ||
> | ||
{i + 1} | ||
</div> | ||
) | ||
} | ||
return result | ||
} | ||
|
||
if (total < pageSize) { | ||
return null | ||
} | ||
|
||
return ( | ||
<div className={styles.root}> | ||
<div className={cn(styles.item, isFirstPage && styles.disabled)} onClick={handlePrev}> | ||
<Icon icon='chevron-left' /> | ||
</div> | ||
<div className={styles.pages}>{renderPages()}</div> | ||
<div className={cn(styles.item, isLastPage && styles.disabled)} onClick={handleNext}> | ||
<Icon icon='chevron-right' /> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
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,43 @@ | ||
@value colors: "~styles/colors.module.css"; | ||
@value red, tipsColor, nameColor, bgColor from colors; | ||
|
||
.root { | ||
display: flex; | ||
font-size: 0.9em; | ||
} | ||
|
||
.pages { | ||
display: flex; | ||
} | ||
|
||
.item { | ||
padding: 5px 0; | ||
width: 30px; | ||
margin-right: 3px; | ||
border-radius: 3px; | ||
border: 1px solid tipsColor; | ||
color: nameColor; | ||
text-align: center; | ||
cursor: pointer; | ||
|
||
&:hover { | ||
background-color: bgColor; | ||
} | ||
} | ||
|
||
.active { | ||
border: 1px solid red; | ||
background-color: red; | ||
color: #fff; | ||
&:hover { | ||
background-color: red; | ||
} | ||
} | ||
|
||
.disabled { | ||
color: tipsColor; | ||
cursor: default; | ||
&:hover { | ||
background-color: #fff; | ||
} | ||
} |
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,3 @@ | ||
export const PAGE: number = 1 | ||
export const PAGE_SIZE: number = 100 | ||
export const TOTAL: number = 0 |
This file was deleted.
Oops, something went wrong.
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