Skip to content
This repository was archived by the owner on Aug 25, 2020. It is now read-only.

fix: refactored rules and added wrap rule #90

Merged
merged 4 commits into from
Jan 24, 2019
Merged
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
2 changes: 1 addition & 1 deletion dist/ngx-forms.js

Large diffs are not rendered by default.

24 changes: 11 additions & 13 deletions src/app/containers/dynamic-form/dynamic-form.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { async, ComponentFixture, TestBed, getTestBed } from '@angular/core/testing';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DynamicPanelComponent } from '../dynamic-panel/dynamic-panel.component';
import { DynamicFieldDirective } from '../../components/dynamic-field/dynamic-field.directive';
import { Component, NgModule } from "@angular/core";
import { DynamicFormComponent, ConditionType } from "./dynamic-form.component";
import { DynamicFormComponent } from "./dynamic-form.component";
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { FormInputComponent } from '../../components/form-input/form-input.component';
import { CommonModule } from '@angular/common';
import { FormSelectComponent } from '../../components/form-select/form-select.component';
import { FormInputHiddenComponent } from '../../components/form-hidden/form-hidden.component';
import { By } from '@angular/platform-browser';
import { FormNavModule } from '../../../nav/nav-app';
import { FieldDictionary, FIELD_DICT_TOKEN } from '../../types';
import { FieldDictionary, FIELD_DICT_TOKEN, ConditionType } from '../../types';

interface IFormConfig {
interface FormConfig {
form: any;
fields: any[]
}

interface IDynamicForm {
formConfig: IFormConfig
formConfig: FormConfig
data: any
dynamicForm: any
lookups: any
Expand Down Expand Up @@ -233,6 +233,12 @@ describe('DynamicFormComponent Core', () => {
expect(component.checkRules(cfg, model)).toBeTruthy();
});

it('should run one rule, wrap equals, and return true', () => {
const model = { title: 'test' };
const cfg = { enableWhen: { rules: [{ field: "title", equals: "test" }] } };
expect(component.checkRules(cfg, model)).toBeTruthy();
});

it('should run one rule and return false', () => {
const model = { title: 'test' };
const cfg = { enableWhen: { rules: [{ field: "title", equals: ["test1"] }] } };
Expand Down Expand Up @@ -321,14 +327,6 @@ describe('DynamicFormComponent Core', () => {
expect(component.checkRules(cfg, {})).toBeFalsy();
});

it('should throw error', () => {
expect(() => {
const model = { title: 'test', count: 1 };
const cfg = { enableWhen: { rules: [{ field: "title", equals: ["test"] }, { field: "count", equals: [1222] }] } };
component.checkRules(cfg, model)
}).toThrowError('enableWhen type must be defined');
});

});

});
Expand Down
72 changes: 14 additions & 58 deletions src/app/containers/dynamic-form/dynamic-form.component.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,6 @@
import { Component, Input, OnInit } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FieldConfig, ILookup } from '../../types';

export const enum ConditionType {
And = 'and',
Or = 'or'
}

export interface IFormConfig {
form: PanelGroup[];
}

export interface ConditionRule {
field: string;
equals: any[];
}

export interface EnableWhenConfig {
type?: ConditionType;
rules: ConditionRule[];
}

export interface PanelConfig {
label?: string;
fields?: FieldConfig[];
enableWhen?: any;
}

export interface PanelGroup {
label?: string;
fields?: FieldConfig[];
panels?: PanelConfig[];
enableWhen?: EnableWhenConfig;
}
import { FieldConfig, ILookup, FormConfig, PanelGroup, ConditionType } from '../../types';

@Component({
exportAs: 'dynamicForm',
Expand All @@ -41,7 +9,7 @@ export interface PanelGroup {
styles: [require('./dynamic-form.component.scss').toString()]
})
export class DynamicFormComponent implements OnInit {
@Input() formConfig: IFormConfig;
@Input() formConfig: FormConfig;
@Input() model: any;
@Input() lookups: object;

Expand Down Expand Up @@ -92,37 +60,25 @@ export class DynamicFormComponent implements OnInit {
if (!group.enableWhen) { return; }
const enableWhen = group.enableWhen;

// TODO: add check if `equals is not array and wrap it equals = [equals]
// TODO: Refactor this code
if (!enableWhen.rules.length) { return true; }

if (enableWhen.rules.length === 1) {
const rule = enableWhen.rules[0];
const checkRule = rule => {
let field;
const value = data[rule.field] || (field = group.fields.find(f => f.name === rule.field)) && field.value || '';
enabled = rule.equals.indexOf(value) > -1;
}
if (!Array.isArray(rule.equals)) { rule.equals = [rule.equals]; }
return rule.equals.indexOf(value) > -1;
};

if (enableWhen.rules.length > 1) {
if (!enableWhen.type) { throw new Error('enableWhen type must be defined'); }

if (enableWhen.type === ConditionType.Or) {
enabled = false;
enableWhen.rules.forEach(rule => {
let field;
const value = data[rule.field] || (field = group.fields.find(f => f.name === rule.field)) && field.value || '';
enabled = enabled || rule.equals.indexOf(value) > -1;
});
}
if (enableWhen.type === ConditionType.Or || !enableWhen.type) {
enabled = enableWhen.rules.some(checkRule);
}

if (enableWhen.type === ConditionType.And) {
enableWhen.rules.forEach(rule => {
let field;
const value = data[rule.field] || (field = group.fields.find(f => f.name === rule.field)) && field.value || '';
enabled = enabled && rule.equals.indexOf(value) > -1;
});
}
if (enableWhen.type === ConditionType.And) {
enabled = enableWhen.rules.every(checkRule);
}

// leave if() conditions in case other logic operators needed (XOR, etc)

return enabled;
}

Expand Down
32 changes: 32 additions & 0 deletions src/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,35 @@ export type Type<T> = new (...args: any[]) => T;
export interface FieldDictionary { [key: string]: Type<Field>; }

export const FIELD_DICT_TOKEN = new InjectionToken<FieldDictionary>('fields');

export const enum ConditionType {
And = 'and',
Or = 'or'
}

export interface FormConfig {
form: PanelGroup[];
}

export interface ConditionRule {
field: string;
equals: any[] | any;
}

export interface EnableWhenConfig {
type?: ConditionType;
rules: ConditionRule[];
}

export interface PanelConfig {
label?: string;
fields?: FieldConfig[];
enableWhen?: any;
}

export interface PanelGroup {
label?: string;
fields?: FieldConfig[];
panels?: PanelConfig[];
enableWhen?: EnableWhenConfig;
}