Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shouldFocusInput option [Fix #1541] #2140

Merged
merged 1 commit into from Mar 7, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 24 additions & 9 deletions select2.js
Expand Up @@ -1340,7 +1340,7 @@ the specific language governing permissions and limitations under the Apache Lic
if (self.opts.selectOnBlur) {
self.selectHighlighted({noFocus: true});
}
self.close({focus:true});
self.close();
e.preventDefault();
e.stopPropagation();
}
Expand Down Expand Up @@ -1927,14 +1927,13 @@ the specific language governing permissions and limitations under the Apache Lic
},

// single
close: function (params) {
close: function () {
if (!this.opened()) return;
this.parent.close.apply(this, arguments);

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

if (params.focus) {
if (this.opts.shouldFocusInput(this)) {
this.focusser.focus();
}
},
Expand All @@ -1945,7 +1944,9 @@ the specific language governing permissions and limitations under the Apache Lic
this.close();
} else {
this.focusser.prop("disabled", false);
this.focusser.focus();
if (this.opts.shouldFocusInput(this)) {
this.focusser.focus();
}
}
},

Expand All @@ -1958,7 +1959,10 @@ the specific language governing permissions and limitations under the Apache Lic
cancel: function () {
this.parent.cancel.apply(this, arguments);
this.focusser.prop("disabled", false);
this.focusser.focus();

if (this.opts.shouldFocusInput(this)) {
this.focusser.focus();
}
},

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

if (!options || !options.noFocus)
if ((!options || !options.noFocus) && this.opts.shouldFocusInput(this)) {
this.focusser.focus();
}

if (!equal(old, this.id(data))) { this.triggerChange({added:data,removed:oldData}); }
if (!equal(old, this.id(data))) {
this.triggerChange({ added: data, removed: oldData });
}
},

// single
Expand Down Expand Up @@ -3345,7 +3352,15 @@ the specific language governing permissions and limitations under the Apache Lic
nextSearchTerm: function(selectedObject, currentSearchTerm) { return undefined; },
hideSelectionFromResult: function(selectedObject) { return undefined; },
searchInputPlaceholder: '',
createSearchChoicePosition: 'top'
createSearchChoicePosition: 'top',
shouldFocusInput: function (instance) {
// Never focus the input if search is disabled
if (instance.opts.minimumResultsForSearch < 0) {
return false;
}

return true;
}
};

$.fn.select2.ajaxDefaults = {
Expand Down