From 045cc713b08cccfe209d9406071fa4a30c4b520a Mon Sep 17 00:00:00 2001 From: Mike Turley Date: Thu, 14 Dec 2023 20:06:12 -0500 Subject: [PATCH] :bug: Fix pagination effects - for client tables, use `filteredItems` instead of `items` length for `totalItemCount` (#1625) Made the same fix in the new react-table-batteries code as well. Note to self: Perhaps we should rename the `totalItemCount` to `filteredItemCount` in the new version, since it really should be the count of all items just before pagination (after filtering), not the total number of items in the unfiltered collection. For client-side tables, the `totalItemCount` is derived internally inside `getLocalTableControlDerivedState`, and it was incorrectly using `items.length`. Changing that to `filteredItems.length` makes the logic in `usePaginationEffects` behave as intended, which means if the user ends up on an invalid pagination page (e.g. they are on page 2 and they apply a filter that has results on one page), it will kick them back to the last valid page. cc @ibolton336 Signed-off-by: Mike Turley Co-authored-by: Ian Bolton --- .../hooks/table-controls/getLocalTableControlDerivedState.ts | 2 +- client/src/app/hooks/table-controls/types.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/app/hooks/table-controls/getLocalTableControlDerivedState.ts b/client/src/app/hooks/table-controls/getLocalTableControlDerivedState.ts index 9128b1d598..ec98ee9359 100644 --- a/client/src/app/hooks/table-controls/getLocalTableControlDerivedState.ts +++ b/client/src/app/hooks/table-controls/getLocalTableControlDerivedState.ts @@ -48,7 +48,7 @@ export const getLocalTableControlDerivedState = < items: sortedItems, }); return { - totalItemCount: items.length, + totalItemCount: filteredItems.length, currentPageItems: isPaginationEnabled ? currentPageItems : sortedItems, }; }; diff --git a/client/src/app/hooks/table-controls/types.ts b/client/src/app/hooks/table-controls/types.ts index 256166ac2e..a96829c6a1 100644 --- a/client/src/app/hooks/table-controls/types.ts +++ b/client/src/app/hooks/table-controls/types.ts @@ -231,7 +231,7 @@ export type ITableControlDerivedState = { */ currentPageItems: TItem[]; /** - * The total number of items in the entire un-filtered, un-paginated table (the size of the entire API collection being tabulated). + * The total number of items after filtering but before pagination. */ totalItemCount: number; };