Skip to content

Commit

Permalink
if menu is open and select value is updated via val() method, update …
Browse files Browse the repository at this point in the history
…the selected option styling (#2256)
  • Loading branch information
caseyjhol committed Apr 10, 2019
1 parent c13a986 commit 8d5d51f
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions js/bootstrap-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@
});
},

setOptionStatus: function () {
setOptionStatus: function (selectedOnly) {
var that = this;

that.noScroll = false;
Expand All @@ -2114,10 +2114,12 @@
option = liData.option;

if (option) {
that.setDisabled(
liData.index,
liData.disabled
);
if (selectedOnly !== true) {
that.setDisabled(
liData.index,
liData.disabled
);
}

that.setSelected(
liData.index,
Expand Down Expand Up @@ -2170,14 +2172,12 @@
a.setAttribute('aria-selected', selected);
}

if (!keepActive) {
if (!activeIndexIsSet && selected && this.prevActiveIndex !== undefined) {
prevActive = this.selectpicker.main.elements[this.prevActiveIndex];
if (!keepActive && !activeIndexIsSet && selected && this.prevActiveIndex !== undefined) {
prevActive = this.selectpicker.main.elements[this.prevActiveIndex];

prevActive.classList.remove('active');
if (prevActive.firstChild) {
prevActive.firstChild.classList.remove('active');
}
prevActive.classList.remove('active');
if (prevActive.firstChild) {
prevActive.firstChild.classList.remove('active');
}
}
},
Expand Down Expand Up @@ -2307,6 +2307,7 @@
clickedIndex = clickedData.index,
prevValue = getSelectValues(element),
prevIndex = element.selectedIndex,
prevOption = element.options[prevIndex],
triggerChange = true;

// Don't close on multi choice menu
Expand Down Expand Up @@ -2335,7 +2336,7 @@
}

if (!that.multiple) { // Deselect all others if not multi select box
$options.prop('selected', false);
prevOption.selected = false;
option.selected = true;
that.setSelected(clickedIndex, true);
} else { // Toggle the one we have chosen if we are multi select.
Expand Down Expand Up @@ -2575,15 +2576,30 @@
},

val: function (value) {
var element = this.$element[0];

if (typeof value !== 'undefined') {
var prevValue = getSelectValues(this.$element[0]);
var prevValue = getSelectValues(element);

changedArguments = [null, null, prevValue];

this.$element
.val(value)
.trigger('changed' + EVENT_KEY, changedArguments);

if (this.$newElement.hasClass(classNames.SHOW)) {
if (this.multiple) {
this.setOptionStatus(true);
} else {
var liSelectedIndex = (element.options[element.selectedIndex] || {}).liIndex;

if (typeof liSelectedIndex === 'number') {
this.setSelected(this.selectedIndex, false);
this.setSelected(liSelectedIndex, true);
}
}
}

this.render();

changedArguments = null;
Expand Down

0 comments on commit 8d5d51f

Please sign in to comment.