Skip to content

Commit

Permalink
fix tree selection via spacebar and release version patch
Browse files Browse the repository at this point in the history
  • Loading branch information
radubrehar committed Oct 29, 2024
1 parent 0644c26 commit cf78e9d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
54 changes: 36 additions & 18 deletions examples/src/pages/tests/table/treegrid/selection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
import { test, expect } from '@testing';
import { test, expect, Page } from '@testing';

async function getCheckboxes(page: Page) {
let checkboxes = await page.$$('.InfiniteBody input[type="checkbox"]');

return await Promise.all(
checkboxes.map(
async (checkbox) =>
await checkbox.evaluate((el) =>
(el as HTMLInputElement).indeterminate
? null
: (el as HTMLInputElement).checked,
),
),
);
}

export default test.describe('TreeSelectionProp', () => {
test('select via spacebar works', async ({ rowModel, page }) => {
await page.waitForInfinite();

await rowModel.clickRow(0);

await page.keyboard.press('Space');

expect(await getCheckboxes(page)).toEqual([
true,
true,
true,
true,
true,
true,
true,
]);
});

test('when defined, makes selectionMode default to multi-row', async ({
page,
}) => {
await page.waitForInfinite();

async function getCheckboxes() {
let checkboxes = await page.$$('.InfiniteBody input[type="checkbox"]');

return await Promise.all(
checkboxes.map(
async (checkbox) =>
await checkbox.evaluate((el) =>
(el as HTMLInputElement).indeterminate
? null
: (el as HTMLInputElement).checked,
),
),
);
}

let checkboxes = await getCheckboxes();
let checkboxes = await getCheckboxes(page);
expect(checkboxes.length).toBe(7);

const headerCheckbox = await page.locator(
Expand All @@ -40,7 +58,7 @@ export default test.describe('TreeSelectionProp', () => {

await page.click('button:text("Select all")');

checkboxes = await getCheckboxes();
checkboxes = await getCheckboxes(page);
expect(checkboxes).toEqual([true, true, true, true, true, true, true]);

expect(
Expand Down
14 changes: 9 additions & 5 deletions source/src/components/DataSource/TreeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,25 +292,29 @@ export class TreeApiImpl<T> implements TreeApi<T> {
this.actions.treeSelection = treeSelectionState;
}
isNodeSelected(nodePath: NodePath) {
const { treeSelection, selectionMode } = this.getState();
const {
treeSelectionState,
treeSelection: singleTreeSelection,
selectionMode,
} = this.getState();

if (selectionMode === 'single-row') {
const pk = nodePath[nodePath.length - 1];
if (Array.isArray(treeSelection)) {
if (Array.isArray(singleTreeSelection)) {
// @ts-ignore
return treeSelection.join(',') === nodePath.join(',');
}
return (treeSelection as any) === pk;
return (singleTreeSelection as any) === pk;
}

if (selectionMode !== 'multi-row') {
throw 'Selection mode is not multi-row or single-row';
}
if (!(treeSelection instanceof TreeSelectionState)) {
if (!(treeSelectionState instanceof TreeSelectionState)) {
throw 'Invalid tree selection';
}

return treeSelection.isNodeSelected(nodePath);
return treeSelectionState.isNodeSelected(nodePath);
}

selectNode(nodePath: NodePath) {
Expand Down

0 comments on commit cf78e9d

Please sign in to comment.