|
| 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 { |
| 10 | + booleanAttribute, |
| 11 | + computed, |
| 12 | + contentChildren, |
| 13 | + Directive, |
| 14 | + ElementRef, |
| 15 | + inject, |
| 16 | + input, |
| 17 | + model, |
| 18 | +} from '@angular/core'; |
| 19 | +import {ListboxPattern, OptionPattern} from '@angular/cdk-experimental/ui-patterns'; |
| 20 | +import {Directionality} from '@angular/cdk/bidi'; |
| 21 | +import {toSignal} from '@angular/core/rxjs-interop'; |
| 22 | +import {_IdGenerator} from '@angular/cdk/a11y'; |
| 23 | + |
| 24 | +/** |
| 25 | + * A listbox container. |
| 26 | + * |
| 27 | + * Listboxes are used to display a list of items for a user to select from. The CdkListbox is meant |
| 28 | + * to be used in conjunction with CdkOption as follows: |
| 29 | + * |
| 30 | + * ```html |
| 31 | + * <ul cdkListbox> |
| 32 | + * <li cdkOption>Item 1</li> |
| 33 | + * <li cdkOption>Item 2</li> |
| 34 | + * <li cdkOption>Item 3</li> |
| 35 | + * </ul> |
| 36 | + * ``` |
| 37 | + */ |
| 38 | +@Directive({ |
| 39 | + selector: '[cdkListbox]', |
| 40 | + exportAs: 'cdkListbox', |
| 41 | + host: { |
| 42 | + 'role': 'listbox', |
| 43 | + 'class': 'cdk-listbox', |
| 44 | + '[attr.tabindex]': 'pattern.tabindex()', |
| 45 | + '[attr.aria-disabled]': 'pattern.disabled()', |
| 46 | + '[attr.aria-orientation]': 'pattern.orientation()', |
| 47 | + '[attr.aria-multiselectable]': 'pattern.multiselectable()', |
| 48 | + '[attr.aria-activedescendant]': 'pattern.activedescendant()', |
| 49 | + '(keydown)': 'pattern.onKeydown($event)', |
| 50 | + '(pointerdown)': 'pattern.onPointerdown($event)', |
| 51 | + }, |
| 52 | +}) |
| 53 | +export class CdkListbox { |
| 54 | + /** The directionality (LTR / RTL) context for the application (or a subtree of it). */ |
| 55 | + private readonly _directionality = inject(Directionality); |
| 56 | + |
| 57 | + /** The CdkOptions nested inside of the CdkListbox. */ |
| 58 | + private readonly _cdkOptions = contentChildren(CdkOption, {descendants: true}); |
| 59 | + |
| 60 | + /** A signal wrapper for directionality. */ |
| 61 | + protected textDirection = toSignal(this._directionality.change, { |
| 62 | + initialValue: this._directionality.value, |
| 63 | + }); |
| 64 | + |
| 65 | + /** The Option UIPatterns of the child CdkOptions. */ |
| 66 | + protected items = computed(() => this._cdkOptions().map(option => option.pattern)); |
| 67 | + |
| 68 | + /** Whether the list is vertically or horizontally oriented. */ |
| 69 | + orientation = input<'vertical' | 'horizontal'>('vertical'); |
| 70 | + |
| 71 | + /** Whether multiple items in the list can be selected at once. */ |
| 72 | + multiselectable = input(false, {transform: booleanAttribute}); |
| 73 | + |
| 74 | + /** Whether focus should wrap when navigating. */ |
| 75 | + wrap = input(true, {transform: booleanAttribute}); |
| 76 | + |
| 77 | + /** Whether disabled items in the list should be skipped when navigating. */ |
| 78 | + skipDisabled = input(true, {transform: booleanAttribute}); |
| 79 | + |
| 80 | + /** The focus strategy used by the list. */ |
| 81 | + focusMode = input<'roving' | 'activedescendant'>('roving'); |
| 82 | + |
| 83 | + /** The selection strategy used by the list. */ |
| 84 | + selectionMode = input<'follow' | 'explicit'>('follow'); |
| 85 | + |
| 86 | + /** The amount of time before the typeahead search is reset. */ |
| 87 | + typeaheadDelay = input<number>(0.5); // Picked arbitrarily. |
| 88 | + |
| 89 | + /** Whether the listbox is disabled. */ |
| 90 | + disabled = input(false, {transform: booleanAttribute}); |
| 91 | + |
| 92 | + // TODO(wagnermaciel): Figure out how we want to expose control over the current listbox value. |
| 93 | + /** The ids of the current selected items. */ |
| 94 | + selectedIds = model<string[]>([]); |
| 95 | + |
| 96 | + /** The current index that has been navigated to. */ |
| 97 | + activeIndex = model<number>(0); |
| 98 | + |
| 99 | + /** The Listbox UIPattern. */ |
| 100 | + pattern: ListboxPattern = new ListboxPattern({ |
| 101 | + ...this, |
| 102 | + items: this.items, |
| 103 | + textDirection: this.textDirection, |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +/** A selectable option in a CdkListbox. */ |
| 108 | +@Directive({ |
| 109 | + selector: '[cdkOption]', |
| 110 | + exportAs: 'cdkOption', |
| 111 | + host: { |
| 112 | + 'role': 'option', |
| 113 | + 'class': 'cdk-option', |
| 114 | + '[attr.tabindex]': 'pattern.tabindex()', |
| 115 | + '[attr.aria-selected]': 'pattern.selected()', |
| 116 | + '[attr.aria-disabled]': 'pattern.disabled()', |
| 117 | + }, |
| 118 | +}) |
| 119 | +export class CdkOption { |
| 120 | + /** A reference to the option element. */ |
| 121 | + private readonly _elementRef = inject(ElementRef); |
| 122 | + |
| 123 | + /** The parent CdkListbox. */ |
| 124 | + private readonly _cdkListbox = inject(CdkListbox); |
| 125 | + |
| 126 | + /** A unique identifier for the option. */ |
| 127 | + private readonly _generatedId = inject(_IdGenerator).getId('cdk-option-'); |
| 128 | + |
| 129 | + // TODO(wagnermaciel): https://github.com/angular/components/pull/30495#discussion_r1972601144. |
| 130 | + /** A unique identifier for the option. */ |
| 131 | + protected id = computed(() => this._generatedId); |
| 132 | + |
| 133 | + // TODO(wagnermaciel): See if we want to change how we handle this since textContent is not |
| 134 | + // reactive. See https://github.com/angular/components/pull/30495#discussion_r1961260216. |
| 135 | + /** The text used by the typeahead search. */ |
| 136 | + protected searchTerm = computed(() => this.label() ?? this.element().textContent); |
| 137 | + |
| 138 | + /** The parent Listbox UIPattern. */ |
| 139 | + protected listbox = computed(() => this._cdkListbox.pattern); |
| 140 | + |
| 141 | + /** A reference to the option element to be focused on navigation. */ |
| 142 | + protected element = computed(() => this._elementRef.nativeElement); |
| 143 | + |
| 144 | + /** Whether an item is disabled. */ |
| 145 | + disabled = input(false, {transform: booleanAttribute}); |
| 146 | + |
| 147 | + /** The text used by the typeahead search. */ |
| 148 | + label = input<string>(); |
| 149 | + |
| 150 | + /** The Option UIPattern. */ |
| 151 | + pattern = new OptionPattern({ |
| 152 | + ...this, |
| 153 | + id: this.id, |
| 154 | + listbox: this.listbox, |
| 155 | + element: this.element, |
| 156 | + searchTerm: this.searchTerm, |
| 157 | + }); |
| 158 | +} |
0 commit comments