From 7ca0ffb65856d12ac76be8a7bef1b20712fcafdd Mon Sep 17 00:00:00 2001 From: James Scharett Date: Fri, 27 Jul 2018 15:01:15 -0400 Subject: [PATCH 1/2] MAde updateValue able to handle arrays --- .../material-checkboxes.component.ts | 5 ++++- src/lib/src/json-schema-form.service.ts | 20 ++++++++++--------- .../widget-library/checkboxes.component.ts | 5 ++++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/lib/src/framework-library/material-design-framework/material-checkboxes.component.ts b/src/lib/src/framework-library/material-design-framework/material-checkboxes.component.ts index aa995ba1..1e0ae3d1 100755 --- a/src/lib/src/framework-library/material-design-framework/material-checkboxes.component.ts +++ b/src/lib/src/framework-library/material-design-framework/material-checkboxes.component.ts @@ -97,7 +97,10 @@ export class MaterialCheckboxesComponent implements OnInit { updateValue() { this.options.showErrors = true; if (this.boundControl) { - this.jsf.updateArrayCheckboxList(this, this.checkboxList); + this.jsf.updateValue(this, this.checkboxList + .filter(checkboxItem => checkboxItem.checked) + .map(checkboxItem => checkboxItem.value) + ); } } diff --git a/src/lib/src/json-schema-form.service.ts b/src/lib/src/json-schema-form.service.ts index 372f51c2..810ed08b 100755 --- a/src/lib/src/json-schema-form.service.ts +++ b/src/lib/src/json-schema-form.service.ts @@ -499,8 +499,12 @@ export class JsonSchemaFormService { // Set value of current control ctx.controlValue = value; if (ctx.boundControl) { - ctx.formControl.setValue(value); - ctx.formControl.markAsDirty(); + if (isArray(value)) { + this.updateArray(ctx, value); + } else { + ctx.formControl.setValue(value); + ctx.formControl.markAsDirty(); + } } ctx.layoutNode.value = value; @@ -516,7 +520,7 @@ export class JsonSchemaFormService { } } - updateArrayCheckboxList(ctx: any, checkboxList: TitleMapItem[]): void { + protected updateArray(ctx: any, items: any[]): void { const formArray = this.getFormControl(ctx); // Remove all existing items @@ -526,12 +530,10 @@ export class JsonSchemaFormService { const refPointer = removeRecursiveReferences( ctx.layoutNode.dataPointer + '/-', this.dataRecursiveRefMap, this.arrayMap ); - for (const checkboxItem of checkboxList) { - if (checkboxItem.checked) { - const newFormControl = buildFormGroup(this.templateRefLibrary[refPointer]); - newFormControl.setValue(checkboxItem.value); - formArray.push(newFormControl); - } + for (const item of items) { + const newFormControl = buildFormGroup(this.templateRefLibrary[refPointer]); + newFormControl.setValue(item); + formArray.push(newFormControl); } formArray.markAsDirty(); } diff --git a/src/lib/src/widget-library/checkboxes.component.ts b/src/lib/src/widget-library/checkboxes.component.ts index cc3069db..9499cab3 100755 --- a/src/lib/src/widget-library/checkboxes.component.ts +++ b/src/lib/src/widget-library/checkboxes.component.ts @@ -97,7 +97,10 @@ export class CheckboxesComponent implements OnInit { } } if (this.boundControl) { - this.jsf.updateArrayCheckboxList(this, this.checkboxList); + this.jsf.updateValue(this, this.checkboxList + .filter(checkboxItem => checkboxItem.checked) + .map(checkboxItem => checkboxItem.value) + ); } } } From 939b19f4ab85795d9955c22806534912592ebf37 Mon Sep 17 00:00:00 2001 From: James Scharett Date: Mon, 30 Jul 2018 09:34:45 -0400 Subject: [PATCH 2/2] Resolve circular conflict During the build, _.isEqual is getting replaced with isEqual from the import. This is conflicting with the local isEqual causing a circular issue. --- src/lib/src/shared/json.validators.ts | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/lib/src/shared/json.validators.ts b/src/lib/src/shared/json.validators.ts index 34ebb8e0..62cddfc6 100755 --- a/src/lib/src/shared/json.validators.ts +++ b/src/lib/src/shared/json.validators.ts @@ -186,18 +186,17 @@ export class JsonValidators { return (control: AbstractControl, invert = false): ValidationErrors|null => { if (isEmpty(control.value)) { return null; } const currentValue: any = control.value; - const isEqual = (enumValue, inputValue) => - enumValue === inputValue || - (isNumber(enumValue) && +inputValue === +enumValue) || - (isBoolean(enumValue, 'strict') && - toJavaScriptType(inputValue, 'boolean') === enumValue) || - (enumValue === null && !hasValue(inputValue)) || - _.isEqual(enumValue, inputValue); - const isValid = isArray(currentValue) ? - currentValue.every(inputValue => allowedValues.some(enumValue => - isEqual(enumValue, inputValue) - )) : - allowedValues.some(enumValue => isEqual(enumValue, currentValue)); + const areEqual = (enumValue, inputValue) => + enumValue === inputValue + || (isNumber(enumValue) && +inputValue === +enumValue) + || (isBoolean(enumValue, 'strict') && toJavaScriptType(inputValue, 'boolean') === enumValue) + || (enumValue === null && !hasValue(inputValue)) + || _.isEqual(enumValue, inputValue); + const isValid = isArray(currentValue) + ? currentValue.every(inputValue => allowedValues.some(enumValue => + areEqual(enumValue, inputValue) + )) + : allowedValues.some(enumValue => areEqual(enumValue, currentValue)); return xor(isValid, invert) ? null : { 'enum': { allowedValues, currentValue } }; };