File
Implements
Metadata
| encapsulation |
ViewEncapsulation.None |
| host |
{ } |
| selector |
stbui-search |
| styleUrls |
./search.component.scss |
| templateUrl |
./search.component.html |
Index
Properties
|
|
|
Methods
|
|
|
Inputs
|
|
|
Outputs
|
|
|
|
delay
|
Type : number
|
Default value : 200
|
|
|
|
placeholder
|
Type : string
|
Default value : '查找...'
|
|
|
Outputs
|
onSearch
|
Type : EventEmitter<string | number>
|
|
|
|
onSearchChange
|
Type : EventEmitter<string | number>
|
|
|
Methods
|
ngAfterViewInit
|
ngAfterViewInit()
|
|
|
|
|
|
inputRef
|
Type : ElementRef
|
Decorators :
@ViewChild('inputRef', {static: false})
|
|
|
|
inputValue
|
Type : string | number
|
|
|
import {
Component,
Input,
Output,
EventEmitter,
ElementRef,
ViewChild,
AfterViewInit,
ViewEncapsulation
} from '@angular/core';
import { fromEvent } from 'rxjs';
import { map, debounceTime, distinctUntilChanged } from 'rxjs/operators';
@Component({
selector: 'stbui-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.scss'],
host: {
class: 'stbui-search'
},
encapsulation: ViewEncapsulation.None
})
export class SearchComponent implements AfterViewInit {
inputValue: string | number;
@Input() placeholder: string = '查找...';
@Input() delay: number = 200;
@Output() onSearch: EventEmitter<string | number> = new EventEmitter<string | number>();
@Output() onSearchChange: EventEmitter<string | number> = new EventEmitter<string | number>();
@ViewChild('inputRef', { static: false }) inputRef: ElementRef;
constructor() { }
ngAfterViewInit() {
fromEvent(this.inputRef.nativeElement, 'keyup')
.pipe(
debounceTime(this.delay),
distinctUntilChanged(),
map(() => this.inputValue),
)
.subscribe(input => this.onSearchChange.emit(input));
}
enterUp() {
this.onSearch.emit(this.inputValue);
}
}
<div class="stbui-search-container">
<mat-icon>search</mat-icon>
<input
#inputRef
type="search"
autocomplete="off"
spellcheck="false"
[placeholder]="placeholder"
(keyup.enter)="enterUp()"
[(ngModel)]="inputValue"
/>
</div>
$stbui-search-height: 56px;
.stbui-search {
display: block;
&-container {
display: flex;
align-items: center;
min-height: $stbui-search-height;
max-height: $stbui-search-height;
> mat-icon {
margin-right: 12px;
margin-left: 12px;
}
> input {
flex: 1 1 auto;
height: 56px;
border: 0;
outline: none;
margin-right: 10px;
background: transparent;
}
}
}
Legend
Html element with directive