/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
afterRenderEffect,
afterNextRender,
booleanAttribute,
computed,
Directive,
ElementRef,
inject,
input,
model,
OnDestroy,
signal,
Signal,
untracked,
} from '@angular/core';
import {Directionality} from '@angular/cdk/bidi';
import {_IdGenerator} from '@angular/cdk/a11y';
import {ListboxPattern, SortedCollection, tabIndexTransform, reportViolations} from '../private';
import {Option} from './option';
import {LISTBOX} from './tokens';
/**
* Represents a container used to display a list of items for a user to select from.
*
* The `ngListbox` is meant to be used in conjunction with `ngOption` directives to create a
* selectable list. It supports single and multiple selection modes, as well as various focus and
* orientation strategies.
*
* ```html
*
* @for (item of items; track item.id) {
*
* {{item.name}}
*
* }
*
* ```
*
* @see [Listbox](guide/aria/listbox)
* @see [Autocomplete](guide/aria/autocomplete)
* @see [Select](guide/aria/select)
* @see [Multiselect](guide/aria/multiselect)
*/
@Directive({
selector: '[ngListbox]',
exportAs: 'ngListbox',
host: {
'role': 'listbox',
'[attr.id]': 'id()',
'[attr.tabindex]': 'tabIndex() !== undefined ? tabIndex() : _pattern.tabIndex()',
'[attr.aria-readonly]': '_pattern.readonly()',
'[attr.aria-disabled]': '_pattern.disabled()',
'[attr.aria-orientation]': '_pattern.orientation()',
'[attr.aria-multiselectable]': '_pattern.multi()',
'[attr.aria-activedescendant]': '_pattern.activeDescendant()',
'(keydown)': '_pattern.onKeydown($event)',
'(click)': '_pattern.onClick($event)',
'(focusin)': '_pattern.onFocusIn($event)',
},
providers: [{provide: LISTBOX, useExisting: Listbox}],
})
export class Listbox implements OnDestroy {
/** A unique identifier for the listbox. */
readonly id = input(inject(_IdGenerator).getId('ng-listbox-', true));
/** A reference to the host element. */
private readonly _elementRef = inject(ElementRef);
/** A reference to the host element. */
readonly element = this._elementRef.nativeElement as HTMLElement;
/** The collection of Options. */
readonly _collection = new SortedCollection