-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyna-form-base.component.ts
66 lines (55 loc) · 2.05 KB
/
dyna-form-base.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
import { Component, OnDestroy } from '@angular/core';
import { FormArray, FormControl } from '@angular/forms';
import { AppInjector } from './app-injector';
import { DynaFormService } from './services/dyna-form.service';
export type ControlTuple = [string, FormControl | FormArray];
@Component({ template: '' })
export abstract class DynaFormBaseComponent implements OnDestroy {
#controls?: ControlTuple[];
private dynaFormService: DynaFormService;
// private builder: FormBuilder;
// private formsManager: NgFormsManager;
constructor() {
const injector = AppInjector.getInjector();
this.dynaFormService = injector.get(DynaFormService);
// this.init = this.dynaFormService.init;
// this.builder = injector.get(FormBuilder);
// this.formsManager = injector.get(NgFormsManager);
// this.form = this.builder.group({ name: ['yinon'] });
// this.form.addControl('workplace', new FormControl('vonage'));
// const skills = new FormArray([]);
// /** Or inside a FormGroup */
// const config = new FormGroup({
// skills: new FormArray([]),
// });
// this.formsManager
// .upsert('skills', skills, { arrControlFactory: (value) => new FormControl(value) })
// .upsert('config', config, {
// arrControlFactory: { skills: (value) => new FormControl(value) },
// });
// this.formsManager.upsert('onboarding', this.form, { persistState: true });
// this.formsManager
// .valueChanges('onboarding')
// // .pipe(map(() => this.form.value))
// .subscribe(console.log);
}
set controls(controls: ControlTuple[]) {
this.#controls = controls;
controls.forEach(([name, control]) => {
// add new control if is undefined or null
this.dynaFormService.form.addControl(name, control);
// console.log(this.form.value);
});
}
get form() {
return this.dynaFormService.form;
}
getFormControl(name: string) {
return this.form.get(name);
}
ngOnDestroy(): void {
this.#controls.forEach(([name]) => {
this.form.removeControl(name);
});
}
}