Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): support transforming config before model creation #1010

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
NG_VALIDATORS,
NG_ASYNC_VALIDATORS
} from "@angular/forms";
import { DynamicFormService } from "./dynamic-form.service";
import {
DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY,
DynamicFormService,
ModelJSONTransformFn
} from './dynamic-form.service';
import { DynamicFormValidationService } from "./dynamic-form-validation.service";
import { DynamicFormModel } from "../model/dynamic-form.model";
import { DynamicCheckboxModel } from "../model/checkbox/dynamic-checkbox.model";
Expand All @@ -31,6 +35,10 @@ import { DynamicTimePickerModel } from "../model/timepicker/dynamic-timepicker.m
import { DynamicFormValueControlModel } from "../model/dynamic-form-value-control.model";

describe("DynamicFormService test suite", () => {
const labelSuffixTranformer: (suffix: string) => ModelJSONTransformFn<DynamicInputModel> = (suffix: string) => (jsonModel) => {
jsonModel.label = `${jsonModel.label}${suffix}`;
return jsonModel;
};

let testModel: DynamicFormModel,
service: DynamicFormService;
Expand All @@ -50,6 +58,7 @@ describe("DynamicFormService test suite", () => {
providers: [
DynamicFormService,
DynamicFormValidationService,
{provide: DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY, useValue: [labelSuffixTranformer('*')]},
{provide: NG_VALIDATORS, useValue: testValidator, multi: true},
{provide: NG_ASYNC_VALIDATORS, useValue: testAsyncValidator, multi: true}
]
Expand Down Expand Up @@ -192,7 +201,6 @@ describe("DynamicFormService test suite", () => {
expect(formGroup.get("testColorPicker") instanceof FormControl).toBe(true);
});


it("should parse dynamic form JSON", () => {

let json = JSON.stringify(testModel),
Expand Down Expand Up @@ -220,6 +228,15 @@ describe("DynamicFormService test suite", () => {
});


it("should transform dynamic form control JSON", () => {
const model = new DynamicInputModel({ id: 'name', label: 'Name' }),
json = JSON.stringify([model]),
formModel = service.fromJSON(json);
expect(formModel[0]).toEqual(jasmine.objectContaining({ label: 'Name*' }));
expect(service.getCustomJSONTransform(model)).toEqual(jasmine.objectContaining({ label: 'Name*' }));
});


it("should throw when unknown DynamicFormControlModel id is specified in JSON", () => {

expect(() => service.fromJSON([{id: "test"}]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from "@angular/core";
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
import { AbstractControl, FormArray, FormControl, FormGroup } from "@angular/forms";
import { AbstractControlOptions } from "@angular/forms";
import { DynamicFormControlModel, FormHooks } from "../model/dynamic-form-control.model";
Expand Down Expand Up @@ -45,15 +45,26 @@ import { DynamicFormValidationService } from "./dynamic-form-validation.service"
import { DynamicFormModel, DynamicUnionFormModel } from "../model/dynamic-form.model";
import { DynamicPathable } from "../model/misc/dynamic-form-control-path.model";
import { DynamicValidatorsConfig } from "../model/misc/dynamic-form-control-validation.model";
import { maskFromString, parseReviver } from "../utils/json.utils";
import { isString } from "../utils/core.utils";
import { maskFromString, parseReviver, pipe } from '../utils/json.utils';
import { isString } from '../utils/core.utils';

export type ModelJSONTransformFn<T> = (modelJSON: T) => T;

export type DynamicFormControlJSONTransformFnArray<T = any> = ModelJSONTransformFn<T>[];
export const DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY = new InjectionToken<DynamicFormControlJSONTransformFnArray>(
'DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY'
);

@Injectable({
providedIn: "root"
})
export class DynamicFormService {

constructor(private validationService: DynamicFormValidationService) {}
constructor(private validationService: DynamicFormValidationService,
@Inject(DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY) @Optional()
private readonly DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY: any) {
this.DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY = DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY as DynamicFormControlJSONTransformFnArray;
}


private createAbstractControlOptions(validatorsConfig: DynamicValidatorsConfig | null = null,
Expand Down Expand Up @@ -321,7 +332,7 @@ export class DynamicFormService {
formModel: DynamicFormModel = [];

formModelJSON.forEach((model: any) => {

model = this.getCustomJSONTransform(model);
let layout = model.layout || null;

switch (model.type) {
Expand Down Expand Up @@ -421,4 +432,10 @@ export class DynamicFormService {

return formModel;
}

getCustomJSONTransform(model: object): object {
return Array.isArray(this.DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY)
? pipe(...this.DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY)(model)
: model;
}
}
12 changes: 11 additions & 1 deletion projects/ng-dynamic-forms/core/src/lib/utils/json.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { maskFromString, maskToString, parseReviver } from "./json.utils";
import { maskFromString, maskToString, parseReviver, pipe } from './json.utils';

describe("JSON utils test suite", () => {

Expand Down Expand Up @@ -40,4 +40,14 @@ describe("JSON utils test suite", () => {

expect(parseReviver("test", testValue1)).toEqual(new Date(testValue1));
});

it("should pass object in multiple functions", () => {
const object = { id: 'test' },
modifyId = (modifier: string) => (x: any) => {
x.id = `${x.id}${modifier}`;
return x;
};

expect(pipe(modifyId('1'), modifyId('2'))(object)).toEqual(jasmine.objectContaining({ id: 'test12' }));
});
});
4 changes: 4 additions & 0 deletions projects/ng-dynamic-forms/core/src/lib/utils/json.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ export function parseReviver(_key: string, value: any): any {

return isString(value) && regexDateISO.test(value) ? new Date(value) : value;
}

export function pipe<T = any>(...fns: ((x: T) => T)[]): (x: T) => T {
return x => fns.reduce((v, f) => f(v), x);
}