Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add align property to Pagination #106

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions src/components/navigation/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ import { RefreshButtonContainer } from './PaginationStyles'
import RefreshIcon from '@mui/icons-material/Refresh'
import { PaginationProps, DisplayedRows } from './types'
import IconButton from '../../buttons/IconButton'
import { T, always, cond, equals } from 'ramda'

const displayedRows =
(rowsOfText: string) =>
({ from, to, count }: DisplayedRows) => {
return `${from}-${to} ${rowsOfText} ${count}`
}

const contentAlignment = cond([
[equals('left'), always('flex-start')],
[equals('right'), always('flex-end')],
[equals('center'), always('center')],
[T, always('flex-end')]
])

/**
* The Pagination component was designed to paginate a list of arbitrary items when infinite loading isn't used. It's preferred in contexts where SEO is important, for instance, a blog.
*
Expand All @@ -29,6 +37,7 @@ const Pagination: React.FC<PaginationProps> = ({
onRefresh,
rowsOfText = 'of',
rowsPerPageOptions = [10, 25, 50, 100],
align,
...rest
}) => {
const handlePageChange = useCallback(
Expand All @@ -50,25 +59,25 @@ const Pagination: React.FC<PaginationProps> = ({
const handleRefresh = useCallback(() => onRefresh && onRefresh(), [onRefresh])

return (
<Grid container alignItems="center" justifyContent="flex-end">
<Grid container alignItems="center" justifyContent={contentAlignment(align)}>
{!loading && (
<TablePagination
component="div"
count={count}
page={page}
onPageChange={handlePageChange}
rowsPerPage={pageSize}
onRowsPerPageChange={handleRowsPerPageChange}
labelRowsPerPage={rowsPerPageText}
labelDisplayedRows={displayedRows(rowsOfText)}
rowsPerPageOptions={rowsPerPageOptions}
{...rest}
/>
<TablePagination
component="div"
count={count}
page={page}
onPageChange={handlePageChange}
rowsPerPage={pageSize}
onRowsPerPageChange={handleRowsPerPageChange}
labelRowsPerPage={rowsPerPageText}
labelDisplayedRows={displayedRows(rowsOfText)}
rowsPerPageOptions={rowsPerPageOptions}
{...rest}
/>
)}
{onRefresh && (
<RefreshButtonContainer>
<IconButton onClick={handleRefresh} color="default" variant="text" disabled={loading}>
<RefreshIcon/>
<RefreshIcon />
</IconButton>
</RefreshButtonContainer>
)}
Expand Down Expand Up @@ -135,7 +144,12 @@ Pagination.propTypes = {
value: PropTypes.number.isRequired
})
]).isRequired
)
),
/**
* Align container.
* @default 'right'
*/
align: PropTypes.oneOf(['left', 'right', 'center'])
}

export default Pagination
2 changes: 1 addition & 1 deletion src/components/surfaces/Card/CardActions/CardActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CardActions.propTypes = {
*/
filled: PropTypes.bool,
/**
* Align actions to left or right.
* Align actions.
* @default 'left'
*/
align: PropTypes.oneOf(['left', 'right', 'center'])
Expand Down
29 changes: 26 additions & 3 deletions src/stories/surfaces/Card/BasicCardsPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import React, { useState, useCallback } from 'react'
import { Grid } from '@mui/material'
import { Button, Typography, Card } from 'components'
import { Button, Typography, Card, Pagination } from 'components'
import robot from '../../assets/img/robot.png'
import spaceship from '../../assets/img/spaceship.png'

Expand All @@ -13,6 +13,18 @@ const LearnMoreButton = () => {
}

const BasicCardsPreview = () => {
const [page, setPage] = useState<number>(0)
const [pageSize, setPageSize] = useState<number>(10)

const handleChangePage = useCallback((value: number) => {
setPage(value)
}, [])

const handleChangeRowsPerPage = useCallback((value: number) => {
setPageSize(value)
setPage(0)
}, [])

return (
<Grid container spacing={2} justifyContent="center">
<Grid item xs={10}>
Expand Down Expand Up @@ -52,7 +64,18 @@ const BasicCardsPreview = () => {
</Card>
</Grid>
<Grid item xs={10}>
<Card footer={<LearnMoreButton />} footerProps={{ align: 'center' }}>
<Card
footer={
<Pagination
count={10}
page={page}
onPageChange={handleChangePage}
pageSize={pageSize}
onRowsPerPageChange={handleChangeRowsPerPage}
align="center"
/>
}
>
<Grid container spacing={2}>
<Grid item xs={8}>
<Typography gutterBottom variant="h5">
Expand Down
Loading