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

[EuiBasicTable][EuiInMemoryTable][EuiDataGrid] Update to use EuiTablePagination component defaults #6993

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[EuiDataGrid] Convert pagination to use EuiProvider componentDefaults
+ update props documentation
  • Loading branch information
cee-chen committed Aug 1, 2023
commit 6ff1312a12805c43833aed376ae24787d3958317
4 changes: 2 additions & 2 deletions src-docs/src/views/datagrid/_snippets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ renderCustomDataGridBody={({ visibleColumns, visibleRowData, Cell }) => (
)}`,
pagination: `pagination={{
pageIndex: 1,
pageSize: 100,
pageSizeOptions: [50, 100, 200],
pageSize: 100, // If not specified, defaults to EuiTablePagination.itemsPerPage
pageSizeOptions: [50, 100, 200], // If not specified, defaults to EuiTablePagination.itemsPerPageOptions
onChangePage: () => {},
onChangeItemsPerPage: () => {},
}}`,
Expand Down
10 changes: 7 additions & 3 deletions src/components/datagrid/data_grid_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export type CommonGridProps = CommonProps &
*/
inMemory?: EuiDataGridInMemory;
/**
* A #EuiDataGridPagination object. Omit to disable pagination completely.
* A #EuiDataGridPaginationProps object. Omit to disable pagination completely.
*/
pagination?: EuiDataGridPaginationProps;
/**
Expand Down Expand Up @@ -918,12 +918,16 @@ export interface EuiDataGridPaginationProps {
/**
* How many rows should initially be shown per page.
* Pass `0` to display the selected "Show all" option and hide the pagination.
*
* @default 10
*/
pageSize: number;
pageSize?: number;
/**
* An array of page sizes the user can select from.
* Pass `0` as one of the options to create a "Show all" option.
* Leave this prop undefined or use an empty array to hide "Rows per page" select button.
* Pass an empty array to hide "Rows per page" select button.
*
* @default [10, 25, 50]
*/
pageSizeOptions?: number[];
/**
Expand Down
18 changes: 18 additions & 0 deletions src/components/datagrid/utils/data_grid_pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import React from 'react';
import { shallow, mount } from 'enzyme';
import { fireEvent } from '@testing-library/react';
import { render } from '../../../test/rtl';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉


import { DataGridFocusContext } from './focus';
import { mockFocusContext } from './__mocks__/focus_context';
Expand Down Expand Up @@ -157,4 +159,20 @@ describe('EuiDataGridPaginationRenderer', () => {
onChangePage(3);
expect(mockFocusContext.setFocusedCell).toHaveBeenCalledWith([0, 0]);
});

describe('EuiProvider component defaults', () => {
it('falls back to EuiTablePagination defaults if pageSize and pageSizeOptions are undefined', () => {
const { getByText } = render(
<EuiDataGridPaginationRenderer
{...props}
pageSize={undefined}
pageSizeOptions={undefined}
/>
);
fireEvent.click(getByText('Rows per page: 10'));
expect(getByText('10 rows')).toBeTruthy();
expect(getByText('25 rows')).toBeTruthy();
expect(getByText('50 rows')).toBeTruthy();
});
});
});
21 changes: 14 additions & 7 deletions src/components/datagrid/utils/data_grid_pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
*/

import React, { useCallback, useContext } from 'react';

import { useEuiI18n } from '../../i18n'; // Note: this file must be named data_grid_pagination to match i18n tokens
import { EuiTablePagination } from '../../table/table_pagination';
import {
EuiTablePagination,
useEuiTablePaginationDefaults,
} from '../../table/table_pagination';
import {
EuiDataGridPaginationProps,
EuiDataGridPaginationRendererProps,
Expand All @@ -17,14 +21,18 @@ import { DataGridFocusContext } from './focus';

export const EuiDataGridPaginationRenderer = ({
pageIndex,
pageSize,
pageSizeOptions,
pageSize: _pageSize,
pageSizeOptions: _pageSizeOptions,
onChangePage: _onChangePage,
onChangeItemsPerPage,
rowCount,
controls,
'aria-label': ariaLabel,
}: EuiDataGridPaginationRendererProps) => {
const defaults = useEuiTablePaginationDefaults();
const pageSize = _pageSize ?? defaults.itemsPerPage;
const pageSizeOptions = _pageSizeOptions ?? defaults.itemsPerPageOptions;

const detailedPaginationLabel = useEuiI18n(
'euiDataGridPagination.detailedPaginationLabel',
'Pagination for preceding grid: {label}',
Expand All @@ -46,8 +54,7 @@ export const EuiDataGridPaginationRenderer = ({
);

const pageCount = pageSize ? Math.ceil(rowCount / pageSize) : 1;
const minSizeOption =
pageSizeOptions && [...pageSizeOptions].sort((a, b) => a - b)[0];
const minSizeOption = [...pageSizeOptions].sort((a, b) => a - b)[0];

if (rowCount < (minSizeOption || pageSize)) {
/**
Expand All @@ -58,8 +65,8 @@ export const EuiDataGridPaginationRenderer = ({
return null;
}

// hide select rows per page if pageSizeOptions is undefined or an empty array
const hidePerPageOptions = !pageSizeOptions || pageSizeOptions.length === 0;
// Hide select rows per page if pageSizeOptions is an empty array
const hidePerPageOptions = pageSizeOptions.length === 0;

return (
<div className="euiDataGrid__pagination">
Expand Down