Skip to content

Commit a651243

Browse files
authored
Users/jes/tree expansion (#34282)
1 parent 5963862 commit a651243

4 files changed

Lines changed: 31 additions & 2 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "[fix] only set expanded is expanded is not set",
4+
"packageName": "@fluentui/web-components",
5+
"email": "jes@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

packages/web-components/src/tree-item/tree-item.base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ export class BaseTreeItem extends FASTElement {
115115
}
116116

117117
//If a tree item is nested and initially set to selected expand the tree items so the selected item is visible
118-
this.expanded = Array.from(this.querySelectorAll('*')).some(el => isTreeItem(el) && el.selected);
118+
if (!this.expanded) {
119+
this.expanded = Array.from(this.querySelectorAll('[selected]')).some(el => isTreeItem(el));
120+
}
119121

120122
this.childTreeItems.forEach(item => {
121123
this.setIndent(item);

packages/web-components/src/tree-item/tree-item.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,24 @@ test.describe('Tree Item', () => {
9595
await expect(element.nth(0)).toHaveAttribute('expanded');
9696
await expect(selectedItems).toBeVisible();
9797
});
98+
99+
test('should keep intitally set expanded attribute when no child item is set to selected', async ({ fastPage }) => {
100+
const { element } = fastPage;
101+
await fastPage.setTemplate({
102+
attributes: { expanded: true },
103+
innerHTML: /* html */ `
104+
Item 1
105+
<fluent-tree-item>
106+
Nested Item A
107+
<fluent-tree-item>
108+
Nested Item B
109+
<fluent-tree-item>Nested Item C</fluent-tree-item>
110+
</fluent-tree-item>
111+
</fluent-tree-item>
112+
`,
113+
});
114+
const nestedItem = element.nth(0).locator('fluent-tree-item').nth(0);
115+
await expect(element.nth(0)).toHaveAttribute('expanded');
116+
await expect(nestedItem).toBeVisible();
117+
});
98118
});

packages/web-components/src/tree/tree.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type Story = StoryObj<FluentTree>;
77

88
const storyTemplate = html<StoryArgs<FluentTree>>`
99
<fluent-tree size="${x => x.size}" appearance="${x => x.appearance}">
10-
<fluent-tree-item>
10+
<fluent-tree-item expanded>
1111
Item 1
1212
<fluent-tree-item>
1313
<span slot="start">

0 commit comments

Comments
 (0)