Skip to content

Commit b890735

Browse files
committed
chore(style guide): added hero form component
1 parent 866043a commit b890735

9 files changed

Lines changed: 117 additions & 25 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.ng-valid[required], .ng-valid.required {
2+
border-left: 5px solid #42A948;
3+
}
4+
5+
.ng-invalid:not(form) {
6+
border-left: 5px solid #a94442;
7+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<div class="container">
2+
<div [hidden]="submitted">
3+
<h1>New hero</h1>
4+
<form (ngSubmit)="onSubmit()" #heroForm="ngForm">
5+
<div class="form-group">
6+
<label for="name">Name</label>
7+
<input type="text" class="form-control" id="name"
8+
required
9+
[(ngModel)]="hero.name" name="name"
10+
#heroName="ngModel">
11+
<div [hidden]="heroName.valid || heroName.pristine"
12+
class="alert alert-danger">
13+
Name is required
14+
</div>
15+
</div>
16+
<div class="form-group">
17+
<label for="alterEgo">Alter Ego</label>
18+
<input type="text" class="form-control" id="alterEgo"
19+
[(ngModel)]="hero.alterEgo" name="alterEgo">
20+
</div>
21+
<div class="form-group">
22+
<label for="power">Hero Power</label>
23+
<select class="form-control" id="power"
24+
required
25+
[(ngModel)]="hero.power" name="power">
26+
<option *ngFor="let power of powers" [value]="power">{{power}}</option>
27+
</select>
28+
</div>
29+
<button type="submit" class="btn btn-default" [disabled]="!heroForm.form.valid">Create new hero!</button>
30+
</form>
31+
</div>
32+
<div [hidden]="!submitted">
33+
<h2>You submitted the following:</h2>
34+
<div class="row">
35+
<div class="col-xs-3">Name</div>
36+
<div class="col-xs-9 pull-left">{{ hero.name }}</div>
37+
</div>
38+
<div class="row">
39+
<div class="col-xs-3">Alter Ego</div>
40+
<div class="col-xs-9 pull-left">{{ hero.alterEgo }}</div>
41+
</div>
42+
<div class="row">
43+
<div class="col-xs-3">Power</div>
44+
<div class="col-xs-9 pull-left">{{ hero.power }}</div>
45+
</div>
46+
<br>
47+
<button class="btn btn-default" (click)="submitted=false">Edit</button>
48+
</div>
49+
</div>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Component, Input } from '@angular/core';
2+
3+
import { Hero } from './../shared/hero.model';
4+
5+
import { HeroService } from './../shared/hero.service';
6+
7+
@Component({
8+
moduleId: module.id,
9+
selector: 'toh-hero-form',
10+
templateUrl: 'hero-form.component.html',
11+
styleUrls: [ 'hero-form.component.css' ]
12+
})
13+
14+
export class HeroFormComponent {
15+
@Input() heroes: Hero[];
16+
@Input() selectedHero: Hero;
17+
18+
hero: Hero;
19+
submitted: boolean;
20+
powers: string[];
21+
22+
constructor(private heroService: HeroService) {
23+
this.hero = new Hero(-1, '', '');
24+
this.submitted = false;
25+
26+
// TODO
27+
this.powers = [ 'Really Smart', 'Super Flexible',
28+
'Super Hot', 'Weather Changer' ];
29+
}
30+
31+
onSubmit() {
32+
this.heroService.create(this.hero)
33+
.then(hero => {
34+
this.heroes.push(hero);
35+
this.selectedHero = null;
36+
this.submitted = true;
37+
});
38+
}
39+
}

app/heroes/hero-list/hero-list.component.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
margin: 0 0 2em 0;
88
list-style-type: none;
99
padding: 0;
10-
width: 15em;
10+
width: 23em;
1111
}
1212

1313
.heroes li {

app/heroes/hero-list/hero-list.component.html

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<h2>My Heroes</h2>
2-
<div>
3-
<label>Hero name:</label> <input #heroName/>
4-
<button (click)="add(heroName.value); heroName.value=''">
5-
Add
6-
</button>
7-
</div>
2+
<br>
3+
<toh-hero-form [heroes]="heroes"
4+
[selectedHero]="selectedHero">
5+
</toh-hero-form>
6+
<br>
87
<ul class="heroes">
98
<li *ngFor="let hero of heroes" (click)="onSelect(hero)"
109
[class.selected]="hero === selectedHero">
1110
<span class="badge">{{hero.id}}</span>
12-
<span>{{hero.name}}</span>
11+
<span>{{hero.name}} | {{hero.alterEgo}} | {{hero.power}}</span>
1312
<button class="remove"
1413
(click)="remove(hero); $event.stopPropagation()">x
1514
</button>

app/heroes/hero-list/hero-list.component.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ export class HeroListComponent implements OnInit {
3737
this.router.navigate([ `/${this.appConfig.routes.heroes}/`, this.selectedHero.id ]);
3838
}
3939

40-
add(name: string): void {
41-
name = name.trim();
42-
if (!name) {
43-
return;
44-
}
45-
this.heroService.create(name)
46-
.then(hero => {
47-
this.heroes.push(hero);
48-
this.selectedHero = null;
49-
});
50-
}
51-
5240
remove(hero: Hero): void {
5341
this.heroService
5442
.remove(hero.id)

app/heroes/heroes.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { HeroRoutingModule } from './heroes-routing.module';
77
import { HeroListComponent } from './hero-list/hero-list.component';
88
import { HeroSearchComponent } from './hero-search/hero-search.component';
99
import { HeroDetailComponent } from './hero-detail/hero-detail.component';
10+
import { HeroFormComponent } from './hero-form/hero-form.component';
1011

1112
import { HeroService } from './shared/hero.service';
1213

@@ -19,7 +20,8 @@ import { HeroService } from './shared/hero.service';
1920
declarations: [
2021
HeroListComponent,
2122
HeroSearchComponent,
22-
HeroDetailComponent
23+
HeroDetailComponent,
24+
HeroFormComponent
2325
],
2426
providers: [
2527
HeroService

app/heroes/shared/hero.model.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
export class Hero {
2-
id: number;
3-
name: string;
2+
constructor(
3+
public id: number,
4+
public name: string,
5+
public power: string,
6+
public alterEgo?: string
7+
) { }
48
}

app/heroes/shared/hero.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ export class HeroService {
3333
.catch(this.handleError);
3434
}
3535

36-
create(name: string): Promise<Hero> {
36+
create(hero: Hero): Promise<Hero> {
3737
return this.http
38-
.post(this.heroesUrl, JSON.stringify({ name: name }), { headers: this.headers })
38+
.post(this.heroesUrl, JSON.stringify({
39+
name: hero.name,
40+
alterEgo: hero.alterEgo,
41+
power: hero.power
42+
}), { headers: this.headers })
3943
.toPromise()
4044
.then(res => res.json().data)
4145
.catch(this.handleError);

0 commit comments

Comments
 (0)