|
| 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> { |
| 10 | + <span class="hljs-title class_">Combobox</span>, |
| 11 | + <span class="hljs-title class_">ComboboxInput</span>, |
| 12 | + <span class="hljs-title class_">ComboboxPopup</span>, |
| 13 | + <span class="hljs-title class_">ComboboxPopupContainer</span>, |
| 14 | +} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/aria/combobox'</span>; |
| 15 | +<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>; |
| 16 | +<span class="hljs-keyword">import</span> { |
| 17 | + afterRenderEffect, |
| 18 | + <span class="hljs-title class_">ChangeDetectionStrategy</span>, |
| 19 | + <span class="hljs-title class_">Component</span>, |
| 20 | + <span class="hljs-title class_">ElementRef</span>, |
| 21 | + signal, |
| 22 | + viewChild, |
| 23 | +} <span class="hljs-keyword">from</span> <span class="hljs-string">'@angular/core'</span>; |
| 24 | +<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>; |
| 25 | + |
| 26 | +<span class="hljs-comment">/** <span class="hljs-doctag">@title</span> Readonly multiselectable combobox. */</span> |
| 27 | +<span class="hljs-meta">@Component</span>({ |
| 28 | + <span class="hljs-attr">selector</span>: <span class="hljs-string">'combobox-readonly-multiselect-example'</span>, |
| 29 | + <span class="hljs-attr">templateUrl</span>: <span class="hljs-string">'combobox-readonly-multiselect-example.html'</span>, |
| 30 | + <span class="hljs-attr">styleUrl</span>: <span class="hljs-string">'../combobox-examples.css'</span>, |
| 31 | + <span class="hljs-attr">imports</span>: [ |
| 32 | + <span class="hljs-title class_">Combobox</span>, |
| 33 | + <span class="hljs-title class_">ComboboxInput</span>, |
| 34 | + <span class="hljs-title class_">ComboboxPopup</span>, |
| 35 | + <span class="hljs-title class_">ComboboxPopupContainer</span>, |
| 36 | + <span class="hljs-title class_">Listbox</span>, |
| 37 | + <span class="hljs-title class_">Option</span>, |
| 38 | + <span class="hljs-title class_">FormsModule</span>, |
| 39 | + ], |
| 40 | + <span class="hljs-attr">changeDetection</span>: <span class="hljs-title class_">ChangeDetectionStrategy</span>.<span class="hljs-property">OnPush</span>, |
| 41 | +}) |
| 42 | +<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">ComboboxReadonlyMultiselectExample</span> { |
| 43 | + popover = viewChild<<span class="hljs-title class_">ElementRef</span>>(<span class="hljs-string">'popover'</span>); |
| 44 | + listbox = viewChild<<span class="hljs-title class_">Listbox</span><<span class="hljs-built_in">any</span>>>(<span class="hljs-title class_">Listbox</span>); |
| 45 | + combobox = viewChild<<span class="hljs-title class_">Combobox</span><<span class="hljs-built_in">any</span>>>(<span class="hljs-title class_">Combobox</span>); |
| 46 | + |
| 47 | + options = <span class="hljs-function">() =></span> states; |
| 48 | + searchString = <span class="hljs-title function_">signal</span>(<span class="hljs-string">''</span>); |
| 49 | + |
| 50 | + <span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>) { |
| 51 | + <span class="hljs-title function_">afterRenderEffect</span>(<span class="hljs-function">() =></span> { |
| 52 | + <span class="hljs-keyword">const</span> popover = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">popover</span>()!; |
| 53 | + <span class="hljs-keyword">const</span> combobox = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">combobox</span>()!; |
| 54 | + combobox.<span class="hljs-property">_pattern</span>.<span class="hljs-title function_">expanded</span>() ? <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">showPopover</span>() : popover.<span class="hljs-property">nativeElement</span>.<span class="hljs-title function_">hidePopover</span>(); |
| 55 | + |
| 56 | + <span class="hljs-comment">// TODO(wagnermaciel): Make this easier for developers to do.</span> |
| 57 | + <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">listbox</span>()?.<span class="hljs-property">_pattern</span>.<span class="hljs-property">inputs</span>.<span class="hljs-title function_">activeItem</span>()?.<span class="hljs-title function_">element</span>()?.<span class="hljs-title function_">scrollIntoView</span>({<span class="hljs-attr">block</span>: <span class="hljs-string">'nearest'</span>}); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + <span class="hljs-title function_">showPopover</span>(<span class="hljs-params"></span>) { |
| 62 | + <span class="hljs-keyword">const</span> popover = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">popover</span>()!; |
| 63 | + <span class="hljs-keyword">const</span> combobox = <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">combobox</span>()!; |
| 64 | + |
| 65 | + <span class="hljs-keyword">const</span> comboboxRect = combobox.<span class="hljs-property">_pattern</span>.<span class="hljs-property">inputs</span>.<span class="hljs-title function_">inputEl</span>()?.<span class="hljs-title function_">getBoundingClientRect</span>(); |
| 66 | + <span class="hljs-keyword">const</span> popoverEl = popover.<span class="hljs-property">nativeElement</span>; |
| 67 | + |
| 68 | + <span class="hljs-keyword">if</span> (comboboxRect) { |
| 69 | + popoverEl.<span class="hljs-property">style</span>.<span class="hljs-property">width</span> = <span class="hljs-string">`<span class="hljs-subst">${comboboxRect.width}</span>px`</span>; |
| 70 | + popoverEl.<span class="hljs-property">style</span>.<span class="hljs-property">top</span> = <span class="hljs-string">`<span class="hljs-subst">${comboboxRect.bottom + <span class="hljs-number">4</span>}</span>px`</span>; |
| 71 | + popoverEl.<span class="hljs-property">style</span>.<span class="hljs-property">left</span> = <span class="hljs-string">`<span class="hljs-subst">${comboboxRect.left - <span class="hljs-number">1</span>}</span>px`</span>; |
| 72 | + } |
| 73 | + |
| 74 | + popover.<span class="hljs-property">nativeElement</span>.<span class="hljs-title function_">showPopover</span>(); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +<span class="hljs-keyword">const</span> states = [<span class="hljs-string">'Option 1'</span>, <span class="hljs-string">'Option 2'</span>, <span class="hljs-string">'Option 3'</span>]; |
0 commit comments