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] Move to next-to-last page if all items of last pages are deleted. #3431

Merged
merged 8 commits into from
May 18, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
**Bug Fixes**

- Fixed `EuiSuperSelect` not rendering full width when `isOpen` is `true` ([#3495](https://github.com/elastic/eui/pull/3495))
- Fixed `EuiBasicTable` shows no items if all items of last page is deleted ([#3422](https://github.com/elastic/eui/pull/3422))
ashikmeerankutty marked this conversation as resolved.
Show resolved Hide resolved

**Deprecations**

Expand Down
6 changes: 3 additions & 3 deletions src/components/basic_table/basic_table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ export class EuiBasicTable<T = any> extends Component<
);

const table = this.renderTable();
const paginationBar = this.renderPaginationBar(items.length);
const paginationBar = this.renderPaginationBar();

return (
<div className={classes} {...rest}>
Expand Down Expand Up @@ -1235,9 +1235,9 @@ export class EuiBasicTable<T = any> extends Component<
return profile.align;
}

renderPaginationBar(itemsLength: number) {
renderPaginationBar() {
const { error, pagination, onChange } = this.props;
if (!error && pagination && itemsLength > 0) {
if (!error && pagination && pagination.totalItemCount > 0) {
if (!onChange) {
throw new Error(`The Basic Table is configured with pagination but [onChange] is
not configured. This callback must be implemented to handle pagination changes`);
Expand Down
9 changes: 8 additions & 1 deletion src/components/basic_table/pagination_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import React from 'react';
import React, { useEffect } from 'react';
import { EuiSpacer } from '../spacer';
import { EuiTablePagination } from '../table';
import {
Expand Down Expand Up @@ -52,6 +52,13 @@ export const PaginationBar = ({
? pagination.pageSizeOptions
: defaults.pageSizeOptions;
const pageCount = Math.ceil(pagination.totalItemCount / pagination.pageSize);

useEffect(() => {
if (pageCount < pagination.pageIndex + 1) {
onPageChange(pageCount - 1);
}
}, [pageCount, onPageChange, pagination]);

return (
<div>
<EuiSpacer size="m" />
Expand Down