Skip to content

Commit

Permalink
Fix number input in pagination control
Browse files Browse the repository at this point in the history
Cleans and clamps the page selected
Makes the control uncontrolled to make it easier to accept intermediate values like
empty strings
  • Loading branch information
thostetler committed Mar 17, 2024
1 parent 47081e1 commit c5c7cdc
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/components/ResultList/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { APP_DEFAULTS } from '@config';
import { NumPerPageType, SafeSearchUrlParams } from '@types';
import { makeSearchParams, stringifySearchParams } from '@utils';
import { useRouter } from 'next/router';
import { curryN } from 'ramda';
import { clamp, curryN } from 'ramda';
import { Dispatch, FC, KeyboardEventHandler, ReactElement, useCallback, useMemo, useRef, useState } from 'react';
import { MenuPlacement } from 'react-select';
import { calculatePagination, PaginationAction, PaginationResult } from './usePagination';
Expand Down Expand Up @@ -266,6 +266,8 @@ const PaginationButton: FC<{ page: number; noLinks: boolean; onlyUpdatePageParam
);
};

const cleanPage = (page: number) => (Number.isNaN(page) ? 1 : page);
const clampPage = (page: number, totalPages: number) => clamp(1, totalPages - 1, page);
/**
* Popover for manually selecting a page
*/
Expand All @@ -289,12 +291,15 @@ const ManualPageSelect = ({
const open = () => setIsOpen(!isOpen);
const close = () => setIsOpen(false);

const handleChange = (_: string, page: number) => {
setPage(Number.isNaN(page) ? 1 : page);
};
const handleChange = useCallback(
(_: string, page: number) => {
setPage(clampPage(cleanPage(page), totalPages));
},
[totalPages],
);

// submit the change to page
const handleSubmit = () => {
const handleSubmit = useCallback(() => {
if (page !== currentPage) {
if (!skipRouting) {
void router.push({
Expand All @@ -311,7 +316,7 @@ const ManualPageSelect = ({
}
}
close();
};
}, [page, currentPage, skipRouting, dispatch, onPageSelect]);

// on enter, submit the change
const handleKeyDown: KeyboardEventHandler<HTMLDivElement> = (e) => {
Expand Down Expand Up @@ -352,7 +357,6 @@ const ManualPageSelect = ({
defaultValue={currentPage}
min={1}
max={totalPages}
value={page}
onChange={handleChange}
onKeyDown={handleKeyDown}
>
Expand All @@ -362,7 +366,7 @@ const ManualPageSelect = ({
<NumberDecrementStepper />
</NumberInputStepper>
</NumberInput>
<Button onClick={handleSubmit}>Goto Page {page}</Button>
<Button onClick={handleSubmit}>Goto Page {page.toLocaleString()}</Button>
</Stack>
</PopoverBody>
</PopoverContent>
Expand Down

0 comments on commit c5c7cdc

Please sign in to comment.