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
47 changes: 47 additions & 0 deletions packages/core/src/components/tree/test/tree.ct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,50 @@ regressionTest(
await expect(items.nth(2)).not.toBeVisible();
}
);

regressionTest(
'should handle click events on custom rendered tree items',
async ({ mount, page }) => {
const tree = await initializeTree(mount, page);

await tree.evaluate(
(t) =>
((t as HTMLIxTreeElement).renderItem = (
_index,
item,
_dataList,
context,
update
) => {
const el = document.createElement('ix-tree-item');
const treeItem = item as TreeItem<any>;
el.hasChildren = treeItem.hasChildren;
el.context = context[treeItem.id];

const container = document.createElement('div');
container.classList.add('custom-content');
container.style.display = 'flex';
container.style.alignItems = 'center';

const name = document.createElement('span');
name.innerText = treeItem.data.name;

container.appendChild(name);
el.appendChild(container);

update((updateTreeItem) => {
name.innerText = updateTreeItem.data.name;
});

return el;
})
);

const root = tree.locator('ix-tree-item').first();
await root.locator('.icon-toggle-container').click();

const customItem = tree.locator('ix-tree-item').nth(1);
await customItem.click();
await expect(customItem).toHaveClass(/selected/);
}
);
26 changes: 23 additions & 3 deletions packages/core/src/components/tree/tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,16 @@ export class Tree {
return;
}

const id = target.getAttribute('data-tree-node-id');
let treeNodeElement = target;
let id = treeNodeElement.dataset.treeNodeId;

while (!id && treeNodeElement && treeNodeElement !== this.hostElement) {
treeNodeElement = treeNodeElement.parentElement as HTMLElement;
if (treeNodeElement) {
id = treeNodeElement.dataset.treeNodeId;
}
}

if (!id) {
return;
}
Expand All @@ -361,7 +370,16 @@ export class Tree {
return;
}

const id = target.getAttribute('data-tree-node-id');
let treeNodeElement = target;
Copy link
Contributor

Choose a reason for hiding this comment

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

Duplicated with the above changed code.

let id = treeNodeElement.dataset.treeNodeId;

while (!id && treeNodeElement && treeNodeElement !== this.hostElement) {
treeNodeElement = treeNodeElement.parentElement as HTMLElement;
if (treeNodeElement) {
id = treeNodeElement.dataset.treeNodeId;
}
}

if (!id) {
return;
}
Expand All @@ -377,7 +395,9 @@ export class Tree {
}

if (!event.defaultPrevented) {
Object.values(this.context).forEach((c) => (c.isSelected = false));
for (const c of Object.values(this.context)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please avoid using single alphabet variable names. Use word.

c.isSelected = false;
}
const context = this.getContext(id);
context.isSelected = true;
this.setContext(id, context);
Expand Down
Loading