Skip to content

Commit d87e93d

Browse files
author
Kevin Brown
committed
Add shouldFocusInput option [Fix select2#1541]
This adds a `shouldFocusInput` option that determines whether or not the focusser should be automatically focused. By default, the focusser will always be focused, unless `minimumResultsForSearch` is less than zero. `close` on single selects took an option `params` argument which at one point was used to tell the focusser to not automatically focus. This was added at one point when tweaking the mask [1] and later reverted to fix a bug that came as a result [2]. The leftovers of this code has been removed in this commit. [1]: ivaynberg@e162a48 [2]: ivaynberg@9bc68f0
1 parent b2b5ebd commit d87e93d

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
@@ -3345,7 +3352,15 @@ the specific language governing permissions and limitations under the Apache Lic
33453352
nextSearchTerm: function(selectedObject, currentSearchTerm) { return undefined; },
33463353
hideSelectionFromResult: function(selectedObject) { return undefined; },
33473354
searchInputPlaceholder: '',
3348-
createSearchChoicePosition: 'top'
3355+
createSearchChoicePosition: 'top',
3356+
shouldFocusInput: function (instance) {
3357+
// Never focus the input if search is disabled
3358+
if (instance.opts.minimumResultsForSearch < 0) {
3359+
return false;
3360+
}
3361+
3362+
return true;
3363+
}
33493364
};
33503365

33513366
$.fn.select2.ajaxDefaults = {

0 commit comments

Comments
 (0)