-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CL-161] Update bit-search support autofocus (#7272)
appAutofocus currently doesn't work on the bit-search component. This PR resolves this issue by introducing a FocusableElement interface components can implement, which is respected by the autofocus directive.
- Loading branch information
Showing
6 changed files
with
72 additions
and
33 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Directive, ElementRef, Input, NgZone, Optional } from "@angular/core"; | ||
import { take } from "rxjs/operators"; | ||
|
||
import { Utils } from "@bitwarden/common/platform/misc/utils"; | ||
|
||
/** | ||
* Interface for implementing focusable components. Used by the AutofocusDirective. | ||
*/ | ||
export abstract class FocusableElement { | ||
focus: () => void; | ||
} | ||
|
||
/** | ||
* Directive to focus an element. | ||
* | ||
* @remarks | ||
* | ||
* If the component provides the `FocusableElement` interface, the `focus` | ||
* method will be called. Otherwise, the native element will be focused. | ||
*/ | ||
@Directive({ | ||
selector: "[appAutofocus], [bitAutofocus]", | ||
}) | ||
export class AutofocusDirective { | ||
@Input() set appAutofocus(condition: boolean | string) { | ||
this.autofocus = condition === "" || condition === true; | ||
} | ||
|
||
private autofocus: boolean; | ||
|
||
constructor( | ||
private el: ElementRef, | ||
private ngZone: NgZone, | ||
@Optional() private focusableElement: FocusableElement, | ||
) {} | ||
|
||
ngOnInit() { | ||
if (!Utils.isMobileBrowser && this.autofocus) { | ||
if (this.ngZone.isStable) { | ||
this.focus(); | ||
} else { | ||
this.ngZone.onStable.pipe(take(1)).subscribe(this.focus.bind(this)); | ||
} | ||
} | ||
} | ||
|
||
private focus() { | ||
if (this.focusableElement) { | ||
this.focusableElement.focus(); | ||
} else { | ||
this.el.nativeElement.focus(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./input.module"; | ||
export * from "./autofocus.directive"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters