-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathcombobox-input.ts
More file actions
92 lines (84 loc) · 2.69 KB
/
combobox-input.ts
File metadata and controls
92 lines (84 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @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,
Directive,
ElementRef,
inject,
model,
untracked,
WritableSignal,
} from '@angular/core';
import {ComboboxDialogPattern} from '../private';
import {Combobox} from './combobox';
/**
* An input that is part of a combobox. It is responsible for displaying the
* current value and handling user input for filtering and selection.
*
* This directive should be applied to an `<input>` element within an `ngCombobox`
* container. It automatically handles keyboard interactions, such as opening the
* popup and navigating through the options.
*
* ```html
* <input
* ngComboboxInput
* placeholder="Search..."
* [(value)]="searchString"
* />
* ```
*
* @developerPreview 21.0
*
* @see [Combobox](guide/aria/combobox)
* @see [Select](guide/aria/select)
* @see [Multiselect](guide/aria/multiselect)
* @see [Autocomplete](guide/aria/autocomplete)
*/
@Directive({
selector: 'input[ngComboboxInput]',
exportAs: 'ngComboboxInput',
host: {
'role': 'combobox',
'[value]': 'value()',
'[attr.aria-disabled]': 'combobox._pattern.disabled()',
'[attr.aria-expanded]': 'combobox._pattern.expanded()',
'[attr.aria-activedescendant]': 'combobox._pattern.activeDescendant()',
'[attr.aria-controls]': 'combobox._pattern.popupId()',
'[attr.aria-haspopup]': 'combobox._pattern.hasPopup()',
'[attr.aria-autocomplete]': 'combobox._pattern.autocomplete()',
'[attr.readonly]': 'combobox._pattern.readonly()',
},
})
export class ComboboxInput {
/** The element that the combobox is attached to. */
private readonly _elementRef = inject<ElementRef<HTMLInputElement>>(ElementRef);
/** A reference to the input element. */
readonly element = this._elementRef.nativeElement as HTMLElement;
/** The combobox that the input belongs to. */
readonly combobox = inject(Combobox);
/** The value of the input. */
readonly value = model<string>('');
constructor() {
(this.combobox._pattern.inputs.inputEl as WritableSignal<HTMLInputElement>).set(
this._elementRef.nativeElement,
);
this.combobox._pattern.inputs.inputValue = this.value;
const controls = this.combobox.popup()?._controls();
if (controls instanceof ComboboxDialogPattern) {
return;
}
/** Focuses & selects the first item in the combobox if the user changes the input value. */
afterRenderEffect({
write: () => {
this.value();
controls?.items();
untracked(() => this.combobox._pattern.onFilter());
},
});
}
}