Skip to content

Commit e3ded0e

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 e048c2a commit e3ded0e

File tree

16 files changed

+39
-38
lines changed

16 files changed

+39
-38
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
@@ -123,8 +123,8 @@ export class MatButton extends _MatButtonMixinBase
123123
}
124124

125125
/** Focuses the button. */
126-
focus(): void {
127-
this._getHostElement().focus();
126+
focus(options?: FocusOptions): void {
127+
this._getHostElement().focus(options);
128128
}
129129

130130
_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
@@ -149,8 +149,8 @@ export class MatChipInput implements MatChipTextControl, OnChanges {
149149
}
150150

151151
/** Focuses the input. */
152-
focus(): void {
153-
this._inputElement.focus();
152+
focus(options?: FocusOptions): void {
153+
this._inputElement.focus(options);
154154
}
155155

156156
/** 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 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
@@ -160,8 +160,8 @@ export class MatExpansionPanelHeader implements OnDestroy, FocusableOption {
160160
* @param origin Origin of the action that triggered the focus.
161161
* @docs-private
162162
*/
163-
focus(origin: FocusOrigin = 'program') {
164-
this._focusMonitor.focusVia(this._element, origin);
163+
focus(origin: FocusOrigin = 'program', options?: FocusOptions) {
164+
this._focusMonitor.focusVia(this._element, origin, options);
165165
}
166166

167167
ngOnDestroy() {

src/lib/input/input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export class MatInput extends _MatInputMixinBase implements MatFormFieldControl<
310310
}
311311

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

315315
/** Callback for the cases where the focused state of the input changes. */
316316
_focusChanged(isFocused: boolean) {

src/lib/list/selection-list.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ export class MatListOption extends _MatListOptionMixinBase
280280
providers: [MAT_SELECTION_LIST_VALUE_ACCESSOR],
281281
changeDetection: ChangeDetectionStrategy.OnPush
282282
})
283-
export class MatSelectionList extends _MatSelectionListMixinBase implements FocusableOption,
284-
CanDisableRipple, AfterContentInit, ControlValueAccessor, OnDestroy {
283+
export class MatSelectionList extends _MatSelectionListMixinBase implements CanDisableRipple,
284+
AfterContentInit, ControlValueAccessor, OnDestroy {
285285

286286
/** The FocusKeyManager which handles focus. */
287287
_keyManager: FocusKeyManager<MatListOption>;
@@ -374,8 +374,8 @@ export class MatSelectionList extends _MatSelectionListMixinBase implements Focu
374374
}
375375

376376
/** Focuses the last active list option. */
377-
focus() {
378-
this._element.nativeElement.focus();
377+
focus(options?: FocusOptions) {
378+
this._element.nativeElement.focus(options);
379379
}
380380

381381
/** 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
@@ -94,11 +94,11 @@ export class MatMenuItem extends _MatMenuItemMixinBase
9494
}
9595

9696
/** Focuses the menu item. */
97-
focus(origin: FocusOrigin = 'program'): void {
97+
focus(origin: FocusOrigin = 'program', options?: FocusOptions): void {
9898
if (this._focusMonitor) {
99-
this._focusMonitor.focusVia(this._getHostElement(), origin);
99+
this._focusMonitor.focusVia(this._getHostElement(), origin, options);
100100
} else {
101-
this._getHostElement().focus();
101+
this._getHostElement().focus(options);
102102
}
103103
}
104104

src/lib/menu/menu-trigger.ts

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

src/lib/radio/radio.ts

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

493493
/** Focuses the radio button. */
494-
focus(): void {
495-
this._focusMonitor.focusVia(this._inputElement, 'keyboard');
494+
focus(options?: FocusOptions): void {
495+
this._focusMonitor.focusVia(this._inputElement, 'keyboard', options);
496496
}
497497

498498
/**

src/lib/select/select.ts

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

10111011
/** Focuses the select element. */
1012-
focus(): void {
1013-
this._elementRef.nativeElement.focus();
1012+
focus(options?: FocusOptions): void {
1013+
this._elementRef.nativeElement.focus(options);
10141014
}
10151015

10161016
/** 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
@@ -294,8 +294,8 @@ export class MatSlider extends _MatSliderMixinBase
294294
}
295295

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

301301
/** blur the host element */
@@ -747,8 +747,8 @@ export class MatSlider extends _MatSliderMixinBase
747747
* Focuses the native element.
748748
* Currently only used to allow a blur event to fire but will be used with keyboard input later.
749749
*/
750-
private _focusHostElement() {
751-
this._elementRef.nativeElement.focus();
750+
private _focusHostElement(options?: FocusOptions) {
751+
this._elementRef.nativeElement.focus(options);
752752
}
753753

754754
/** Blurs the native element. */

0 commit comments

Comments
 (0)