Skip to content

Commit

Permalink
Fix: Survey-React, conditional rendering is broken with inline questi…
Browse files Browse the repository at this point in the history
…ons #3031 (#3033)
  • Loading branch information
andrewtelnov committed Jun 29, 2021
1 parent d81d7c1 commit 66f074b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 26 deletions.
25 changes: 21 additions & 4 deletions src/panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export class QuestionRowModel extends Base {
this.isNeedRender = !this.isLazyRendering;
this.visible = panel.areInvisibleElementsShowing;
this.createNewArray("elements");
this.createNewArray("visibleElements");
}
public get isLazyRendering() {
return (
Expand All @@ -116,6 +117,9 @@ export class QuestionRowModel extends Base {
public get elements(): Array<IElement> {
return this.getPropertyValue("elements");
}
public get visibleElements(): Array<IElement> {
return this.getPropertyValue("visibleElements");
}
public get visible(): boolean {
return this.getPropertyValue("visible", true);
}
Expand All @@ -128,9 +132,6 @@ export class QuestionRowModel extends Base {
public set isNeedRender(val: boolean) {
this.setPropertyValue("isneedrender", val);
}
public get visibleElements(): Array<IElement> {
return this.elements.filter((e) => e.isVisible);
}
public updateVisible() {
this.visible = this.calcVisible();
this.setWidth();
Expand Down Expand Up @@ -212,7 +213,23 @@ export class QuestionRowModel extends Base {
return Helpers.isNumber(width) ? width + "px" : width;
}
private calcVisible(): boolean {
return this.visibleElements.length > 0;
var visElements: Array<IElement> = [];
for (var i = 0; i < this.elements.length; i++) {
if (this.elements[i].isVisible) {
visElements.push(this.elements[i]);
}
}
if (this.needToUpdateVisibleElements(visElements)) {
this.setPropertyValue("visibleElements", visElements);
}
return visElements.length > 0;
}
private needToUpdateVisibleElements(visElements: Array<IElement>): boolean {
if (visElements.length !== this.visibleElements.length) return true;
for (var i = 0; i < visElements.length; i++) {
if (visElements[i] !== this.visibleElements[i]) return true;
}
return false;
}
public dispose() {
super.dispose();
Expand Down
36 changes: 17 additions & 19 deletions src/react/row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,23 @@ export class SurveyRow extends SurveyElementBase<any, any> {
protected renderElementContent(): JSX.Element {
var elements = null;
if (this.row.isNeedRender) {
elements = this.row.elements
.filter((element) => element.isVisible)
.map((element) => {
const innerElement = this.createElement(element);
var rootStyle: { [index: string]: any } = {};
if (element.renderWidth) {
rootStyle["width"] = element.renderWidth;
rootStyle["flexGrow"] = 1;
rootStyle["flexShrink"] = 1;
rootStyle["flexBasis"] = element.renderWidth;
rootStyle["minWidth"] = element.minWidth;
rootStyle["maxWidth"] = element.maxWidth;
}
return (
<div style={rootStyle} key={innerElement.key}>
{innerElement}
</div>
);
});
elements = this.row.visibleElements.map((element) => {
const innerElement = this.createElement(element);
var rootStyle: { [index: string]: any } = {};
if (element.renderWidth) {
rootStyle["width"] = element.renderWidth;
rootStyle["flexGrow"] = 1;
rootStyle["flexShrink"] = 1;
rootStyle["flexBasis"] = element.renderWidth;
rootStyle["minWidth"] = element.minWidth;
rootStyle["maxWidth"] = element.maxWidth;
}
return (
<div style={rootStyle} key={innerElement.key}>
{innerElement}
</div>
);
});
}

return (
Expand Down
43 changes: 43 additions & 0 deletions testCafe/survey/elementsVisibilityOnSameRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { frameworks, url, initSurvey, getSurveyResult } from "../helper";
import { Selector, ClientFunction } from "testcafe";
const assert = require("assert");
const title = `Elements Visibility on Same row`;

const json = {
pages: [
{
elements: [
{ type: "radiogroup", name: "q1", choices: ["Yes", "No"] },
{
type: "text",
name: "q2",
startWithNewLine: false,
visibleIf: "{q1} = 'Yes'",
},
],
},
],
};

frameworks.forEach((framework) => {
fixture`${framework} ${title}`.page`${url}${framework}`.beforeEach(
async (t) => {
await initSurvey(framework, json);
}
);
test(`make element on same row visible`, async (t) => {
await t.expect(Selector("span").withText("2.").visible).notOk();
const yesChoice = Selector(`input[value="Yes"]`);
const noChoice = Selector(`input[value="No"]`);
await t
.click(yesChoice)
.expect(yesChoice.checked)
.ok();
await t.expect(Selector("span").withText("2.").visible).ok();
await t
.click(noChoice)
.expect(noChoice.checked)
.ok();
await t.expect(Selector("span").withText("2.").visible).notOk();
});
});
34 changes: 31 additions & 3 deletions tests/paneltests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ QUnit.test("Panel.ensureRowsVisibility", function(assert) {
page.onFirstRendering();
assert.equal(panel.rows.length, 2);

panel.rows.forEach(row => {
panel.rows.forEach((row) => {
assert.equal(row["_updateVisibility"], undefined);
assert.equal(row["_scrollableParent"], undefined);
row["_updateVisibility"] = handler;
Expand Down Expand Up @@ -796,7 +796,7 @@ QUnit.test("Panel.startLazyRendering isNeedRender=true", function(assert) {
page.onFirstRendering();
assert.equal(panel.rows.length, 2);

panel.rows.forEach(row => {
panel.rows.forEach((row) => {
assert.equal(row["_scrollableParent"], undefined);
assert.equal(row["_updateVisibility"], undefined);
assert.equal(row.isNeedRender, false);
Expand Down Expand Up @@ -854,7 +854,7 @@ QUnit.test("Panel.startLazyRendering isNeedRender=false", function(assert) {
page.onFirstRendering();
assert.equal(panel.rows.length, 2);

panel.rows.forEach(row => {
panel.rows.forEach((row) => {
assert.equal(row["_scrollableParent"], undefined);
assert.equal(row["_updateVisibility"], undefined);
assert.equal(row.isNeedRender, false);
Expand All @@ -875,3 +875,31 @@ QUnit.test("Panel.startLazyRendering isNeedRender=false", function(assert) {
settings.lazyRowsRendering = prevLazyRowsRendering;
}
});
QUnit.test("row.visibleElements make it reactive", function(assert) {
var page = new PageModel();
page.addNewQuestion("text", "q1");
page.addNewQuestion("text", "q2");
page.addNewQuestion("text", "q3");
page.questions[1].startWithNewLine = false;
page.questions[2].startWithNewLine = false;
assert.equal(page.rows.length, 1, "We have one row");
var row = page.rows[0];
assert.equal(row.elements.length, 3, "We have 3 elements in row");
assert.equal(row.visibleElements.length, 3, "All elements are visible");
assert.equal(
row.getPropertyValue("visibleElements").length,
3,
"visibleElements are reactive"
);
page.questions[1].visible = false;
assert.equal(row.visibleElements.length, 2, "Two elements are visible");
assert.equal(row.visibleElements[0].name, "q1", "First element");
assert.equal(row.visibleElements[1].name, "q3", "Second element");
page.removeElement(page.questions[2]);
assert.equal(row.visibleElements.length, 1, "One element is visible");
assert.equal(row.visibleElements[0].name, "q1", "First element, #2");
page.questions[1].visible = true;
assert.equal(row.visibleElements.length, 2, "Two elements are visible, #3");
assert.equal(row.visibleElements[0].name, "q1", "First element, #3");
assert.equal(row.visibleElements[1].name, "q2", "Second element, #3");
});

0 comments on commit 66f074b

Please sign in to comment.