Skip to content

Commit 6de9f88

Browse files
committed
fix: layout/schema changes dont update form
Closes #27
1 parent 63758fa commit 6de9f88

File tree

4 files changed

+16
-23
lines changed

4 files changed

+16
-23
lines changed

projects/ngx-json-schema-form/src/lib/core/widget/widget.spec.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, NO_ERRORS_SCHEMA, SimpleChange } from '@angular/core';
1+
import { Component, NO_ERRORS_SCHEMA } from '@angular/core';
22
import { ComponentFixture, inject, TestBed } from '@angular/core/testing';
33

44
import { JsonSchemaFormService } from '../../form/services/json-schema-form.service';
@@ -93,9 +93,7 @@ describe('widgets', () => {
9393
options: {},
9494
type: ''
9595
} as any as LayoutNode;
96-
const change = new SimpleChange(component.layoutNode, newLayoutNode, true);
9796
component.layoutNode = newLayoutNode;
98-
component[`ngOnChanges`]({layoutNode: change});
9997
expect(edsService.set).toHaveBeenCalledWith(jasmine.any(HTMLElement), 'layout', newLayoutNode.layoutDefinition);
10098
});
10199

@@ -104,14 +102,8 @@ describe('widgets', () => {
104102
(<jasmine.Spy>edsService.set).calls.reset();
105103
const newLayoutIndex = [1];
106104
const newDataIndex = [1];
107-
const change1 = new SimpleChange(component.layoutIndex, newLayoutIndex, true);
108-
const change2 = new SimpleChange(component.dataIndex, newDataIndex, true);
109105
component.layoutIndex = newLayoutIndex;
110106
component.dataIndex = newDataIndex;
111-
component[`ngOnChanges`]({
112-
dataIndex: change2,
113-
layoutIndex: change1
114-
});
115107
expect(edsService.set).not.toHaveBeenCalled();
116108
});
117109

projects/ngx-json-schema-form/src/lib/core/widget/widget.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AfterViewInit, ElementRef, Input, OnChanges, OnInit, SimpleChanges, ViewChild } from '@angular/core';
1+
import { AfterViewInit, ElementRef, Input, OnInit, ViewChild } from '@angular/core';
22
import { AbstractControl } from '@angular/forms';
33

44
import { JsonSchemaFormService } from '../../form/services/json-schema-form.service';
@@ -7,7 +7,7 @@ import { LayoutNode } from '../models/layout-node';
77
import { ElementDataStorageService } from '../services/element-data-storage.service';
88

99
/** AbstractWidget */
10-
export abstract class AbstractWidget implements OnChanges, OnInit, AfterViewInit {
10+
export abstract class AbstractWidget implements OnInit, AfterViewInit {
1111
/** Flag to disable the control */
1212
controlDisabled = false;
1313
/** Name for the control */
@@ -20,7 +20,15 @@ export abstract class AbstractWidget implements OnChanges, OnInit, AfterViewInit
2020
options: any;
2121

2222
/** Layout Node describing the control */
23-
@Input() layoutNode: LayoutNode;
23+
private _layoutNode: LayoutNode;
24+
@Input() set layoutNode(value: LayoutNode) {
25+
this._layoutNode = value;
26+
this.options = this._layoutNode.options;
27+
this.updateData();
28+
}
29+
get layoutNode(): LayoutNode {
30+
return this._layoutNode;
31+
}
2432
/** Index of the layout in the Layout array */
2533
@Input() layoutIndex: Array<number>;
2634
/** Index of the data in data array */
@@ -32,18 +40,11 @@ export abstract class AbstractWidget implements OnChanges, OnInit, AfterViewInit
3240
/** constructor */
3341
constructor(protected jsf: JsonSchemaFormService, protected elementDataStorage: ElementDataStorageService) {}
3442

35-
/** Propagate layoutNode changes to the ElementDataStorage */
36-
ngOnChanges(changes: SimpleChanges) {
37-
if (changes.layoutNode) {
38-
this.updateData();
39-
}
40-
}
4143
/**
4244
* Initialize the control and populate the options
4345
*/
4446
ngOnInit() {
4547
this.jsf.initializeControl(this);
46-
this.options = this.layoutNode.options;
4748
}
4849

4950
/** Set the initial layout in the ElementDataStorage */

projects/ngx-json-schema-form/src/lib/form/json-schema-form.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class JsonSchemaFormComponent implements OnChanges, OnDestroy, OnInit {
105105
}
106106

107107
ngOnChanges(changes: SimpleChanges) {
108-
if (changes.hasOwnProperty('schema')) {
108+
if (changes.hasOwnProperty('schema') || changes.hasOwnProperty('layout')) {
109109
this.formInitialized = false;
110110
}
111111
this.updateForm();

projects/ngx-json-schema-form/src/lib/select-widget/select-widget.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { JsonSchemaFormService } from '../form/services/json-schema-form.service
1818
styleUrls: ['./select-widget.component.css'],
1919
templateUrl: './select-widget.component.html'
2020
})
21-
export class SelectWidgetComponent extends AbstractWidget implements OnInit, OnChanges {
21+
export class SelectWidgetComponent extends AbstractWidget implements OnChanges, OnInit {
2222
private newComponent: ComponentRef<any>;
2323

2424
/** Placeholder for injecting widget */
@@ -31,11 +31,11 @@ export class SelectWidgetComponent extends AbstractWidget implements OnInit, OnC
3131
super(jsf, elementDataStorage);
3232
}
3333

34-
ngOnInit() {
34+
ngOnChanges() {
3535
this.updateComponent();
3636
}
3737

38-
ngOnChanges() {
38+
ngOnInit() {
3939
this.updateComponent();
4040
}
4141

0 commit comments

Comments
 (0)