|
| 1 | +import { attr, css, ElementStyles, FASTElement, observable } from '@microsoft/fast-element'; |
| 2 | +import { toggleState } from '../utils/element-internals.js'; |
| 3 | +import { isTreeItem } from './tree-item.options.js'; |
| 4 | + |
| 5 | +export class BaseTreeItem extends FASTElement { |
| 6 | + /** |
| 7 | + * The internal {@link https://developer.mozilla.org/docs/Web/API/ElementInternals | `ElementInternals`} instance for the component. |
| 8 | + * |
| 9 | + * @internal |
| 10 | + */ |
| 11 | + public elementInternals: ElementInternals = this.attachInternals(); |
| 12 | + |
| 13 | + constructor() { |
| 14 | + super(); |
| 15 | + this.elementInternals.role = 'treeitem'; |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * When true, the control will be appear expanded by user interaction. |
| 20 | + * @public |
| 21 | + * HTML Attribute: `expanded` |
| 22 | + */ |
| 23 | + @attr({ mode: 'boolean' }) |
| 24 | + expanded: boolean = false; |
| 25 | + |
| 26 | + /** |
| 27 | + * Handles changes to the expanded attribute |
| 28 | + * @param prev - the previous state |
| 29 | + * @param next - the next state |
| 30 | + * |
| 31 | + * @public |
| 32 | + */ |
| 33 | + public expandedChanged(prev: boolean, next: boolean): void { |
| 34 | + toggleState(this.elementInternals, 'expanded', next); |
| 35 | + if (this.childTreeItems && this.childTreeItems.length > 0) { |
| 36 | + this.elementInternals.ariaExpanded = next ? 'true' : 'false'; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * When true, the control will appear selected by user interaction. |
| 42 | + * @public |
| 43 | + * @remarks |
| 44 | + * HTML Attribute: selected |
| 45 | + */ |
| 46 | + @attr({ mode: 'boolean' }) |
| 47 | + selected: boolean = false; |
| 48 | + |
| 49 | + /** |
| 50 | + * Handles changes to the selected attribute |
| 51 | + * @param prev - the previous state |
| 52 | + * @param next - the next state |
| 53 | + * |
| 54 | + * @internal |
| 55 | + */ |
| 56 | + protected selectedChanged(prev: boolean, next: boolean): void { |
| 57 | + this.$emit('change'); |
| 58 | + toggleState(this.elementInternals, 'selected', next); |
| 59 | + this.elementInternals.ariaSelected = next ? 'true' : 'false'; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * When true, the control has no child tree items |
| 64 | + * @public |
| 65 | + * HTML Attribute: empty |
| 66 | + */ |
| 67 | + @attr({ mode: 'boolean' }) |
| 68 | + public empty: boolean = false; |
| 69 | + |
| 70 | + private styles: ElementStyles | undefined; |
| 71 | + |
| 72 | + /** |
| 73 | + * The indent of the tree item element. |
| 74 | + * This is not needed once css attr() is supported (--indent: attr(data-indent type(<number>))); |
| 75 | + * @public |
| 76 | + */ |
| 77 | + @attr({ attribute: 'data-indent' }) |
| 78 | + public dataIndent!: number | undefined; |
| 79 | + |
| 80 | + private dataIndentChanged(prev: number, next: number) { |
| 81 | + if (this.styles !== undefined) { |
| 82 | + this.$fastController.removeStyles(this.styles); |
| 83 | + } |
| 84 | + |
| 85 | + this.styles = css` |
| 86 | + :host { |
| 87 | + --indent: ${next as any}; |
| 88 | + } |
| 89 | + `; |
| 90 | + |
| 91 | + this.$fastController.addStyles(this.styles); |
| 92 | + } |
| 93 | + |
| 94 | + @observable |
| 95 | + public childTreeItems: BaseTreeItem[] | undefined = []; |
| 96 | + |
| 97 | + /** |
| 98 | + * Handles changes to the child tree items |
| 99 | + * |
| 100 | + * @public |
| 101 | + */ |
| 102 | + public childTreeItemsChanged() { |
| 103 | + this.empty = this.childTreeItems?.length === 0; |
| 104 | + this.updateChildTreeItems(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Updates the childrens indent |
| 109 | + * |
| 110 | + * @public |
| 111 | + */ |
| 112 | + public updateChildTreeItems() { |
| 113 | + if (!this.childTreeItems?.length) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + this.childTreeItems.forEach(item => { |
| 118 | + this.setIndent(item); |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Sets the indent for each item |
| 124 | + */ |
| 125 | + private setIndent(item: BaseTreeItem): void { |
| 126 | + const indent = this.dataIndent ?? 0; |
| 127 | + item.dataIndent = indent + 1; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Handle focus events |
| 132 | + * |
| 133 | + * @public |
| 134 | + */ |
| 135 | + public focusHandler(e: FocusEvent): void { |
| 136 | + if ( |
| 137 | + e.target === this || |
| 138 | + // In case where the tree-item contains a focusable element, we should not set the tabindex to 0 when the focus is on its child focusable element, |
| 139 | + // so users can shift+tab to navigate to the tree-item from its child focusable element. |
| 140 | + this.contains(e.target as Node) |
| 141 | + ) { |
| 142 | + this.setAttribute('tabindex', '0'); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Handle blur events |
| 148 | + * |
| 149 | + * @public |
| 150 | + */ |
| 151 | + public blurHandler(e: FocusEvent): void { |
| 152 | + if (e.target === this) { |
| 153 | + this.setAttribute('tabindex', '-1'); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Toggle the expansion state of the tree item |
| 159 | + * |
| 160 | + * @public |
| 161 | + */ |
| 162 | + public toggleExpansion() { |
| 163 | + if (this.childTreeItems?.length) { |
| 164 | + this.expanded = !this.expanded; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Toggle the single selection state of the tree item |
| 170 | + * |
| 171 | + * @public |
| 172 | + */ |
| 173 | + public toggleSelection() { |
| 174 | + this.selected = !this.selected; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Whether the tree is nested |
| 179 | + * @internal |
| 180 | + */ |
| 181 | + get isNestedItem() { |
| 182 | + return isTreeItem(this.parentElement); |
| 183 | + } |
| 184 | +} |
0 commit comments