Skip to content

Commit

Permalink
feat: pagination custom page (#792)
Browse files Browse the repository at this point in the history
* feat: pagination custom page

* fix: add pagination custom page to transactions table
  • Loading branch information
leduyhien152 committed Mar 1, 2024
1 parent 2161237 commit 511686b
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-dolphins-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mochi-ui/pagination": minor
---

Add custom page input
1 change: 1 addition & 0 deletions apps/mochi-web/components/Profile/TransactionSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const TransactionOverviewSection = () => {
onItemPerPageChange: setSize,
onPageChange: setPage,
className: 'px-4',
allowCustomPage: true,
},
}}
onRow={(tx) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const TransactionSection = () => {
totalItems: total,
onItemPerPageChange: setSize,
onPageChange: setPage,
allowCustomPage: true,
},
}}
className="px-6"
Expand Down
4 changes: 3 additions & 1 deletion packages/components/pagination/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"dependencies": {
"@mochi-ui/icons": "workspace:*",
"@mochi-ui/select": "workspace:*",
"@mochi-ui/theme": "workspace:*"
"@mochi-ui/theme": "workspace:*",
"@mochi-ui/separator": "workspace:*",
"@mochi-ui/input": "workspace:*"
},
"clean-package": "../../../clean-package.config.json"
}
35 changes: 35 additions & 0 deletions packages/components/pagination/src/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
SelectTrigger,
SelectValue,
} from '@mochi-ui/select'
import { Separator } from '@mochi-ui/separator'
import { TextFieldInput } from '@mochi-ui/input'
import { formatNumber } from './utils'

const {
Expand All @@ -34,6 +36,7 @@ interface PaginationProps {
recordName?: string
hideOnSinglePage?: boolean
allowCustomItemsPerPage?: boolean
allowCustomPage?: boolean
}

function PageButton({
Expand Down Expand Up @@ -70,14 +73,17 @@ export default function Pagination({
recordName = 'members',
hideOnSinglePage = false,
allowCustomItemsPerPage = true,
allowCustomPage = false,
}: PaginationProps) {
const [currentPage, setCurrentPage] = useState(page)
const [currentItemPerPage, setCurrentItemPerPage] = useState(initItemsPerPage)
const [customPage, setCustomPage] = useState('')

useEffect(() => {
if (onPageChange) {
onPageChange(currentPage)
}
setCustomPage('')
}, [currentPage, onPageChange])

useEffect(() => {
Expand All @@ -95,6 +101,17 @@ export default function Pagination({

const totalPages = Math.ceil(totalItems / currentItemPerPage) || initTotalPage

const onCustomPageChange = (value: string) => {
if (!value) return
const page = Number(value)
if (page >= 1 && page <= totalPages && Number.isInteger(page)) {
setCurrentPage(page)
} else if (page > totalPages && Number.isInteger(page)) {
setCurrentPage(totalPages)
}
setCustomPage('')
}

const renderPagination = () => {
const pages = []

Expand Down Expand Up @@ -281,6 +298,24 @@ export default function Pagination({
<div>
{recordName} of {formatNumber(totalItems)}
</div>
{allowCustomPage && (
<div className="flex items-center h-4 space-x-2">
<Separator orientation="vertical" />
<span>Go to</span>
<TextFieldInput
className="h-[34px] w-14 text-center"
value={customPage}
onChange={(e) => setCustomPage(e.target.value)}
onBlur={(e) => onCustomPageChange(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
onCustomPageChange(e.currentTarget.value)
}
}}
/>
<span>Page</span>
</div>
)}
</div>
)}

Expand Down
24 changes: 24 additions & 0 deletions packages/components/pagination/stories/pagination.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,27 @@ export const Default: Story = {
)
},
}

export const AllowCustomPage: Story = {
render: () => {
// eslint-disable-next-line react-hooks/rules-of-hooks -- we're in a component
const [currentPage, setCurrentPage] = useState(1)
// eslint-disable-next-line react-hooks/rules-of-hooks -- we're in a component
const [itemPerPage, setItemPerPage] = useState(15)

return (
<div className="md:min-w-[32rem] space-y-4">
<Pagination totalItems={25} allowCustomPage />
<Pagination
initItemsPerPage={itemPerPage}
initalPage={currentPage}
onItemPerPageChange={setItemPerPage}
onPageChange={setCurrentPage}
totalItems={100000}
totalPages={Math.ceil(100000 / itemPerPage)}
allowCustomPage
/>
</div>
)
},
}
12 changes: 9 additions & 3 deletions packages/theme/src/components/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ const paginationEllipsisButtonClsx = ({
const paginationWrapperClsx = ({
className = '',
}: { className?: string } = {}) =>
clsx('flex items-center justify-between w-full sm:space-x-6', className)
clsx(
'flex items-center justify-between w-full sm:gap-x-6 sm:gap-y-4 flex-wrap',
className,
)

const paginationAmountPerPageWrapperClsx = ({
className = '',
}: { className?: string } = {}) =>
clsx('hidden sm:inline-flex justify-start items-center space-x-2', className)
clsx(
'hidden sm:inline-flex justify-start items-center gap-x-2 gap-y-4 flex-wrap',
className,
)

const paginationPageNavigateButtonGroupClsx = ({
className = '',
Expand All @@ -42,7 +48,7 @@ const paginationNavigationClsx = ({
className = '',
}: { className?: string } = {}) =>
clsx(
'flex-1 flex justify-center sm:justify-end items-center space-x-2',
'flex items-center justify-center space-x-2 flex-1 sm:flex-none',
className,
)

Expand Down
48 changes: 27 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 511686b

Please sign in to comment.