From 69ddbea8bad5bb9e3a541050503e83df73b85d32 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Fri, 19 Apr 2024 20:18:10 +0200 Subject: [PATCH] chore: fix types for pagination navigation (#10263) Signed-off-by: Jon Koops --- .../src/components/Pagination/Navigation.tsx | 24 +++++++++---------- .../src/components/Pagination/Pagination.tsx | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/react-core/src/components/Pagination/Navigation.tsx b/packages/react-core/src/components/Pagination/Navigation.tsx index 4072166aac3..1ab46102fb9 100644 --- a/packages/react-core/src/components/Pagination/Navigation.tsx +++ b/packages/react-core/src/components/Pagination/Navigation.tsx @@ -29,7 +29,7 @@ export interface NavigationProps extends React.HTMLProps { /** Label for the English word "of". */ ofWord?: string; /** The number of the current page. */ - page: string | number; + page: number; /** The title of a page displayed beside the page number. */ pagesTitle?: string; /** The title of a page displayed beside the page number (the plural form). */ @@ -55,13 +55,13 @@ export interface NavigationProps extends React.HTMLProps { /** Function called when user clicks to navigate to previous page. */ onPreviousClick?: (event: React.SyntheticEvent, page: number) => void; /** Function called when user inputs page number. */ - onPageInput?: (event: React.SyntheticEvent, page: number) => void; + onPageInput?: (event: React.KeyboardEvent, page: number) => void; /** Function called when page is changed. */ onSetPage: OnSetPage; } export interface NavigationState { - userInputPage?: React.ReactText; + userInputPage?: number | string; } class Navigation extends React.Component { @@ -93,7 +93,7 @@ class Navigation extends React.Component { onPageInput: () => undefined as any }; - private static parseInteger(input: React.ReactText, lastPage: number): number { + private static parseInteger(input: number | string, lastPage: number): number { // eslint-disable-next-line radix let inputPage = Number.parseInt(input as string, 10); if (!Number.isNaN(inputPage)) { @@ -105,14 +105,14 @@ class Navigation extends React.Component { private onChange(event: React.FormEvent, lastPage: number): void { const inputPage = Navigation.parseInteger(event.currentTarget.value, lastPage); - this.setState({ userInputPage: Number.isNaN(inputPage as number) ? event.currentTarget.value : inputPage }); + this.setState({ userInputPage: Number.isNaN(inputPage) ? event.currentTarget.value : inputPage }); } private onKeyDown( event: React.KeyboardEvent, - page: number | string, + page: number, lastPage: number, - onPageInput: (event: React.SyntheticEvent, page: number) => void + onPageInput: (event: React.KeyboardEvent, page: number) => void ): void { const allowedKeys = [ 'Tab', @@ -126,9 +126,9 @@ class Navigation extends React.Component { 'ArrowDown' ]; if (event.key === KeyTypes.Enter) { - const inputPage = Navigation.parseInteger(this.state.userInputPage, lastPage) as number; - onPageInput(event, Number.isNaN(inputPage) ? (page as number) : inputPage); - this.handleNewPage(event, Number.isNaN(inputPage) ? (page as number) : inputPage); + const inputPage = Navigation.parseInteger(this.state.userInputPage, lastPage); + onPageInput(event, Number.isNaN(inputPage) ? page : inputPage); + this.handleNewPage(event, Number.isNaN(inputPage) ? page : inputPage); } else if (!/^\d*$/.test(event.key) && !allowedKeys.includes(event.key)) { event.preventDefault(); } @@ -206,7 +206,7 @@ class Navigation extends React.Component { isDisabled={isDisabled || page === firstPage || page === 0} data-action="previous" onClick={(event) => { - const newPage = (page as number) - 1 >= 1 ? (page as number) - 1 : 1; + const newPage = page - 1 >= 1 ? page - 1 : 1; onPreviousClick(event, newPage); this.handleNewPage(event, newPage); this.setState({ userInputPage: newPage }); @@ -244,7 +244,7 @@ class Navigation extends React.Component { aria-label={toNextPageAriaLabel} data-action="next" onClick={(event) => { - const newPage = (page as number) + 1 <= lastPage ? (page as number) + 1 : lastPage; + const newPage = page + 1 <= lastPage ? page + 1 : lastPage; onNextClick(event, newPage); this.handleNewPage(event, newPage); this.setState({ userInputPage: newPage }); diff --git a/packages/react-core/src/components/Pagination/Pagination.tsx b/packages/react-core/src/components/Pagination/Pagination.tsx index dffd2052c0e..f4c412da26e 100644 --- a/packages/react-core/src/components/Pagination/Pagination.tsx +++ b/packages/react-core/src/components/Pagination/Pagination.tsx @@ -148,7 +148,7 @@ export interface PaginationProps extends React.HTMLProps, OUIAPr /** Function called when user clicks on navigate to next page. */ onNextClick?: (event: React.SyntheticEvent, page: number) => void; /** Function called when user inputs page number. */ - onPageInput?: (event: React.SyntheticEvent, page: number) => void; + onPageInput?: (event: React.KeyboardEvent, page: number) => void; /** Function called when user selects number of items per page. */ onPerPageSelect?: OnPerPageSelect; /** Function called when user clicks on navigate to previous page. */