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

fix: table multiselect with pagination bug #2282

Merged
merged 7 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/mighty-chicken-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@razorpay/blade": patch
---

fix: table multiselect with pagination bug
7 changes: 4 additions & 3 deletions packages/blade/src/components/Table/Table.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,11 @@ const _Table = <Item,>({

// Selection Logic
const onSelectChange: MiddlewareFunction = (action, state): void => {
const selectedIDs: Identifier[] = state.id ? [state.id] : state.ids ?? [];
setSelectedRows(selectedIDs);
const selectedIds: Identifier[] = state.id ? [state.id] : state.ids ?? [];
setSelectedRows(selectedIds);
onSelectionChange?.({
values: data.nodes.filter((node) => selectedIDs.includes(node.id)),
selectedIds,
values: data.nodes.filter((node) => selectedIds.includes(node.id)),
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,10 @@ describe('<Table />', () => {

const firstSelectableRow = getByText('rzp01').closest('td');
if (firstSelectableRow) await user.click(firstSelectableRow);
expect(onSelectionChange).toHaveBeenCalledWith({ values: [nodes[0]] });
expect(onSelectionChange).toHaveBeenCalledWith({ values: [nodes[0]], selectedIds: ['1'] });
const secondSelectableRow = getByText('rzp02').closest('td');
if (secondSelectableRow) await user.click(secondSelectableRow);
expect(onSelectionChange).toHaveBeenCalledWith({ values: [nodes[1]] });
expect(onSelectionChange).toHaveBeenCalledWith({ values: [nodes[1]], selectedIds: ['2'] });
});

it('should render table with multi select', async () => {
Expand Down Expand Up @@ -787,14 +787,17 @@ describe('<Table />', () => {
expect(getAllByRole('checkbox')).toHaveLength(6);
const firstSelectableRow = getByText('rzp01').closest('td');
if (firstSelectableRow) await user.click(firstSelectableRow);
expect(onSelectionChange).toHaveBeenCalledWith({ values: [nodes[0]] });
expect(onSelectionChange).toHaveBeenCalledWith({ values: [nodes[0]], selectedIds: ['1'] });
const secondSelectableRow = getByText('rzp02').closest('td');
if (secondSelectableRow) await user.click(secondSelectableRow);
expect(onSelectionChange).toHaveBeenCalledWith({ values: [nodes[0], nodes[1]] });
expect(onSelectionChange).toHaveBeenCalledWith({
values: [nodes[0], nodes[1]],
selectedIds: ['1', '2'],
});
expect(getByText('2 Items Selected')).toBeInTheDocument();
const deselectButton = getByText('Deselect');
await user.click(deselectButton);
expect(onSelectionChange).toHaveBeenCalledWith({ values: [] });
expect(onSelectionChange).toHaveBeenCalledWith({ values: [], selectedIds: [] });
});

it('should render table with client side pagination', async () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/blade/src/components/Table/docs/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,9 @@ function App(): React.ReactElement {
<Table
data={data}
selectionType="multiple"
onSelectionChange={({ values }) => setSelectedItems(values)}
onSelectionChange={({ selectedIds }) => {
setSelectedItems(data.nodes.filter((node) => selectedIds.includes(node.id)));
}}
toolbar={
<TableToolbar
title="Showing Recent Transactions"
Expand Down
20 changes: 19 additions & 1 deletion packages/blade/src/components/Table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,25 @@ type TableProps<Item> = {
* The onSelectionChange prop is a function that is called when the selection changes.
* The function is called with an object that has a values property that is an array of the selected rows.
**/
onSelectionChange?: ({ values }: { values: TableNode<Item>[] }) => void;
onSelectionChange?: ({
values,
selectedIds,
anuraghazra marked this conversation as resolved.
Show resolved Hide resolved
}: {
/**
* Note: on server side paginated data, this prop will only contain the selected rows on the current page.
*
* Thus, it's recommended to use `selectedIds` for more consistent state management across server/client paginated data.
*
* *Deprecated:* Use `selectedIds` instead.
*
* @deprecated
*/
values: TableNode<Item>[];
/**
* An array of selected row ids.
*/
selectedIds: Identifier[];
}) => void;
/**
* The isHeaderSticky prop determines whether the table header is sticky or not.
* The default value is `false`.
Expand Down
Loading