33 Input ,
44 EventEmitter ,
55 Output ,
6- HostBinding
6+ HostBinding ,
7+ ElementRef ,
8+ HostListener
79} from "@angular/core" ;
810import { NG_VALUE_ACCESSOR , ControlValueAccessor } from "@angular/forms" ;
911import { 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}
0 commit comments