fix issues with repeatable fields

This commit is contained in:
Art Lowel
2020-04-01 11:06:31 +02:00
parent a8d5ad9c37
commit 2989ca4d96
21 changed files with 156 additions and 72 deletions

View File

@@ -69,6 +69,9 @@ export class SectionFormOperationsService {
case 'change':
this.dispatchOperationsFromChangeEvent(pathCombiner, event, previousValue, hasStoredValue);
break;
case 'add':
this.dispatchOperationsFromAddEvent(pathCombiner, event);
break;
default:
break;
}
@@ -294,6 +297,41 @@ export class SectionFormOperationsService {
}
}
/**
* Handle form add operations
*
* @param pathCombiner
* the [[JsonPatchOperationPathCombiner]] object for the specified operation
* @param event
* the [[DynamicFormControlEvent]] for the specified operation
*/
protected dispatchOperationsFromAddEvent(pathCombiner: JsonPatchOperationPathCombiner,
event: DynamicFormControlEvent
): void {
const path = this.getFieldPathSegmentedFromChangeEvent(event);
const value = this.getFieldValueFromChangeEvent(event);
if (isNotEmpty(value)) {
value.place = this.getArrayIndexFromEvent(event);
if (hasValue(event.group) && hasValue(event.group.value)) {
const valuesInGroup = event.group.value
.map((g) => Object.values(g))
.reduce((accumulator, currentValue) => accumulator.concat(currentValue))
.filter((v) => isNotEmpty(v));
if (valuesInGroup.length === 1) {
// The first add for a field needs to be a different PATCH operation
// for some reason
this.operationsBuilder.add(
pathCombiner.getPath([path]),
[value], false);
} else {
this.operationsBuilder.add(
pathCombiner.getPath([path, '-']),
value, false);
}
}
}
}
/**
* Handle form change operations
*