Skip to content

Commit

Permalink
Fix row selection on paginated pages (#5706)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tymek authored Dec 21, 2023
1 parent 4e56d1d commit 9b79810
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { TableEmptyState } from './TableEmptyState/TableEmptyState';
import { useRowActions } from './hooks/useRowActions';
import { useUiFlag } from 'hooks/useUiFlag';
import { FeatureTagCell } from 'component/common/Table/cells/FeatureTagCell/FeatureTagCell';
import { useSelectedData } from './hooks/useSelectedData';

interface IPaginatedProjectFeatureTogglesProps {
environments: IProject['environments'];
Expand Down Expand Up @@ -372,6 +373,8 @@ export const PaginatedProjectFeatureToggles = ({
[columnVisibility, setTableState],
);

const selectedData = useSelectedData(features, rowSelection);

return (
<>
<PageContent
Expand Down Expand Up @@ -482,10 +485,10 @@ export const PaginatedProjectFeatureToggles = ({
{featureToggleModals}
</div>
</PageContent>
<BatchSelectionActionsBar count={Object.keys(rowSelection).length}>
<BatchSelectionActionsBar count={selectedData.length}>
<ProjectFeaturesBatchActions
selectedIds={Object.keys(rowSelection)}
data={features}
data={selectedData}
projectId={projectId}
onResetSelection={table.resetRowSelection}
onChange={refetch}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState, useEffect } from 'react';

export const useSelectedData = <T extends { name: string }>(
data: T[],
selection: Record<string, boolean>,
): T[] => {
const [selectedData, setSelectedData] = useState<T[]>([]);

useEffect(() => {
setSelectedData((prevSelectedData) => {
const combinedData = [...data];
prevSelectedData.forEach((item) => {
if (
!combinedData.find(
(dataItem) => dataItem.name === item.name,
)
) {
combinedData.push(item);
}
});

return combinedData.filter((item) => selection[item.name]);
});
}, [data, selection]);

return selectedData;
};

1 comment on commit 9b79810

@vercel
Copy link

@vercel vercel bot commented on 9b79810 Dec 21, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.