UNPKG

@codeforges/ng-forms

Version:

Angular 4+ Dynamic form builder, decorate your models to generate forms or create them manually

47 lines 1.36 kB
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from "@angular/core"; import { FormGroup } from "@angular/forms"; export class DynamicFormComponent { constructor() { this.inputs = []; this.options = { columns: 1, submitLabel: 'Submit' }; this.formSubmit = new EventEmitter(); } getWidth() { switch (this.options.columns) { case 1: return 'w-100'; case 2: return 'w-50'; case 3: return 'w-33'; } } ngOnInit() { this.buildForm(); } onSubmit(form) { this.formSubmit.emit(form); } buildForm() { const formGroup = {}; this.inputs.forEach(input => formGroup[input.name] = input.getFormControl()); this.form = new FormGroup(formGroup); } } DynamicFormComponent.decorators = [ { type: Component, args: [{ selector: 'ng-forms', templateUrl: './dynamicForm.html', changeDetection: ChangeDetectionStrategy.OnPush, },] }, ]; /** @nocollapse */ DynamicFormComponent.propDecorators = { "inputs": [{ type: Input },], "options": [{ type: Input },], "formSubmit": [{ type: Output },], }; //# sourceMappingURL=DynamicFormComponent.js.map