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

Commit 3cb219c

Browse files
committed
docs(dynamic-cookbook): upgrade to use new forms api
convert exiting to use deprecated name converted to new api text warnings fix plunker text test weak text space text lint order tweak
1 parent 749c5ea commit 3cb219c

29 files changed

+516
-29
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path='../_protractor/e2e.d.ts' />
2+
'use strict';
3+
/* tslint:disable:quotemark */
4+
describe('Dynamic Form Deprecated', function () {
5+
6+
beforeAll(function () {
7+
browser.get('');
8+
});
9+
10+
it('should submit form', function () {
11+
let firstNameElement = element.all(by.css('input[id=firstName]')).get(0);
12+
expect(firstNameElement.getAttribute('value')).toEqual('Bombasto');
13+
14+
let emailElement = element.all(by.css('input[id=emailAddress]')).get(0);
15+
let email = '[email protected]';
16+
emailElement.sendKeys(email);
17+
expect(emailElement.getAttribute('value')).toEqual(email);
18+
19+
element(by.css('select option[value="solid"]')).click();
20+
21+
let saveButton = element.all(by.css('button')).get(0);
22+
saveButton.click().then(function(){
23+
expect(element(by.xpath("//strong[contains(text(),'Saved the following values')]")).isPresent()).toBe(true);
24+
});
25+
});
26+
27+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// #docregion
2+
import { Component } from '@angular/core';
3+
4+
import { DynamicFormComponent } from './dynamic-form.component';
5+
import { QuestionService } from './question.service';
6+
7+
@Component({
8+
selector: 'my-app',
9+
template: `
10+
<div>
11+
<h2>Job Application for Heroes</h2>
12+
<dynamic-form [questions]="questions"></dynamic-form>
13+
</div>
14+
`,
15+
directives: [DynamicFormComponent],
16+
providers: [QuestionService]
17+
})
18+
export class AppComponent {
19+
questions: any[];
20+
21+
constructor(service: QuestionService) {
22+
this.questions = service.getQuestions();
23+
}
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- #docregion -->
2+
<div [ngFormModel]="form">
3+
<label [attr.for]="question.key">{{question.label}}</label>
4+
5+
<div [ngSwitch]="question.controlType">
6+
7+
<input *ngSwitchWhen="'textbox'" [ngControl]="question.key"
8+
[id]="question.key" [type]="question.type">
9+
10+
<select [id]="question.key" *ngSwitchWhen="'dropdown'" [ngControl]="question.key">
11+
<option *ngFor="let opt of question.options" [value]="opt.key">{{opt.value}}</option>
12+
</select>
13+
14+
</div>
15+
16+
<div class="errorMessage" *ngIf="!isValid">{{question.label}} is required</div>
17+
</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// #docregion
2+
import { Component, Input } from '@angular/core';
3+
import { ControlGroup } from '@angular/common';
4+
5+
import { QuestionBase } from './question-base';
6+
7+
@Component({
8+
selector: 'df-question',
9+
templateUrl: 'app/dynamic-form-question.component.html'
10+
})
11+
export class DynamicFormQuestionComponent {
12+
@Input() question: QuestionBase<any>;
13+
@Input() form: ControlGroup;
14+
get isValid() { return this.form.controls[this.question.key].valid; }
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- #docregion -->
2+
<div>
3+
<form (ngSubmit)="onSubmit()" [ngFormModel]="form">
4+
5+
<div *ngFor="let question of questions" class="form-row">
6+
<df-question [question]="question" [form]="form"></df-question>
7+
</div>
8+
9+
<div class="form-row">
10+
<button type="submit" [disabled]="!form.valid">Save</button>
11+
</div>
12+
</form>
13+
14+
<div *ngIf="payLoad" class="form-row">
15+
<strong>Saved the following values</strong><br>{{payLoad}}
16+
</div>
17+
</div>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// #docregion
2+
import { Component, Input, OnInit } from '@angular/core';
3+
import { ControlGroup } from '@angular/common';
4+
5+
import { QuestionBase } from './question-base';
6+
import { QuestionControlService } from './question-control.service';
7+
import { DynamicFormQuestionComponent } from './dynamic-form-question.component';
8+
9+
@Component({
10+
selector: 'dynamic-form',
11+
templateUrl: 'app/dynamic-form.component.html',
12+
directives: [DynamicFormQuestionComponent],
13+
providers: [QuestionControlService]
14+
})
15+
export class DynamicFormComponent implements OnInit {
16+
17+
@Input() questions: QuestionBase<any>[] = [];
18+
form: ControlGroup;
19+
payLoad = '';
20+
21+
constructor(private qcs: QuestionControlService) { }
22+
23+
ngOnInit() {
24+
this.form = this.qcs.toControlGroup(this.questions);
25+
}
26+
27+
onSubmit() {
28+
this.payLoad = JSON.stringify(this.form.value);
29+
}
30+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { bootstrap } from '@angular/platform-browser-dynamic';
2+
3+
import { AppComponent } from './app.component';
4+
5+
bootstrap(AppComponent, [])
6+
.catch((err: any) => console.error(err));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// #docregion
2+
export class QuestionBase<T>{
3+
value: T;
4+
key: string;
5+
label: string;
6+
required: boolean;
7+
order: number;
8+
controlType: string;
9+
10+
constructor(options: {
11+
value?: T,
12+
key?: string,
13+
label?: string,
14+
required?: boolean,
15+
order?: number,
16+
controlType?: string
17+
} = {}) {
18+
this.value = options.value;
19+
this.key = options.key || '';
20+
this.label = options.label || '';
21+
this.required = !!options.required;
22+
this.order = options.order === undefined ? 1 : options.order;
23+
this.controlType = options.controlType || '';
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// #docregion
2+
import { Injectable } from '@angular/core';
3+
import { FormBuilder, Validators } from '@angular/common';
4+
import { QuestionBase } from './question-base';
5+
6+
@Injectable()
7+
export class QuestionControlService {
8+
constructor(private fb: FormBuilder) { }
9+
10+
toControlGroup(questions: QuestionBase<any>[] ) {
11+
let group = {};
12+
13+
questions.forEach(question => {
14+
group[question.key] = question.required ? [question.value || '', Validators.required] : [question.value || ''];
15+
});
16+
return this.fb.group(group);
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// #docregion
2+
import { QuestionBase } from './question-base';
3+
4+
export class DropdownQuestion extends QuestionBase<string> {
5+
controlType = 'dropdown';
6+
options: {key: string, value: string}[] = [];
7+
8+
constructor(options: {} = {}) {
9+
super(options);
10+
this.options = options['options'] || [];
11+
}
12+
}

0 commit comments

Comments
 (0)