8000 Fix duplicate row selections in the Admin Console by FayezBast · Pull Request #55557 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
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
90 changes: 90 additions & 0 deletions js/libs/ui-shared/src/controls/table/KeycloakDataTable.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {
cleanup,
fireEvent,
render,
screen,
within,
} from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { ErrorBoundaryProvider } from "../../utils/ErrorBoundary";
import { KeycloakDataTable } from "./KeycloakDataTable";

vi.mock("react-i18next", () => ({
useTranslation: () => ({ t: (key: string) => key }),
}));

describe("KeycloakDataTable", () => {
afterEach(() => {
cleanup();
localStorage.removeItem("pageSize");
});

it("selects enabled rows once across pages", async () => {
localStorage.setItem("pageSize", "3");
const rows = Array.from({ length: 6 }, (_, index) => ({
id: `user-${index + 1}`,
disabled: index === 5,
}));
const >
render(
<KeycloakDataTable
ariaLabelKey="users"
loader={rows}
columns={[{ name: "id" }]}
isRowDisabled={(row) => row.disabled}
canSelectAll
>
/>,
{ wrapper: ErrorBoundaryProvider },
);

const firstRow = await screen.findByRole("row", { name: /user-1/ });
fireEvent.click(within(firstRow).getByRole("checkbox"));
fireEvent.click(
screen.getAllByRole("button", { name: "Go to next page" })[0],
);

const nextRow = await screen.findByRole("row", { name: /user-4/ });
fireEvent.click(within(nextRow).getB 8000 yRole("checkbox"));
const selectAll = screen.getByRole("checkbox", { name: "Select all rows" });
fireEvent.click(selectAll);

expect(onSelect).toHaveBeenLastCalledWith([rows[0], rows[3], rows[4]]);

fireEvent.click(selectAll);

expect(onSelect).toHaveBeenLastCalledWith([rows[0]]);
});

it("selects and clears rows without ids independently", async () => {
const rows = [{ name: "Profile one" }, { name: "Profile two" }];
const >
render(
<KeycloakDataTable
ariaLabelKey="profilesList"
loader={rows}
columns={[{ name: "name" }]}
canSelectAll
>
/>,
{ wrapper: ErrorBoundaryProvider },
);

const firstRow = await screen.findByRole("row", { name: /Profile one/ });
const secondRow = screen.getByRole("row", { name: /Profile two/ });
const firstCheckbox = within(firstRow).getByRole("checkbox");
const secondCheckbox = within(secondRow).getByRole("checkbox");
const selectAll = screen.getByRole("checkbox", { name: "Select all rows" });

fireEvent.click(firstCheckbox);
expect(firstCheckbox).toHaveProperty("checked", true);
expect(secondCheckbox).toHaveProperty("checked", false);

fireEvent.click(selectAll);
expect(selectAll).toHaveProperty("checked", true);
expect(onSelect).toHaveBeenLastCalledWith(rows);

fireEvent.click(firstCheckbox);
expect(onSelect).toHaveBeenLastCalledWith([rows[1]]);
});
});
61 changes: 33 additions & 28 deletions js/libs/ui-shared/src/controls/table/KeycloakDataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ import {
Thead,
Tr,
} from "@patternfly/react-table";
import { cloneDeep, get, intersectionBy } from "lodash-es";
import {
cloneDeep,
differenceBy,
get,
intersectionBy,
unionBy,
} from "lodash-es";
import {
ComponentClass,
ReactNode,
Expand Down Expand Up @@ -83,6 +89,11 @@ type CellRendererProps = {
const isRow = (c: ReactNode | IRowCell): c is IRowCell =>
!!c && (c as IRowCell).title !== undefined;

const getSelectionKey = (row: unknown) =>
typeof row === "object" && row !== null && "id" in row
? (row.id ?? row)
: row;

const CellRenderer = ({
row,
index,
Expand Down Expand Up @@ -133,7 +144,7 @@ function DataTable<T>({
intersectionBy(
selectedRows,
rows.map((row) => row.data),
"id",
getSelectionKey,
),
[selectedRows, rows],
);
Expand Down Expand Up @@ -176,30 +187,23 @@ function DataTable<T>({
if (isRadio) {
const selectedRow = isSelected ? [rows[rowIndex].data] : [];
updateSelectedRows(selectedRow);
return;
}

let rowsToUpdate: T[];
if (rowIndex === -1) {
rowsToUpdate = isSelected
? selectableRows.map((row) => row.data)
: rowsSelectedOnPage;
} else {
if (rowIndex === -1) {
const rowsSelectedOnPageIds = rowsSelectedOnPage.map((v) =>
get(v, "id"),
);
updateSelectedRows(
isSelected
? [...selectedRows, ...selectableRows.map((row) => row.data)]
: selectedRows.filter(
(v) => !rowsSelectedOnPageIds.includes(get(v, "id")),
),
);
} else {
if (isSelected) {
updateSelectedRows([...selectedRows, rows[rowIndex].data]);
} else {
updateSelectedRows(
selectedRows.filter(
(v) => get(v, "id") !== (rows[rowIndex] as IRow).data.id,
),
);
}
}
rowsToUpdate = [rows[rowIndex].data];
}

updateSelectedRows(
isSelected
? unionBy(selectedRows, rowsToUpdate, getSelectionKey)
: differenceBy(selectedRows, rowsToUpdate, getSelectionKey),
);
};

return (
Expand Down Expand Up @@ -251,8 +255,8 @@ function DataTable<T>({
onSelect: (_, isSelected, rowIndex) => {
updateState(rowIndex, isSelected);
},
isSelected: !!selectedRows.find(
(v) => get(v, "id") === row.data.id,
isSelected: selectedRows.some(
(v) => getSelectionKey(v) === getSelectionKey(row.data),
),
variant: isRadio ? "radio" : "checkbox",
isDisabled: row.disableSelection,
Expand Down Expand Up @@ -472,8 +476,9 @@ export function KeycloakDataTable<T>({
data: value,
disableSelection: disabledRow,
disableActions: disabledRow,
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- lodash get performs dynamic property access at runtime
selected: !!selected.find((v) => get(v, "id") === get(value, "id")),
selected: selected.some(
(v) => getSelectionKey(v) === getSelectionKey(value),
),
isOpen: isDetailColumnsEnabled(value) ? false : undefined,
cells: renderCell(columns, value),
},
Expand Down
0