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+ 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}
0 commit comments