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
7 changes: 7 additions & 0 deletions .changeset/chilly-sloths-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@siemens/ix": patch
---

Add property **disabled** to **ix-tree-item**.

Fixes #2091
4 changes: 2 additions & 2 deletions packages/angular/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2806,14 +2806,14 @@ export declare interface IxTooltip extends Components.IxTooltip {}


@ProxyCmp({
inputs: ['ariaLabelChevronIcon', 'context', 'hasChildren', 'text']
inputs: ['ariaLabelChevronIcon', 'context', 'disabled', 'hasChildren', 'text']
})
@Component({
selector: 'ix-tree-item',
changeDetection: ChangeDetectionStrategy.OnPush,
template: '<ng-content></ng-content>',
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
inputs: ['ariaLabelChevronIcon', 'context', 'hasChildren', 'text'],
inputs: ['ariaLabelChevronIcon', 'context', 'disabled', 'hasChildren', 'text'],
outputs: ['toggle', 'itemClick'],
standalone: false
})
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/standalone/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2907,14 +2907,14 @@ export declare interface IxTooltip extends Components.IxTooltip {}

@ProxyCmp({
defineCustomElementFn: defineIxTreeItem,
inputs: ['ariaLabelChevronIcon', 'context', 'hasChildren', 'text']
inputs: ['ariaLabelChevronIcon', 'context', 'disabled', 'hasChildren', 'text']
})
@Component({
selector: 'ix-tree-item',
changeDetection: ChangeDetectionStrategy.OnPush,
template: '<ng-content></ng-content>',
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
inputs: ['ariaLabelChevronIcon', 'context', 'hasChildren', 'text'],
inputs: ['ariaLabelChevronIcon', 'context', 'disabled', 'hasChildren', 'text'],
outputs: ['toggle', 'itemClick'],
})
export class IxTreeItem {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3932,6 +3932,11 @@ export namespace Components {
* Context
*/
"context"?: TreeItemContext;
/**
* Disable tree item
* @default false
*/
"disabled": boolean;
/**
* Has tree item children
* @default false
Expand Down Expand Up @@ -9919,6 +9924,11 @@ declare namespace LocalJSX {
* Context
*/
"context"?: TreeItemContext;
/**
* Disable tree item
* @default false
*/
"disabled"?: boolean;
/**
* Has tree item children
* @default false
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/components/tree-item/default-tree-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ export function renderDefaultItem(
el.hasChildren = item.hasChildren;
el.context = context;
el.text = item.data.name;
el.disabled = item.disabled || false;

update((updateTreeItem) => {
el.text = updateTreeItem.data.name;
el.disabled = updateTreeItem.disabled || false;
});
return el;
}
7 changes: 7 additions & 0 deletions packages/core/src/components/tree-item/tree-item.scss
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,10 @@
:host(.selected:active) {
background-color: var(--theme-tree-item--background--selected-active);
}

:host(.disabled),
:host(:disabled) {
cursor: not-allowed;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
cursor: not-allowed;

opacity: 0.5;
pointer-events: none;
}
14 changes: 14 additions & 0 deletions packages/core/src/components/tree-item/tree-item.tsx
Copy link
Collaborator

Choose a reason for hiding this comment

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

A11y support is missing

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export class TreeItem {
*/
@Prop() context?: TreeItemContext;

/**
* Disable tree item
Copy link
Collaborator

Choose a reason for hiding this comment

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

since tag missing

*/
@Prop() disabled = false;

/**
* ARIA label for the chevron icon
*/
Expand All @@ -48,10 +53,13 @@ export class TreeItem {
@Event() itemClick!: EventEmitter<void>;

render() {
const isDisabled = this.disabled || this.context?.isDisabled;

return (
<Host
class={{
selected: !!this.context?.isSelected,
disabled: !!isDisabled,
}}
>
<div class="icon-toggle-container">
Expand All @@ -64,6 +72,9 @@ export class TreeItem {
}}
color="color-std-text"
onClick={(e: Event) => {
if (isDisabled) {
return;
}
e.preventDefault();
e.stopPropagation();
this.toggle.emit();
Expand All @@ -75,6 +86,9 @@ export class TreeItem {
<div
class="tree-node-container"
onClick={() => {
if (isDisabled) {
return;
}
this.itemClick.emit();
}}
>
Expand Down
158 changes: 158 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,161 @@ regressionTest(
await expect(items.nth(2)).not.toBeVisible();
}
);

regressionTest(
'disabled item should have disabled class',
async ({ mount, page }) => {
await mount(`
<div style="height: 20rem; width: 100%;">
<ix-tree root="root"></ix-tree>
</div>
`);

const tree = page.locator('ix-tree');

const modelWithDisabled = {
...defaultModel,
'sample-child-1': {
...defaultModel['sample-child-1'],
disabled: true,
},
};

await tree.evaluate(
(element: HTMLIxTreeElement, [model]) => {
element.model = model;
},
[modelWithDisabled]
);

await expect(tree).toHaveClass(/hydrated/);

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

const disabledItem = tree.locator('ix-tree-item', {
hasText: 'Sample Child 1',
});

await expect(disabledItem).toBeVisible();
await expect(disabledItem).toHaveClass(/disabled/);
}
);

regressionTest(
'disabled item should have reduced opacity',
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test case should be part of visual regression

async ({ mount, page }) => {
await mount(`
<div style="height: 20rem; width: 100%;">
<ix-tree root="root"></ix-tree>
</div>
`);

const tree = page.locator('ix-tree');

const modelWithDisabled = {
...defaultModel,
'sample-child-1': {
...defaultModel['sample-child-1'],
disabled: true,
},
};

await tree.evaluate(
(element: HTMLIxTreeElement, [model]) => {
element.model = model;
},
[modelWithDisabled]
);

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

const disabledItem = tree.locator('ix-tree-item', {
hasText: 'Sample Child 1',
});

await expect(disabledItem).toBeVisible();
await expect(disabledItem).toHaveCSS('opacity', '0.5');
}
);

regressionTest(
'disabled item should have not-allowed cursor',
async ({ mount, page }) => {
await mount(`
<div style="height: 20rem; width: 100%;">
<ix-tree root="root"></ix-tree>
</div>
`);

const tree = page.locator('ix-tree');

const modelWithDisabled = {
...defaultModel,
'sample-child-1': {
...defaultModel['sample-child-1'],
disabled: true,
},
};

await tree.evaluate(
(element: HTMLIxTreeElement, [model]) => {
element.model = model;
},
[modelWithDisabled]
);

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

const disabledItem = tree.locator('ix-tree-item', {
hasText: 'Sample Child 1',
});

await expect(disabledItem).toBeVisible();
await expect(disabledItem).toHaveCSS('cursor', 'not-allowed');
}
);

Comment on lines +545 to +582
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
regressionTest(
'disabled item should have not-allowed cursor',
async ({ mount, page }) => {
await mount(`
<div style="height: 20rem; width: 100%;">
<ix-tree root="root"></ix-tree>
</div>
`);
const tree = page.locator('ix-tree');
const modelWithDisabled = {
...defaultModel,
'sample-child-1': {
...defaultModel['sample-child-1'],
disabled: true,
},
};
await tree.evaluate(
(element: HTMLIxTreeElement, [model]) => {
element.model = model;
},
[modelWithDisabled]
);
const sampleItem = tree.locator('ix-tree-item').first();
await sampleItem.locator('ix-icon').click();
const disabledItem = tree.locator('ix-tree-item', {
hasText: 'Sample Child 1',
});
await expect(disabledItem).toBeVisible();
await expect(disabledItem).toHaveCSS('cursor', 'not-allowed');
}
);

regressionTest(
'enabled items should still be selectable when other items are disabled',
async ({ mount, page }) => {
await mount(`
<div style="height: 20rem; width: 100%;">
<ix-tree root="root"></ix-tree>
</div>
`);

const tree = page.locator('ix-tree');

const modelWithDisabled = {
...defaultModel,
'sample-child-1': {
...defaultModel['sample-child-1'],
disabled: true,
},
};

await tree.evaluate(
(element: HTMLIxTreeElement, [model]) => {
element.model = model;
},
[modelWithDisabled]
);

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

const enabledItem = tree.locator('ix-tree-item', {
hasText: 'Sample Child 2',
});

await expect(enabledItem).toBeVisible();
await expect(enabledItem).not.toHaveClass(/disabled/);

await enabledItem.click();

await expect(enabledItem).toHaveClass(/selected/);
}
);
Comment on lines +467 to +623
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

To improve maintainability and reduce code duplication, you could extract the test setup logic into a helper function. All the new tests share the same setup code.

Here's an example of how you could refactor it:

const setupTreeWithDisabledItem = async (mount: Mount, page: Page) => {
  await mount(`
    <div style="height: 20rem; width: 100%;">
      <ix-tree root="root"></ix-tree>
    </div>
  `);

  const tree = page.locator('ix-tree');

  const modelWithDisabled = {
    ...defaultModel,
    'sample-child-1': {
      ...defaultModel['sample-child-1'],
      disabled: true,
    },
  };

  await tree.evaluate(
    (element: HTMLIxTreeElement, [model]) => {
      element.model = model;
    },
    [modelWithDisabled]
  );

  await expect(tree).toHaveClass(/hydrated/);

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

  const disabledItem = tree.locator('ix-tree-item', {
    hasText: 'Sample Child 1',
  });

  await expect(disabledItem).toBeVisible();

  return { tree, disabledItem };
};

regressionTest(
  'disabled item should have disabled class',
  async ({ mount, page }) => {
    const { disabledItem } = await setupTreeWithDisabledItem(mount, page);
    await expect(disabledItem).toHaveClass(/disabled/);
  }
);

This would make the tests cleaner and easier to maintain.

Comment on lines +583 to +623
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
regressionTest(
'enabled items should still be selectable when other items are disabled',
async ({ mount, page }) => {
await mount(`
<div style="height: 20rem; width: 100%;">
<ix-tree root="root"></ix-tree>
</div>
`);
const tree = page.locator('ix-tree');
const modelWithDisabled = {
...defaultModel,
'sample-child-1': {
...defaultModel['sample-child-1'],
disabled: true,
},
};
await tree.evaluate(
(element: HTMLIxTreeElement, [model]) => {
element.model = model;
},
[modelWithDisabled]
);
const sampleItem = tree.locator('ix-tree-item').first();
await sampleItem.locator('ix-icon').click();
const enabledItem = tree.locator('ix-tree-item', {
hasText: 'Sample Child 2',
});
await expect(enabledItem).toBeVisible();
await expect(enabledItem).not.toHaveClass(/disabled/);
await enabledItem.click();
await expect(enabledItem).toHaveClass(/selected/);
}
);

2 changes: 2 additions & 0 deletions packages/core/src/components/tree/tree-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface TreeItem<T> {
data: T;
hasChildren: boolean;
children: TreeItemId[];
disabled?: boolean;
}

export interface TreeItemVisual<T> extends TreeItem<T> {
Expand All @@ -30,4 +31,5 @@ export interface TreeItemVisual<T> extends TreeItem<T> {
export interface TreeItemContext {
isExpanded: boolean;
isSelected: boolean;
isDisabled?: boolean;
}
11 changes: 11 additions & 0 deletions packages/core/src/components/tree/tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,14 @@ export class Tree {
return {
isExpanded: false,
isSelected: false,
isDisabled: this.model[id]?.disabled,
};
}
if (!this.context[id]) {
this.context[id] = {
isExpanded: false,
isSelected: false,
isDisabled: this.model[id]?.disabled,
};
}
return this.context[id];
Expand Down Expand Up @@ -349,6 +351,10 @@ export class Tree {
return;
}

if (item.disabled) {
return;
}

const context = this.getContext(id);
context.isExpanded = !context.isExpanded;
this.nodeToggled.emit({ id, isExpanded: context.isExpanded });
Expand All @@ -367,6 +373,11 @@ export class Tree {
}

const item = this.model[id];

if (item.disabled) {
return;
}

const path = event.composedPath();
const treeIndex = path.indexOf(this.hostElement);
const treePath = path.slice(0, treeIndex);
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/tree/tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const IxTree = React.forwardRef(
const rootNode = ReactDOM.createRoot(treeItem);
treeItem.hasChildren = data.hasChildren;
treeItem.context = context[data.id];
treeItem.disabled = data.disabled || false;

if (props.renderItem) {
rootNode.render(props.renderItem(data.data));
Expand All @@ -55,6 +56,7 @@ export const IxTree = React.forwardRef(
update((itemData, newContext) => {
treeItem.context = newContext[itemData.id];
treeItem.hasChildren = itemData.hasChildren;
treeItem.disabled = itemData.disabled || false;

if (props.renderItem) {
rootNode.render(props.renderItem(itemData.data));
Expand Down
1 change: 1 addition & 0 deletions packages/vue/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,7 @@ export const IxTreeItem: StencilVueComponent<JSX.IxTreeItem> = /*@__PURE__*/ def
'text',
'hasChildren',
'context',
'disabled',
'ariaLabelChevronIcon',
'toggle',
'itemClick'
Expand Down