463 lines
13 KiB
HTML
463 lines
13 KiB
HTML
<html lang="en"><head></head><body><form id="mainForm" method="post" action="http://plnkr.co/edit/?p=preview" target="_self"><input type="hidden" name="files[app/app.component.ts]" value="import { Component } from '@angular/core';
|
|
|
|
import { QuestionService } from './question.service';
|
|
|
|
@Component({
|
|
selector: 'my-app',
|
|
template: `
|
|
<div>
|
|
<h2>Job Application for Heroes</h2>
|
|
<dynamic-form [questions]="questions"></dynamic-form>
|
|
</div>
|
|
`,
|
|
providers: [QuestionService]
|
|
})
|
|
export class AppComponent {
|
|
questions: any[];
|
|
|
|
constructor(service: QuestionService) {
|
|
this.questions = service.getQuestions();
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/app.module.ts]" value="import { BrowserModule } from '@angular/platform-browser';
|
|
import { ReactiveFormsModule } from '@angular/forms';
|
|
import { NgModule } from '@angular/core';
|
|
|
|
import { AppComponent } from './app.component';
|
|
import { DynamicFormComponent } from './dynamic-form.component';
|
|
import { DynamicFormQuestionComponent } from './dynamic-form-question.component';
|
|
|
|
@NgModule({
|
|
imports: [ BrowserModule, ReactiveFormsModule ],
|
|
declarations: [ AppComponent, DynamicFormComponent, DynamicFormQuestionComponent ],
|
|
bootstrap: [ AppComponent ]
|
|
})
|
|
export class AppModule {
|
|
constructor() {
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/dynamic-form-question.component.ts]" value="import { Component, Input } from '@angular/core';
|
|
import { FormGroup } from '@angular/forms';
|
|
|
|
import { QuestionBase } from './question-base';
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'df-question',
|
|
templateUrl: './dynamic-form-question.component.html'
|
|
})
|
|
export class DynamicFormQuestionComponent {
|
|
@Input() question: QuestionBase<any>;
|
|
@Input() form: FormGroup;
|
|
get isValid() { return this.form.controls[this.question.key].valid; }
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/dynamic-form.component.ts]" value="import { Component, Input, OnInit } from '@angular/core';
|
|
import { FormGroup } from '@angular/forms';
|
|
|
|
import { QuestionBase } from './question-base';
|
|
import { QuestionControlService } from './question-control.service';
|
|
|
|
@Component({
|
|
moduleId: module.id,
|
|
selector: 'dynamic-form',
|
|
templateUrl: './dynamic-form.component.html',
|
|
providers: [ QuestionControlService ]
|
|
})
|
|
export class DynamicFormComponent implements OnInit {
|
|
|
|
@Input() questions: QuestionBase<any>[] = [];
|
|
form: FormGroup;
|
|
payLoad = '';
|
|
|
|
constructor(private qcs: QuestionControlService) { }
|
|
|
|
ngOnInit() {
|
|
this.form = this.qcs.toFormGroup(this.questions);
|
|
}
|
|
|
|
onSubmit() {
|
|
this.payLoad = JSON.stringify(this.form.value);
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/question-base.ts]" value="export class QuestionBase<T>{
|
|
value: T;
|
|
key: string;
|
|
label: string;
|
|
required: boolean;
|
|
order: number;
|
|
controlType: string;
|
|
|
|
constructor(options: {
|
|
value?: T,
|
|
key?: string,
|
|
label?: string,
|
|
required?: boolean,
|
|
order?: number,
|
|
controlType?: string
|
|
} = {}) {
|
|
this.value = options.value;
|
|
this.key = options.key || '';
|
|
this.label = options.label || '';
|
|
this.required = !!options.required;
|
|
this.order = options.order === undefined ? 1 : options.order;
|
|
this.controlType = options.controlType || '';
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/question-control.service.ts]" value="import { Injectable } from '@angular/core';
|
|
import { FormControl, FormGroup, Validators } from '@angular/forms';
|
|
|
|
import { QuestionBase } from './question-base';
|
|
|
|
@Injectable()
|
|
export class QuestionControlService {
|
|
constructor() { }
|
|
|
|
toFormGroup(questions: QuestionBase<any>[] ) {
|
|
let group: any = {};
|
|
|
|
questions.forEach(question => {
|
|
group[question.key] = question.required ? new FormControl(question.value || '', Validators.required)
|
|
: new FormControl(question.value || '');
|
|
});
|
|
return new FormGroup(group);
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/question-dropdown.ts]" value="import { QuestionBase } from './question-base';
|
|
|
|
export class DropdownQuestion extends QuestionBase<string> {
|
|
controlType = 'dropdown';
|
|
options: {key: string, value: string}[] = [];
|
|
|
|
constructor(options: {} = {}) {
|
|
super(options);
|
|
this.options = options['options'] || [];
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/question-textbox.ts]" value="import { QuestionBase } from './question-base';
|
|
|
|
export class TextboxQuestion extends QuestionBase<string> {
|
|
controlType = 'textbox';
|
|
type: string;
|
|
|
|
constructor(options: {} = {}) {
|
|
super(options);
|
|
this.type = options['type'] || '';
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/question.service.ts]" value="import { Injectable } from '@angular/core';
|
|
|
|
import { DropdownQuestion } from './question-dropdown';
|
|
import { QuestionBase } from './question-base';
|
|
import { TextboxQuestion } from './question-textbox';
|
|
|
|
@Injectable()
|
|
export class QuestionService {
|
|
|
|
// Todo: get from a remote source of question metadata
|
|
// Todo: make asynchronous
|
|
getQuestions() {
|
|
|
|
let questions: QuestionBase<any>[] = [
|
|
|
|
new DropdownQuestion({
|
|
key: 'brave',
|
|
label: 'Bravery Rating',
|
|
options: [
|
|
{key: 'solid', value: 'Solid'},
|
|
{key: 'great', value: 'Great'},
|
|
{key: 'good', value: 'Good'},
|
|
{key: 'unproven', value: 'Unproven'}
|
|
],
|
|
order: 3
|
|
}),
|
|
|
|
new TextboxQuestion({
|
|
key: 'firstName',
|
|
label: 'First name',
|
|
value: 'Bombasto',
|
|
required: true,
|
|
order: 1
|
|
}),
|
|
|
|
new TextboxQuestion({
|
|
key: 'emailAddress',
|
|
label: 'Email',
|
|
type: 'email',
|
|
order: 2
|
|
})
|
|
];
|
|
|
|
return questions.sort((a, b) => a.order - b.order);
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[main.ts]" value="import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
|
import { AppModule } from './app/app.module';
|
|
|
|
platformBrowserDynamic().bootstrapModule(AppModule);
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[sample.css]" value=".errorMessage{
|
|
color:red;
|
|
}
|
|
|
|
.form-row{
|
|
margin-top: 10px;
|
|
}
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[styles.css]" value="/* Master Styles */
|
|
h1 {
|
|
color: #369;
|
|
font-family: Arial, Helvetica, sans-serif;
|
|
font-size: 250%;
|
|
}
|
|
h2, h3 {
|
|
color: #444;
|
|
font-family: Arial, Helvetica, sans-serif;
|
|
font-weight: lighter;
|
|
}
|
|
body {
|
|
margin: 2em;
|
|
}
|
|
body, input[text], button {
|
|
color: #888;
|
|
font-family: Cambria, Georgia;
|
|
}
|
|
a {
|
|
cursor: pointer;
|
|
cursor: hand;
|
|
}
|
|
button {
|
|
font-family: Arial;
|
|
background-color: #eee;
|
|
border: none;
|
|
padding: 5px 10px;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
cursor: hand;
|
|
}
|
|
button:hover {
|
|
background-color: #cfd8dc;
|
|
}
|
|
button:disabled {
|
|
background-color: #eee;
|
|
color: #aaa;
|
|
cursor: auto;
|
|
}
|
|
|
|
/* Navigation link styles */
|
|
nav a {
|
|
padding: 5px 10px;
|
|
text-decoration: none;
|
|
margin-right: 10px;
|
|
margin-top: 10px;
|
|
display: inline-block;
|
|
background-color: #eee;
|
|
border-radius: 4px;
|
|
}
|
|
nav a:visited, a:link {
|
|
color: #607D8B;
|
|
}
|
|
nav a:hover {
|
|
color: #039be5;
|
|
background-color: #CFD8DC;
|
|
}
|
|
nav a.active {
|
|
color: #039be5;
|
|
}
|
|
|
|
/* items class */
|
|
.items {
|
|
margin: 0 0 2em 0;
|
|
list-style-type: none;
|
|
padding: 0;
|
|
width: 24em;
|
|
}
|
|
.items li {
|
|
cursor: pointer;
|
|
position: relative;
|
|
left: 0;
|
|
background-color: #EEE;
|
|
margin: .5em;
|
|
padding: .3em 0;
|
|
height: 1.6em;
|
|
border-radius: 4px;
|
|
}
|
|
.items li:hover {
|
|
color: #607D8B;
|
|
background-color: #DDD;
|
|
left: .1em;
|
|
}
|
|
.items li.selected {
|
|
background-color: #CFD8DC;
|
|
color: white;
|
|
}
|
|
.items li.selected:hover {
|
|
background-color: #BBD8DC;
|
|
}
|
|
.items .text {
|
|
position: relative;
|
|
top: -3px;
|
|
}
|
|
.items .badge {
|
|
display: inline-block;
|
|
font-size: small;
|
|
color: white;
|
|
padding: 0.8em 0.7em 0 0.7em;
|
|
background-color: #607D8B;
|
|
line-height: 1em;
|
|
position: relative;
|
|
left: -1px;
|
|
top: -4px;
|
|
height: 1.8em;
|
|
margin-right: .8em;
|
|
border-radius: 4px 0 0 4px;
|
|
}
|
|
/* everywhere else */
|
|
* {
|
|
font-family: Arial, Helvetica, sans-serif;
|
|
}
|
|
|
|
|
|
/*
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
*/"><input type="hidden" name="files[app/dynamic-form-question.component.html]" value="<div [formGroup]="form">
|
|
<label [attr.for]="question.key">{{question.label}}</label>
|
|
|
|
<div [ngSwitch]="question.controlType">
|
|
|
|
<input *ngSwitchCase="'textbox'" [formControlName]="question.key"
|
|
[id]="question.key" [type]="question.type">
|
|
|
|
<select [id]="question.key" *ngSwitchCase="'dropdown'" [formControlName]="question.key">
|
|
<option *ngFor="let opt of question.options" [value]="opt.key">{{opt.value}}</option>
|
|
</select>
|
|
|
|
</div>
|
|
|
|
<div class="errorMessage" *ngIf="!isValid">{{question.label}} is required</div>
|
|
</div>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="files[app/dynamic-form.component.html]" value="<div>
|
|
<form (ngSubmit)="onSubmit()" [formGroup]="form">
|
|
|
|
<div *ngFor="let question of questions" class="form-row">
|
|
<df-question [question]="question" [form]="form"></df-question>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<button type="submit" [disabled]="!form.valid">Save</button>
|
|
</div>
|
|
</form>
|
|
|
|
<div *ngIf="payLoad" class="form-row">
|
|
<strong>Saved the following values</strong><br>{{payLoad}}
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="files[index.html]" value="<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<script>document.write('<base href="' + document.location + '" />');</script>
|
|
<title>Dynamic Form</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<link rel="stylesheet" href="styles.css">
|
|
<link rel="stylesheet" href="sample.css">
|
|
|
|
<!-- Polyfills -->
|
|
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
|
|
|
|
<script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
|
|
<script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
|
|
|
|
<script src="https://cdn.rawgit.com/angular/angular.io/b3c65a9/public/docs/_examples/_boilerplate/systemjs.config.web.js"></script>
|
|
<script>
|
|
System.import('main.js').catch(function(err){ console.error(err); });
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<my-app>Loading app...</my-app>
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
|
<!--
|
|
Copyright 2016 Google Inc. All Rights Reserved.
|
|
Use of this source code is governed by an MIT-style license that
|
|
can be found in the LICENSE file at http://angular.io/license
|
|
-->"><input type="hidden" name="tags[0]" value="angular"><input type="hidden" name="tags[1]" value="example"><input type="hidden" name="tags[2]" value="cookbook"><input type="hidden" name="private" value="true"><input type="hidden" name="description" value="Angular Example - Dynamic Form"></form><script>document.getElementById("mainForm").submit();</script></body></html> |