Skip to content

Commit

Permalink
Fix maximumSelectionLength being ignored by closeOnSelect
Browse files Browse the repository at this point in the history
There was a bug where the `maximumSelectionLength` option would not
kick in if the `closeOnSelect` option was enabled. Normally, this
was enabled by someone in their global configuration, but it could
also be seen when somoene selected an option while holding the
meta/ctrl/alt keys. This would implicitly enable the `closeOnSelect`
behaviour, even when it was not globally enabled, and cause the bug.

This fixes that issue by listening to the `select` event which is
triggered whenever an option is selected, and triggers the "maximum
selected" message based on that event. This should now force the
message to be displayed, even when the results did not have to be
queried another time.

Fixes #3514
Fixes #3860
Closes #5333
  • Loading branch information
kevin-brown committed Jul 21, 2019
1 parent 7da17ea commit 7767663
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/js/select2/data/maximumSelectionLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,30 @@ define([
decorated.call(this, $e, options);
}

MaximumSelectionLength.prototype.bind =
function (decorated, container, $container) {
var self = this;

decorated.call(this, container, $container);

container.on('select', function () {
self._checkIfMaximumSelected();
});
};

MaximumSelectionLength.prototype.query =
function (decorated, params, callback) {
var self = this;

this._checkIfMaximumSelected(function () {
decorated.call(self, params, callback);
});
};

MaximumSelectionLength.prototype._checkIfMaximumSelected =
function (_, successCallback) {
var self = this;

this.current(function (currentData) {
var count = currentData != null ? currentData.length : 0;
if (self.maximumSelectionLength > 0 &&
Expand All @@ -23,7 +43,10 @@ define([
});
return;
}
decorated.call(self, params, callback);

if (successCallback) {
successCallback();
}
});
};

Expand Down
25 changes: 25 additions & 0 deletions tests/data/maximumSelectionLength-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,28 @@ test('triggers when >= 1 selection' , function (assert) {
assert.ok(false, 'The results should not be queried');
});
});

test('triggers after selection' , function (assert) {
assert.expect(1);

var $select = $('#qunit-fixture .multiple');

var maxOfOneOptions = new Options({
maximumSelectionLength: 1
});

var container = new MockContainer();
var data = new MaximumSelectionData($select, maxOfOneOptions);

data.bind(container, null);

data.on('results:message', function () {
assert.ok(true, 'The message should be displayed');
});

$select.val(['One']);

container.trigger('select', {
data: {}
});
});

0 comments on commit 7767663

Please sign in to comment.