From 6903978abfa8376f4830369d6018f9413eb2efa2 Mon Sep 17 00:00:00 2001 From: Constance Chen Date: Fri, 11 Feb 2022 13:03:13 -0800 Subject: [PATCH] Update EuiTablePagination to accept number | 'all' instead of 0 for all --- .../src/views/pagination/table_pagination.tsx | 10 ++-- .../table_pagination.test.tsx.snap | 42 ++++++++++++++++ .../table_pagination.test.tsx | 13 +++++ .../table_pagination/table_pagination.tsx | 48 ++++++++----------- 4 files changed, 81 insertions(+), 32 deletions(-) diff --git a/src-docs/src/views/pagination/table_pagination.tsx b/src-docs/src/views/pagination/table_pagination.tsx index dbcbe981c0b..4ae75bd63fb 100644 --- a/src-docs/src/views/pagination/table_pagination.tsx +++ b/src-docs/src/views/pagination/table_pagination.tsx @@ -5,12 +5,14 @@ import { EuiTablePagination } from '../../../../src'; export default () => { const totalEntries = 1250; const [activePage, setActivePage] = useState(0); - const [rowSize, setRowSize] = useState(50); + const [rowSize, setRowSize] = useState(50); const [pageCount, setPageCount] = useState(Math.ceil(totalEntries / 50)); const goToPage = (pageNumber: number) => setActivePage(pageNumber); - const changeItemsPerPage = (pageSize: number) => { - setPageCount(Math.ceil(totalEntries / pageSize)); + const changeItemsPerPage = (pageSize: number | 'all') => { + const pageCount = + pageSize === 'all' ? 1 : Math.ceil(totalEntries / pageSize); + setPageCount(pageCount); setRowSize(pageSize); setActivePage(0); }; @@ -23,7 +25,7 @@ export default () => { onChangePage={goToPage} itemsPerPage={rowSize} onChangeItemsPerPage={changeItemsPerPage} - itemsPerPageOptions={[10, 20, 0]} + itemsPerPageOptions={[10, 20, 'all']} /> ); }; diff --git a/src/components/table/table_pagination/__snapshots__/table_pagination.test.tsx.snap b/src/components/table/table_pagination/__snapshots__/table_pagination.test.tsx.snap index 5ce87f3bc71..f9dda474959 100644 --- a/src/components/table/table_pagination/__snapshots__/table_pagination.test.tsx.snap +++ b/src/components/table/table_pagination/__snapshots__/table_pagination.test.tsx.snap @@ -354,3 +354,45 @@ exports[`EuiTablePagination is rendered when hiding the per page options 1`] = ` `; + +exports[`EuiTablePagination renders a "show all" itemsPerPage option 1`] = ` +
+
+
+
+ +
+
+
+
+
+`; diff --git a/src/components/table/table_pagination/table_pagination.test.tsx b/src/components/table/table_pagination/table_pagination.test.tsx index 63a8e456446..3326f98d8c5 100644 --- a/src/components/table/table_pagination/table_pagination.test.tsx +++ b/src/components/table/table_pagination/table_pagination.test.tsx @@ -37,4 +37,17 @@ describe('EuiTablePagination', () => { expect(component).toMatchSnapshot(); }); + + test('renders a "show all" itemsPerPage option', () => { + const component = render( + + ); + + expect(component).toMatchSnapshot(); + }); }); diff --git a/src/components/table/table_pagination/table_pagination.tsx b/src/components/table/table_pagination/table_pagination.tsx index 1e00881cb0e..b4eaa6f7476 100644 --- a/src/components/table/table_pagination/table_pagination.tsx +++ b/src/components/table/table_pagination/table_pagination.tsx @@ -16,7 +16,7 @@ import { EuiPopover } from '../../popover'; import { EuiI18n } from '../../i18n'; export type PageChangeHandler = EuiPaginationProps['onPageClick']; -export type ItemsPerPageChangeHandler = (pageSize: number) => void; +export type ItemsPerPageChangeHandler = (pageSize: number | 'all') => void; export interface EuiTablePaginationProps extends Omit { @@ -26,14 +26,14 @@ export interface EuiTablePaginationProps showPerPageOptions?: boolean; /** * Current selection for "Rows per page". - * Pass `0` as to display the selected "Show all" option and hide the pagination. + * Pass `'all'` to display the selected "Show all" option and hide the pagination. */ - itemsPerPage?: number; + itemsPerPage?: number | 'all'; /** * Custom array of options for "Rows per page". - * Pass `0` as one of option to create a "Show all" option. + * Pass `'all'` as one of the options to create a "Show all" option. */ - itemsPerPageOptions?: number[]; + itemsPerPageOptions?: Array; /** * Click handler that passes back selected `pageSize` number */ @@ -75,7 +75,7 @@ export const EuiTablePagination: FunctionComponent = ({ data-test-subj="tablePaginationPopoverButton" onClick={onButtonClick} > - {itemsPerPage === 0 ? ( + {itemsPerPage === 'all' ? ( ) : ( <> @@ -90,22 +90,7 @@ export const EuiTablePagination: FunctionComponent = ({ ); const items = itemsPerPageOptions.map((itemsPerPageOption) => { - return itemsPerPageOption === 0 ? ( - { - closePopover(); - onChangeItemsPerPage(itemsPerPageOption); - }} - data-test-subj={`tablePagination-${'all'}-rows`} - > - - - ) : ( + return ( = ({ }} data-test-subj={`tablePagination-${itemsPerPageOption}-rows`} > - + {itemsPerPageOption === 'all' ? ( + + ) : ( + + )} ); }); @@ -149,7 +141,7 @@ export const EuiTablePagination: FunctionComponent = ({ - {itemsPerPage > 0 && ( + {itemsPerPage !== 'all' && (