diff --git a/CHANGELOG.md b/CHANGELOG.md index 05dff42..b281d92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ Feature: - Updated the peerDependencies for Angular 12 +Change: +- Extracted an interface of the FormBuilderComponent to remove a cyclic dependency. + BREAKING CHANGE: - Dropped support for Angular <10.0.0 diff --git a/src/lib/abstraction/form-builder.ts b/src/lib/abstraction/form-builder.ts new file mode 100644 index 0000000..3dbf37c --- /dev/null +++ b/src/lib/abstraction/form-builder.ts @@ -0,0 +1,24 @@ +import {AbstractGroupType, IGroupTypeOptions} from '../form-type/abstract-group-type'; +import {FormModel} from '../form-type/abstract-type'; +import {EventEmitter, QueryList} from '@angular/core'; +import {FormSlotDirective} from '../form-slot/form-slot.directive'; +import {FormGroup} from './form-group'; +import {FormArray} from './form-array'; +import {ValidationErrors} from '@angular/forms'; + +export interface IFormBuilder { + fieldType: AbstractGroupType, T>; + mwFormModel: FormModel; + mwFormData: T; + mwFormSubmit: EventEmitter; + mwSlots: QueryList; + group: FormGroup; + readonly valid: boolean; + readonly invalid: boolean; + + rebuildForm(): void; + + submit(): T; + + getErrors(group?: FormGroup | FormArray): { [key: string]: ValidationErrors } | ValidationErrors[] | null; +} diff --git a/src/lib/abstraction/index.ts b/src/lib/abstraction/index.ts index 812e41b..287624d 100644 --- a/src/lib/abstraction/index.ts +++ b/src/lib/abstraction/index.ts @@ -1,4 +1,5 @@ export * from './form-array'; +export * from './form-builder'; export * from './form-control'; export * from './form-group'; export * from './init-handler'; diff --git a/src/lib/form-builder/form-builder.component.ts b/src/lib/form-builder/form-builder.component.ts index 4fb1bdb..5a688e5 100644 --- a/src/lib/form-builder/form-builder.component.ts +++ b/src/lib/form-builder/form-builder.component.ts @@ -1,11 +1,10 @@ -import {Component, ContentChildren, EventEmitter, Input, OnInit, Output, QueryList} from '@angular/core'; -import {OnChanges, SimpleChanges} from '@angular/core'; +import {Component, ContentChildren, EventEmitter, Input, OnChanges, OnInit, Output, QueryList, SimpleChanges} from '@angular/core'; import {ValidationErrors} from '@angular/forms'; import {FormSlotDirective} from '../form-slot/form-slot.directive'; import {ModelHandler} from '../model-handler'; import {AbstractGroupType, IGroupTypeOptions} from '../form-type/abstract-group-type'; import {AbstractCollectionType} from '../form-type/abstract-collection-type'; -import {FormArray, FormControl, FormGroup} from '../abstraction'; +import {FormArray, FormControl, FormGroup, IFormBuilder} from '../abstraction'; import {AbstractFormControl} from '../types'; import {FormGroupComponent, FormGroupType} from '../form-group/form-group.component'; import {AbstractLayoutType} from '../form-type/abstract-layout-type'; @@ -25,7 +24,7 @@ import {FormModel} from '../form-type/abstract-type'; `, styles: [] }) -export class FormBuilderComponent implements OnInit, OnChanges { +export class FormBuilderComponent implements OnInit, OnChanges, IFormBuilder { public fieldType: AbstractGroupType, T>; diff --git a/src/lib/form-type/abstract-type.ts b/src/lib/form-type/abstract-type.ts index bcd3e12..286ec1c 100644 --- a/src/lib/form-type/abstract-type.ts +++ b/src/lib/form-type/abstract-type.ts @@ -1,8 +1,8 @@ import {AbstractControlOptions, AsyncValidatorFn, ValidatorFn} from '@angular/forms'; -import {FormBuilderComponent} from '../form-builder/form-builder.component'; import {AbstractFormControl, Constructor} from '../types'; import {AbstractGroupType, IGroupTypeOptions} from './abstract-group-type'; import {AbstractLayoutType} from './abstract-layout-type'; +import {IFormBuilder} from '../abstraction'; export type FormModel = @@ -13,7 +13,7 @@ export type FormModel = export abstract class AbstractType { public abstract readonly component: Constructor; - public builderInstance: FormBuilderComponent; + public builderInstance: IFormBuilder; public control: AbstractFormControl; diff --git a/src/lib/model-handler.ts b/src/lib/model-handler.ts index fb550b7..eed6c89 100644 --- a/src/lib/model-handler.ts +++ b/src/lib/model-handler.ts @@ -1,18 +1,17 @@ -import {FormBuilderComponent} from './form-builder/form-builder.component'; import {AbstractCollectionType} from './form-type/abstract-collection-type'; import {AbstractGroupType} from './form-type/abstract-group-type'; import {AbstractType, FormModel} from './form-type/abstract-type'; -import {FormArray, FormControl, FormGroup} from './abstraction'; +import {FormArray, FormControl, FormGroup, IFormBuilder} from './abstraction'; import {AbstractLayoutType} from './form-type/abstract-layout-type'; export class ModelHandler { - public static build(model: FormModel, builderInstance: FormBuilderComponent): FormGroup { + public static build(model: FormModel, builderInstance: IFormBuilder): FormGroup { const group = new FormGroup({}); this.handleModel(model, group, builderInstance); return group; } - private static handleModel(model: FormModel, group: FormGroup, builderInstance: FormBuilderComponent): void { + private static handleModel(model: FormModel, group: FormGroup, builderInstance: IFormBuilder): void { Object.keys(model).forEach((name) => { const field: AbstractType = model[name]; if (!field) { @@ -40,7 +39,7 @@ export class ModelHandler { } public static buildSingleField(field: AbstractType, - builderInstance: FormBuilderComponent + builderInstance: IFormBuilder ): FormArray | FormGroup | FormControl | null { let component: FormArray | FormGroup | FormControl; @@ -58,7 +57,7 @@ export class ModelHandler { return component; } - private static handleArray(field: AbstractCollectionType, array: FormArray, builderInstance: FormBuilderComponent): void { + private static handleArray(field: AbstractCollectionType, array: FormArray, builderInstance: IFormBuilder): void { const control = ModelHandler.buildSingleField(field.fieldInstance, builderInstance); if (!control) { return; diff --git a/tsconfig.lib.json b/tsconfig.lib.json index ca3f522..a937679 100644 --- a/tsconfig.lib.json +++ b/tsconfig.lib.json @@ -25,7 +25,8 @@ "strictMetadataEmit": true, "fullTemplateTypeCheck": true, "strictInjectionParameters": true, - "enableResourceInlining": true + "enableResourceInlining": true, + "compilationMode": "partial" }, "exclude": [ "src/test.ts",