Skip to content

Commit 4c796a2

Browse files
EffakTrisadams
authored andcommitted
Added a throttle on mousedown preveting a reopen after click
1 parent 273f887 commit 4c796a2

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

docs/usage.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ $(function() {
141141
<td valign="top"><code>boolean</code></td>
142142
<td valign="top"><code>false</code></td>
143143
</tr>
144+
<tr>
145+
<td valign="top"><code>closeDropdownThreshold</code></td>
146+
<td valign="top">The number of milliseconds to throttle the opening of the dropdown after it is closed by clicking on the control. Setting this to 0 will reopen the dropdown after clicking on the control when the dropdown is open. This does not affect multi-selects.</td>
147+
<td valign="top"><code>int</code></td>
148+
<td valign="top"><code>250</code></td>
149+
</tr>
144150
<tr>
145151
<td valign="top"><code>allowEmptyOption</code></td>
146152
<td valign="top">If true, Selectize will treat any options with a "" value like normal. This defaults to false to accomodate the common &lt;select&gt; practice of having the first empty option to act as a placeholder.</td>

src/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Selectize.defaults = {
2424
showEmptyOptionInDropdown: false,
2525
emptyOptionLabel: '--',
2626
closeAfterSelect: false,
27+
closeDropdownThreshold: 250, // number of ms to prevent reopening of dropdown after mousedown
2728

2829
scrollDuration: 60,
2930
deselectBehavior: 'previous', //top, previous

src/selectize.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var Selectize = function($input, settings) {
4141
caretPos : 0,
4242
loading : 0,
4343
loadedSearches : {},
44+
isDropdownClosing: false,
4445

4546
$activeOption : null,
4647
$activeItems : [],
@@ -368,6 +369,12 @@ $.extend(Selectize.prototype, {
368369
onClick: function(e) {
369370
var self = this;
370371

372+
// if the dropdown is closing due to a mousedown, we don't want to
373+
// refocus the element.
374+
if (self.isDropdownClosing) {
375+
return;
376+
}
377+
371378
// necessary for mobile webkit devices (manual focus triggering
372379
// is ignored unless invoked within a click event)
373380
// also necessary to reopen a dropdown that has been closed by
@@ -398,6 +405,15 @@ $.extend(Selectize.prototype, {
398405
if (self.settings.mode === 'single') {
399406
// toggle dropdown
400407
self.isOpen ? self.close() : self.open();
408+
409+
// when closing the dropdown, we set a isDropdownClosing
410+
// varible temporaily to prevent the dropdown from reopening
411+
// from the onClick event
412+
self.isDropdownClosing = true;
413+
setTimeout(function() {
414+
self.isDropdownClosing = false;
415+
}, self.settings.closeDropdownThreshold);
416+
401417
} else if (!defaultPrevented) {
402418
self.setActiveItem(null);
403419
}

0 commit comments

Comments
 (0)