mirror of
https://github.com/bitwarden/browser
synced 2026-02-07 12:13:45 +00:00
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.
67 lines
1.5 KiB
TypeScript
67 lines
1.5 KiB
TypeScript
import { Component, ElementRef, Input, ViewChild } from "@angular/core";
|
|
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
|
|
|
|
import { FocusableElement } from "../input/autofocus.directive";
|
|
|
|
let nextId = 0;
|
|
|
|
@Component({
|
|
selector: "bit-search",
|
|
templateUrl: "./search.component.html",
|
|
providers: [
|
|
{
|
|
provide: NG_VALUE_ACCESSOR,
|
|
multi: true,
|
|
useExisting: SearchComponent,
|
|
},
|
|
{
|
|
provide: FocusableElement,
|
|
useExisting: SearchComponent,
|
|
},
|
|
],
|
|
})
|
|
export class SearchComponent implements ControlValueAccessor, FocusableElement {
|
|
private notifyOnChange: (v: string) => void;
|
|
private notifyOnTouch: () => void;
|
|
|
|
@ViewChild("input") private input: ElementRef<HTMLInputElement>;
|
|
|
|
protected id = `search-id-${nextId++}`;
|
|
protected searchText: string;
|
|
|
|
@Input() disabled: boolean;
|
|
@Input() placeholder: string;
|
|
|
|
focus() {
|
|
this.input.nativeElement.focus();
|
|
}
|
|
|
|
onChange(searchText: string) {
|
|
if (this.notifyOnChange != undefined) {
|
|
this.notifyOnChange(searchText);
|
|
}
|
|
}
|
|
|
|
onTouch() {
|
|
if (this.notifyOnTouch != undefined) {
|
|
this.notifyOnTouch();
|
|
}
|
|
}
|
|
|
|
registerOnChange(fn: (v: string) => void): void {
|
|
this.notifyOnChange = fn;
|
|
}
|
|
|
|
registerOnTouched(fn: () => void): void {
|
|
this.notifyOnTouch = fn;
|
|
}
|
|
|
|
writeValue(searchText: string): void {
|
|
this.searchText = searchText;
|
|
}
|
|
|
|
setDisabledState(isDisabled: boolean) {
|
|
this.disabled = isDisabled;
|
|
}
|
|
}
|