Skip to content

Commit ca7670f

Browse files
committed
fix(material/chips): creates default aria-placeholder for chips input
Updates Angular Components Chips input so that if no placeholder value is provided and if no ariaPlaceholder value is provided, that a default value will be used. This improves accessibility by providing an aria-placeholder value for VoiceControl to use as a name for the input. Fixes b/380092814
1 parent d141f83 commit ca7670f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/dev-app/chips/chips-demo.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ <h4>Input is next sibling child of chip grid</h4>
209209
<input [matChipInputFor]="chipGrid2"
210210
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
211211
[matChipInputAddOnBlur]="addOnBlur"
212-
(matChipInputTokenEnd)="add($event)" />
212+
(matChipInputTokenEnd)="add($event)"
213+
ariaPlaceholder="New contributor input..." />
213214
</mat-form-field>
214215

215216
<p>

src/material/chips/chip-input.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export interface MatChipInputEvent {
5959
'[id]': 'id',
6060
'[attr.disabled]': 'disabled || null',
6161
'[attr.placeholder]': 'placeholder || null',
62+
'[attr.aria-placeholder]': 'getAriaPlaceholder()',
6263
'[attr.aria-invalid]': '_chipGrid && _chipGrid.ngControl ? _chipGrid.ngControl.invalid : null',
6364
'[attr.aria-required]': '_chipGrid && _chipGrid.required || null',
6465
'[attr.required]': '_chipGrid && _chipGrid.required || null',
@@ -104,6 +105,9 @@ export class MatChipInput implements MatChipTextControl, OnChanges, OnDestroy {
104105
/** The input's placeholder text. */
105106
@Input() placeholder: string = '';
106107

108+
/** Screenreader placeholder for the input, only used if placeholder is not provided. */
109+
@Input() ariaPlaceholder: string | null;
110+
107111
/** Unique id for the input. */
108112
@Input() id: string = inject(_IdGenerator).getId('mat-mdc-chip-list-input-');
109113

@@ -125,6 +129,10 @@ export class MatChipInput implements MatChipTextControl, OnChanges, OnDestroy {
125129
/** The native input element to which this directive is attached. */
126130
readonly inputElement!: HTMLInputElement;
127131

132+
/** Default Screen-reader placeholder for the input if no placeholder or
133+
* ariaPlaceholder is provided. */
134+
private readonly _defaultAriaPlaceholder = 'Enter input';
135+
128136
constructor(...args: unknown[]);
129137

130138
constructor() {
@@ -223,4 +231,11 @@ export class MatChipInput implements MatChipTextControl, OnChanges, OnDestroy {
223231
private _isSeparatorKey(event: KeyboardEvent) {
224232
return !hasModifierKey(event) && new Set(this.separatorKeyCodes).has(event.keyCode);
225233
}
234+
235+
/** Checks whether placeholder is used, if not checks for ariaPlaceholder, and resorts
236+
* to default value if neither is provided.
237+
*/
238+
getAriaPlaceholder(): string | null {
239+
return this.placeholder ? null : this.ariaPlaceholder || this._defaultAriaPlaceholder;
240+
}
226241
}

0 commit comments

Comments
 (0)