-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(directives): auto trim for string values (#97)
* Added auto trim directive for string values * feature(docs): add directives doc * update(directives): correct type, remove OnInit
- Loading branch information
1 parent
023d3f3
commit 01cbd92
Showing
9 changed files
with
129 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/app/components/components/directives/directives.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<md-card> | ||
<md-card-title>Directives</md-card-title> | ||
<md-card-subtitle>Core Covalent directives & utilities</md-card-subtitle> | ||
<md-divider></md-divider> | ||
<md-card-content> | ||
<p>A directive is essentially like a component functionally without a template. There are structural and attribute directives.</p> | ||
<p>Structural directives can change the DOM layout by adding and removing DOM elements. NgFor and NgIf are two familiar examples.</p> | ||
<p>An Attribute directive can change the appearance or behavior of an element. The built-in NgStyle directive, for example, can change several element styles at the same time.</p> | ||
</md-card-content> | ||
<md-divider></md-divider> | ||
<md-card-actions> | ||
<a md-button color="primary" href="https://angular.io/docs/ts/latest/guide/attribute-directives.html" target="_blank"> | ||
More on Angular 2 directives | ||
</a> | ||
</md-card-actions> | ||
</md-card> | ||
|
||
<md-card> | ||
<md-card-title>Autotrim directive</md-card-title> | ||
<md-divider></md-divider> | ||
<md-card-content> | ||
<p class="md-body-1">Use <code>tdAutoTrim</code> on an input to automatically trim the characters.</p> | ||
<p>Try entering white spaces before or after a word this input:</p> | ||
<div layout="row"> | ||
<md-input flex tdAutoTrim [(ngModel)]="trim" placeholder="This will be autotrimmed"></md-input> | ||
</div> | ||
<p>Usage:</p> | ||
<td-highlight lang="html"> | ||
<![CDATA[ | ||
<md-input tdAutoTrim [(ngModel)]="yourmodel" placeholder="This will be autotrimmed"></md-input> | ||
]]> | ||
</td-highlight> | ||
</md-card-content> | ||
</md-card> | ||
|
||
<md-card> | ||
<md-card-title>Toggle directive</md-card-title> | ||
<md-divider></md-divider> | ||
<md-card-content> | ||
<p class="md-body-1">Use <code>[tdToggle]="variable"</code> on an element to toggle it open or closed (used in Stepper & Expansion Panels).</p> | ||
<p>Click on this to open a div:</p> | ||
<button md-raised-button color="accent" (click)="toggle()">Toggle</button> | ||
<div [tdToggle]="toggleDiv" [hidden]> | ||
Reveal or hide with a toggle click! | ||
</div> | ||
<p>HTML:</p> | ||
<td-highlight lang="html"> | ||
<![CDATA[ | ||
<button md-raised-button color="accent" (click)="toggle()">Toggle</button> | ||
<div [tdToggle]="toggleDiv" [hidden]> | ||
Reveal or hide with a toggle click! | ||
</div> | ||
]]> | ||
</td-highlight> | ||
<p>TypeScript:</p> | ||
<td-highlight lang="typescript"> | ||
toggleDiv: boolean = true; | ||
|
||
toggle(): any {{ '{' }} | ||
this.toggleDiv = !this.toggleDiv; | ||
{{ '}' }} | ||
</td-highlight> | ||
</md-card-content> | ||
</md-card> |
Empty file.
15 changes: 15 additions & 0 deletions
15
src/app/components/components/directives/directives.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'directives-demo', | ||
styleUrls: [ 'directives.component.scss' ], | ||
templateUrl: 'directives.component.html', | ||
}) | ||
export class DirectivesComponent { | ||
|
||
toggleDiv: boolean = true; | ||
|
||
toggle(): void { | ||
this.toggleDiv = !this.toggleDiv; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/platform/core/directives/auto-trim/auto-trim.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Directive } from '@angular/core'; | ||
import { HostListener, Host, Optional } from '@angular/core'; | ||
import { NgModel } from '@angular/forms'; | ||
|
||
@Directive({ | ||
selector: '[tdAutoTrim]', | ||
}) | ||
export class TdAutoTrimDirective { | ||
|
||
constructor(@Optional() @Host() private _model: NgModel) {} | ||
|
||
/** | ||
* Listens to host's (blur) event and trims value. | ||
*/ | ||
@HostListener('blur', ['$event']) | ||
onBlur(event: Event): void { | ||
if (this._model && this._model.value && typeof(this._model.value) === 'string') { | ||
this._model.update.emit(this._model.value.trim()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters