Skip to content

Commit 213d281

Browse files
committed
fix first char being lost. fixes select2#196
1 parent 207139b commit 213d281

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

select2.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,22 +1720,31 @@ the specific language governing permissions and limitations under the Apache Lic
17201720

17211721
// single
17221722
opening: function () {
1723-
var el, range;
1723+
var el, range, len;
1724+
1725+
if (this.opts.minimumResultsForSearch >= 0) {
1726+
this.showSearch(true);
1727+
}
1728+
17241729
this.parent.opening.apply(this, arguments);
1730+
17251731
if (this.showSearchInput !== false) {
17261732
// IE appends focusser.val() at the end of field :/ so we manually insert it at the beginning using a range
17271733
// all other browsers handle this just fine
17281734

17291735
this.search.val(this.focusser.val());
17301736
}
17311737
this.search.focus();
1732-
// in IE we have to move the cursor to the end after focussing, otherwise it will be at the beginning and
1738+
// move the cursor to the end after focussing, otherwise it will be at the beginning and
17331739
// new text will appear *before* focusser.val()
17341740
el = this.search.get(0);
17351741
if (el.createTextRange) {
17361742
range = el.createTextRange();
17371743
range.collapse(false);
17381744
range.select();
1745+
} else if (el.setSelectionRange) {
1746+
len = this.search.val().length;
1747+
el.setSelectionRange(len, len);
17391748
}
17401749

17411750
this.focusser.prop("disabled", true).val("");
@@ -1780,7 +1789,11 @@ the specific language governing permissions and limitations under the Apache Lic
17801789
container = this.container,
17811790
dropdown = this.dropdown;
17821791

1783-
this.showSearch(false);
1792+
if (this.opts.minimumResultsForSearch < 0) {
1793+
this.showSearch(false);
1794+
} else {
1795+
this.showSearch(true);
1796+
}
17841797

17851798
this.selection = selection = container.find(".select2-choice");
17861799

@@ -1864,9 +1877,11 @@ the specific language governing permissions and limitations under the Apache Lic
18641877

18651878
installKeyUpChangeEvent(this.focusser);
18661879
this.focusser.on("keyup-change input", this.bind(function(e) {
1867-
e.stopPropagation();
1868-
if (this.opened()) return;
1869-
this.open();
1880+
if (this.opts.minimumResultsForSearch >= 0) {
1881+
e.stopPropagation();
1882+
if (this.opened()) return;
1883+
this.open();
1884+
}
18701885
}));
18711886

18721887
selection.on("mousedown", "abbr", this.bind(function (e) {
@@ -2043,19 +2058,20 @@ the specific language governing permissions and limitations under the Apache Lic
20432058
this.highlight(selected);
20442059
}
20452060

2046-
// show the search box if this is the first we got the results and there are enough of them for search
2061+
// hide the search box if this is the first we got the results and there are enough of them for search
20472062

2048-
if (initial === true && this.showSearchInput === false) {
2049-
var min=this.opts.minimumResultsForSearch;
2050-
if (min>=0) {
2051-
this.showSearch(countResults(data.results)>=min);
2063+
if (initial === true) {
2064+
var min = this.opts.minimumResultsForSearch;
2065+
if (min >= 0) {
2066+
this.showSearch(countResults(data.results) >= min);
20522067
}
20532068
}
2054-
20552069
},
20562070

20572071
// single
20582072
showSearch: function(showSearchInput) {
2073+
if (this.showSearchInput === showSearchInput) return;
2074+
20592075
this.showSearchInput = showSearchInput;
20602076

20612077
this.dropdown.find(".select2-search").toggleClass("select2-search-hidden", !showSearchInput);

0 commit comments

Comments
 (0)