Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Commit

Permalink
fix: only init definitions when bound
Browse files Browse the repository at this point in the history
  • Loading branch information
jbockle committed Nov 11, 2021
1 parent e9ac783 commit ad8051f
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions packages/core/src/elements/fieldsets/aujsf-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@ export class AujsfArray extends AujsfBase<JsonSchemaArray, any[]> {
this._observer = this.engine?.collectionObserver(this.value).subscribe(changeRecords => {
this.valueChanged(this.value, changeRecords);
});
this.updateDefinitions();
this.initDefinitions();
}

public unbind(): void {
this._observer?.dispose();
}

public add(): void {
const newItemValue = this.context.schemaDefaults?.getArrayItemDefaults(this.value.length, this.schema) ?? null;
public add(): void;
public add(value: any): void;
public add(value: any = undefined): void {
const newItemValue = value ?? this.context.schemaDefaults?.getArrayItemDefaults(this.value.length, this.schema) ?? null;
this.definitions.push(this.getIndexDefinition(this.definitions.length));
this.value.push(newItemValue);
this.updateDefinitions();
}

public delete(index: number): void {
if (index in this.value) {
this.definitions.splice(index, 1);
this.value.splice(index, 1);
this.updateDefinitions();
}
}

Expand All @@ -56,7 +58,7 @@ export class AujsfArray extends AujsfBase<JsonSchemaArray, any[]> {

public definitions: ArrayKeyDefinition[] = [];

private updateDefinitions(): void {
private initDefinitions(): void {
const length = this.value?.length ?? 0;

if (length === 0) {
Expand All @@ -69,15 +71,7 @@ export class AujsfArray extends AujsfBase<JsonSchemaArray, any[]> {
}

for (let index = 0; index < (length); index++) {
const schema = this.getItemJsonSchema(index, this.schema, this.context.schema!);
const uiSchema = this.getItemUiSchema(index, this.uiSchema, schema);

const definition = {
key: index,
schema,
uiSchema,
pointer: new JsonPointer([...this.pointer.segments, index.toString()]),
};
const definition = this.getIndexDefinition(index);

this.definitions[index] || this.definitions.push(definition);
if (this.definitions[index].key !== definition.key) {
Expand All @@ -86,6 +80,18 @@ export class AujsfArray extends AujsfBase<JsonSchemaArray, any[]> {
}
}

private getIndexDefinition(index: number): ArrayKeyDefinition {
const schema = this.getItemJsonSchema(index, this.schema, this.context.schema!);
const uiSchema = this.getItemUiSchema(index, this.uiSchema, schema);

return {
key: index,
schema,
uiSchema,
pointer: new JsonPointer([...this.pointer.segments, index.toString()]),
};
}

private getItemJsonSchema(index: number, schema: JsonSchemaArray, root: JsonSchema): JsonSchema {
const itemSchema = utils.jsonSchema.getItemJsonSchema(index, schema, root);

Expand Down

0 comments on commit ad8051f

Please sign in to comment.