|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {_IdGenerator} from '@angular/cdk/a11y'; |
| 10 | +import { |
| 11 | + booleanAttribute, |
| 12 | + computed, |
| 13 | + contentChildren, |
| 14 | + Directive, |
| 15 | + ElementRef, |
| 16 | + inject, |
| 17 | + input, |
| 18 | + model, |
| 19 | + Signal, |
| 20 | +} from '@angular/core'; |
| 21 | +import {Directionality} from '@angular/cdk/bidi'; |
| 22 | +import {GridCellPattern} from '../private'; |
| 23 | +import {GridCellWidget} from './grid-cell-widget'; |
| 24 | +import {GRID_CELL, GRID_ROW} from './grid-tokens'; |
| 25 | + |
| 26 | +/** |
| 27 | + * Represents a cell within a grid row. It is the primary focusable element |
| 28 | + * within the grid. It can be disabled and can have its selection state managed |
| 29 | + * through the `selected` input. |
| 30 | + * |
| 31 | + * ```html |
| 32 | + * <td ngGridCell [disabled]="isDisabled" [(selected)]="isSelected"> |
| 33 | + * Cell Content |
| 34 | + * </td> |
| 35 | + * ``` |
| 36 | + * |
| 37 | + * @developerPreview 21.0 |
| 38 | + */ |
| 39 | +@Directive({ |
| 40 | + selector: '[ngGridCell]', |
| 41 | + exportAs: 'ngGridCell', |
| 42 | + host: { |
| 43 | + '[attr.role]': 'role()', |
| 44 | + '[attr.id]': '_pattern.id()', |
| 45 | + '[attr.rowspan]': '_pattern.rowSpan()', |
| 46 | + '[attr.colspan]': '_pattern.colSpan()', |
| 47 | + '[attr.data-active]': 'active()', |
| 48 | + '[attr.data-anchor]': '_pattern.anchor()', |
| 49 | + '[attr.aria-disabled]': '_pattern.disabled()', |
| 50 | + '[attr.aria-rowspan]': '_pattern.rowSpan()', |
| 51 | + '[attr.aria-colspan]': '_pattern.colSpan()', |
| 52 | + '[attr.aria-rowindex]': '_pattern.ariaRowIndex()', |
| 53 | + '[attr.aria-colindex]': '_pattern.ariaColIndex()', |
| 54 | + '[attr.aria-selected]': '_pattern.ariaSelected()', |
| 55 | + '[tabindex]': '_tabIndex()', |
| 56 | + }, |
| 57 | + providers: [{provide: GRID_CELL, useExisting: GridCell}], |
| 58 | +}) |
| 59 | +export class GridCell { |
| 60 | + /** A reference to the host element. */ |
| 61 | + private readonly _elementRef = inject(ElementRef); |
| 62 | + |
| 63 | + /** A reference to the host element. */ |
| 64 | + readonly element = this._elementRef.nativeElement as HTMLElement; |
| 65 | + |
| 66 | + /** Whether the cell is currently active (focused). */ |
| 67 | + readonly active = computed(() => this._pattern.active()); |
| 68 | + |
| 69 | + /** The widgets contained within this cell, if any. */ |
| 70 | + private readonly _widgets = contentChildren(GridCellWidget, {descendants: true}); |
| 71 | + |
| 72 | + /** The UI pattern for the widget in this cell. */ |
| 73 | + private readonly _widgetPatterns: Signal<any[]> = computed(() => |
| 74 | + this._widgets().map(w => w._pattern), |
| 75 | + ); |
| 76 | + |
| 77 | + /** The parent row. */ |
| 78 | + private readonly _row = inject(GRID_ROW); |
| 79 | + |
| 80 | + /** Text direction. */ |
| 81 | + readonly textDirection = inject(Directionality).valueSignal; |
| 82 | + |
| 83 | + /** A unique identifier for the cell. */ |
| 84 | + readonly id = input(inject(_IdGenerator).getId('ng-grid-cell-', true)); |
| 85 | + |
| 86 | + /** The ARIA role for the cell. */ |
| 87 | + readonly role = input<'gridcell' | 'columnheader' | 'rowheader'>('gridcell'); |
| 88 | + |
| 89 | + /** The number of rows the cell should span. */ |
| 90 | + readonly rowSpan = input<number>(1); |
| 91 | + |
| 92 | + /** The number of columns the cell should span. */ |
| 93 | + readonly colSpan = input<number>(1); |
| 94 | + |
| 95 | + /** The index of this cell's row within the grid. */ |
| 96 | + readonly rowIndex = input<number>(); |
| 97 | + |
| 98 | + /** The index of this cell's column within the grid. */ |
| 99 | + readonly colIndex = input<number>(); |
| 100 | + |
| 101 | + /** Whether the cell is disabled. */ |
| 102 | + readonly disabled = input(false, {transform: booleanAttribute}); |
| 103 | + |
| 104 | + /** Whether the cell is selected. */ |
| 105 | + readonly selected = model<boolean>(false); |
| 106 | + |
| 107 | + /** Whether the cell is selectable. */ |
| 108 | + readonly selectable = input<boolean>(true); |
| 109 | + |
| 110 | + /** Orientation of the widgets in the cell. */ |
| 111 | + readonly orientation = input<'vertical' | 'horizontal'>('horizontal'); |
| 112 | + |
| 113 | + /** Whether widgets navigation wraps. */ |
| 114 | + readonly wrap = input(true, {transform: booleanAttribute}); |
| 115 | + |
| 116 | + /** The tabindex override. */ |
| 117 | + readonly tabindex = input<number | undefined>(); |
| 118 | + |
| 119 | + /** |
| 120 | + * The tabindex value set to the element. |
| 121 | + * If a focus target exists then return -1. Unless an override. |
| 122 | + */ |
| 123 | + protected readonly _tabIndex: Signal<number> = computed( |
| 124 | + () => this.tabindex() ?? this._pattern.tabIndex(), |
| 125 | + ); |
| 126 | + |
| 127 | + /** The UI pattern for the grid cell. */ |
| 128 | + readonly _pattern = new GridCellPattern({ |
| 129 | + ...this, |
| 130 | + grid: this._row._gridPattern, |
| 131 | + row: () => this._row._pattern, |
| 132 | + widgets: this._widgetPatterns, |
| 133 | + getWidget: e => this._getWidget(e), |
| 134 | + element: () => this.element, |
| 135 | + }); |
| 136 | + |
| 137 | + constructor() {} |
| 138 | + |
| 139 | + /** Gets the cell widget pattern for a given element. */ |
| 140 | + private _getWidget(element: Element | null | undefined): any | undefined { |
| 141 | + let target = element; |
| 142 | + |
| 143 | + while (target) { |
| 144 | + const pattern = this._widgetPatterns().find(w => w.element() === target); |
| 145 | + if (pattern) { |
| 146 | + return pattern; |
| 147 | + } |
| 148 | + |
| 149 | + target = target.parentElement?.closest('[ngGridCellWidget]'); |
| 150 | + } |
| 151 | + |
| 152 | + return undefined; |
| 153 | + } |
| 154 | +} |
0 commit comments