import { Component, ComponentFactoryResolver, ContentChildren, EventEmitter, Input, Output, QueryList, Type, ViewChild, ViewContainerRef } from "@angular/core"; import { FormGroup } from "@angular/forms"; import { DYNAMIC_FORM_CONTROL_TYPE_ARRAY, DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX, DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX_GROUP, DYNAMIC_FORM_CONTROL_TYPE_GROUP, DYNAMIC_FORM_CONTROL_TYPE_INPUT, DYNAMIC_FORM_CONTROL_TYPE_RADIO_GROUP, DYNAMIC_FORM_CONTROL_TYPE_SELECT, DYNAMIC_FORM_CONTROL_TYPE_SWITCH, DYNAMIC_FORM_CONTROL_TYPE_TEXTAREA, DynamicFormArrayGroupModel, DynamicFormControl, DynamicFormControlContainerComponent, DynamicFormControlEvent, DynamicFormControlModel, DynamicFormComponentService, DynamicFormLayout, DynamicFormLayoutService, DynamicFormRelationService, DynamicFormValidationService, DynamicTemplateDirective } from "@ng-dynamic-forms/core"; import { DynamicFoundationTextAreaComponent } from "./textarea/dynamic-foundation-textarea.component"; import { DynamicFoundationSwitchComponent } from "./switch/dynamic-foundation-switch.component"; import { DynamicFoundationSelectComponent } from "./select/dynamic-foundation-select.component"; import { DynamicFoundationRadioGroupComponent } from "./radio-group/dynamic-foundation-radio-group.component"; import { DynamicFoundationInputComponent } from "./input/dynamic-foundation-input.component"; import { DynamicFoundationCheckboxComponent } from "./checkbox/dynamic-foundation-checkbox.component"; import { DynamicFoundationFormArrayComponent } from "./form-array/dynamic-foundation-form-array.component"; import { DynamicFoundationFormGroupComponent } from "./form-group/dynamic-foundation-form-group.component"; @Component({ selector: "dynamic-foundation-form-control", templateUrl: "./dynamic-foundation-form-control-container.component.html" }) export class DynamicFoundationFormControlContainerComponent extends DynamicFormControlContainerComponent { @ContentChildren(DynamicTemplateDirective) contentTemplateList: QueryList; @Input("templates") inputTemplateList: QueryList; @Input() context: DynamicFormArrayGroupModel | null = null; @Input() group: FormGroup; @Input() layout: DynamicFormLayout; @Input() model: DynamicFormControlModel; @Output() blur: EventEmitter = new EventEmitter(); @Output() change: EventEmitter = new EventEmitter(); @Output() focus: EventEmitter = new EventEmitter(); @ViewChild("componentViewContainer", { read: ViewContainerRef, static: true }) componentViewContainerRef: ViewContainerRef; constructor(protected componentFactoryResolver: ComponentFactoryResolver, protected layoutService: DynamicFormLayoutService, protected validationService: DynamicFormValidationService, protected componentService: DynamicFormComponentService, protected relationService: DynamicFormRelationService) { super(componentFactoryResolver, layoutService, validationService, componentService, relationService); } get componentType(): Type | null { return this.componentService.getCustomComponentType(this.model) || foundationUIFormControlMapFn(this.model); } } export function foundationUIFormControlMapFn(model: DynamicFormControlModel): Type | null { switch (model.type) { case DYNAMIC_FORM_CONTROL_TYPE_ARRAY: return DynamicFoundationFormArrayComponent; case DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX: return DynamicFoundationCheckboxComponent; case DYNAMIC_FORM_CONTROL_TYPE_CHECKBOX_GROUP: return DynamicFoundationFormGroupComponent; case DYNAMIC_FORM_CONTROL_TYPE_GROUP: return DynamicFoundationFormGroupComponent; case DYNAMIC_FORM_CONTROL_TYPE_INPUT: return DynamicFoundationInputComponent; case DYNAMIC_FORM_CONTROL_TYPE_RADIO_GROUP: return DynamicFoundationRadioGroupComponent; case DYNAMIC_FORM_CONTROL_TYPE_SELECT: return DynamicFoundationSelectComponent; case DYNAMIC_FORM_CONTROL_TYPE_SWITCH: return DynamicFoundationSwitchComponent; case DYNAMIC_FORM_CONTROL_TYPE_TEXTAREA: return DynamicFoundationTextAreaComponent; default: return null; } }