-
Notifications
You must be signed in to change notification settings - Fork 1
/
ng-xform.component.ts
121 lines (101 loc) · 2.96 KB
/
ng-xform.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { Validators, FormGroup, FormControl } from '@angular/forms';
import { Component, OnInit, Input, EventEmitter, Output, OnChanges, SimpleChanges } from '@angular/core';
import { DynamicField } from './fields/dynamic-field';
import { NestedFormGroup } from './fields/nested-form-group';
/**
* This component builds a form with input components from fields list.
* If any instance is defined in the model, yours attribute values will be applied to the corresponding field.
* This component can display an error code if that is defined.
*
* :fields: List of configurations to build fields.
* :model: Model instance to edit [optional].
* :errorCode: This can display an error if is defined.
* :editing: Flag to control components state
*/
@Component({
selector: 'ng-xform',
templateUrl: './ng-xform.component.html',
styles: []
})
export class NgXformComponent implements OnInit, OnChanges {
@Input() fields: DynamicField[];
@Input() model: any;
@Input() errorCode: string;
@Input() editing: boolean;
/** To listening submitSuccessful event */
@Output() onSubmit = new EventEmitter();
/** To listening submitSuccessful event */
@Output() onCancel = new EventEmitter();
form: FormGroup;
ngOnInit() {
this.createForm();
this.reset();
}
ngOnChanges(changes: SimpleChanges) {
if (changes.fields) {
this.createForm();
}
if (!this.form) {
return;
}
this.reset();
}
createForm() {
this.form = this.createFormGroup(this.fields);
}
createFormGroup(fields: DynamicField[]): FormGroup {
let group: any = {};
fields.forEach(field => {
if (field instanceof NestedFormGroup) {
group[field.key] = this.createFormGroup(field.fields);
} else {
group[field.key] = field.validators
? new FormControl('', field.validators)
: new FormControl('');
}
});
return new FormGroup(group);
}
private getAttributeValue(attr: string, value: any): any {
const field = this.fields.find(_field => _field.key === attr);
if (value instanceof Object && field && field['valueAttribute']) {
return value[field['valueAttribute']] || null;
}
return value === '' ? null : value;
}
submit() {
if (this.form.invalid) {
return;
}
this.errorCode = undefined;
// copy object
let modelToSend = { ...this.model };
for (const attr in this.form.value) {
if (this.form.value.hasOwnProperty(attr)) {
modelToSend[attr] = this.getAttributeValue(attr, this.form.value[attr]);
}
}
this.onSubmit.emit(modelToSend);
}
/** It is called by cancel button */
cancel() {
this.setEditing(false);
this.reset();
this.onCancel.emit();
}
reset() {
if (this.model) {
this.form.patchValue(this.model);
}
this.errorCode = undefined;
}
clear() {
this.form.reset();
}
setEditing(state: boolean) {
if (this.editing === undefined) {
return;
}
this.editing = state;
}
}