Skip to content

Commit f1927fb

Browse files
authored
Merge pull request #480 from Saeleas/master
Fixing issue when using visibleIf in array items.
2 parents 643abca + 22a580a commit f1927fb

5 files changed

Lines changed: 118 additions & 2 deletions

File tree

projects/schema-form/src/lib/model/arrayproperty.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class ArrayProperty extends PropertyGroup {
2323
addItem(value: any = null): FormProperty {
2424
let newProperty = this.addProperty();
2525
newProperty.reset(value, false);
26+
newProperty._bindVisibility();
2627
return newProperty;
2728
}
2829

src/app/json-schema-example/json-schema-example.component.canonical-path.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('JsonSchemaExampleComponent - canonical-path', () => {
9292

9393
// select demo sample
9494
const select: HTMLSelectElement = fixture.debugElement.query(By.css('#samples')).nativeElement;
95-
select.value = select.options[6].value; // <-- select a new value
95+
select.value = select.options[7].value; // <-- select a new value
9696
select.dispatchEvent(new Event('change'));
9797
fixture.detectChanges();
9898
});

src/app/json-schema-example/json-schema-example.component.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import binding_sample_bindings from './binding_sample_bindings';
1515
import visibility_binding_example from './visibility-binding-example-schema.json';
1616
import visibility_binding_example2 from './visibility-binding-example-schema2.json';
1717
import visibility_binding_example3 from './visibility-binding-example-schema3.json';
18+
import visibility_binding_example4 from './visibility-binding-example-schema4.json';
1819
import sample_canonical_path from './sample-canonical-path.json';
1920

2021
import {AppService, AppData} from '../app.service';
@@ -43,7 +44,8 @@ export class JsonSchemaExampleComponent implements OnInit, OnDestroy {
4344
{label: 'Sample 4 - Visibility binding', event: this.changeSchemaVisibilityBinding, selected: false},
4445
{label: 'Sample 5 - Visibility binding 2', event: this.changeSchemaVisibilityBinding2, selected: false},
4546
{label: 'Sample 6 - Visibility binding 3', event: this.changeSchemaVisibilityBinding3, selected: false},
46-
{label: 'Sample 7 - Canonical path', event: this.changeSchemaCanonicalPath, selected: false},
47+
{label: 'Sample 7 - Visibility binding 4', event: this.changeSchemaVisibilityBinding4, selected: false},
48+
{label: 'Sample 8 - Canonical path', event: this.changeSchemaCanonicalPath, selected: false},
4749
];
4850

4951
constructor(
@@ -229,6 +231,14 @@ export class JsonSchemaExampleComponent implements OnInit, OnDestroy {
229231
this.actions = {};
230232
}
231233

234+
changeSchemaVisibilityBinding4() {
235+
this.schema = visibility_binding_example4 as unknown as ISchema;
236+
this.model = {};
237+
this.fieldBindings = {};
238+
this.fieldValidators = {};
239+
this.actions = {};
240+
}
241+
232242
changeSchemaCanonicalPath(){
233243
this.schema = sample_canonical_path as unknown as ISchema;
234244
this.model = {};

src/app/json-schema-example/json-schema-example.component.visibleIf.spec.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,3 +817,67 @@ describe("JsonSchemaExampleComponent - visibleIf - condition-types (chained cond
817817
});
818818
}));
819819
});
820+
821+
822+
describe("JsonSchemaExampleComponent - visibleIf - array items have visibleIf fields", () => {
823+
let component: JsonSchemaExampleComponent;
824+
let fixture: ComponentFixture<JsonSchemaExampleComponent>;
825+
826+
beforeEach(async(() => {
827+
TestBed.configureTestingModule({
828+
imports: [SchemaFormModule.forRoot(), HttpClientModule, FormsModule, ReactiveFormsModule],
829+
declarations: [JsonSchemaExampleComponent],
830+
providers: [
831+
{ provide: WidgetRegistry, useClass: DefaultWidgetRegistry },
832+
{
833+
provide: SchemaValidatorFactory,
834+
useClass: ZSchemaValidatorFactory
835+
}
836+
]
837+
}).compileComponents();
838+
}));
839+
840+
beforeEach(() => {
841+
fixture = TestBed.createComponent(JsonSchemaExampleComponent);
842+
component = fixture.componentInstance;
843+
fixture.detectChanges();
844+
});
845+
846+
beforeEach(() => {
847+
// select demo sample
848+
const select: HTMLSelectElement = fixture.debugElement.query(By.css("#samples")).nativeElement;
849+
select.value = select.options[6].value; // <-- select a new value
850+
select.dispatchEvent(new Event("change"));
851+
fixture.detectChanges();
852+
});
853+
854+
it(`# 1. Test 'VisibleIf' within an element of an array`, async(() => {
855+
// Visible component shows up if status value is 'Warn' or 'Fail'
856+
857+
fixture.whenStable().then(() => {
858+
fixture.detectChanges();
859+
860+
// expect page containing a sf-form element
861+
const sf_form = fixture.debugElement.query(By.css("sf-form"));
862+
expect(sf_form).toBeTruthy();
863+
864+
// Add an element
865+
const _add_button = fixture.debugElement.query(By.css(".array-add-button")).nativeElement;
866+
_add_button.click();
867+
fixture.detectChanges();
868+
// initial state
869+
const _select: HTMLSelectElement = fixture.debugElement.query(By.css("#arrayObject\\.0\\.showHiddenField")).nativeElement;
870+
let _input = fixture.debugElement.query(By.css("#arrayObject\\.0\\.visibleTest"));
871+
expect(_select).toBeDefined();
872+
expect(_input).toBeNull();
873+
874+
// change state to make field visible
875+
_select.value = _select.options[1].value;
876+
_select.dispatchEvent(new Event("change"));
877+
fixture.detectChanges();
878+
_input = fixture.debugElement.query(By.css("#arrayObject\\.0\\.visibleTest")).nativeElement;
879+
expect(_input).toBeDefined();
880+
});
881+
}));
882+
883+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"type": "object",
3+
"title": "Example of visibleIf inside of an array item",
4+
"description": "This example shows how to use visibility options inside of items of an array",
5+
"properties": {
6+
"arrayObject": {
7+
"type": "array",
8+
"items": {
9+
"type": "object",
10+
"title": "Object with visibleIf properties.",
11+
"properties": {
12+
"showHiddenField": {
13+
"type": "integer",
14+
"widget": "select",
15+
"title": "Choose 1 to show the hidden field or 0 to hide it. By default the field is hidden.",
16+
"oneOf": [
17+
{
18+
"enum": [0],
19+
"description": "Hide field."
20+
},
21+
{
22+
"enum": [1],
23+
"description": "Show hidden field."
24+
}
25+
]
26+
},
27+
"visibleTest": {
28+
"type": "string",
29+
"title": "This is shown if previous field is 1.",
30+
"visibleIf": {
31+
"showHiddenField": [1]
32+
}
33+
}
34+
}
35+
}
36+
}
37+
},
38+
"required": [
39+
"arrayObject"
40+
]
41+
}

0 commit comments

Comments
 (0)