Skip to content

Commit 3630729

Browse files
committed
docs(aria/combobox): readonly example
1 parent d7b36d7 commit 3630729

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

src/components-examples/aria/combobox/combobox-examples.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
border-radius: var(--mat-sys-corner-extra-small);
1919
}
2020

21+
.example-combobox-input[readonly='true'] {
22+
cursor: pointer;
23+
padding: 0.7rem 1rem;
24+
}
25+
2126
.example-combobox-container:focus-within .example-combobox-input {
2227
outline: 1.5px solid var(--mat-sys-primary);
2328
box-shadow: 0 0 0 4px color-mix(in srgb, var(--mat-sys-primary) 25%, transparent);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<div ngCombobox #combobox="ngCombobox" class="example-combobox-container" [readonly]="true">
2+
<div class="example-combobox-input-container">
3+
<input
4+
ngComboboxInput
5+
class="example-combobox-input"
6+
placeholder="Search..."
7+
[(value)]="searchString"
8+
/>
9+
<span class="material-symbols-outlined example-icon example-arrow-icon">arrow_drop_down</span>
10+
</div>
11+
12+
<div popover="manual" #popover class="example-popover">
13+
<ng-template ngComboboxPopupContainer>
14+
<div ngListbox class="example-listbox">
15+
@for (option of options(); track option) {
16+
<div
17+
class="example-option example-selectable example-stateful"
18+
ngOption
19+
[value]="option"
20+
[label]="option"
21+
>
22+
<span>{{option}}</span>
23+
<span
24+
aria-hidden="true"
25+
class="material-symbols-outlined example-icon example-selected-icon"
26+
>check</span
27+
>
28+
</div>
29+
}
30+
</div>
31+
</ng-template>
32+
</div>
33+
</div>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
Combobox,
11+
ComboboxInput,
12+
ComboboxPopup,
13+
ComboboxPopupContainer,
14+
} from '@angular/aria/combobox';
15+
import {Listbox, Option} from '@angular/aria/listbox';
16+
import {
17+
afterRenderEffect,
18+
ChangeDetectionStrategy,
19+
Component,
20+
computed,
21+
ElementRef,
22+
signal,
23+
viewChild,
24+
} from '@angular/core';
25+
import {FormsModule} from '@angular/forms';
26+
27+
/** @title Readonly combobox. */
28+
@Component({
29+
selector: 'combobox-readonly-example',
30+
templateUrl: 'combobox-readonly-example.html',
31+
styleUrl: '../combobox-examples.css',
32+
imports: [
33+
Combobox,
34+
ComboboxInput,
35+
ComboboxPopup,
36+
ComboboxPopupContainer,
37+
Listbox,
38+
Option,
39+
FormsModule,
40+
],
41+
changeDetection: ChangeDetectionStrategy.OnPush,
42+
})
43+
export class ComboboxReadonlyExample {
44+
popover = viewChild<ElementRef>('popover');
45+
listbox = viewChild<Listbox<any>>(Listbox);
46+
combobox = viewChild<Combobox<any>>(Combobox);
47+
48+
options = () => states;
49+
searchString = signal('');
50+
51+
constructor() {
52+
afterRenderEffect(() => {
53+
const popover = this.popover()!;
54+
const combobox = this.combobox()!;
55+
combobox.pattern.expanded() ? this.showPopover() : popover.nativeElement.hidePopover();
56+
57+
// TODO(wagnermaciel): Make this easier for developers to do.
58+
this.listbox()?.pattern.inputs.activeItem()?.element().scrollIntoView({block: 'nearest'});
59+
});
60+
}
61+
62+
showPopover() {
63+
const popover = this.popover()!;
64+
const combobox = this.combobox()!;
65+
66+
const comboboxRect = combobox.pattern.inputs.inputEl()?.getBoundingClientRect();
67+
const popoverEl = popover.nativeElement;
68+
69+
if (comboboxRect) {
70+
popoverEl.style.width = `${comboboxRect.width}px`;
71+
popoverEl.style.top = `${comboboxRect.bottom + 4}px`;
72+
popoverEl.style.left = `${comboboxRect.left - 1}px`;
73+
}
74+
75+
popover.nativeElement.showPopover();
76+
}
77+
}
78+
79+
const states = ['Option 1', 'Option 2', 'Option 3'];

src/components-examples/aria/combobox/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export {ComboboxHighlightExample} from './combobox-highlight/combobox-highlight-
44
export {ComboboxTreeManualExample} from './combobox-tree-manual/combobox-tree-manual-example';
55
export {ComboboxTreeAutoSelectExample} from './combobox-tree-auto-select/combobox-tree-auto-select-example';
66
export {ComboboxTreeHighlightExample} from './combobox-tree-highlight/combobox-tree-highlight-example';
7+
export {ComboboxReadonlyExample} from './combobox-readonly/combobox-readonly-example';

src/dev-app/aria-combobox/combobox-demo.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,10 @@ <h2>Combobox with tree popup and auto-select filtering</h2>
2929
<h2>Combobox with tree popup and highlight filtering</h2>
3030
<combobox-tree-highlight-example></combobox-tree-highlight-example>
3131
</div>
32+
33+
<div class="example-combobox-container">
34+
<h2>Readonly Combobox</h2>
35+
<combobox-readonly-example></combobox-readonly-example>
36+
</div>
3237
</div>
3338
</div>

src/dev-app/aria-combobox/combobox-demo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ComboboxTreeAutoSelectExample,
1414
ComboboxTreeHighlightExample,
1515
ComboboxTreeManualExample,
16+
ComboboxReadonlyExample,
1617
} from '@angular/components-examples/aria/combobox';
1718
import {ChangeDetectionStrategy, Component} from '@angular/core';
1819

@@ -26,6 +27,7 @@ import {ChangeDetectionStrategy, Component} from '@angular/core';
2627
ComboboxTreeManualExample,
2728
ComboboxTreeAutoSelectExample,
2829
ComboboxTreeHighlightExample,
30+
ComboboxReadonlyExample,
2931
],
3032
changeDetection: ChangeDetectionStrategy.OnPush,
3133
})

0 commit comments

Comments
 (0)