Skip to content

Commit 2d906ee

Browse files
committed
Merge pull request select2#2140 from ivaynberg/issue_1541
Add `shouldFocusInput` option [Fix select2#1541]
2 parents 51e5d43 + d87e93d commit 2d906ee

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

select2.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ the specific language governing permissions and limitations under the Apache Lic
13401340
if (self.opts.selectOnBlur) {
13411341
self.selectHighlighted({noFocus: true});
13421342
}
1343-
self.close({focus:true});
1343+
self.close();
13441344
e.preventDefault();
13451345
e.stopPropagation();
13461346
}
@@ -1927,14 +1927,13 @@ the specific language governing permissions and limitations under the Apache Lic
19271927
},
19281928

19291929
// single
1930-
close: function (params) {
1930+
close: function () {
19311931
if (!this.opened()) return;
19321932
this.parent.close.apply(this, arguments);
19331933

1934-
params = params || {focus: true};
19351934
this.focusser.prop("disabled", false);
19361935

1937-
if (params.focus) {
1936+
if (this.opts.shouldFocusInput(this)) {
19381937
this.focusser.focus();
19391938
}
19401939
},
@@ -1945,7 +1944,9 @@ the specific language governing permissions and limitations under the Apache Lic
19451944
this.close();
19461945
} else {
19471946
this.focusser.prop("disabled", false);
1948-
this.focusser.focus();
1947+
if (this.opts.shouldFocusInput(this)) {
1948+
this.focusser.focus();
1949+
}
19491950
}
19501951
},
19511952

@@ -1958,7 +1959,10 @@ the specific language governing permissions and limitations under the Apache Lic
19581959
cancel: function () {
19591960
this.parent.cancel.apply(this, arguments);
19601961
this.focusser.prop("disabled", false);
1961-
this.focusser.focus();
1962+
1963+
if (this.opts.shouldFocusInput(this)) {
1964+
this.focusser.focus();
1965+
}
19621966
},
19631967

19641968
// single
@@ -2339,10 +2343,13 @@ the specific language governing permissions and limitations under the Apache Lic
23392343
this.nextSearchTerm = this.opts.nextSearchTerm(data, this.search.val());
23402344
this.close();
23412345

2342-
if (!options || !options.noFocus)
2346+
if ((!options || !options.noFocus) && this.opts.shouldFocusInput(this)) {
23432347
this.focusser.focus();
2348+
}
23442349

2345-
if (!equal(old, this.id(data))) { this.triggerChange({added:data,removed:oldData}); }
2350+
if (!equal(old, this.id(data))) {
2351+
this.triggerChange({ added: data, removed: oldData });
2352+
}
23462353
},
23472354

23482355
// single
@@ -3347,7 +3354,15 @@ the specific language governing permissions and limitations under the Apache Lic
33473354
nextSearchTerm: function(selectedObject, currentSearchTerm) { return undefined; },
33483355
hideSelectionFromResult: function(selectedObject) { return undefined; },
33493356
searchInputPlaceholder: '',
3350-
createSearchChoicePosition: 'top'
3357+
createSearchChoicePosition: 'top',
3358+
shouldFocusInput: function (instance) {
3359+
// Never focus the input if search is disabled
3360+
if (instance.opts.minimumResultsForSearch < 0) {
3361+
return false;
3362+
}
3363+
3364+
return true;
3365+
}
33513366
};
33523367

33533368
$.fn.select2.ajaxDefaults = {

0 commit comments

Comments
 (0)