-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.component.ts
86 lines (71 loc) · 1.83 KB
/
form.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
import {
AfterViewInit,
Component,
EventEmitter,
Input,
Output
} from '@angular/core';
import { FormComponentInterface } from './interfaces/component.interface';
import { FormInterface } from './interfaces/form.interface';
import { CoreService } from 'wacom';
@Component({
selector: 'wform',
templateUrl: './form.component.html',
styleUrls: ['./form.component.scss'],
standalone: false
})
export class FormComponent implements AfterViewInit {
@Input() config: FormInterface;
@Input() submition: Record<string, unknown> = {};
@Output() wChange = new EventEmitter();
@Output() wSubmit = new EventEmitter();
constructor(private _core: CoreService) {}
ngAfterViewInit(): void {
this.submition['data'] = this.submition['data'] || {};
}
component(
key: string,
components = this.config.components
): FormComponentInterface | false {
for (const component of components) {
if (component.key === key) {
return component;
}
if (component.components?.length) {
const deepComponent = this.component(key, component.components);
if (deepComponent) {
return deepComponent;
}
}
}
return false;
}
onSubmit(): void {
this._core.afterWhile(this, () => {
for (const component of this.config.components) {
if (
component.key &&
component.required &&
((component.valid && !component.valid()) ||
(!component.valid && !this.submition[component.key]))
) {
if (typeof component.focus === 'function') {
component.focus();
}
return;
}
}
this.wSubmit.emit(this.submition);
});
}
onChange(): void {
this._core.afterWhile(this, () => {
this.wChange.emit(this.submition);
});
}
onClick(/* component: FormComponentInterface */): void {
// if (typeof component.click === 'function') {
// component.click(this.submition);
// }
}
}