Skip to content

Commit 51e5d43

Browse files
committed
Merge pull request select2#2160 from s7anley/bugfix/2073-select2-removing
Multiselect items are not removed when event is prevented
2 parents e6fbfcd + f3c6d6d commit 51e5d43

1 file changed

Lines changed: 21 additions & 19 deletions

File tree

select2.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,13 +2610,15 @@ the specific language governing permissions and limitations under the Apache Lic
26102610
selectedChoice = next.length ? next : null;
26112611
}
26122612
else if (e.which === KEY.BACKSPACE) {
2613-
this.unselect(selected.first());
2614-
this.search.width(10);
2615-
selectedChoice = prev.length ? prev : next;
2613+
if (this.unselect(selected.first())) {
2614+
this.search.width(10);
2615+
selectedChoice = prev.length ? prev : next;
2616+
}
26162617
} else if (e.which == KEY.DELETE) {
2617-
this.unselect(selected.first());
2618-
this.search.width(10);
2619-
selectedChoice = next.length ? next : null;
2618+
if (this.unselect(selected.first())) {
2619+
this.search.width(10);
2620+
selectedChoice = next.length ? next : null;
2621+
}
26202622
} else if (e.which == KEY.ENTER) {
26212623
selectedChoice = null;
26222624
}
@@ -2948,13 +2950,11 @@ the specific language governing permissions and limitations under the Apache Lic
29482950
.on("click dblclick", this.bind(function (e) {
29492951
if (!this.isInterfaceEnabled()) return;
29502952

2951-
$(e.target).closest(".select2-search-choice").fadeOut('fast', this.bind(function(){
2952-
this.unselect($(e.target));
2953-
this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
2954-
this.close();
2955-
this.focusSearch();
2956-
})).dequeue();
2953+
this.unselect($(e.target));
2954+
this.selection.find(".select2-search-choice-focus").removeClass("select2-search-choice-focus");
29572955
killEvent(e);
2956+
this.close();
2957+
this.focusSearch();
29582958
})).on("focus", this.bind(function () {
29592959
if (!this.isInterfaceEnabled()) return;
29602960
this.container.addClass("select2-container-active");
@@ -2988,25 +2988,27 @@ the specific language governing permissions and limitations under the Apache Lic
29882988
return;
29892989
}
29902990

2991-
while((index = indexOf(this.id(data), val)) >= 0) {
2992-
val.splice(index, 1);
2993-
this.setVal(val);
2994-
if (this.select) this.postprocessResults();
2995-
}
2996-
29972991
var evt = $.Event("select2-removing");
29982992
evt.val = this.id(data);
29992993
evt.choice = data;
30002994
this.opts.element.trigger(evt);
30012995

30022996
if (evt.isDefaultPrevented()) {
3003-
return;
2997+
return false;
2998+
}
2999+
3000+
while((index = indexOf(this.id(data), val)) >= 0) {
3001+
val.splice(index, 1);
3002+
this.setVal(val);
3003+
if (this.select) this.postprocessResults();
30043004
}
30053005

30063006
selected.remove();
30073007

30083008
this.opts.element.trigger({ type: "select2-removed", val: this.id(data), choice: data });
30093009
this.triggerChange({ removed: data });
3010+
3011+
return true;
30103012
},
30113013

30123014
// multi

0 commit comments

Comments
 (0)