Skip to content

Commit

Permalink
davidstutz#1153: Fix disabledText option which was displayed for enab…
Browse files Browse the repository at this point in the history
…led dropdowns
  • Loading branch information
s-eckard committed Nov 24, 2020
1 parent 1d3d893 commit 27d5a94
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
22 changes: 12 additions & 10 deletions dist/js/bootstrap-multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,35 +248,33 @@
* @param {jQuery} select
* @returns {String}
*/
buttonText: function (options, select) {
if (this.disabledText.length > 0
&& (select.prop('disabled') || (options.length == 0 && this.disableIfEmpty))) {

buttonText: function (selectedOptions, select) {
if (this.disabledText.length > 0 && select.prop('disabled')) {
return this.disabledText;
}
else if (options.length === 0) {
else if (selectedOptions.length === 0) {
return this.nonSelectedText;
}
else if (this.allSelectedText
&& options.length === $('option', $(select)).length
&& selectedOptions.length === $('option', $(select)).length
&& $('option', $(select)).length !== 1
&& this.multiple) {

if (this.selectAllNumber) {
return this.allSelectedText + ' (' + options.length + ')';
return this.allSelectedText + ' (' + selectedOptions.length + ')';
}
else {
return this.allSelectedText;
}
}
else if (this.numberDisplayed != 0 && options.length > this.numberDisplayed) {
return options.length + ' ' + this.nSelectedText;
else if (this.numberDisplayed != 0 && selectedOptions.length > this.numberDisplayed) {
return selectedOptions.length + ' ' + this.nSelectedText;
}
else {
var selected = '';
var delimiter = this.delimiterText;

options.each(function () {
selectedOptions.each(function () {
var label = ($(this).attr('label') !== undefined) ? $(this).attr('label') : $(this).text();
selected += label + delimiter;
});
Expand Down Expand Up @@ -1617,6 +1615,8 @@
this.$select.prop('disabled', false);
this.$button.prop('disabled', false)
.removeClass('disabled');

this.updateButtonText();
},

/**
Expand All @@ -1626,6 +1626,8 @@
this.$select.prop('disabled', true);
this.$button.prop('disabled', true)
.addClass('disabled');

this.updateButtonText();
},

/**
Expand Down
2 changes: 1 addition & 1 deletion dist/js/bootstrap-multiselect.min.js

Large diffs are not rendered by default.

41 changes: 37 additions & 4 deletions tests/spec/bootstrap-multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe('Bootstrap Multiselect "Core".', function() {
},
checkboxName: function($option) {
return 'value-' + $($option).attr('value');
}
},
disabledText: "Disabled"
});
});

Expand Down Expand Up @@ -110,6 +111,7 @@ describe('Bootstrap Multiselect "Core".', function() {
$('#multiselect').multiselect('disable');

expect($('#multiselect').prop('disabled')).toBe(true);
expect($('#multiselect-container button .multiselect-selected-text').text()).toBe('Disabled');
});

it('Should be able to enable the multiselect', function() {
Expand Down Expand Up @@ -415,7 +417,7 @@ describe('Bootstrap Multiselect "Single Selection".', function() {

describe('Bootstrap Multiselect "Individual Configuration Options".', function() {

describe('disableIfEmpty.', function() {
describe('disableIfEmpty without options.', function() {

var $select = null;
beforeEach(function() {
Expand All @@ -425,12 +427,14 @@ describe('Bootstrap Multiselect "Individual Configuration Options".', function()

$select.multiselect({
buttonContainer: '<div id="multiselect-container"></div>',
disableIfEmpty: true
disableIfEmpty: true,
disabledText: 'Disabled'
});
});

it('Should disable button if emppty.', function() {
it('Should disable button if empty.', function() {
expect($('#multiselect-container button').prop('disabled')).toBe(true);
expect($('#multiselect-container button .multiselect-selected-text').text()).toBe('Disabled');
});

it('Should still be disabled after invoking rebuild.', function() {
Expand Down Expand Up @@ -461,6 +465,35 @@ describe('Bootstrap Multiselect "Individual Configuration Options".', function()
$('#multiselect').remove();
});
});

describe('disableIfEmpty with options.', function() {

var $select = null;
beforeEach(function() {
$select = $('<select id="multiselect" multiple="multiple"></select>');
$select.append('<option value="value-1">Option 1</option>');
$select.append('<option value="value-2">Option 2</option>');
$select.append('<option value="value-3">Option 3</option>');

$('body').append($select);

$select.multiselect({
buttonContainer: '<div id="multiselect-container"></div>',
disableIfEmpty: true,
nonSelectedText: 'Enabled'
});
});

it('Should enable button.', function() {
expect($('#multiselect-container button').prop('disabled')).toBe(false);
expect($('#multiselect-container button .multiselect-selected-text').text()).toBe('Enabled');
});

afterEach(function() {
$('#multiselect').multiselect('destroy');
$('#multiselect').remove();
});
});
});

describe('Bootstrap Multiselect "individual Methods".', function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/spec/bootstrap-multiselect.min.js

Large diffs are not rendered by default.

0 comments on commit 27d5a94

Please sign in to comment.