-
Notifications
You must be signed in to change notification settings - Fork 0
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
7 changed files
with
16,964 additions
and
0 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,42 @@ | ||
import { Story, Meta } from '@storybook/react' | ||
import { Pagination, PaginationProps } from 'mibao-ui' | ||
|
||
export default { | ||
component: Pagination, | ||
title: `Components/${Pagination.name}`, | ||
argTypes: { | ||
current: { | ||
type: 'number', | ||
defaultValue: 1, | ||
controls: 'text' | ||
}, | ||
total: { | ||
type: 'number', | ||
defaultValue: 1, | ||
controls: 'text' | ||
}, | ||
onChange: { | ||
action: 'onChange' | ||
} | ||
} | ||
} as Meta<PaginationProps> | ||
|
||
export const ShowAllPages: Story<PaginationProps> = (args) => ( | ||
<Pagination onChange={args.onChange} total={2} current={1} /> | ||
) | ||
|
||
export const ShowFirstPagesAndLastPage: Story<PaginationProps> = (args) => ( | ||
<Pagination onChange={args.onChange} total={100} current={3} /> | ||
) | ||
|
||
export const ShowFirstPageAndLastPages: Story<PaginationProps> = (args) => ( | ||
<Pagination onChange={args.onChange} total={100} current={98} /> | ||
) | ||
|
||
export const ShowEndPagesAndCenterPages: Story<PaginationProps> = (args) => ( | ||
<Pagination onChange={args.onChange} total={100} current={4} /> | ||
) | ||
|
||
export const ControlledPagination: Story<PaginationProps> = (args) => ( | ||
<Pagination {...args} /> | ||
) |
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,32 @@ | ||
// TODO: set color in theme | ||
.container { | ||
display: flex; | ||
width: min-content; | ||
padding: 2px; | ||
button { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
min-width: 55px; | ||
height: 55px; | ||
color: #5065e5; | ||
font-size: 1.125rem; | ||
appearance: none; | ||
background: none; | ||
border: none; | ||
border-radius: 10px; | ||
&[disabled] { | ||
color: #666; | ||
} | ||
&:hover:not([disabled]) { | ||
cursor: pointer; | ||
background-color: #ffffff3a; | ||
} | ||
&[data-active='true'] { | ||
color: #1d1b23; | ||
background-color: #fff; | ||
} | ||
} | ||
background-color: #eee; | ||
border-radius: 10px; | ||
} |
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,12 @@ | ||
import { render } from '@testing-library/react' | ||
|
||
import Pagination from './pagination' | ||
|
||
describe('Pagination', () => { | ||
it('should render successfully', () => { | ||
const { baseElement } = render( | ||
<Pagination current={1} total={100} onChange={() => undefined} /> | ||
) | ||
expect(baseElement).toBeTruthy() | ||
}) | ||
}) |
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,93 @@ | ||
import { useMemo } from 'react' | ||
import { ChevronLeftIcon, ChevronRightIcon } from '@chakra-ui/icons' | ||
import styles from './pagination.module.scss' | ||
|
||
export interface PaginationProps { | ||
current: number | ||
total: number | ||
onChange: (page: number) => void | ||
} | ||
|
||
export const Pagination: React.FC<PaginationProps> = ({ | ||
current, | ||
total, | ||
onChange | ||
}) => { | ||
const pages = useMemo(() => { | ||
if (current <= 0) { | ||
return [] | ||
} | ||
const RANGE = 1 | ||
const COUNT = RANGE * 2 + 1 | ||
let list = new Array(Math.min(total, COUNT)).fill(undefined) | ||
|
||
switch (true) { | ||
case current <= COUNT: { | ||
list = list.map((_, i) => i + 1) | ||
break | ||
} | ||
case current > total - COUNT: { | ||
list = list.map((_, i) => total - COUNT + i + 1) | ||
break | ||
} | ||
default: { | ||
list = list.map((_, i) => current - RANGE + i) | ||
break | ||
} | ||
} | ||
|
||
if (list[0] >= 2) { | ||
list = list[0] === 2 ? [1, ...list] : [1, 'left_ellipsis', ...list] | ||
} | ||
|
||
if (list[list.length - 1] <= total - 1) { | ||
list = | ||
list[list.length - 1] === total - 1 | ||
? [...list, total] | ||
: [...list, 'right_ellipsis', total] | ||
} | ||
|
||
return list | ||
}, [current, total]) | ||
|
||
const handlePageClick = ( | ||
e: React.SyntheticEvent<HTMLDivElement, MouseEvent> | ||
) => { | ||
e.stopPropagation() | ||
e.preventDefault() | ||
const { | ||
dataset: { page } | ||
} = e.target as HTMLButtonElement | ||
if (!page) { | ||
return | ||
} | ||
|
||
const p = +page | ||
if (Number.isNaN(p)) { | ||
return | ||
} | ||
onChange(p) | ||
} | ||
|
||
return ( | ||
<div onClick={handlePageClick} className={styles.container}> | ||
<button disabled={current === 1} data-page={1}> | ||
<ChevronLeftIcon pointerEvents="none" w={20} h={20} /> | ||
</button> | ||
{pages.map((page) => ( | ||
<button | ||
key={page} | ||
data-page={page} | ||
data-active={(page === current).toString()} | ||
disabled={page === current || typeof page === 'string'} | ||
> | ||
{typeof page === 'string' ? '...' : page} | ||
</button> | ||
))} | ||
<button disabled={current === total} data-page={total}> | ||
<ChevronRightIcon pointerEvents="none" w={20} h={20} /> | ||
</button> | ||
</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
Oops, something went wrong.