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

[Feature] Add editable page numbers #387

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
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
47 changes: 46 additions & 1 deletion components/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HStack, Text, IconButton } from '@chakra-ui/react'; // Chakra UI
import { HStack, Text, IconButton, Input } from '@chakra-ui/react'; // Chakra UI
import { ChevronLeftIcon, ChevronRightIcon } from '@chakra-ui/icons'; // Chakra UI Icons
import { useState } from 'react';

type Props = {
readonly pageSize: number; // Number of items per page
Expand All @@ -22,12 +23,15 @@ export default function Pagination(props: Props) {
const lowerBound = totalCount === 0 ? 0 : pageNumber * pageSize + 1; // Lower bound of current page (item #)
const upperBound = isLastPage ? totalCount : (pageNumber + 1) * pageSize; // Upper bound of current page (item #)

const [inputValue, setInputValue] = useState<string | number>(pageNumber + 1);

/**
* Go to the next page if possible, and invoke callback if defined
*/
const goToNextPage = () => {
if (!isLastPage) {
onPageChange(pageNumber + 1);
setInputValue(pageNumber + 2);
}
};

Expand All @@ -37,6 +41,34 @@ export default function Pagination(props: Props) {
const goToPreviousPage = () => {
if (!isFirstPage) {
onPageChange(pageNumber - 1);
setInputValue(pageNumber);
}
};

/**
* Handle direct page number input
*/
const handlePageInput = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
if (value === '') {
setInputValue(value); // Allow empty string for cleared input
} else {
const numericValue = Number(value);
if (!isNaN(numericValue) && numericValue > 0 && numericValue <= totalPages) {
setInputValue(numericValue);
}
}
};

/**
* Navigate to the page number entered in the input field
*/
const goToPage = () => {
const targetPage = Number(inputValue) - 1;
if (!isNaN(targetPage) && targetPage >= 0 && targetPage < totalPages) {
onPageChange(targetPage);
} else {
setInputValue(pageNumber + 1); // Reset input to the current page if invalid
}
};

Expand Down Expand Up @@ -69,6 +101,19 @@ export default function Pagination(props: Props) {
disabled={isFirstPage}
onClick={goToPreviousPage}
/>
<Input
value={inputValue}
width="50px"
height="24px"
textAlign="center"
onChange={handlePageInput}
onBlur={goToPage}
onKeyPress={e => {
if (e.key === 'Enter') {
goToPage();
}
}}
/>
<IconButton
icon={<ChevronRightIcon />}
height="24px"
Expand Down
Loading