Skip to content

Commit f382f3d

Browse files
committed
Merge pull request select2#2755 from kupishkis/2753
After clearSearch nextSearchTerm is not cleared
2 parents 9913cb7 + c1f338c commit f382f3d

1 file changed

Lines changed: 29 additions & 24 deletions

File tree

select2.js

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ the specific language governing permissions and limitations under the Apache Lic
814814
// focusin can cause focus wars between modals and select2 since the dropdown is outside the modal.
815815
this.dropdown.on("click mouseup mousedown touchstart touchend focusin", function (e) { e.stopPropagation(); });
816816

817-
this.nextSearchTerm = undefined;
817+
this.lastSearchTerm = undefined;
818818

819819
if ($.isFunction(this.opts.initSelection)) {
820820
// initialize selection based on the current value of the source element
@@ -1500,6 +1500,27 @@ the specific language governing permissions and limitations under the Apache Lic
15001500

15011501
},
15021502

1503+
/**
1504+
* @return {Boolean} Whether or not search value was changed.
1505+
* @private
1506+
*/
1507+
prefillNextSearchTerm: function () {
1508+
// initializes search's value with nextSearchTerm (if defined by user)
1509+
// ignore nextSearchTerm if the dropdown is opened by the user pressing a letter
1510+
if(this.search.val() !== "") {
1511+
return false;
1512+
}
1513+
1514+
var nextSearchTerm = this.opts.nextSearchTerm(this.data(), this.lastSearchTerm);
1515+
if(nextSearchTerm !== undefined){
1516+
this.search.val(nextSearchTerm);
1517+
this.search.select();
1518+
return true;
1519+
}
1520+
1521+
return false;
1522+
},
1523+
15031524
//abstract
15041525
getMaximumSelectionSize: function() {
15051526
return evaluate(this.opts.maximumSelectionSize, this.opts.element);
@@ -2016,14 +2037,7 @@ the specific language governing permissions and limitations under the Apache Lic
20162037
}
20172038
}
20182039

2019-
// initializes search's value with nextSearchTerm (if defined by user)
2020-
// ignore nextSearchTerm if the dropdown is opened by the user pressing a letter
2021-
if(this.search.val() === "") {
2022-
if(this.nextSearchTerm != undefined){
2023-
this.search.val(this.nextSearchTerm);
2024-
this.search.select();
2025-
}
2026-
}
2040+
this.prefillNextSearchTerm();
20272041

20282042
this.focusser.prop("disabled", true).val("");
20292043
this.updateResults(true);
@@ -2318,7 +2332,7 @@ the specific language governing permissions and limitations under the Apache Lic
23182332
self.updateSelection(selected);
23192333
self.close();
23202334
self.setPlaceholder();
2321-
self.nextSearchTerm = self.opts.nextSearchTerm(selected, self.search.val());
2335+
self.lastSearchTerm = self.search.val();
23222336
}
23232337
});
23242338
}
@@ -2455,7 +2469,7 @@ the specific language governing permissions and limitations under the Apache Lic
24552469

24562470
this.opts.element.trigger({ type: "select2-selected", val: this.id(data), choice: data });
24572471

2458-
this.nextSearchTerm = this.opts.nextSearchTerm(data, this.search.val());
2472+
this.lastSearchTerm = this.search.val();
24592473
this.close();
24602474

24612475
if ((!options || !options.noFocus) && this.opts.shouldFocusInput(this)) {
@@ -2923,16 +2937,9 @@ the specific language governing permissions and limitations under the Apache Lic
29232937

29242938
this.focusSearch();
29252939

2926-
// initializes search's value with nextSearchTerm (if defined by user)
2927-
// ignore nextSearchTerm if the dropdown is opened by the user pressing a letter
2928-
if(this.search.val() === "") {
2929-
if(this.nextSearchTerm != undefined){
2930-
this.search.val(this.nextSearchTerm);
2931-
this.search.select();
2932-
}
2933-
}
2934-
2940+
this.prefillNextSearchTerm();
29352941
this.updateResults(true);
2942+
29362943
if (this.opts.shouldFocusInput(this)) {
29372944
this.search.focus();
29382945
}
@@ -2999,7 +3006,7 @@ the specific language governing permissions and limitations under the Apache Lic
29993006
this.opts.element.trigger({ type: "selected", val: this.id(data), choice: data });
30003007

30013008
// keep track of the search's value before it gets cleared
3002-
this.nextSearchTerm = this.opts.nextSearchTerm(data, this.search.val());
3009+
this.lastSearchTerm = this.search.val();
30033010

30043011
this.clearSearch();
30053012
this.updateResults();
@@ -3019,10 +3026,8 @@ the specific language governing permissions and limitations under the Apache Lic
30193026
this.updateResults(true);
30203027
} else {
30213028
// initializes search's value with nextSearchTerm and update search result
3022-
if(this.nextSearchTerm != undefined){
3023-
this.search.val(this.nextSearchTerm);
3029+
if (this.prefillNextSearchTerm()) {
30243030
this.updateResults();
3025-
this.search.select();
30263031
}
30273032
}
30283033
this.positionDropdown();

0 commit comments

Comments
 (0)