Cart Buy Licenses Contact Integrate into App Docs Support Login/Register
v 1.9.0
v 1.9.0
Overview Examples Docs Source

React Select

Custom widget: React Select

Third-party libraries: React Select MIT

                        
        
            Survey.StylesManager.applyTheme("modern");
        

class CustomSelect extends Survey.SurveyQuestionDropdown {
    constructor(props) {
        super(props);
    }
    get options() {
      return this.question.visibleChoices.map((c) => {
         return {value: c.value, label: c.value }
      });
    }
    get selectedOption() {
      return this.options.filter((o) => o.value == this.question.value);
    }
    onSelectChange = (selectedOption) => {
      this.setValueCore(selectedOption.value);
      console.log(window.survey.data);
    }
    renderElement() {
        if (this.isDisplayMode) {
        return (
<div id={this.question.inputId} className={this.question.getControlClass()} disabled>
    {(this.question.displayValue || this.question.optionsCaption)}
</div>);
        };

        return (
<Select id={this.question.inputId}
        value={this.selectedOption}
        onChange={this.onSelectChange}
        options={this.options}
        required={this.question.isRequired}
        menuPortalTarget={document.querySelector('body')} />
        );
    }
}

Survey.ReactQuestionFactory.Instance.registerQuestion(
  "sv-dropdown-react",
  (props) => {
    return React.createElement(CustomSelect, props);
  }
);

Survey.RendererFactory.Instance.registerRenderer(
  "dropdown",
  "dropdown-react",
  "sv-dropdown-react"
);
            
                var json = {
    "questions": [
       {
            "type": "dropdown",
            "name": "car",
            "title": "What car are you driving?",
            "isRequired": true,
            "hasNone": true,
            "choices": [
                "Ford",
                "Vauxhall",
                "Volkswagen",
                "Nissan",
                "Audi",
                "Mercedes-Benz",
                "BMW",
                "Peugeot",
                "Toyota",
                "Citroen"
            ],
           "renderAs": "dropdown-react"        
       }
    ]
};

                window.survey = new Survey.Model(json);
            
        
            survey.onComplete.add(function(sender) {
            document.querySelector('#surveyResult').textContent =
            "Result JSON:\n" + JSON.stringify(sender.data, null, 3);
            });
        
survey.data = { car: ['Ford'] };
        
            ReactDOM.render(
            <Survey.Survey model={survey}  />, document.getElementById("surveyElement"));
        

                    
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Custom widget: React Select, Reactjs Survey Library Example</title>

<meta name="viewport" content="width=device-width" />
        <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.6.0/polyfill.js"></script>
        <script src="https://unpkg.com/react@16.5.0/umd/react.production.min.js"></script>
        <script src="https://unpkg.com/react-dom@16.5.0/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-react/survey.react.min.js"></script>
    <link href="/DevBuilds/survey-core/modern.min.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="./index.css">
<script src="https://cdn.jsdelivr.net/npm/emotion@9.2.12/dist/emotion.umd.min.js"></script>
<script src="https://unpkg.com/prop-types@15.5.10/prop-types.min.js"></script>
<script src="https://unpkg.com/react-input-autosize@2.2.1/dist/react-input-autosize.min.js"></script>
<script src="https://unpkg.com/react-select@2.4.4/dist/react-select.min.js"></script>
    <script src="/DevBuilds/surveyjs-widgets/surveyjs-widgets.min.js"></script>

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

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

</body>
</html>