Skip to content

Commit 732a0d7

Browse files
authored
fix(aria/tree): tree item visibility issue (#32156)
1 parent b45cda3 commit 732a0d7

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/aria/tree/tree.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
OnInit,
2121
OnDestroy,
2222
untracked,
23+
afterNextRender,
2324
} from '@angular/core';
2425
import {_IdGenerator} from '@angular/cdk/a11y';
2526
import {Directionality} from '@angular/cdk/bidi';
@@ -267,7 +268,11 @@ export class TreeItem<V> extends DeferredContentAware implements OnInit, OnDestr
267268

268269
constructor() {
269270
super();
270-
this.preserveContent.set(true);
271+
afterNextRender(() => {
272+
if (this.tree().pattern instanceof ComboboxTreePattern) {
273+
this.preserveContent.set(true);
274+
}
275+
});
271276
// Connect the group's hidden state to the DeferredContentAware's visibility.
272277
afterRenderEffect(() => {
273278
this.tree().pattern instanceof ComboboxTreePattern

src/aria/ui-patterns/tree/tree.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,15 +1189,44 @@ describe('Tree Pattern', () => {
11891189
});
11901190

11911191
it('should correctly compute visible state', () => {
1192-
const {allItems} = createTree(treeExample, treeInputs);
1192+
const {allItems} = createTree(
1193+
[
1194+
{
1195+
value: 'Item 0',
1196+
children: [
1197+
{
1198+
value: 'Item 0-0',
1199+
children: [{value: 'Item 0-0-0', disabled: false, selectable: true}],
1200+
disabled: false,
1201+
selectable: true,
1202+
},
1203+
{
1204+
value: 'Item 0-1',
1205+
disabled: false,
1206+
selectable: true,
1207+
},
1208+
],
1209+
disabled: false,
1210+
selectable: true,
1211+
},
1212+
],
1213+
treeInputs,
1214+
);
11931215
const item0 = getItemByValue(allItems(), 'Item 0');
11941216
const item0_0 = getItemByValue(allItems(), 'Item 0-0');
1217+
const item0_0_0 = getItemByValue(allItems(), 'Item 0-0-0');
11951218

11961219
expect(item0_0.visible()).toBe(false);
1220+
expect(item0_0_0.visible()).toBe(false);
11971221
item0.expansion.open();
11981222
expect(item0_0.visible()).toBe(true);
1223+
expect(item0_0_0.visible()).toBe(false);
1224+
item0_0.expansion.open();
1225+
expect(item0_0.visible()).toBe(true);
1226+
expect(item0_0_0.visible()).toBe(true);
11991227
item0.expansion.close();
12001228
expect(item0_0.visible()).toBe(false);
1229+
expect(item0_0_0.visible()).toBe(false);
12011230
});
12021231

12031232
it('should correctly compute expanded state', () => {

src/aria/ui-patterns/tree/tree.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ export class TreeItemPattern<V> implements ListItem<V>, ExpansionItem {
8080
readonly expanded = computed(() => this.expansion.isExpanded());
8181

8282
/** Whether this item is visible. */
83-
readonly visible = computed(() => this.parent().expanded());
83+
readonly visible: SignalLike<boolean> = computed(
84+
() => this.parent().expanded() && this.parent().visible(),
85+
);
8486

8587
/** The number of items under the same parent at the same level. */
8688
readonly setsize = computed(() => this.parent().children().length);
@@ -183,6 +185,9 @@ export class TreePattern<V> {
183185
/** The root is always expanded. */
184186
readonly expanded = () => true;
185187

188+
/** The roow is always visible. */
189+
readonly visible = () => true;
190+
186191
/** The tabindex of the tree. */
187192
readonly tabindex: SignalLike<-1 | 0> = computed(() => this.listBehavior.tabindex());
188193

0 commit comments

Comments
 (0)