Skip to content

Commit 61ac981

Browse files
authored
Merge pull request #479 from cal-smith/issue#465issue#464
fix(search): focus input, and remove it when not in use
2 parents 1d40cbd + 7d7a58a commit 61ac981

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

src/search/search.component.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,26 @@ import {
55
Output,
66
HostBinding,
77
ElementRef,
8-
HostListener
8+
HostListener,
9+
ViewChild
910
} from "@angular/core";
1011
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from "@angular/forms";
1112
import { I18n } from "../i18n/i18n.module";
1213

1314
/**
1415
* Used to emit changes performed on search components.
15-
* @export
16-
* @class SearchChange
1716
*/
1817
export class SearchChange {
1918
/**
2019
* Contains the `Search` that has been changed.
21-
* @type {Search}
22-
* @memberof SearchChange
2320
*/
2421
source: Search;
2522
/**
2623
* The value of the `Search` field encompassed in the `SearchChange` class.
27-
* @type {string}
28-
* @memberof SearchChange
2924
*/
3025
value: string;
3126
}
3227

33-
/**
34-
* @export
35-
* @class Search
36-
* @implements {ControlValueAccessor}
37-
*/
3828
@Component({
3929
selector: "ibm-search",
4030
template: `
@@ -54,6 +44,8 @@ export class SearchChange {
5444
<div *ngIf="skeleton; else enableInput" class="bx--search-input"></div>
5545
<ng-template #enableInput>
5646
<input
47+
#input
48+
*ngIf="!toolbar || active"
5749
class="bx--search-input"
5850
type="text"
5951
role="search"
@@ -178,10 +170,11 @@ export class Search implements ControlValueAccessor {
178170
*/
179171
@Output() change = new EventEmitter<SearchChange>();
180172

173+
@ViewChild("input") inputRef: ElementRef;
174+
181175
/**
182176
* Creates an instance of `Search`.
183177
* @param i18n The i18n translations.
184-
* @memberof Search
185178
*/
186179
constructor(protected elementRef: ElementRef, protected i18n: I18n) {
187180
Search.searchCount++;
@@ -197,8 +190,6 @@ export class Search implements ControlValueAccessor {
197190

198191
/**
199192
* Sets a method in order to propagate changes back to the form.
200-
* @param {any} fn
201-
* @memberof Search
202193
*/
203194
public registerOnChange(fn: any) {
204195
this.propagateChange = fn;
@@ -214,13 +205,11 @@ export class Search implements ControlValueAccessor {
214205

215206
/**
216207
* Called when search input is blurred. Needed to properly implement `ControlValueAccessor`.
217-
* @memberof Search
218208
*/
219209
onTouched: () => any = () => {};
220210

221211
/**
222212
* Method set in `registerOnChange` to propagate changes back to the form.
223-
* @memberof Search
224213
*/
225214
propagateChange = (_: any) => {};
226215

@@ -235,7 +224,6 @@ export class Search implements ControlValueAccessor {
235224

236225
/**
237226
* Called when clear button is clicked.
238-
* @memberof Search
239227
*/
240228
clearSearch(): void {
241229
this.value = "";
@@ -253,9 +241,9 @@ export class Search implements ControlValueAccessor {
253241
this.propagateChange(this.value);
254242
}
255243

256-
openSearch(event) {
244+
openSearch() {
257245
this.active = true;
258-
this.elementRef.nativeElement.querySelector(".bx--search-input").focus();
246+
setTimeout(() => this.inputRef.nativeElement.focus());
259247
}
260248

261249
@HostListener("keydown", ["$event"])
@@ -264,7 +252,7 @@ export class Search implements ControlValueAccessor {
264252
if (event.key === "Escape") {
265253
this.active = false;
266254
} else if (event.key === "Enter") {
267-
this.openSearch(event);
255+
this.openSearch();
268256
}
269257
}
270258
}

0 commit comments

Comments
 (0)