forked from medly/medly-components
-
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.
Merge pull request medly#169 from medly/feature-pagination
feat(core): update pagination component as per new design
- Loading branch information
Showing
23 changed files
with
1,012 additions
and
820 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
23 changes: 5 additions & 18 deletions
23
packages/core/src/components/Pagination/Pagination.stories.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 |
---|---|---|
@@ -1,20 +1,7 @@ | ||
import { boolean, number } from '@storybook/addon-knobs'; | ||
import React, { useState } from 'react'; | ||
import { defaultTheme, PaginationTheme } from '@medly-components/theme/src'; | ||
import React from 'react'; | ||
|
||
import { Pagination } from './Pagination'; | ||
|
||
export const Basic = () => { | ||
const [activePage, setActivePage] = useState(1); | ||
|
||
return ( | ||
<Pagination | ||
activePage={activePage} | ||
totalItems={number('Total Items', 1000)} | ||
itemsPerPage={number('Items per page', 20)} | ||
pageRangeDisplayed={number('Page Range Displayed', 5)} | ||
hideFirstLastLinks={boolean('Hide First Last Links', false)} | ||
hidePrevNextLinks={boolean('Hide Prev Next Links', false)} | ||
onPageClick={setActivePage} | ||
/> | ||
); | ||
export const ThemeInterface: React.FC<PaginationTheme> = () => null; | ||
ThemeInterface.defaultProps = { | ||
...defaultTheme.pagination | ||
}; |
86 changes: 86 additions & 0 deletions
86
packages/core/src/components/Pagination/Pagination.styled.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 { ChevronLeftIcon, ChevronRightIcon, SvgIcon } from '@medly-components/icons'; | ||
import { css, styled } from '@medly-components/utils'; | ||
import List from '../List'; | ||
|
||
export const ListWrapper = styled(List)` | ||
& > li { | ||
margin: 0; | ||
} | ||
`; | ||
|
||
export const getPageNumberButtonStyleByState = ( | ||
itemKey: 'pageNumber' | 'overlayPageNumber', | ||
state: 'default' | 'active' | 'pressed' | 'hovered' | ||
) => css` | ||
color: ${({ theme: { pagination } }) => pagination[itemKey].color[state]}; | ||
background: ${({ theme: { pagination } }) => pagination[itemKey].bgColor[state]}; | ||
`; | ||
|
||
const getPageNavButtonStyleByState = (state: 'default' | 'pressed' | 'hovered' | 'disabled') => css` | ||
background: ${({ theme: { pagination } }) => pagination.pageNav.bgColor[state]}; | ||
${SvgIcon} { | ||
* { | ||
fill: ${({ theme: { pagination } }) => pagination.pageNav.color[state]}; | ||
} | ||
} | ||
`; | ||
|
||
const pageNavButtonState = css` | ||
&:hover { | ||
${getPageNavButtonStyleByState('hovered')} | ||
} | ||
&:active { | ||
${getPageNavButtonStyleByState('pressed')} | ||
} | ||
`; | ||
|
||
export const BaseButton = styled.button` | ||
height: 4rem; | ||
width: 4rem; | ||
display: block; | ||
border-radius: 50%; | ||
user-select: none; | ||
border: none; | ||
padding: 0; | ||
text-align: center; | ||
transition: all 100ms ease-out; | ||
${getPageNumberButtonStyleByState('pageNumber', 'default')} | ||
&:hover { | ||
cursor: pointer; | ||
} | ||
&:focus { | ||
outline: none; | ||
} | ||
&:disabled { | ||
cursor: not-allowed; | ||
} | ||
`; | ||
export const PageNumberButton = styled(BaseButton)<{ isActive?: boolean }>` | ||
margin: 4px; | ||
height: 3.2rem; | ||
width: 3.2rem; | ||
${props => getPageNumberButtonStyleByState('pageNumber', props.isActive ? 'active' : 'default')}; | ||
&:hover { | ||
${props => !props.isActive && getPageNumberButtonStyleByState('pageNumber', 'hovered')}; | ||
} | ||
&:active { | ||
font-weight: ${({ theme }) => theme.font.weights.Strong}; | ||
${getPageNumberButtonStyleByState('pageNumber', 'pressed')} | ||
} | ||
`; | ||
|
||
export const PageNavButton = styled(BaseButton)<{ disabled: boolean }>` | ||
& > ${ChevronLeftIcon.Style}, ${ChevronRightIcon.Style} { | ||
margin: 4px; | ||
padding: 4px; | ||
} | ||
${getPageNavButtonStyleByState('default')} | ||
${props => (props.disabled ? getPageNavButtonStyleByState('disabled') : pageNavButtonState)}; | ||
`; |
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 |
---|---|---|
@@ -1,100 +1,71 @@ | ||
import { ChevronLeftIcon, ChevronRightIcon } from '@medly-components/icons'; | ||
import { WithStyle } from '@medly-components/utils'; | ||
import React, { FC, useMemo } from 'react'; | ||
import Button from '../Button'; | ||
import List from '../List'; | ||
import Popover from '../Popover'; | ||
import Text from '../Text'; | ||
import { paginator } from './helper'; | ||
import { ListWrapper, PageNavButton, PageNumberButton } from './Pagination.styled'; | ||
import PaginationPopup from './PaginationPopup'; | ||
import { PaginationProps } from './types'; | ||
|
||
export const Pagination: FC<PaginationProps> & WithStyle = React.memo( | ||
React.forwardRef((props, ref) => { | ||
const links = [], | ||
{ | ||
hideFirstLastLinks, | ||
hidePrevNextLinks, | ||
activePage, | ||
itemsPerPage, | ||
totalItems, | ||
pageRangeDisplayed, | ||
onPageClick, | ||
...restProps | ||
} = props, | ||
pagesConfig = useMemo(() => paginator(totalItems, activePage, itemsPerPage, pageRangeDisplayed), [ | ||
{ hidePrevNextLinks, activePage, itemsPerPage, totalItems, onPageClick, ...restProps } = props, | ||
{ currentPage, linkItems, totalPages } = useMemo(() => paginator(totalItems, activePage, itemsPerPage), [ | ||
totalItems, | ||
activePage, | ||
itemsPerPage, | ||
pageRangeDisplayed | ||
itemsPerPage | ||
]); | ||
|
||
const onClickHandler = (page: number) => () => { | ||
onPageClick(page); | ||
}; | ||
const onClickHandler = (page: number | '...') => () => page !== '...' && onPageClick(page); | ||
|
||
for (let i = pagesConfig.startPage; i <= pagesConfig.endPage; i++) { | ||
links.push( | ||
<Button size="S" key={i} onClick={onClickHandler(i)} variant={i === pagesConfig.currentPage ? 'solid' : 'outlined'}> | ||
{i} | ||
</Button> | ||
); | ||
for (let i = 0; i < linkItems.length; i++) { | ||
if (linkItems[i] === '...') | ||
links.push( | ||
<Popover interactionType="click"> | ||
<PaginationPopup | ||
prevPageNumber={linkItems[i - 1] as number} | ||
nextPageNumber={linkItems[i + 1] as number} | ||
onClickHandler={onPageClick} | ||
></PaginationPopup> | ||
</Popover> | ||
); | ||
else | ||
links.push( | ||
<PageNumberButton key={i} onClick={onClickHandler(linkItems[i])} isActive={linkItems[i] === currentPage}> | ||
<Text textVariant="h5" textAlign="center" textWeight={linkItems[i] === currentPage ? 'Strong' : 'Medium'}> | ||
{linkItems[i]} | ||
</Text> | ||
</PageNumberButton> | ||
); | ||
} | ||
|
||
if (!hidePrevNextLinks) { | ||
links.unshift( | ||
<Button | ||
size="S" | ||
key="prev" | ||
disabled={pagesConfig.currentPage < 2} | ||
onClick={onClickHandler(pagesConfig.currentPage - 1)} | ||
variant="outlined" | ||
> | ||
Prev | ||
</Button> | ||
); | ||
links.push( | ||
<Button | ||
size="S" | ||
key="next" | ||
disabled={pagesConfig.currentPage === pagesConfig.totalPages} | ||
onClick={onClickHandler(pagesConfig.currentPage + 1)} | ||
variant="outlined" | ||
> | ||
Next | ||
</Button> | ||
); | ||
} | ||
|
||
if (!hideFirstLastLinks) { | ||
links.unshift( | ||
<Button size="S" key="first" disabled={pagesConfig.currentPage < 2} onClick={onClickHandler(1)} variant="outlined"> | ||
First | ||
</Button> | ||
<PageNavButton key="first" disabled={currentPage < 2} onClick={onClickHandler(currentPage - 1)}> | ||
<ChevronLeftIcon size="M" /> | ||
</PageNavButton> | ||
); | ||
links.push( | ||
<Button | ||
size="S" | ||
key="last" | ||
disabled={pagesConfig.currentPage === pagesConfig.totalPages} | ||
onClick={onClickHandler(pagesConfig.totalPages)} | ||
variant="outlined" | ||
> | ||
Last | ||
</Button> | ||
<PageNavButton key="last" disabled={currentPage === totalPages} onClick={onClickHandler(currentPage + 1)}> | ||
<ChevronRightIcon size="M" /> | ||
</PageNavButton> | ||
); | ||
} | ||
|
||
return ( | ||
<List ref={ref} variant="horizontal" {...restProps}> | ||
<ListWrapper ref={ref} variant="horizontal" {...restProps}> | ||
{links} | ||
</List> | ||
</ListWrapper> | ||
); | ||
}) | ||
); | ||
|
||
Pagination.displayName = 'Pagination'; | ||
Pagination.Style = List.Style; | ||
Pagination.Style = ListWrapper.Style; | ||
Pagination.defaultProps = { | ||
activePage: 1, | ||
itemsPerPage: 20, | ||
pageRangeDisplayed: 5, | ||
hideFirstLastLinks: false, | ||
hidePrevNextLinks: false | ||
}; |
Oops, something went wrong.