Skip to content

Commit ca2c062

Browse files
committed
build form from formConfig array
1 parent 0c02b16 commit ca2c062

File tree

5 files changed

+129
-5
lines changed

5 files changed

+129
-5
lines changed

JavaScript/impl-angular/src/app/app.component.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44
</nav>
55

66
<div class="container">
7-
<form>
7+
<form *ngIf="true">
8+
<div *ngFor="let config of formConfig" [ngSwitch]="config.type">
9+
<app-email *ngSwitchCase="'email'" [config]="config"></app-email>
10+
<app-secret *ngSwitchCase="'secret'" [config]="config"></app-secret>
11+
<app-toggle *ngSwitchCase="'toggle'" [config]="config"></app-toggle>
12+
<app-submit *ngSwitchCase="'submit'" [config]="config"></app-submit>
13+
</div>
14+
</form>
15+
16+
17+
18+
<form *ngIf="false">
819
<div class="form-group">
920
<label>Email</label>
1021
<input type="email" class="form-control" />

JavaScript/impl-angular/src/app/app.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { BrowserModule } from '@angular/platform-browser';
22
import { NgModule } from '@angular/core';
33

44
import { AppComponent } from './app.component';
5+
import * as Controls from './controls';
56

67
@NgModule({
78
declarations: [
8-
AppComponent
9+
AppComponent,
10+
...Object.values(Controls),
911
],
1012
imports: [
11-
BrowserModule
13+
BrowserModule,
1214
],
1315
providers: [],
1416
bootstrap: [AppComponent]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Component, Input } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-email',
5+
template: `
6+
<div class="form-group">
7+
<label>{{ config.label }}</label>
8+
<input type="email" class="form-control" />
9+
</div>
10+
`})
11+
export class EmailComponent {
12+
@Input() config: any;
13+
}
14+
15+
16+
17+
@Component({
18+
selector: 'app-secret',
19+
template: `
20+
<div class="form-group">
21+
<label>{{ config.label }}</label>
22+
<input type="password" class="form-control" />
23+
</div>
24+
`})
25+
export class SecretComponent {
26+
@Input() config: any;
27+
}
28+
29+
30+
31+
@Component({
32+
selector: 'app-toggle',
33+
template: `
34+
<div class="form-group form-check">
35+
<input type="checkbox" class="form-check-input" />
36+
<label class="form-check-label">{{ config.label }}</label>
37+
</div>
38+
`})
39+
export class ToggleComponent {
40+
@Input() config: any;
41+
}
42+
43+
44+
45+
@Component({
46+
selector: 'app-submit',
47+
template: `
48+
<button type="submit" class="btn btn-primary">{{ config.label }}</button>
49+
`})
50+
export class SubmitComponent {
51+
@Input() config: any;
52+
}

JavaScript/impl-react/src/Components/App.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react';
22
import './App.css';
33
import { formConfig } from '../FormBuilder/formConfig.js';
4+
import * as Controls from './controls';
45

56
export class App extends Component {
67
render() {
@@ -11,7 +12,18 @@ export class App extends Component {
1112
</nav>
1213

1314
<div className="container">
14-
<form>
15+
{formConfig.map((config, index) => {
16+
switch (config.type) {
17+
case 'email': return <Controls.Email key={index} config={config} />;
18+
case 'secret': return <Controls.Secret key={index} config={config} />;
19+
case 'toggle': return <Controls.Toggle key={index} config={config} />;
20+
case 'submit': return <Controls.Submit key={index} config={config} />;
21+
default: return null;
22+
}
23+
})}
24+
25+
26+
{/*<form>
1527
<div className="form-group">
1628
<label>Email</label>
1729
<input type="email" className="form-control" />
@@ -25,7 +37,7 @@ export class App extends Component {
2537
<label className="form-check-label">Remain logged in</label>
2638
</div>
2739
<button type="submit" className="btn btn-primary">Login</button>
28-
</form>
40+
</form>*/}
2941
</div>
3042
</div>
3143
);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React, { Component } from 'react';
2+
3+
export class Email extends Component {
4+
render() {
5+
const {config} = this.props;
6+
return (
7+
<div className="form-group">
8+
<label>{config.label}</label>
9+
<input type="email" className="form-control" />
10+
</div>
11+
);
12+
}
13+
}
14+
15+
export class Secret extends Component {
16+
render() {
17+
const {config} = this.props;
18+
return (
19+
<div className="form-group">
20+
<label>{config.label}</label>
21+
<input type="password" className="form-control" />
22+
</div>
23+
);
24+
}
25+
}
26+
27+
export class Toggle extends Component {
28+
render() {
29+
const {config} = this.props;
30+
return (
31+
<div className="form-group form-check">
32+
<input type="checkbox" className="form-check-input" />
33+
<label className="form-check-label">{config.label}</label>
34+
</div>
35+
);
36+
}
37+
}
38+
39+
40+
export class Submit extends Component {
41+
render() {
42+
const {config} = this.props;
43+
return (
44+
<button type="submit" className="btn btn-primary">{config.label}</button>
45+
);
46+
}
47+
}

0 commit comments

Comments
 (0)