UNPKG

@chatterton/angular2-schema-form

Version:

Angular2 Schema Form (DISCLAIMER: it is not related to angular-schema-form)

35 lines (26 loc) 985 B
import { AfterViewInit } from '@angular/core'; import { FormControl } from '@angular/forms'; import { ArrayProperty, FormProperty, ObjectProperty } from './model'; export abstract class Widget<T extends FormProperty> { formProperty: T; control: FormControl; id: string = ''; name: string = ''; schema: any = {}; } export class ControlWidget extends Widget<FormProperty> implements AfterViewInit { ngAfterViewInit() { let control = this.control; this.formProperty.valueChanges.subscribe((newValue) => { if (control.value !== newValue) { control.setValue(newValue, {emitEvent: false}); } }); this.formProperty.errorsChanges.subscribe((errors) => { control.setErrors(errors, true); }); control.valueChanges.subscribe((newValue) => { this.formProperty.setValue(newValue, false); }); } } export class ArrayLayoutWidget extends Widget<ArrayProperty> {} export class ObjectLayoutWidget extends Widget<ObjectProperty> {}