Skip to content

Commit 10bda78

Browse files
gronostajoalexweissman
authored andcommitted
Fix value not being cleared when unselect event is emitted
Fixes select2#5049.
1 parent 43ffaea commit 10bda78

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

src/js/select2/selection/allowClear.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ define([
4545

4646
var data = $clear.data('data');
4747

48+
var previousVal = this.$element.val();
49+
this.$element.val(this.placeholder.id);
50+
4851
for (var d = 0; d < data.length; d++) {
4952
var unselectData = {
5053
data: data[d]
@@ -56,11 +59,12 @@ define([
5659

5760
// If the event was prevented, don't clear it out.
5861
if (unselectData.prevented) {
62+
this.$element.val(previousVal);
5963
return;
6064
}
6165
}
6266

63-
this.$element.val(this.placeholder.id).trigger('change');
67+
this.$element.trigger('change');
6468

6569
this.trigger('toggle', {});
6670
};

tests/selection/allowClear-tests.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ test('clear is not displayed for single placeholder', function (assert) {
4343

4444
test('clear is not displayed for multiple placeholder', function (assert) {
4545
var selection = new AllowClearPlaceholder(
46-
$('#qunit-fixture .single-with-placeholder'),
46+
$('#qunit-fixture .multiple'),
4747
allowClearOptions
4848
);
4949

@@ -90,7 +90,7 @@ test('clicking clear will set the placeholder value', function (assert) {
9090

9191
var $selection = selection.render();
9292

93-
selection.bind(container, $('<div></div'));
93+
selection.bind(container, $('<div></div>'));
9494

9595
$element.val('One');
9696
selection.update([{
@@ -109,7 +109,7 @@ test('clicking clear will set the placeholder value', function (assert) {
109109
});
110110

111111
test('clicking clear will trigger the unselect event', function (assert) {
112-
assert.expect(3);
112+
assert.expect(4);
113113

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

@@ -121,7 +121,7 @@ test('clicking clear will trigger the unselect event', function (assert) {
121121

122122
var $selection = selection.render();
123123

124-
selection.bind(container, $('<div></div'));
124+
selection.bind(container, $('<div></div>'));
125125

126126
$element.val('One');
127127
selection.update([{
@@ -143,16 +143,20 @@ test('clicking clear will trigger the unselect event', function (assert) {
143143
assert.equal(
144144
ev.data.id,
145145
'One',
146-
'The previous object should be unselected'
146+
'The data should be the unselected object'
147+
);
148+
149+
assert.equal(
150+
$element.val(),
151+
'placeholder',
152+
'The previous value should be unselected'
147153
);
148154
});
149155

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

154-
155-
156160
test('preventing the unselect event cancels the clearing', function (assert) {
157161
var $element = $('#qunit-fixture .single-with-placeholder');
158162

@@ -164,7 +168,7 @@ test('preventing the unselect event cancels the clearing', function (assert) {
164168

165169
var $selection = selection.render();
166170

167-
selection.bind(container, $('<div></div'));
171+
selection.bind(container, $('<div></div>'));
168172

169173
$element.val('One');
170174
selection.update([{
@@ -197,7 +201,7 @@ test('clear does not work when disabled', function (assert) {
197201

198202
var $selection = selection.render();
199203

200-
selection.bind(container, $('<div></div'));
204+
selection.bind(container, $('<div></div>'));
201205

202206
selection.update([{
203207
id: 'One',

0 commit comments

Comments
 (0)