Skip to content

Commit cbfa11f

Browse files
committed
feat(search): Add toolbar search
1 parent 6d1c2fe commit cbfa11f

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

src/search/search.component.ts

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
Input,
44
EventEmitter,
55
Output,
6-
HostBinding
6+
HostBinding,
7+
ElementRef,
8+
HostListener
79
} from "@angular/core";
810
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from "@angular/forms";
911
import { I18n } from "../i18n/i18n.module";
@@ -42,7 +44,9 @@ export class SearchChange {
4244
'bx--search--sm': size === 'sm',
4345
'bx--search--lg': size === 'lg',
4446
'bx--search--light': theme === 'light',
45-
'bx--skeleton': skeleton
47+
'bx--skeleton': skeleton,
48+
'bx--toolbar-search': toolbar,
49+
'bx--toolbar-search--active': toolbar && active
4650
}"
4751
role="search">
4852
<label class="bx--label" [for]="id">{{label}}</label>
@@ -59,15 +63,25 @@ export class SearchChange {
5963
[disabled]="disabled"
6064
[required]="required"
6165
(input)="onSearch($event.target.value)"/>
62-
<svg
63-
class="bx--search-magnifier"
64-
width="16"
65-
height="16"
66-
viewBox="0 0 16 16">
67-
<path
68-
d="M6.5 12a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zm4.936-1.27l4.563 4.557-.707.708-4.563-4.558a6.5 6.5 0 1 1 .707-.707z"
69-
fill-rule="nonzero"/>
70-
</svg>
66+
<button
67+
*ngIf="toolbar; else svg"
68+
class="bx--toolbar-search__btn"
69+
aria-label="Toolbar search"
70+
tabindex="0"
71+
(click)="openSearch($event)">
72+
<ng-template [ngTemplateOutlet]="svg"></ng-template>
73+
</button>
74+
<ng-template #svg>
75+
<svg
76+
class="bx--search-magnifier"
77+
width="16"
78+
height="16"
79+
viewBox="0 0 16 16">
80+
<path
81+
d="M6.5 12a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zm4.936-1.27l4.563 4.557-.707.708-4.563-4.558a6.5 6.5 0 1 1 .707-.707z"
82+
fill-rule="nonzero"/>
83+
</svg>
84+
</ng-template>
7185
</ng-template>
7286
7387
<button
@@ -119,6 +133,10 @@ export class Search implements ControlValueAccessor {
119133
* Set to `true` for a disabled search input.
120134
*/
121135
@Input() disabled = false;
136+
/**
137+
* Set to `true` for a toolbar search component.
138+
*/
139+
@Input() toolbar = false;
122140
/**
123141
* Set to `true` for a loading search component.
124142
*/
@@ -156,12 +174,14 @@ export class Search implements ControlValueAccessor {
156174
*/
157175
@Output() change = new EventEmitter<SearchChange>();
158176

177+
active = false;
178+
159179
/**
160180
* Creates an instance of `Search`.
161181
* @param i18n The i18n translations.
162182
* @memberof Search
163183
*/
164-
constructor(protected i18n: I18n) {
184+
constructor(protected elementRef: ElementRef, protected i18n: I18n) {
165185
Search.searchCount++;
166186
}
167187

@@ -230,4 +250,27 @@ export class Search implements ControlValueAccessor {
230250
this.change.emit(event);
231251
this.propagateChange(this.value);
232252
}
253+
254+
openSearch(event) {
255+
this.active = true;
256+
this.elementRef.nativeElement.querySelector(".bx--search-input").focus();
257+
}
258+
259+
@HostListener("keydown", ["$event"])
260+
keyDown(event: KeyboardEvent) {
261+
if (this.toolbar) {
262+
if (event.key === "Escape") {
263+
this.active = false;
264+
} else if (event.key === "Enter") {
265+
this.openSearch(event);
266+
}
267+
}
268+
}
269+
270+
@HostListener("focusout", ["$event"])
271+
focusOut(event) {
272+
if (this.toolbar && event.relatedTarget === null) {
273+
this.active = false;
274+
}
275+
}
233276
}

src/search/search.stories.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ storiesOf("Search", module).addDecorator(
2020
placeholder: text("placeholder", "Search")
2121
}
2222
}))
23+
.add("Toolbar search", () => ({
24+
template: `
25+
<div class="bx--toolbar">
26+
<ibm-search placeholder="search" size="sm" toolbar="true"></ibm-search>
27+
</div>
28+
`
29+
}))
2330
.add("Skeleton", () => ({
2431
template: `
2532
<div style="width: 200px;">

0 commit comments

Comments
 (0)