|
| 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 | + computed, |
| 11 | + contentChildren, |
| 12 | + Directive, |
| 13 | + ElementRef, |
| 14 | + inject, |
| 15 | + input, |
| 16 | + model, |
| 17 | + signal, |
| 18 | +} from '@angular/core'; |
| 19 | +import {Option} from '../ui-patterns/listbox/option'; |
| 20 | +import {ListboxInputs, ListboxPattern} from '../ui-patterns/listbox/listbox'; |
| 21 | +import {Directionality} from '@angular/cdk/bidi'; |
| 22 | +import {startWith, takeUntil} from 'rxjs/operators'; |
| 23 | +import {Subject} from 'rxjs'; |
| 24 | + |
| 25 | +@Directive({ |
| 26 | + selector: '[cdkListbox]', |
| 27 | + exportAs: 'cdkListbox', |
| 28 | + host: { |
| 29 | + 'role': 'listbox', |
| 30 | + 'class': 'cdk-listbox', |
| 31 | + '[attr.tabindex]': 'state.tabindex()', |
| 32 | + // '[attr.aria-disabled]': 'state.disabled()', |
| 33 | + '[attr.aria-multiselectable]': 'state.multiselectable()', |
| 34 | + '[attr.aria-activedescendant]': 'state.activedescendant()', |
| 35 | + '[attr.aria-orientation]': 'state.orientation()', |
| 36 | + '(focusin)': 'state.onFocus()', |
| 37 | + '(keydown)': 'state.onKeydown($event)', |
| 38 | + '(mousedown)': 'state.onMousedown($event)', |
| 39 | + // '(focusout)': '_handleFocusOut($event)', |
| 40 | + // '(focusin)': '_handleFocusIn()', |
| 41 | + }, |
| 42 | +}) |
| 43 | +export class CdkListbox implements ListboxInputs { |
| 44 | + /** Whether the list is vertically or horizontally oriented. */ |
| 45 | + orientation = input<'vertical' | 'horizontal'>('vertical'); |
| 46 | + |
| 47 | + /** Whether multiple items in the list can be selected at once. */ |
| 48 | + multiselectable = input<boolean>(false); |
| 49 | + |
| 50 | + /** Whether focus should wrap when navigating. */ |
| 51 | + wrap = input<boolean>(true); |
| 52 | + |
| 53 | + /** Whether disabled items in the list should be skipped when navigating. */ |
| 54 | + skipDisabled = input<boolean>(true); |
| 55 | + |
| 56 | + /** The focus strategy used by the list. */ |
| 57 | + focusStrategy = input<'roving tabindex' | 'activedescendant'>('roving tabindex'); |
| 58 | + |
| 59 | + /** The selection strategy used by the list. */ |
| 60 | + selectionStrategy = input<'follow' | 'explicit'>('follow'); |
| 61 | + |
| 62 | + /** The amount of time before the typeahead search is reset. */ |
| 63 | + delay = input<number>(0.5); |
| 64 | + |
| 65 | + /** The ids of the current selected items. */ |
| 66 | + selectedIds = model<string[]>([]); |
| 67 | + |
| 68 | + /** The current index that has been navigated to. */ |
| 69 | + activeIndex = model<number>(0); |
| 70 | + |
| 71 | + /** The CdkOptions nested inside of the CdkListbox. */ |
| 72 | + private cdkOptions = contentChildren(CdkOption, {descendants: true}); |
| 73 | + |
| 74 | + /** The Option UIPatterns of the child CdkOptions. */ |
| 75 | + items = computed(() => this.cdkOptions().map(option => option.state)); |
| 76 | + |
| 77 | + /** The directionality (LTR / RTL) context for the application (or a subtree of it). */ |
| 78 | + private dir = inject(Directionality); |
| 79 | + |
| 80 | + /** A signal wrapper for directionality. */ |
| 81 | + directionality = signal<'rtl' | 'ltr'>('rtl'); |
| 82 | + |
| 83 | + /** Emits when the list has been destroyed. */ |
| 84 | + private readonly destroyed = new Subject<void>(); |
| 85 | + |
| 86 | + /** The Listbox UIPattern. */ |
| 87 | + state: ListboxPattern = new ListboxPattern(this); |
| 88 | + |
| 89 | + constructor() { |
| 90 | + this.dir.change |
| 91 | + .pipe(startWith(this.dir.value), takeUntil(this.destroyed)) |
| 92 | + .subscribe(value => this.directionality.set(value)); |
| 93 | + } |
| 94 | + |
| 95 | + ngOnDestroy() { |
| 96 | + this.destroyed.complete(); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +@Directive({ |
| 101 | + selector: '[cdkOption]', |
| 102 | + exportAs: 'cdkOption', |
| 103 | + host: { |
| 104 | + 'role': 'option', |
| 105 | + 'class': 'cdk-option', |
| 106 | + '[attr.aria-selected]': 'state.selected()', |
| 107 | + '[attr.tabindex]': 'state.tabindex()', |
| 108 | + '[attr.aria-disabled]': 'state.disabled()', |
| 109 | + // '[class.cdk-option-active]': 'isActive()', |
| 110 | + // '(click)': '_clicked.next($event)', |
| 111 | + // '(focus)': '_handleFocus()', |
| 112 | + }, |
| 113 | +}) |
| 114 | +export class CdkOption { |
| 115 | + /** Whether an item is disabled. */ |
| 116 | + disabled = input<boolean>(false); |
| 117 | + |
| 118 | + /** The text used by the typeahead search. */ |
| 119 | + label = input<string>(); |
| 120 | + |
| 121 | + /** The text used by the typeahead search. */ |
| 122 | + searchTerm = computed(() => this.label() ?? this.element().textContent); |
| 123 | + |
| 124 | + /** A reference to the option element. */ |
| 125 | + private elementRef = inject(ElementRef); |
| 126 | + |
| 127 | + element = computed(() => this.elementRef.nativeElement); |
| 128 | + |
| 129 | + /** The parent CdkListbox. */ |
| 130 | + private cdkListbox = inject(CdkListbox); |
| 131 | + |
| 132 | + /** The parent Listbox UIPattern. */ |
| 133 | + listbox = computed(() => this.cdkListbox.state); |
| 134 | + |
| 135 | + /** The Option UIPattern. */ |
| 136 | + state: Option = new Option(this); |
| 137 | +} |
0 commit comments