11import { attr , css , type ElementStyles , FASTElement , observable } from '@microsoft/fast-element' ;
22import { toggleState } from '../utils/element-internals.js' ;
3- import { maybeSetAutoFocus } from '../utils/autofocus .js' ;
3+ import { getUpgradedCustomElements , runAfterPendingDefinitions } from '../utils/custom-elements .js' ;
44import { isTreeItem } from './tree-item.options.js' ;
55
6- /**
7- * Base class for Tree Item Custom HTML Element.
8- * Based largely on the {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/li | `<li>`} element.
9- *
10- * @fires { ToggleEvent } toggle - Fires when the expanded state changes
11- * @fires { Event } change - Fires when the selected state changes
12- *
13- * @public
14- */
156export class BaseTreeItem extends FASTElement {
167 /**
178 * The internal {@link https://developer.mozilla.org/docs/Web/API/ElementInternals | `ElementInternals`} instance for the component.
@@ -39,18 +30,6 @@ export class BaseTreeItem extends FASTElement {
3930 this . elementInternals . role = 'treeitem' ;
4031 }
4132
42- connectedCallback ( ) : void {
43- super . connectedCallback ( ) ;
44-
45- this . tabIndex = Number ( this . getAttribute ( 'tabindex' ) || '0' ) ;
46-
47- if ( isTreeItem ( this . parentElement ) ) {
48- this . slot ||= 'item' ;
49- }
50-
51- maybeSetAutoFocus ( this ) ;
52- }
53-
5433 /**
5534 * When true, the control will be appear expanded by user interaction.
5635 * When true, the control will be appear expanded by user interaction.
@@ -75,7 +54,7 @@ export class BaseTreeItem extends FASTElement {
7554 newState : next ? 'open' : 'closed' ,
7655 } ) ;
7756 toggleState ( this . elementInternals , 'expanded' , next ) ;
78- if ( this . childTreeItems ?. length ) {
57+ if ( this . childTreeItems && this . childTreeItems . length > 0 ) {
7958 this . elementInternals . ariaExpanded = next ? 'true' : 'false' ;
8059 // Update focusgroup attributes after subtree show/hide rendering is done.
8160 requestAnimationFrame ( ( ) => {
@@ -115,11 +94,8 @@ export class BaseTreeItem extends FASTElement {
11594 */
11695 protected selectedChanged ( prev : boolean , next : boolean ) : void {
11796 this . $emit ( 'change' ) ;
118-
119- if ( this . elementInternals ) {
120- toggleState ( this . elementInternals , 'selected' , next ) ;
121- this . elementInternals . ariaSelected = next ? 'true' : 'false' ;
122- }
97+ toggleState ( this . elementInternals , 'selected' , next ) ;
98+ this . elementInternals . ariaSelected = next ? 'true' : 'false' ;
12399 }
124100
125101 /**
@@ -235,6 +211,14 @@ export class BaseTreeItem extends FASTElement {
235211
236212 /** @internal */
237213 public handleItemSlotChange ( ) {
238- this . childTreeItems = this . itemSlot . assignedElements ( ) . filter ( el => isTreeItem ( el ) ) ;
214+ const assignedElements = this . itemSlot . assignedElements ( ) ;
215+
216+ this . childTreeItems = getUpgradedCustomElements ( assignedElements , isTreeItem ) ;
217+
218+ runAfterPendingDefinitions ( assignedElements , isTreeItem , ( ) => {
219+ if ( this . isConnected ) {
220+ this . handleItemSlotChange ( ) ;
221+ }
222+ } ) ;
239223 }
240224}
0 commit comments