Skip to content

Commit 04d3a5d

Browse files
gronostajoalexweissman
authored andcommitted
Add dedicated event for clearing
Closes select2#4929, select2#5045.
1 parent 10bda78 commit 04d3a5d

4 files changed

Lines changed: 105 additions & 4 deletions

File tree

src/js/select2/core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ define([
417417
'open': 'opening',
418418
'close': 'closing',
419419
'select': 'selecting',
420-
'unselect': 'unselecting'
420+
'unselect': 'unselecting',
421+
'clear': 'clearing'
421422
};
422423

423424
if (args === undefined) {

src/js/select2/selection/allowClear.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,17 @@ define([
4848
var previousVal = this.$element.val();
4949
this.$element.val(this.placeholder.id);
5050

51+
var unselectData = {
52+
data: data
53+
};
54+
this.trigger('clear', unselectData);
55+
if (unselectData.prevented) {
56+
this.$element.val(previousVal);
57+
return;
58+
}
59+
5160
for (var d = 0; d < data.length; d++) {
52-
var unselectData = {
61+
unselectData = {
5362
data: data[d]
5463
};
5564

src/js/select2/selection/eventRelay.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ define([
99
'open', 'opening',
1010
'close', 'closing',
1111
'select', 'selecting',
12-
'unselect', 'unselecting'
12+
'unselect', 'unselecting',
13+
'clear', 'clearing'
1314
];
1415

15-
var preventableEvents = ['opening', 'closing', 'selecting', 'unselecting'];
16+
var preventableEvents = [
17+
'opening', 'closing', 'selecting', 'unselecting', 'clearing'
18+
];
1619

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

tests/selection/allowClear-tests.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,94 @@ test('preventing the unselect event cancels the clearing', function (assert) {
190190
);
191191
});
192192

193+
test('clicking clear will trigger the clear event', function (assert) {
194+
assert.expect(5);
195+
196+
var $element = $('#qunit-fixture .single-with-placeholder');
197+
198+
var selection = new AllowClearPlaceholder(
199+
$element,
200+
allowClearOptions
201+
);
202+
var container = new MockContainer();
203+
204+
var $selection = selection.render();
205+
206+
selection.bind(container, $('<div></div>'));
207+
208+
$element.val('One');
209+
selection.update([{
210+
id: 'One',
211+
text: 'One'
212+
}]);
213+
214+
selection.on('clear', function (ev) {
215+
assert.ok(
216+
'data' in ev && ev.data,
217+
'The event should have been triggered with the data property'
218+
);
219+
220+
assert.ok(
221+
$.isArray(ev.data),
222+
'The data should be an array'
223+
);
224+
225+
assert.equal(
226+
ev.data.length,
227+
1,
228+
'The data should contain one item for each value'
229+
);
230+
231+
assert.equal(
232+
ev.data[0].id,
233+
'One',
234+
'The data should contain unselected objects'
235+
);
236+
237+
assert.equal(
238+
$element.val(),
239+
'placeholder',
240+
'The previous value should be unselected'
241+
);
242+
});
243+
244+
var $remove = $selection.find('.select2-selection__clear');
245+
$remove.trigger('mousedown');
246+
});
247+
248+
test('preventing the clear event cancels the clearing', function (assert) {
249+
var $element = $('#qunit-fixture .single-with-placeholder');
250+
251+
var selection = new AllowClearPlaceholder(
252+
$element,
253+
allowClearOptions
254+
);
255+
var container = new MockContainer();
256+
257+
var $selection = selection.render();
258+
259+
selection.bind(container, $('<div></div>'));
260+
261+
$element.val('One');
262+
selection.update([{
263+
id: 'One',
264+
text: 'One'
265+
}]);
266+
267+
selection.on('clear', function (ev) {
268+
ev.prevented = true;
269+
});
270+
271+
var $remove = $selection.find('.select2-selection__clear');
272+
$remove.trigger('mousedown');
273+
274+
assert.equal(
275+
$element.val(),
276+
'One',
277+
'The placeholder should not have been set'
278+
);
279+
});
280+
193281
test('clear does not work when disabled', function (assert) {
194282
var $element = $('#qunit-fixture .single-with-placeholder');
195283

0 commit comments

Comments
 (0)