|
| 1 | +<span class="hljs-comment">/** |
| 2 | + * <span class="hljs-doctag">@license</span> |
| 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 | + */</span> |
| 8 | + |
| 9 | +<span class="hljs-keyword">import</span> {<span class="hljs-title class_">Combobox</span>, <span class="hljs-title class_">ComboboxPopup</span>, <span class="hljs-title class_">ComboboxWidget</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/aria/combobox'</span>; |
| 10 | +<span class="hljs-keyword">import</span> {<span class="hljs-title class_">Listbox</span>, <span class="hljs-title class_">Option</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/aria/listbox'</span>; |
| 11 | +<span class="hljs-keyword">import</span> { |
| 12 | + afterRenderEffect, |
| 13 | + <span class="hljs-title class_">ChangeDetectionStrategy</span>, |
| 14 | + <span class="hljs-title class_">Component</span>, |
| 15 | + computed, |
| 16 | + signal, |
| 17 | + viewChild, |
| 18 | +} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>; |
| 19 | +<span class="hljs-keyword">import</span> {<span class="hljs-variable constant_">COUNTRIES</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'../countries'</span>; |
| 20 | +<span class="hljs-keyword">import</span> {<span class="hljs-title class_">OverlayModule</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/cdk/overlay'</span>; |
| 21 | +<span class="hljs-keyword">import</span> {<span class="hljs-title class_">FormsModule</span>} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/forms'</span>; |
| 22 | + |
| 23 | +<span class="hljs-comment">/** <span class="hljs-doctag">@title</span> Autocomplete with auto-select filtering. */</span> |
| 24 | +<span class="hljs-meta">@Component</span>({ |
| 25 | + <span class="hljs-attr">selector</span>: <span class="hljs-string">'autocomplete-auto-select-example'</span>, |
| 26 | + <span class="hljs-attr">templateUrl</span>: <span class="hljs-string">'autocomplete-auto-select-example.html'</span>, |
| 27 | + <span class="hljs-attr">styleUrl</span>: <span class="hljs-string">'../autocomplete.css'</span>, |
| 28 | + <span class="hljs-attr">imports</span>: [<span class="hljs-title class_">Combobox</span>, <span class="hljs-title class_">ComboboxPopup</span>, <span class="hljs-title class_">ComboboxWidget</span>, <span class="hljs-title class_">Listbox</span>, <span class="hljs-title class_">Option</span>, <span class="hljs-title class_">OverlayModule</span>, <span class="hljs-title class_">FormsModule</span>], |
| 29 | + <span class="hljs-attr">changeDetection</span>: <span class="hljs-title class_">ChangeDetectionStrategy</span>.<span class="hljs-property">OnPush</span>, |
| 30 | +}) |
| 31 | +<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">AutocompleteAutoSelectExample</span> { |
| 32 | + <span class="hljs-comment">/** The selected value of the combobox. */</span> |
| 33 | + <span class="hljs-keyword">readonly</span> listbox = <span class="hljs-title function_">viewChild</span>(<span class="hljs-title class_">Listbox</span>); |
| 34 | + <span class="hljs-keyword">readonly</span> combobox = <span class="hljs-title function_">viewChild</span>(<span class="hljs-title class_">Combobox</span>); |
| 35 | + |
| 36 | + popupExpanded = <span class="hljs-title function_">signal</span>(<span class="hljs-literal">false</span>); |
| 37 | + searchString = <span class="hljs-title function_">signal</span>(<span class="hljs-string">''</span>); |
| 38 | + selectedOption = signal<<span class="hljs-built_in">string</span>[]>([]); |
| 39 | + |
| 40 | + <span class="hljs-comment">/** The query string used to filter the list of countries. */</span> |
| 41 | + query = <span class="hljs-title function_">computed</span>(<span class="hljs-function">() =></span> <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">searchString</span>()); |
| 42 | + |
| 43 | + <span class="hljs-comment">/** The list of countries filtered by the query. */</span> |
| 44 | + countries = <span class="hljs-title function_">computed</span>(<span class="hljs-function">() =></span> |
| 45 | + <span class="hljs-variable constant_">COUNTRIES</span>.<span class="hljs-title function_">filter</span>(<span class="hljs-function"><span class="hljs-params">country</span> =></span> country.<span class="hljs-title function_">toLowerCase</span>().<span class="hljs-title function_">startsWith</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">query</span>().<span class="hljs-title function_">toLowerCase</span>())), |
| 46 | + ); |
| 47 | + |
| 48 | + <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) { |
| 49 | + <span class="hljs-title function_">afterRenderEffect</span>(<span class="hljs-function">() =></span> { |
| 50 | + <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">listbox</span>()?.<span class="hljs-title function_">scrollActiveItemIntoView</span>(); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + <span class="hljs-comment">/** Clears the query and the listbox value. */</span> |
| 55 | + <span class="hljs-title function_">clear</span>(): <span class="hljs-built_in">void</span> { |
| 56 | + <span class="hljs-variable language_">this</span>.<span class="hljs-property">searchString</span>.<span class="hljs-title function_">set</span>(<span class="hljs-string">''</span>); |
| 57 | + <span class="hljs-variable language_">this</span>.<span class="hljs-property">selectedOption</span>.<span class="hljs-title function_">set</span>([]); |
| 58 | + } |
| 59 | + |
| 60 | + <span class="hljs-title function_">onCommit</span>(<span class="hljs-params"></span>) { |
| 61 | + <span class="hljs-keyword">const</span> selectedOption = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">selectedOption</span>(); |
| 62 | + <span class="hljs-keyword">if</span> (selectedOption.<span class="hljs-property">length</span> > <span class="hljs-number">0</span>) { |
| 63 | + <span class="hljs-variable language_">this</span>.<span class="hljs-property">searchString</span>.<span class="hljs-title function_">set</span>(selectedOption[<span class="hljs-number">0</span>]); |
| 64 | + } |
| 65 | + <span class="hljs-variable language_">this</span>.<span class="hljs-property">popupExpanded</span>.<span class="hljs-title function_">set</span>(<span class="hljs-literal">false</span>); |
| 66 | + <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">combobox</span>()?.<span class="hljs-property">element</span>.<span class="hljs-title function_">focus</span>(); |
| 67 | + } |
| 68 | + |
| 69 | + <span class="hljs-comment">/** Handles keydown events on the clear button. */</span> |
| 70 | + <span class="hljs-title function_">onKeydown</span>(<span class="hljs-attr">event</span>: <span class="hljs-title class_">KeyboardEvent</span>): <span class="hljs-built_in">void</span> { |
| 71 | + <span class="hljs-keyword">if</span> (event.<span class="hljs-property">key</span> === <span class="hljs-string">'Enter'</span>) { |
| 72 | + <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">clear</span>(); |
| 73 | + <span class="hljs-variable language_">this</span>.<span class="hljs-property">popupExpanded</span>.<span class="hljs-title function_">set</span>(<span class="hljs-literal">false</span>); |
| 74 | + event.<span class="hljs-title function_">stopPropagation</span>(); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments