Skip to content

Commit 150160f

Browse files
committed
sync css classes. fixes select2#834
1 parent ebc436c commit 150160f

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

select2.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ the specific language governing permissions and limitations under the Apache Lic
583583
}
584584

585585
if (opts.element.attr("class") !== undefined) {
586-
this.container.addClass(opts.element.attr("class").replace(/validate\[[\S ]+] ?/, ''));
586+
this.container.addClass(opts.element.attr("class"));
587587
}
588588

589589
this.container.css(evaluate(opts.containerCss));
@@ -863,8 +863,13 @@ the specific language governing permissions and limitations under the Apache Lic
863863
}));
864864

865865
sync = this.bind(function () {
866-
var enabled = this.opts.element.attr("disabled") !== "disabled";
867-
var readonly = this.opts.element.attr("readonly") === "readonly";
866+
867+
var enabled, readonly, self = this;
868+
869+
// sync enabled state
870+
871+
enabled = this.opts.element.attr("disabled") !== "disabled";
872+
readonly = this.opts.element.attr("readonly") === "readonly";
868873

869874
enabled = enabled && !readonly;
870875

@@ -875,6 +880,35 @@ the specific language governing permissions and limitations under the Apache Lic
875880
this.disable();
876881
}
877882
}
883+
884+
function syncCssClasses(dest, accept) {
885+
var classes, replacements = [];
886+
887+
classes = dest.attr("class");
888+
if (typeof classes === "string") {
889+
$(classes.split(" ")).each2(function() {
890+
if (this.indexOf("select2-") === 0) {
891+
replacements.push(this);
892+
}
893+
});
894+
}
895+
classes = self.opts.element.attr("class");
896+
if (typeof classes === "string") {
897+
$(classes.split(" ")).each2(function() {
898+
if (this.indexOf("select2-") !== 0 && accept(this)) {
899+
replacements.push(this);
900+
}
901+
});
902+
}
903+
dest.attr("class", replacements.join(" "));
904+
}
905+
906+
syncCssClasses(this.container, this.opts.acceptContainerCssClass);
907+
this.container.addClass(evaluate(this.opts.containerCssClass));
908+
909+
syncCssClasses(this.dropdown, this.opts.acceptDropdownCssClass);
910+
this.dropdown.addClass(evaluate(this.opts.dropdownCssClass));
911+
878912
});
879913

880914
// mozilla and IE
@@ -2580,7 +2614,9 @@ the specific language governing permissions and limitations under the Apache Lic
25802614
});
25812615
},
25822616
blurOnChange: false,
2583-
selectOnBlur: false
2617+
selectOnBlur: false,
2618+
acceptContainerCssClass: function(c) { return true; },
2619+
acceptDropdownCssClass: function(c) { return true; }
25842620
};
25852621

25862622
// exports

0 commit comments

Comments
 (0)