Radio group with Custom Item Template


                        
        
            Survey.StylesManager.applyTheme("defaultV2");
        



                
                    var json = {
  "elements": [
    {
      "type": "radiogroup",
      "renderAs": "custom-radiogroup",
      "name": "Radiogroup with custom items rendering",
      "showOtherItem": true,
      "choices": [
        "Button 1",
        "Button 2",
        "Button 3"
      ]
    }

  ]
};

                    window.survey = new Survey.Model(json);
                
        
            survey.onComplete.add(function(sender) {
            document.querySelector('#surveyResult').textContent =
            "Result JSON:\n" + JSON.stringify(sender.data, null, 3);
            });
        
class CustomRadiogroupQuestion extends SurveyReact.SurveyQuestionRadiogroup {
  renderElement() {
    return (
<React.Fragment>
    <div>You can render any additional markup around the question content</div>
    {super.renderElement()}
</React.Fragment>
    );
  }
}

SurveyReact.ReactQuestionFactory.Instance.registerQuestion(
  "sv-custom-radiogroup",
  function(props) {
    return React.createElement(CustomRadiogroupQuestion, props);
  }
);

class MySurveyQuestionRadioItem extends SurveyReact.ReactSurveyElement {
  renderElement() {
    var props = this.props;
    return (
<div className={this.props.question.getItemClass(this.props.item)}>
    <button type="button" onClick={function() {
            if(props.question.isDesignMode) {
                return;
            }
            alert(props.item.text);
        }}>{this.renderLocString(props.item.locText, props.textStyle)}
    </button>
</div>
    );
  }
}

SurveyReact.ReactElementFactory.Instance.registerElement(
  "survey-radiogroup-item",
  function(props) {
    if(props.question.renderAs === "custom-radiogroup") {
      return React.createElement(MySurveyQuestionRadioItem, props);
    }
    return React.createElement(SurveyReact.SurveyQuestionRadioItem, props);
  }
);

Survey.RendererFactory.Instance.registerRenderer(
  "radiogroup",
  "custom-radiogroup",
  "sv-custom-radiogroup"
);
        
            ReactDOM.render(
            <SurveyReact.Survey model={survey}  />, document.getElementById("surveyElement"));
        

                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Radio group with Custom Item Template, Reactjs Survey Library Example</title>
    
<meta name="viewport" content="width=device-width" />
    <script src="https://unpkg.com/react@17.0.1/umd/react.production.min.js"></script>
    <script src="https://unpkg.com/react-dom@17.0.1/umd/react-dom.production.min.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.2.5/babel.min.js"></script>
    <script src="/DevBuilds/survey-core/survey.core.min.js"></script>
    <script src="/DevBuilds/survey-core/survey.i18n.min.js"></script>
    <script src="/DevBuilds/survey-react-ui/survey-react-ui.min.js"></script>
    <link href="/DevBuilds/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="./index.css">

</head>
<body style="margin: 0">
    
            <div id="surveyElement" style="display:inline-block;width:100%;">
                            </div>
    <div id="surveyResult"></div>

<script type="text/babel" src="./index.js"></script>

</body>
</html>

Loading...
Sorry, we can't retrieve the data from server. Please comeback later.

If you want to customize a radiogroup item, follow the steps below:

  1. Implement a component that renders item markup.

  2. Register the component so that it can be accessed by name.
    In React, register the component in ReactElementFactory as shown in the JavaScript tab.

    In Knockout, use techniques native to these libraries:

Why we use cookies.

This site uses cookies to make your browsing experience more convenient and personal. Cookies store useful information on your computer to help us improve the efficiency and relevance of our site for you. In some cases, they are essential to making the site work properly. By accessing this site, you consent to the use of cookies.

For more information, refer to DevSoft Baltic’ privacy policy and cookie policy.