mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
refactor(focus-monitor): accept ElementRef in focusVia (#12978)
Looks like something that was missed in #12712. For consistency with `monitor` and `stopMonitoring`, accepts an `ElementRef` in `focusVia`.
This commit is contained in:
committed by
Jeremy Elbourn
parent
f39e0911ca
commit
62136c16c4
@@ -173,13 +173,28 @@ export class FocusMonitor implements OnDestroy {
|
||||
* @param origin Focus origin.
|
||||
* @param options Options that can be used to configure the focus behavior.
|
||||
*/
|
||||
focusVia(element: HTMLElement, origin: FocusOrigin, options?: FocusOptions): void {
|
||||
focusVia(element: HTMLElement, origin: FocusOrigin, options?: FocusOptions): void;
|
||||
|
||||
/**
|
||||
* Focuses the element via the specified focus origin.
|
||||
* @param element Element to focus.
|
||||
* @param origin Focus origin.
|
||||
* @param options Options that can be used to configure the focus behavior.
|
||||
*/
|
||||
focusVia(element: ElementRef<HTMLElement>, origin: FocusOrigin, options?: FocusOptions): void;
|
||||
|
||||
focusVia(element: HTMLElement | ElementRef<HTMLElement>,
|
||||
origin: FocusOrigin,
|
||||
options?: FocusOptions): void {
|
||||
|
||||
const nativeElement = this._getNativeElement(element);
|
||||
|
||||
this._setOriginForCurrentEventQueue(origin);
|
||||
|
||||
// `focus` isn't available on the server
|
||||
if (typeof element.focus === 'function') {
|
||||
if (typeof nativeElement.focus === 'function') {
|
||||
// Cast the element to `any`, because the TS typings don't have the `options` parameter yet.
|
||||
(element as any).focus(options);
|
||||
(nativeElement as any).focus(options);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -410,7 +410,7 @@ export class MatCheckbox extends _MatCheckboxMixinBase implements ControlValueAc
|
||||
|
||||
/** Focuses the checkbox. */
|
||||
focus(): void {
|
||||
this._focusMonitor.focusVia(this._inputElement.nativeElement, 'keyboard');
|
||||
this._focusMonitor.focusVia(this._inputElement, 'keyboard');
|
||||
}
|
||||
|
||||
_onInteractionEvent(event: Event) {
|
||||
|
||||
@@ -86,7 +86,7 @@ export class MatExpansionPanelHeader implements OnDestroy, FocusableOption {
|
||||
// Avoids focus being lost if the panel contained the focused element and was closed.
|
||||
panel.closed
|
||||
.pipe(filter(() => panel._containsFocus()))
|
||||
.subscribe(() => _focusMonitor.focusVia(_element.nativeElement, 'program'));
|
||||
.subscribe(() => _focusMonitor.focusVia(_element, 'program'));
|
||||
|
||||
_focusMonitor.monitor(_element).subscribe(origin => {
|
||||
if (origin && panel.accordion) {
|
||||
@@ -158,7 +158,7 @@ export class MatExpansionPanelHeader implements OnDestroy, FocusableOption {
|
||||
* @docs-private
|
||||
*/
|
||||
focus(origin: FocusOrigin = 'program') {
|
||||
this._focusMonitor.focusVia(this._element.nativeElement, origin);
|
||||
this._focusMonitor.focusVia(this._element, origin);
|
||||
}
|
||||
|
||||
ngOnDestroy() {
|
||||
|
||||
@@ -224,7 +224,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
|
||||
*/
|
||||
focus(origin: FocusOrigin = 'program') {
|
||||
if (this._focusMonitor) {
|
||||
this._focusMonitor.focusVia(this._element.nativeElement, origin);
|
||||
this._focusMonitor.focusVia(this._element, origin);
|
||||
} else {
|
||||
this._element.nativeElement.focus();
|
||||
}
|
||||
|
||||
@@ -491,7 +491,7 @@ export class MatRadioButton extends _MatRadioButtonMixinBase
|
||||
|
||||
/** Focuses the radio button. */
|
||||
focus(): void {
|
||||
this._focusMonitor.focusVia(this._inputElement.nativeElement, 'keyboard');
|
||||
this._focusMonitor.focusVia(this._inputElement, 'keyboard');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -279,7 +279,7 @@ export class MatSlideToggle extends _MatSlideToggleMixinBase implements OnDestro
|
||||
|
||||
/** Focuses the slide-toggle. */
|
||||
focus(): void {
|
||||
this._focusMonitor.focusVia(this._inputElement.nativeElement, 'keyboard');
|
||||
this._focusMonitor.focusVia(this._inputElement, 'keyboard');
|
||||
}
|
||||
|
||||
/** Toggles the checked state of the slide-toggle. */
|
||||
|
||||
@@ -84,7 +84,7 @@ export class KitchenSink {
|
||||
focusMonitor: FocusMonitor,
|
||||
elementRef: ElementRef<HTMLElement>,
|
||||
bottomSheet: MatBottomSheet) {
|
||||
focusMonitor.focusVia(elementRef.nativeElement, 'program');
|
||||
focusMonitor.focusVia(elementRef, 'program');
|
||||
snackBar.open('Hello there');
|
||||
dialog.open(TestEntryComponent);
|
||||
bottomSheet.open(TestEntryComponent);
|
||||
|
||||
Reference in New Issue
Block a user