surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.03k stars 786 forks source link

Checkbox choices aren't being rendered after using fromJSON? #3331

Closed leemi closed 2 years ago

leemi commented 2 years ago

Are you requesting a feature, reporting a bug or asking a question?

I am reporting a bug. Thanks!

What is the current behavior?

image

What is the expected behavior?

image

How would you reproduce the current behavior (if this is a bug)?

Load a page with choices into a PageModel via fromJSON

Provide the test code and the tested page URL (if applicable)

Tested page URL: https://codesandbox.io/s/modern-tdd-t2yvh?file=/src/SurveyComponent.jsx

Test code

import React, { Component } from "react";

import * as Survey from "survey-react";

import "survey-react/modern.css";
import "./index.css";

Survey.StylesManager.applyTheme("modern");

class SurveyComponent extends Component {
  render() {
    const json = {
      questions: [
        {
          type: "checkbox",
          name: "car",
          title: "What car are you driving?",
          isRequired: true,
          hasNone: true,
          colCount: 4,
          choices: [
            "Ford",
            "Vauxhall",
            "Volkswagen",
            "Nissan",
            "Audi",
            "Mercedes-Benz",
            "BMW",
            "Peugeot",
            "Toyota",
            "Citroen"
          ]
        }
      ]
    };
    const survey = new Survey.Model(json);

    // Serialize Page to JSON
    const pageModel = survey.pages[0];
    const pageJSON = pageModel.toJSON();

    // Remove Page
    survey.removePage(pageModel);

    // Re-add Page
    const pg = survey.addNewPage();
    pg.fromJSON(pageJSON);
    console.log(pageJSON);

    return <Survey.Survey model={survey} />;
  }
}

export default SurveyComponent;

Specify your

andrewtelnov commented 2 years ago

@leemi I will take a look later. As a work-around, please use the following code from pg.fromJSON(pageJSON); survey.endLoadingFromJson();.

Thank you, Andrew

andrewtelnov commented 2 years ago

@leemi The better work-around would be pg.onSurveyLoad();. I have fixed the issue by commit above, by adding a new unit test. Unfortunately, this fix will likely go to v1.8.67 that we will release on next week and it will not in v1.8.66 that we are going to release shortly.

Thank you, Andrew