Skip to content

Commit

Permalink
fix setStyle (support for multiple classes via classList) (#2221)
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyjhol committed Mar 9, 2019
1 parent 79e4a9e commit 0628e1f
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions js/bootstrap-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@

return {
add: function (classes) {
classes = Array.prototype.slice.call(arguments).join(' ');
return $elem.addClass(classes);
},
remove: function (classes) {
classes = Array.prototype.slice.call(arguments).join(' ');
return $elem.removeClass(classes);
},
toggle: function (classes, force) {
Expand Down Expand Up @@ -176,6 +178,21 @@

var testElement = document.createElement('_');

testElement.classList.add.apply(testElement.classList, ['c1', 'c2']);

if (!testElement.classList.contains('c2')) {
var _add = DOMTokenList.prototype.add,
_remove = DOMTokenList.prototype.remove;

DOMTokenList.prototype.add = function () {
Array.prototype.forEach.call(arguments, _add.bind(this));
}

DOMTokenList.prototype.remove = function () {
Array.prototype.forEach.call(arguments, _remove.bind(this));
}
}

testElement.classList.toggle('c3', false);

// Polyfill for IE 10 and Firefox <24, where classList.toggle does not
Expand Down Expand Up @@ -1719,21 +1736,28 @@
* @param [style]
* @param [status]
*/
setStyle: function (style, status) {
var button = this.$button[0];
setStyle: function (newStyle, status) {
var button = this.$button[0],
style = this.options.style.split(' '),
buttonClass;

if (this.$element.attr('class')) {
this.$newElement.addClass(this.$element.attr('class').replace(/selectpicker|mobile-device|bs-select-hidden|validate\[.*\]/gi, ''));
}

var buttonClass = style || this.options.style;
if (newStyle) {
buttonClass = newStyle.split(' ');
} else {
buttonClass = style;
}

if (status == 'add') {
button.classList.add(buttonClass);
button.classList.add.apply(button.classList, buttonClass);
} else if (status == 'remove') {
button.classList.remove(buttonClass);
button.classList.remove.apply(button.classList, buttonClass);
} else {
button.classList.remove(this.options.style);
button.classList.add(buttonClass);
button.classList.remove.apply(button.classList, style);
button.classList.add.apply(button.classList, buttonClass);
}
},

Expand Down

0 comments on commit 0628e1f

Please sign in to comment.