| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { Pagination, PaginationVariant, Gallery, GalleryItem, Card, CardBody } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
export const PaginationSticky: React.FunctionComponent = () => { |
| 5 |
const [page, setPage] = useState(1); |
| 6 |
const [perPage, setPerPage] = useState(100); |
| 7 |
const [isTopSticky, setIsTopSticky] = useState(true); |
| 8 |
const itemCount = 523; |
| 9 |
|
| 10 |
const onToggleSticky = () => { |
| 11 |
setIsTopSticky((prev) => !prev); |
| 12 |
}; |
| 13 |
|
| 14 |
const onSetPage = (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent, newPage: number) => { |
| 15 |
setPage(newPage); |
| 16 |
}; |
| 17 |
|
| 18 |
const onPerPageSelect = ( |
| 19 |
_event: React.MouseEvent | React.KeyboardEvent | MouseEvent, |
| 20 |
newPerPage: number, |
| 21 |
newPage: number |
| 22 |
) => { |
| 23 |
setPerPage(newPerPage); |
| 24 |
setPage(newPage); |
| 25 |
}; |
| 26 |
|
| 27 |
const buildCards = () => { |
| 28 |
const numberOfCards = (page - 1) * perPage + perPage - 1 >= itemCount ? itemCount - (page - 1) * perPage : perPage; |
| 29 |
|
| 30 |
return Array.from({ length: numberOfCards }).map((_value, index) => ( |
| 31 |
<GalleryItem key={index}> |
| 32 |
<Card> |
| 33 |
<CardBody>This is a card</CardBody> |
| 34 |
</Card> |
| 35 |
</GalleryItem> |
| 36 |
)); |
| 37 |
}; |
| 38 |
|
| 39 |
return isTopSticky ? ( |
| 40 |
<Fragment> |
| 41 |
<Pagination |
| 42 |
itemCount={itemCount} |
| 43 |
perPage={perPage} |
| 44 |
page={page} |
| 45 |
onSetPage={onSetPage} |
| 46 |
widgetId="pagination-options-menu-top" |
| 47 |
onPerPageSelect={onPerPageSelect} |
| 48 |
isSticky |
| 49 |
> |
| 50 |
<button onClick={onToggleSticky}>Toggle to bottom position</button> |
| 51 |
</Pagination> |
| 52 |
<Gallery hasGutter>{buildCards()}</Gallery> |
| 53 |
</Fragment> |
| 54 |
) : ( |
| 55 |
<Fragment> |
| 56 |
<Gallery hasGutter>{buildCards()}</Gallery> |
| 57 |
<Pagination |
| 58 |
itemCount={itemCount} |
| 59 |
perPage={perPage} |
| 60 |
page={page} |
| 61 |
onSetPage={onSetPage} |
| 62 |
widgetId="pagination-options-menu-top" |
| 63 |
onPerPageSelect={onPerPageSelect} |
| 64 |
isSticky |
| 65 |
variant={PaginationVariant.bottom} |
| 66 |
> |
| 67 |
<button onClick={onToggleSticky}>Toggle to top position</button> |
| 68 |
</Pagination> |
| 69 |
</Fragment> |
| 70 |
); |
| 71 |
}; |