Commit 6471f316 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Convert PaginationNavigation to TS

parent 55e561f4
...@@ -3,50 +3,49 @@ import { ...@@ -3,50 +3,49 @@ import {
ArrowLeftIcon, ArrowLeftIcon,
ArrowRightIcon, ArrowRightIcon,
} from '@hypothesis/frontend-shared/lib/next'; } from '@hypothesis/frontend-shared/lib/next';
import type { PresentationalProps } from '@hypothesis/frontend-shared/lib/types';
import type { ButtonCommonProps } from '@hypothesis/frontend-shared/lib/components/input/ButtonBase';
import classnames from 'classnames'; import classnames from 'classnames';
import type { JSX } from 'preact';
import { pageNumberOptions } from '../util/pagination'; import { pageNumberOptions } from '../util/pagination';
/** type NavigationButtonProps = PresentationalProps &
* @typedef {import('@hypothesis/frontend-shared/lib/types').PresentationalProps} PresentationalProps ButtonCommonProps &
* @typedef {import('@hypothesis/frontend-shared/lib/components/input/ButtonBase').ButtonCommonProps} ButtonCommonProps Omit<JSX.HTMLAttributes<HTMLButtonElement>, 'icon' | 'size'>;
* @typedef {Omit<import('preact').JSX.HTMLAttributes<HTMLButtonElement>, 'icon'|'size'>} HTMLButtonAttributes
*/
/** function NavigationButton({ ...buttonProps }: NavigationButtonProps) {
* Style a navigation button.
*
* @param {PresentationalProps & ButtonCommonProps & HTMLButtonAttributes} props
*/
function NavigationButton({ ...buttonProps }) {
return ( return (
<ButtonBase <ButtonBase
classes={classnames( classes={classnames(
'px-3.5 py-2.5 gap-x-1 font-semibold', 'px-3.5 py-2.5 gap-x-1 font-semibold',
// These colors are the same as the "dark" variant of IconButton // These colors are the same as the "dark" variant of IconButton
'text-grey-7 bg-grey-2 enabled:hover:text-grey-9 enabled:hover:bg-grey-3 disabled:text-grey-5 aria-pressed:bg-grey-3 aria-expanded:bg-grey-3' 'text-grey-7 bg-grey-2 enabled:hover:text-grey-9 enabled:hover:bg-grey-3',
'disabled:text-grey-5 aria-pressed:bg-grey-3 aria-expanded:bg-grey-3'
)} )}
{...buttonProps} {...buttonProps}
/> />
); );
} }
/** export type PaginationNavigationProps = {
* @typedef PaginationNavigationProps /** 1-indexed page number of currently-visible page of results */
* @prop {number} currentPage - The currently-visible page of results. Pages currentPage: number;
* start at 1 (not 0). onChangePage: (page: number) => void;
* @prop {(page: number) => void} onChangePage - Callback for changing page totalPages: number;
* @prop {number} totalPages };
*/
/** /**
* Render pagination navigation controls, with buttons to go next, previous * Render pagination navigation controls, with buttons to go next, previous
* and nearby pages. Buttons corresponding to nearby pages are shown on wider * and nearby pages. Buttons corresponding to nearby pages are shown on wider
* screens; for narrow screens only Prev and Next buttons are shown. * screens; for narrow screens only Prev and Next buttons are shown.
* *
* @param {PaginationNavigationProps} props
*/ */
function PaginationNavigation({ currentPage, onChangePage, totalPages }) { function PaginationNavigation({
currentPage,
onChangePage,
totalPages,
}: PaginationNavigationProps) {
// Pages are 1-indexed // Pages are 1-indexed
const hasNextPage = currentPage < totalPages; const hasNextPage = currentPage < totalPages;
const hasPreviousPage = currentPage > 1; const hasPreviousPage = currentPage > 1;
...@@ -56,7 +55,7 @@ function PaginationNavigation({ currentPage, onChangePage, totalPages }) { ...@@ -56,7 +55,7 @@ function PaginationNavigation({ currentPage, onChangePage, totalPages }) {
* @param {number} pageNumber * @param {number} pageNumber
* @param {HTMLElement} element * @param {HTMLElement} element
*/ */
const changePageTo = (pageNumber, element) => { const changePageTo = (pageNumber: number, element: HTMLElement) => {
onChangePage(pageNumber); onChangePage(pageNumber);
// Because changing pagination page doesn't reload the page (as it would // Because changing pagination page doesn't reload the page (as it would
// in a "traditional" HTML context), the clicked-upon navigation button // in a "traditional" HTML context), the clicked-upon navigation button
...@@ -75,10 +74,7 @@ function PaginationNavigation({ currentPage, onChangePage, totalPages }) { ...@@ -75,10 +74,7 @@ function PaginationNavigation({ currentPage, onChangePage, totalPages }) {
<NavigationButton <NavigationButton
title="Go to previous page" title="Go to previous page"
onClick={e => onClick={e =>
changePageTo( changePageTo(currentPage - 1, e.target as HTMLElement)
currentPage - 1,
/** @type {HTMLElement} */ (e.target)
)
} }
> >
<ArrowLeftIcon /> <ArrowLeftIcon />
...@@ -112,9 +108,7 @@ function PaginationNavigation({ currentPage, onChangePage, totalPages }) { ...@@ -112,9 +108,7 @@ function PaginationNavigation({ currentPage, onChangePage, totalPages }) {
key={`page-${idx}`} key={`page-${idx}`}
title={`Go to page ${page}`} title={`Go to page ${page}`}
pressed={page === currentPage} pressed={page === currentPage}
onClick={e => onClick={e => changePageTo(page, e.target as HTMLElement)}
changePageTo(page, /** @type {HTMLElement} */ (e.target))
}
> >
{page.toString()} {page.toString()}
</NavigationButton> </NavigationButton>
...@@ -134,10 +128,7 @@ function PaginationNavigation({ currentPage, onChangePage, totalPages }) { ...@@ -134,10 +128,7 @@ function PaginationNavigation({ currentPage, onChangePage, totalPages }) {
<NavigationButton <NavigationButton
title="Go to next page" title="Go to next page"
onClick={e => onClick={e =>
changePageTo( changePageTo(currentPage + 1, e.target as HTMLElement)
currentPage + 1,
/** @type {HTMLElement} */ (e.target)
)
} }
> >
next next
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment