Skip to content

Commit 64dbbb7

Browse files
committed
refactor: allow options to be passed to focus methods
Updates the `focus` methods on the different components to allow for the `FocusOptions` to be passed in. Fixes #14182.
1 parent 9e74f9d commit 64dbbb7

File tree

16 files changed

+40
-39
lines changed

16 files changed

+40
-39
lines changed

src/lib/button-toggle/button-toggle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ export class MatButtonToggle extends _MatButtonToggleMixinBase implements OnInit
491491
}
492492

493493
/** Focuses the button. */
494-
focus(): void {
495-
this._buttonElement.nativeElement.focus();
494+
focus(options?: FocusOptions): void {
495+
this._buttonElement.nativeElement.focus(options);
496496
}
497497

498498
/** Checks the button toggle due to an interaction with the underlying native button. */

src/lib/button/button.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ export class MatButton extends _MatButtonMixinBase
122122
}
123123

124124
/** Focuses the button. */
125-
focus(): void {
126-
this._getHostElement().focus();
125+
focus(options?: FocusOptions): void {
126+
this._getHostElement().focus(options);
127127
}
128128

129129
_getHostElement() {

src/lib/checkbox/checkbox.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
404404
}
405405

406406
/** Focuses the checkbox. */
407-
focus(): void {
408-
this._focusMonitor.focusVia(this._inputElement, 'keyboard');
407+
focus(options?: FocusOptions): void {
408+
this._focusMonitor.focusVia(this._inputElement, 'keyboard', options);
409409
}
410410

411411
_onInteractionEvent(event: Event) {

src/lib/chips/chip-input.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ export class MatChipInput implements MatChipTextControl, OnChanges {
150150
}
151151

152152
/** Focuses the input. */
153-
focus(): void {
154-
this._inputElement.focus();
153+
focus(options?: FocusOptions): void {
154+
this._inputElement.focus(options);
155155
}
156156

157157
/** Checks whether a keycode is one of the configured separators. */

src/lib/chips/chip-list.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
453453
* Focuses the first non-disabled chip in this chip list, or the associated input when there
454454
* are no eligible chips.
455455
*/
456-
focus(): void {
456+
focus(options?: FocusOptions): void {
457457
if (this.disabled) {
458458
return;
459459
}
@@ -466,15 +466,15 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
466466
this._keyManager.setFirstItemActive();
467467
this.stateChanges.next();
468468
} else {
469-
this._focusInput();
469+
this._focusInput(options);
470470
this.stateChanges.next();
471471
}
472472
}
473473

474474
/** Attempt to focus an input if we have one. */
475-
_focusInput() {
475+
_focusInput(options?: FocusOptions) {
476476
if (this._chipInput) {
477-
this._chipInput.focus();
477+
this._chipInput.focus(options);
478478
}
479479
}
480480

src/lib/chips/chip-text-control.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ export interface MatChipTextControl {
2222
empty: boolean;
2323

2424
/** Focuses the text control. */
25-
focus(): void;
25+
focus(options?: FocusOptions): void;
2626
}

src/lib/core/option/option.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
QueryList,
2525
ViewEncapsulation,
2626
} from '@angular/core';
27+
import {FocusOptions} from '@angular/cdk/a11y';
2728
import {Subject} from 'rxjs';
2829
import {MatOptgroup} from './optgroup';
2930

@@ -161,11 +162,11 @@ export class MatOption implements AfterViewChecked, OnDestroy {
161162
}
162163

163164
/** Sets focus onto this option. */
164-
focus(): void {
165+
focus(options?: FocusOptions): void {
165166
const element = this._getHostElement();
166167

167168
if (typeof element.focus === 'function') {
168-
element.focus();
169+
element.focus(options);
169170
}
170171
}
171172

src/lib/expansion/expansion-panel-header.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ export class MatExpansionPanelHeader implements OnDestroy, FocusableOption {
173173
* @param origin Origin of the action that triggered the focus.
174174
* @docs-private
175175
*/
176-
focus(origin: FocusOrigin = 'program') {
177-
this._focusMonitor.focusVia(this._element, origin);
176+
focus(origin: FocusOrigin = 'program', options?: FocusOptions) {
177+
this._focusMonitor.focusVia(this._element, origin, options);
178178
}
179179

180180
ngOnDestroy() {

src/lib/input/input.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
310310
}
311311

312312
/** Focuses the input. */
313-
focus(): void {
314-
this._elementRef.nativeElement.focus();
313+
focus(options?: FocusOptions): void {
314+
this._elementRef.nativeElement.focus(options);
315315
}
316316

317317
/** Callback for the cases where the focused state of the input changes. */

src/lib/list/selection-list.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ export class MatListOption extends _MatListOptionMixinBase
289289
providers: [MAT_SELECTION_LIST_VALUE_ACCESSOR],
290290
changeDetection: ChangeDetectionStrategy.OnPush
291291
})
292-
export class MatSelectionList extends _MatSelectionListMixinBase implements FocusableOption,
293-
CanDisableRipple, AfterContentInit, ControlValueAccessor, OnDestroy {
292+
export class MatSelectionList extends _MatSelectionListMixinBase implements CanDisableRipple,
293+
AfterContentInit, ControlValueAccessor, OnDestroy {
294294

295295
/** The FocusKeyManager which handles focus. */
296296
_keyManager: FocusKeyManager<MatListOption>;
@@ -383,8 +383,8 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
383383
}
384384

385385
/** Focuses the last active list option. */
386-
focus() {
387-
this._element.nativeElement.focus();
386+
focus(options?: FocusOptions) {
387+
this._element.nativeElement.focus(options);
388388
}
389389

390390
/** Selects all of the options. */

src/lib/menu/menu-item.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ export class MatMenuItem extends _MatMenuItemMixinBase
9898
}
9999

100100
/** Focuses the menu item. */
101-
focus(origin: FocusOrigin = 'program'): void {
101+
focus(origin: FocusOrigin = 'program', options?: FocusOptions): void {
102102
if (this._focusMonitor) {
103-
this._focusMonitor.focusVia(this._getHostElement(), origin);
103+
this._focusMonitor.focusVia(this._getHostElement(), origin, options);
104104
} else {
105-
this._getHostElement().focus();
105+
this._getHostElement().focus(options);
106106
}
107107
}
108108

src/lib/menu/menu-trigger.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,11 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
254254
* Focuses the menu trigger.
255255
* @param origin Source of the menu trigger's focus.
256256
*/
257-
focus(origin: FocusOrigin = 'program') {
257+
focus(origin: FocusOrigin = 'program', options?: FocusOptions) {
258258
if (this._focusMonitor) {
259-
this._focusMonitor.focusVia(this._element, origin);
259+
this._focusMonitor.focusVia(this._element, origin, options);
260260
} else {
261-
this._element.nativeElement.focus();
261+
this._element.nativeElement.focus(options);
262262
}
263263
}
264264

src/lib/radio/radio.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,8 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
477477
}
478478

479479
/** Focuses the radio button. */
480-
focus(): void {
481-
this._focusMonitor.focusVia(this._inputElement, 'keyboard');
480+
focus(options?: FocusOptions): void {
481+
this._focusMonitor.focusVia(this._inputElement, 'keyboard', options);
482482
}
483483

484484
/**

src/lib/select/select.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,8 +1023,8 @@ export class MatSelect extends _MatSelectMixinBase implements AfterContentInit,
10231023
}
10241024

10251025
/** Focuses the select element. */
1026-
focus(): void {
1027-
this._elementRef.nativeElement.focus();
1026+
focus(options?: FocusOptions): void {
1027+
this._elementRef.nativeElement.focus(options);
10281028
}
10291029

10301030
/** Gets the index of the provided option in the option list. */

src/lib/slide-toggle/slide-toggle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
280280
}
281281

282282
/** Focuses the slide-toggle. */
283-
focus(): void {
284-
this._focusMonitor.focusVia(this._inputElement, 'keyboard');
283+
focus(options?: FocusOptions): void {
284+
this._focusMonitor.focusVia(this._inputElement, 'keyboard', options);
285285
}
286286

287287
/** Toggles the checked state of the slide-toggle. */

src/lib/slider/slider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ export class MatSlider extends _MatSliderMixinBase
295295
}
296296

297297
/** set focus to the host element */
298-
focus() {
299-
this._focusHostElement();
298+
focus(options?: FocusOptions) {
299+
this._focusHostElement(options);
300300
}
301301

302302
/** blur the host element */
@@ -750,8 +750,8 @@ export class MatSlider extends _MatSliderMixinBase
750750
* Focuses the native element.
751751
* Currently only used to allow a blur event to fire but will be used with keyboard input later.
752752
*/
753-
private _focusHostElement() {
754-
this._elementRef.nativeElement.focus();
753+
private _focusHostElement(options?: FocusOptions) {
754+
this._elementRef.nativeElement.focus(options);
755755
}
756756

757757
/** Blurs the native element. */

0 commit comments

Comments
 (0)