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

Commit 3e7ecfc

Browse files
committed
docs(cb-rating-component): cookbook about creating a rating component
1 parent 5fd6ae3 commit 3e7ecfc

File tree

18 files changed

+334
-0
lines changed

18 files changed

+334
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/// <reference path='../_protractor/e2e.d.ts' />
2+
'use strict';
3+
describe('Rating component', function () {
4+
5+
beforeAll(function () {
6+
browser.get('');
7+
});
8+
9+
it('should show 5 stars for windstorm', function () {
10+
const windstormRating = element.all(by.tagName('my-hero-rating')).get(0);
11+
const windstormStars = windstormRating.all(by.css('.glyphicon-star'));
12+
expect(windstormStars.count()).toBe(5);
13+
});
14+
15+
it('should show 1 star for bombasto', function() {
16+
const bombastoRating = element.all(by.tagName('my-hero-rating')).get(1);
17+
const bombastoStars = bombastoRating.all(by.css('.glyphicon-star'));
18+
expect(bombastoStars.count()).toBe(1);
19+
});
20+
21+
it('should change the rate on click', function() {
22+
const bombastoRating = element.all(by.tagName('my-hero-rating')).get(1);
23+
const bombastoFourthStar = bombastoRating.all(by.css('.glyphicon')).get(3);
24+
bombastoFourthStar.click().then(function() {
25+
const bombastoStars = bombastoRating.all(by.css('.glyphicon-star'));
26+
expect(bombastoStars.count()).toBe(4);
27+
28+
const div = element.all(by.tagName('div')).get(0);
29+
expect(div.getText()).toEqual('Bombasto rate is 4');
30+
});
31+
});
32+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.js
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// #docregion
2+
import { Component } from '@angular/core';
3+
4+
@Component({
5+
selector: 'my-app',
6+
template: `
7+
<label>Windstorm: </label>
8+
<my-hero-rating></my-hero-rating>
9+
`
10+
})
11+
export class AppComponent { }
12+
// #enddocregion
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'my-app',
5+
template: `
6+
<p>
7+
<label>Windstorm: </label>
8+
<my-hero-rating rate="5"></my-hero-rating>
9+
</p>
10+
<p>
11+
<label>Bombasto: </label>
12+
<my-hero-rating [(rate)]="bombasto"></my-hero-rating>
13+
</p>
14+
<div>Bombasto rate is {{bombasto}}</div>
15+
`
16+
})
17+
export class AppComponent {
18+
bombasto = 1;
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { NgModule } from '@angular/core';
2+
import { BrowserModule } from '@angular/platform-browser';
3+
4+
import { AppComponent } from './app.component';
5+
import { HeroRatingComponent } from './rating.component';
6+
7+
@NgModule({
8+
imports: [ BrowserModule ],
9+
declarations: [ AppComponent, HeroRatingComponent ],
10+
bootstrap: [ AppComponent ]
11+
})
12+
export class AppModule {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
2+
3+
import { AppModule } from './app.module';
4+
5+
platformBrowserDynamic().bootstrapModule(AppModule);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// #docregion
2+
import { Component } from '@angular/core';
3+
4+
@Component({
5+
selector: 'my-hero-rating',
6+
template: '<div>Rating</div>'
7+
})
8+
export class HeroRatingComponent { }
9+
// #enddocregion
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// #docregion
2+
import { Component } from '@angular/core';
3+
4+
@Component({
5+
selector: 'my-hero-rating',
6+
// #docregion template
7+
template: `
8+
<template ngFor [ngForOf]="range" let-index="index">
9+
<span class="sr-only">(*)</span>
10+
<i class="glyphicon glyphicon-star"></i>
11+
</template>
12+
`
13+
// #enddocregion template
14+
})
15+
export class HeroRatingComponent { }
16+
// #enddocregion
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// #docregion
2+
import { Component, Input } from '@angular/core';
3+
4+
@Component({
5+
selector: 'my-hero-rating',
6+
// #docregion template
7+
template: `
8+
<template ngFor [ngForOf]="range" let-index="index">
9+
<span class="sr-only">({{ index < rate ? '*' : ' ' }})</span>
10+
<i class="glyphicon {{index < rate ? 'glyphicon-star' : 'glyphicon-star-empty'}}"></i>
11+
</template>
12+
`
13+
// #enddocregion template
14+
})
15+
export class HeroRatingComponent {
16+
@Input() rate: number;
17+
// #docregion update-method
18+
update(value: number): void {
19+
this.rate = value;
20+
}
21+
// #enddocregion
22+
}
23+
// #enddocregion
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// #docregion
2+
import { Component, EventEmitter, Input, Output } from '@angular/core';
3+
4+
@Component({
5+
selector: 'my-hero-rating',
6+
// #docregion rating-template
7+
template: `
8+
<template ngFor [ngForOf]="range" let-index="index">
9+
<span class="sr-only">({{ index < rate ? '*' : ' ' }})</span>
10+
<i class="glyphicon {{index < rate ? 'glyphicon-star' : 'glyphicon-star-empty'}}" (click)="update(index + 1)"></i>
11+
</template>
12+
`
13+
// #enddocregion rating-template
14+
})
15+
export class HeroRatingComponent {
16+
// #docregion range-attribute
17+
range = new Array(5);
18+
// #enddocregion range-attribute
19+
20+
// #docregion rate-input
21+
@Input() rate: number;
22+
// #enddocregion rate-input
23+
// #docregion rate-output
24+
@Output() rateChange = new EventEmitter<number>();
25+
// #enddocregion rate-output
26+
27+
// #docregion update-method
28+
update(value: number): void {
29+
this.rate = value;
30+
this.rateChange.emit(value);
31+
}
32+
// #enddocregion update-method
33+
}
34+
// #enddocregion

0 commit comments

Comments
 (0)