Skip to content

Commit 1e5e04b

Browse files
authored
Merge pull request #379 from youda97/toolbar-search
feat(search): Add toolbar search
2 parents a4e8721 + 61a6c6a commit 1e5e04b

File tree

3 files changed

+66
-13
lines changed

3 files changed

+66
-13
lines changed

src/i18n/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@
9696
"SEARCH": {
9797
"LABEL": "Search",
9898
"PLACEHOLDER": "Search",
99-
"CLEAR_BUTTON": "Clear search input"
99+
"CLEAR_BUTTON": "Clear search input",
100+
"TOOLBAR_SEARCH": "Toolbar search"
100101
},
101102
"PAGINATION": {
102103
"ITEMS_PER_PAGE": "Items per page:",

src/search/search.component.ts

Lines changed: 57 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+
[attr.aria-label]="i18n.get('SEARCH.TOOLBAR_SEARCH') | async"
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,10 +133,18 @@ 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
*/
125143
@Input() skeleton = false;
144+
/**
145+
* Set to `true` to expand the toolbar search component.
146+
*/
147+
@Input() active = false;
126148
/**
127149
* Sets the name attribute on the `input` element.
128150
*/
@@ -161,7 +183,7 @@ export class Search implements ControlValueAccessor {
161183
* @param i18n The i18n translations.
162184
* @memberof Search
163185
*/
164-
constructor(protected i18n: I18n) {
186+
constructor(protected elementRef: ElementRef, protected i18n: I18n) {
165187
Search.searchCount++;
166188
}
167189

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

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)