Skip to content

Commit

Permalink
Fixed nested arrays #3 (#1511)
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Kononenko committed Jul 20, 2023
1 parent 9dae60b commit 7994a83
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions ui/src/app/core/directives/json-schema-form-patch.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ export class JsonSchemaFormPatchDirective {
}

private fixArray(items: any | any[], formData: any, refPointer: string) {

if (Array.isArray(items)) {
items.filter(x => x.name !== '_bridge' && (x.dataType === 'array' || x.arrayItem)).forEach(item => {
const configItems = items.filter(x => x.name !== '_bridge');
const nestedItems = configItems
.filter(x => x.items && Array.isArray(x.items))
.flatMap(x => x.items)
.filter(x => x.dataType === 'array' || x.arrayItem);

const allItems = configItems.concat(nestedItems);
allItems.filter(x => x.dataType === 'array' || x.arrayItem).forEach(item => {
this.fixNestedArray(item, formData, refPointer);
});
} else {
Expand All @@ -47,13 +53,19 @@ export class JsonSchemaFormPatchDirective {

const ref = item.items.find(x => x.type === '$ref');
if (ref) {
const dataItems = item.items.filter(x => x.type === 'section');

const dataItems = item.items.filter(x => x.type === 'section' || x.type === 'div');

const template = dataItems.length > 0
? dataItems.reduce((a, b) => a.id > b.id ? a : b)
: this.getItemTemplateFromRef(ref);

const data = this.getDataFromPointer(formData, ref.dataPointer.replace(refPointer, ''));

if (data === null) {
return;
}

if (Array.isArray(data)) {
// add missing items
while (item.items.length - 1 < data.length) {
Expand Down Expand Up @@ -83,7 +95,13 @@ export class JsonSchemaFormPatchDirective {
let value = data;

dataPointer.substring(1).split(/\//).filter(x => x !== '-')
.forEach((key: string) => value = value[key]);
.forEach((key: string) => {
try {
value = value[key];
} catch {
value = null;
}
});

return value;
}
Expand Down

0 comments on commit 7994a83

Please sign in to comment.