Select2 Custom Widget

Source code: Survey setup
Survey.defaultBootstrapCss.navigationButton = "btn btn-primary";
Survey.Survey.cssType = "bootstrap";
Survey.JsonObject.metaData.addProperty("dropdown", {name: "renderAs", default: "standard", choices: ["standard", "select2"]});
var survey = new Survey.Model({ questions: [
 { type: "dropdown", renderAs: "select2", choicesByUrl: { url: "https://restcountries.eu/rest/v1/all" }, name: "countries", title: "Please select the country you have arrived from:"}
]});


var widget = {
    name: "select2",
    htmlTemplate: "<select style='width: 100%;'></select>",
    isFit : function(question) { return question["renderAs"] === 'select2'; },
    afterRender: function(question, el) {

        var $el = $(el).find("select");

        var widget = $el.select2({
            theme: "classic"
        });
        var updateChoices = function() {
            $el.select2({data: question.visibleChoices.map(function(choice) { return { id: choice.value, text: choice.text }; })});
        }
        question.choicesChangedCallback = updateChoices;
        updateChoices();
        $el.on('select2:select', function (e) {
            question.value = e.target.value;
        });
        var updateHandler = function() {
            $el.val(question.value).trigger("change");
        };
        question.valueChangedCallback = updateHandler;
        updateHandler();
    }

    ,
    willUnmount: function(question, el) {
        $(el).find("select").select2("destroy");
    } 

}
Survey.CustomWidgetCollection.Instance.addCustomWidget(widget);
survey.data = { countries: "Andorra" };


$("#surveyElement").Survey({
    model: survey
});