Skip to content

Commit

Permalink
Add dedicated event for clearing
Browse files Browse the repository at this point in the history
Closes #4929, #5045.
  • Loading branch information
gronostajo committed Sep 26, 2017
1 parent be4eba2 commit f36115e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/js/select2/core.js
Expand Up @@ -417,7 +417,8 @@ define([
'open': 'opening',
'close': 'closing',
'select': 'selecting',
'unselect': 'unselecting'
'unselect': 'unselecting',
'clear': 'clearing'
};

if (args === undefined) {
Expand Down
11 changes: 10 additions & 1 deletion src/js/select2/selection/allowClear.js
Expand Up @@ -48,8 +48,17 @@ define([
var previousVal = this.$element.val();
this.$element.val(this.placeholder.id);

var unselectData = {
data: data
};
this.trigger('clear', unselectData);
if (unselectData.prevented) {
this.$element.val(previousVal);
return;
}

for (var d = 0; d < data.length; d++) {
var unselectData = {
unselectData = {
data: data[d]
};

Expand Down
5 changes: 3 additions & 2 deletions src/js/select2/selection/eventRelay.js
Expand Up @@ -9,10 +9,11 @@ define([
'open', 'opening',
'close', 'closing',
'select', 'selecting',
'unselect', 'unselecting'
'unselect', 'unselecting',
'clear', 'clearing'
];

var preventableEvents = ['opening', 'closing', 'selecting', 'unselecting'];
var preventableEvents = ['opening', 'closing', 'selecting', 'unselecting', 'clearing'];

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

Expand Down
88 changes: 88 additions & 0 deletions tests/selection/allowClear-tests.js
Expand Up @@ -190,6 +190,94 @@ test('preventing the unselect event cancels the clearing', function (assert) {
);
});

test('clicking clear will trigger the clear event', function (assert) {
assert.expect(5);

var $element = $('#qunit-fixture .single-with-placeholder');

var selection = new AllowClearPlaceholder(
$element,
allowClearOptions
);
var container = new MockContainer();

var $selection = selection.render();

selection.bind(container, $('<div></div>'));

$element.val('One');
selection.update([{
id: 'One',
text: 'One'
}]);

selection.on('clear', function (ev) {
assert.ok(
'data' in ev && ev.data,
'The event should have been triggered with the data property'
);

assert.ok(
$.isArray(ev.data),
'The data should be an array'
);

assert.equal(
ev.data.length,
1,
'The data should contain one item for each value'
);

assert.equal(
ev.data[0].id,
'One',
'The data should contain unselected objects'
);

assert.equal(
$element.val(),
'placeholder',
'The previous value should be unselected'
);
});

var $remove = $selection.find('.select2-selection__clear');
$remove.trigger('mousedown');
});

test('preventing the clear event cancels the clearing', function (assert) {
var $element = $('#qunit-fixture .single-with-placeholder');

var selection = new AllowClearPlaceholder(
$element,
allowClearOptions
);
var container = new MockContainer();

var $selection = selection.render();

selection.bind(container, $('<div></div>'));

$element.val('One');
selection.update([{
id: 'One',
text: 'One'
}]);

selection.on('clear', function (ev) {
ev.prevented = true;
});

var $remove = $selection.find('.select2-selection__clear');
$remove.trigger('mousedown');

assert.equal(
$element.val(),
'One',
'The placeholder should not have been set'
);
});

test('clear does not work when disabled', function (assert) {
var $element = $('#qunit-fixture .single-with-placeholder');

Expand Down

0 comments on commit f36115e

Please sign in to comment.