Skip to content

Commit 87208a1

Browse files
committed
docs(aria/combobox): autocomplete clear buttons (#32531)
(cherry picked from commit 2775f8c)
1 parent 15ce9b4 commit 87208a1

File tree

7 files changed

+95
-1
lines changed

7 files changed

+95
-1
lines changed

src/components-examples/aria/autocomplete/autocomplete-auto-select/autocomplete-auto-select-example.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
[(ngModel)]="query"
88
ngComboboxInput
99
/>
10+
<button
11+
class="example-clear-button"
12+
aria-label="Clear"
13+
(keydown)="onKeydown($event)"
14+
(click)="clear()"
15+
>
16+
<span aria-hidden="true" class="example-clear-icon material-symbols-outlined">close</span>
17+
</button>
1018
</div>
1119

1220
<ng-template ngComboboxPopupContainer>

src/components-examples/aria/autocomplete/autocomplete-auto-select/autocomplete-auto-select-example.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ import {FormsModule} from '@angular/forms';
4444
changeDetection: ChangeDetectionStrategy.OnPush,
4545
})
4646
export class AutocompleteAutoSelectExample {
47+
/** The selected value of the combobox. */
48+
listbox = viewChild<Listbox<string>>(Listbox);
49+
4750
/** The options available in the listbox. */
4851
options = viewChildren<Option<string>>(Option);
4952

@@ -67,4 +70,19 @@ export class AutocompleteAutoSelectExample {
6770
}
6871
});
6972
}
73+
74+
/** Clears the query and the listbox value. */
75+
clear(): void {
76+
this.query.set('');
77+
this.listbox?.()?.values.set([]);
78+
}
79+
80+
/** Handles keydown events on the clear button. */
81+
onKeydown(event: KeyboardEvent): void {
82+
if (event.key === 'Enter') {
83+
this.clear();
84+
this.combobox?.()?.close();
85+
event.stopPropagation();
86+
}
87+
}
7088
}

src/components-examples/aria/autocomplete/autocomplete-highlight/autocomplete-highlight-example.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
[(ngModel)]="query"
88
ngComboboxInput
99
/>
10+
<button
11+
class="example-clear-button"
12+
aria-label="Clear"
13+
(keydown)="onKeydown($event)"
14+
(click)="clear()"
15+
>
16+
<span aria-hidden="true" class="example-clear-icon material-symbols-outlined">close</span>
17+
</button>
1018
</div>
1119

1220
<ng-template ngComboboxPopupContainer>

src/components-examples/aria/autocomplete/autocomplete-highlight/autocomplete-highlight-example.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ import {FormsModule} from '@angular/forms';
4444
changeDetection: ChangeDetectionStrategy.OnPush,
4545
})
4646
export class AutocompleteHighlightExample {
47+
/** The selected value of the combobox. */
48+
listbox = viewChild<Listbox<string>>(Listbox);
49+
4750
/** The options available in the listbox. */
4851
options = viewChildren<Option<string>>(Option);
4952

@@ -67,4 +70,19 @@ export class AutocompleteHighlightExample {
6770
}
6871
});
6972
}
73+
74+
/** Clears the query and the listbox value. */
75+
clear(): void {
76+
this.query.set('');
77+
this.listbox?.()?.values.set([]);
78+
}
79+
80+
/** Handles keydown events on the clear button. */
81+
onKeydown(event: KeyboardEvent): void {
82+
if (event.key === 'Enter') {
83+
this.clear();
84+
this.combobox?.()?.close();
85+
event.stopPropagation();
86+
}
87+
}
7088
}

src/components-examples/aria/autocomplete/autocomplete-manual/autocomplete-manual-example.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
[(ngModel)]="query"
88
ngComboboxInput
99
/>
10+
<button
11+
class="example-clear-button"
12+
aria-label="Clear"
13+
(keydown)="onKeydown($event)"
14+
(click)="clear()"
15+
>
16+
<span aria-hidden="true" class="example-clear-icon material-symbols-outlined">close</span>
17+
</button>
1018
</div>
1119

1220
<ng-template ngComboboxPopupContainer>

src/components-examples/aria/autocomplete/autocomplete-manual/autocomplete-manual-example.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ import {FormsModule} from '@angular/forms';
4444
changeDetection: ChangeDetectionStrategy.OnPush,
4545
})
4646
export class AutocompleteManualExample {
47+
/** The selected value of the combobox. */
48+
listbox = viewChild<Listbox<string>>(Listbox);
49+
4750
/** The options available in the listbox. */
4851
options = viewChildren<Option<string>>(Option);
4952

@@ -67,4 +70,19 @@ export class AutocompleteManualExample {
6770
}
6871
});
6972
}
73+
74+
/** Clears the query and the listbox value. */
75+
clear(): void {
76+
this.query.set('');
77+
this.listbox?.()?.values.set([]);
78+
}
79+
80+
/** Handles keydown events on the clear button. */
81+
onKeydown(event: KeyboardEvent): void {
82+
if (event.key === 'Enter') {
83+
this.clear();
84+
this.combobox?.()?.close();
85+
event.stopPropagation();
86+
}
87+
}
7088
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
width: 13rem;
2323
font-size: 0.9rem;
2424
border-radius: var(--mat-sys-corner-extra-small);
25-
padding: 0.7rem 0.7rem 0.7rem 2.5rem;
25+
padding: 0.7rem 2.5rem;
2626
outline-color: var(--mat-sys-primary);
2727
border: 1px solid var(--mat-sys-outline);
2828
background-color: var(--mat-sys-surface);
@@ -38,6 +38,22 @@
3838
display: none;
3939
}
4040

41+
.example-clear-button {
42+
position: absolute;
43+
right: 0.5rem;
44+
background-color: transparent;
45+
border: none;
46+
display: flex;
47+
width: 2rem;
48+
height: 2rem;
49+
align-items: center;
50+
cursor: pointer;
51+
}
52+
53+
.example-clear-icon {
54+
font-size: 1.25rem;
55+
}
56+
4157
.example-popup {
4258
width: 100%;
4359
margin-top: 2px;

0 commit comments

Comments
 (0)