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

Feature/update array #304

Open
wants to merge 2 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 @@ -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)
);
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/lib/src/json-schema-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -516,7 +520,7 @@ export class JsonSchemaFormService {
}
}

updateArrayCheckboxList(ctx: any, checkboxList: TitleMapItem[]): void {
protected updateArray(ctx: any, items: any[]): void {
const formArray = <FormArray>this.getFormControl(ctx);

// Remove all existing items
Expand All @@ -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();
}
Expand Down
23 changes: 11 additions & 12 deletions src/lib/src/shared/json.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } };
};
Expand Down
5 changes: 4 additions & 1 deletion src/lib/src/widget-library/checkboxes.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}
}