Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Setting default properties for custom widget #143

Open
mkopinsky opened this issue Oct 3, 2019 · 3 comments
Open

[Question] Setting default properties for custom widget #143

mkopinsky opened this issue Oct 3, 2019 · 3 comments
Labels

Comments

@mkopinsky
Copy link

I am creating (for use in survey-creator) a custom widget based on multipletext, except that the set of items are predefined, and shouldn't be editable by the user.

I'm able to define the class and set items to non-editable (mostly*) via the following:

  activatedByChanged: function (activatedBy) {
    // We are creating a new class and derived it from multipletext question type, but with the 'items' editor hidden
    Survey.JsonObject.metaData.addClass(
      "bloodpressure", [
        {
          name: 'items',
          visible: false,
        },
      ],
      null,
      "multipletext"
    );
  }

and pass an initial JSON to editor.toolbox.addItems(...), but it would be great if there were a way to set the items from within the custom widget definition itself.

Am I missing something?

@mkopinsky
Copy link
Author

The "mostly*" above refers to surveyjs/survey-creator#609

@tsv2013
Copy link
Member

tsv2013 commented Oct 7, 2019

You can use the "default" property of the metadata:

Serializer.addClass(
  "page",
  [
    {
      name: "navigationButtonsVisibility",
      default: "inherit",
      choices: ["inherit", "show", "hide"]
    },
    {
      name: "questionsOrder",
      default: "default",
      choices: ["default", "initial", "random"]
    },
    { name: "maxTimeToFinish:number", default: 0 }
  ],
  function() {
    return new PageModel();
  },
  "panelbase"
);

@mkopinsky
Copy link
Author

For some reason that didn't set the default values in the JSON when it's added to the toolbox (in QuestionToolbox.prototype.registerCustomWidgets). Perhaps that function should be looking at JSONMetadata to pre-set props into the JSON??

From poking around with a debugger I was able to get it to work by exporting a defaultJSON key in the widget definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants