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

Open drop-down on mouse-click if openOnFocus is false and when clicking on the options. #1899

Merged
merged 1 commit into from
Nov 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var Selectize = function($input, settings) {
currentResults : null,
lastValue : '',
lastValidValue : '',
lastOpenTarget : false,
caretPos : 0,
loading : 0,
loadedSearches : {},
Expand Down Expand Up @@ -192,7 +193,11 @@ $.extend(Selectize.prototype, {
});

$control_input.on({
mousedown : function(e) { e.stopPropagation(); },
mousedown : function(e) {
if (self.$control_input.val() !== '' || self.settings.openOnFocus) {
e.stopPropagation();
}
},
keydown : function() { return self.onKeyDown.apply(self, arguments); },
keypress : function() { return self.onKeyPress.apply(self, arguments); },
input : function() { return self.onInput.apply(self, arguments); },
Expand Down Expand Up @@ -398,36 +403,40 @@ $.extend(Selectize.prototype, {
var defaultPrevented = e.isDefaultPrevented();
var $target = $(e.target);

if (self.isFocused) {
// retain focus by preventing native handling. if the
// event target is the input it should not be modified.
// otherwise, text selection within the input won't work.
if (e.target !== self.$control_input[0]) {
if (self.settings.mode === 'single') {
// toggle dropdown
self.isOpen ? self.close() : self.open();

// when closing the dropdown, we set a isDropdownClosing
// varible temporaily to prevent the dropdown from reopening
// from the onClick event
self.isDropdownClosing = true;
setTimeout(function() {
self.isDropdownClosing = false;
}, self.settings.closeDropdownThreshold);

} else if (!defaultPrevented) {
self.setActiveItem(null);
}
return false;
}
} else {
if (!self.isFocused) {
// give control focus
if (!defaultPrevented) {
window.setTimeout(function() {
self.focus();
}, 0);
}
}
// retain focus by preventing native handling. if the
// event target is the input it should not be modified.
// otherwise, text selection within the input won't work.
if (e.target !== self.$control_input[0] || self.$control_input.val() === '') {
if (self.settings.mode === 'single') {
// toggle dropdown
self.isOpen ? self.close() : self.open();
} else {
if (!defaultPrevented) {
self.setActiveItem(null);
}
if (!self.settings.openOnFocus) {
if (self.isOpen && e.target === self.lastOpenTarget) {
self.close();
self.lastOpenTarget = false;
} else if (!self.isOpen) {
self.refreshOptions();
self.open();
self.lastOpenTarget = e.target;
} else {
self.lastOpenTarget = e.target;
}
}
}
return false;
}
},

/**
Expand Down