Skip to content

Commit 43a598b

Browse files
committed
Merge pull request select2#2216 from lboynton/fix-search-focus-on-mobile
Fix search focus on mobile
2 parents 937aaa1 + 2d6b74c commit 43a598b

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

select2.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,17 +1915,19 @@ the specific language governing permissions and limitations under the Apache Lic
19151915

19161916
this.search.val(this.focusser.val());
19171917
}
1918-
this.search.focus();
1919-
// move the cursor to the end after focussing, otherwise it will be at the beginning and
1920-
// new text will appear *before* focusser.val()
1921-
el = this.search.get(0);
1922-
if (el.createTextRange) {
1923-
range = el.createTextRange();
1924-
range.collapse(false);
1925-
range.select();
1926-
} else if (el.setSelectionRange) {
1927-
len = this.search.val().length;
1928-
el.setSelectionRange(len, len);
1918+
if (this.opts.shouldFocusInput(this)) {
1919+
this.search.focus();
1920+
// move the cursor to the end after focussing, otherwise it will be at the beginning and
1921+
// new text will appear *before* focusser.val()
1922+
el = this.search.get(0);
1923+
if (el.createTextRange) {
1924+
range = el.createTextRange();
1925+
range.collapse(false);
1926+
range.select();
1927+
} else if (el.setSelectionRange) {
1928+
len = this.search.val().length;
1929+
el.setSelectionRange(len, len);
1930+
}
19291931
}
19301932

19311933
// initializes search's value with nextSearchTerm (if defined by user)
@@ -2142,7 +2144,11 @@ the specific language governing permissions and limitations under the Apache Lic
21422144
killEvent(e);
21432145
}));
21442146

2145-
dropdown.on("mousedown touchstart", this.bind(function() { this.search.focus(); }));
2147+
dropdown.on("mousedown touchstart", this.bind(function() {
2148+
if (this.opts.shouldFocusInput(this)) {
2149+
this.search.focus();
2150+
}
2151+
}));
21462152

21472153
selection.on("focus", this.bind(function(e) {
21482154
killEvent(e);
@@ -2821,7 +2827,9 @@ the specific language governing permissions and limitations under the Apache Lic
28212827
}
28222828

28232829
this.updateResults(true);
2824-
this.search.focus();
2830+
if (this.opts.shouldFocusInput(this)) {
2831+
this.search.focus();
2832+
}
28252833
this.opts.element.trigger($.Event("select2-open"));
28262834
},
28272835

0 commit comments

Comments
 (0)