Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 91044c5

Browse files
committed
feat(navbar): Add basic searchbar component to site.
1 parent e704d54 commit 91044c5

File tree

9 files changed

+140
-7
lines changed

9 files changed

+140
-7
lines changed

src/app/shared/navbar/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './navbar';
2+
export * from './searchbar';

src/app/shared/navbar/navbar.html

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</a>
99
<a md-button class="docs-button" routerLink="components">Components</a>
1010
<a md-button class="docs-button" routerLink="guides">Guides</a>
11+
<search-bar-component></search-bar-component>
1112
<a md-button class="docs-button" href="https://github.com/angular/material2" aria-label="GitHub Repository">
1213
<img class="docs-github-logo"
1314
src="../../../assets/img/homepage/github-circle-white-transparent.svg"

src/app/shared/navbar/navbar.scss

-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
display: flex;
33
flex-wrap: wrap;
44
padding: 8px 16px;
5-
6-
> .mat-button {
7-
&:last-child {
8-
margin-left: auto;
9-
}
10-
}
115
}
126

137
.docs-angular-logo {
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './searchbar';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<div class="search-input-container">
2+
<input
3+
placeholder="Search"
4+
type="text"
5+
(focus)="toggleIsExpanded(true)"
6+
(blur)="toggleIsExpanded(false)"
7+
(keyup.enter)="navigate($event.target.value.toLowerCase())"
8+
[mdAutocomplete]="auto"
9+
[formControl]="searchControl">
10+
</div>
11+
12+
<md-autocomplete #auto="mdAutocomplete">
13+
<md-option *ngFor="let item of filteredSuggestions | async" [value]="item.name">
14+
{{item.name}}
15+
</md-option>
16+
</md-autocomplete>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
$input-bg: #85D9E2;
2+
3+
@mixin color-placeholder() {
4+
-webkit-font-smoothing: antialiased;
5+
color: white;
6+
}
7+
8+
:host {
9+
position: relative;
10+
flex: 2;
11+
12+
* {
13+
box-sizing: border-box;
14+
}
15+
16+
&.expanded .search-input-container {
17+
width: 100%;
18+
}
19+
20+
.search-input-container {
21+
display: block;
22+
position: relative;
23+
margin-left: auto;
24+
height: 100%;
25+
width: 200px;
26+
transition: width .2s ease;
27+
&:after {
28+
content: '';
29+
position: absolute;
30+
left: 15px; top: 50%;
31+
transform: translateY(-50%);
32+
height: 28px;
33+
width: 28px;
34+
background: url('../../../../assets/img/icons/ic_search_white_24px.svg') center center no-repeat;
35+
}
36+
}
37+
38+
input {
39+
background: $input-bg;
40+
border: none;
41+
border-radius: 2px;
42+
color: white;
43+
font-size: 18px;
44+
height: 95%;
45+
line-height: 95%;
46+
padding-left: 50px;
47+
position: relative;
48+
transition: width .2s ease;
49+
width: 100%;
50+
51+
/* Set placeholder text to be white */
52+
&::-webkit-input-placeholder { @include color-placeholder(); } /* Chrome/Opera/Safari */
53+
&::-moz-placeholder { @include color-placeholder(); } /* Firefox 19+ */
54+
&:-moz-placeholder { @include color-placeholder(); } /* Firefox 18- */
55+
&:ms-input-placeholder { @include color-placeholder(); } /* IE 10+ */
56+
57+
&:focus {
58+
outline: none;
59+
}
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {Component, HostBinding} from '@angular/core';
2+
import {Router} from '@angular/router';
3+
import {FormControl} from '@angular/forms';
4+
import {MdSnackBar} from '@angular/material';
5+
import {Observable} from 'rxjs';
6+
7+
import {DocumentationItems, DocItem} from '../../documentation-items/documentation-items';
8+
9+
10+
@Component({
11+
selector: 'search-bar-component',
12+
templateUrl: './searchbar.html',
13+
styleUrls: ['./searchbar.scss']
14+
})
15+
16+
export class SearchBar {
17+
@HostBinding('class.expanded') _isExpanded = false;
18+
public allDocItems: DocItem[];
19+
public filteredSuggestions: Observable<DocItem[]>;
20+
public searchControl: FormControl = new FormControl('');
21+
22+
constructor(
23+
private _docItems: DocumentationItems,
24+
private _router: Router,
25+
private _snackBar: MdSnackBar
26+
) {
27+
this.allDocItems = _docItems.getAllItems();
28+
this.filteredSuggestions = this.searchControl.valueChanges
29+
.startWith(null)
30+
.map(item => this.filterSearchSuggestions(item));
31+
}
32+
33+
public toggleIsExpanded() {
34+
this._isExpanded = !this._isExpanded;
35+
}
36+
37+
public filterSearchSuggestions(searchTerm): DocItem[] {
38+
return this.allDocItems.filter(item => new RegExp(`^${searchTerm}`, 'gi').test(item.name));
39+
}
40+
41+
public navigate(searchValue) {
42+
const category = this.allDocItems.find(item => item.name.toLowerCase() === searchValue);
43+
category ?
44+
this._router.navigateByUrl(`/components/component/${category.id}`) :
45+
this._showError();
46+
}
47+
48+
private _showError() {
49+
this._snackBar.open('No search results found.', null, {duration: 3000});
50+
}
51+
}

src/app/shared/shared-module.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {NgModule} from '@angular/core';
22
import {HttpModule} from '@angular/http';
3+
import {ReactiveFormsModule} from '@angular/forms';
34
import {DocViewer} from './doc-viewer/doc-viewer';
45
import {ExampleViewer} from './example-viewer/example-viewer';
56
import {DocumentationItems} from './documentation-items/documentation-items';
67
import {NavBar} from './navbar/navbar';
8+
import {SearchBar} from './navbar/searchbar/searchbar';
79
import {MaterialModule} from '@angular/material';
810
import {BrowserModule} from '@angular/platform-browser';
911
import {RouterModule} from '@angular/router';
@@ -16,9 +18,10 @@ import {GuideItems} from './guide-items/guide-items';
1618
HttpModule,
1719
RouterModule,
1820
BrowserModule,
21+
ReactiveFormsModule,
1922
MaterialModule,
2023
],
21-
declarations: [DocViewer, ExampleViewer, NavBar, PlunkerButton],
24+
declarations: [DocViewer, ExampleViewer, NavBar, SearchBar, PlunkerButton],
2225
exports: [DocViewer, ExampleViewer, NavBar, PlunkerButton],
2326
providers: [DocumentationItems, GuideItems],
2427
entryComponents: [
Loading

0 commit comments

Comments
 (0)