Skip to content

Commit 525f530

Browse files
fabienwnklrrisadams
authored andcommitted
fix: focus called twice / dropdown width
1 parent 20beb5f commit 525f530

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/plugins/auto_position/plugin.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ Selectize.define("auto_position", function () {
2121
controlPosBottom - dropdownHeight - wrapperHeight >= 0 ?
2222
POSITION.top :
2323
POSITION.bottom;
24-
const w = this.$wrapper[0].style.width !== 'fit-content' ? '100%' : 'max-content';
24+
const w = this.$wrapper[0].style.width !== 'fit-content' ? this.settings.dropdownParent === 'body' ? 'max-content' : '100%' : 'max-content';
2525
const styles = {
2626
width: w,
27+
minWidth : $control.outerWidth(true),
2728
left: offset.left
2829
};
2930

@@ -43,13 +44,11 @@ Selectize.define("auto_position", function () {
4344
this.$control.removeClass('selectize-position-top');
4445
}
4546

46-
this.$dropdown.css(styles);
47-
48-
if (w === 'max-content' && $control[0].getBoundingClientRect().width >= this.$dropdown[0].getBoundingClientRect().width) {
49-
this.$dropdown.css({
50-
width : '100%'
51-
});
47+
if (this.settings.dropdownParent !== 'body' && w === 'max-content' && $control.outerWidth(true) >= this.$dropdown.outerWidth(true)) {
48+
w = '100%';
5249
}
50+
51+
this.$dropdown.css(styles);
5352
};
5453
}());
5554
});

src/selectize.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -426,15 +426,17 @@ $.extend(Selectize.prototype, {
426426
if (!self.isFocused) {
427427
// give control focus
428428
if (!defaultPrevented) {
429-
window.setTimeout(function() {
430-
self.focus();
431-
}, 0);
429+
window.setTimeout(function () {
430+
if (!self.isOpen) {
431+
self.focus();
432+
}
433+
}, 0);
432434
}
433435
}
434436
// retain focus by preventing native handling. if the
435437
// event target is the input it should not be modified.
436438
// otherwise, text selection within the input won't work.
437-
if (e.target !== self.$control_input[0] || self.$control_input.val() === '') {
439+
if ($target !== self.$control_input[0] || self.$control_input.val() === '') {
438440
if (self.settings.mode === 'single') {
439441
// toggle dropdown
440442
self.isOpen ? self.close() : self.open();
@@ -443,15 +445,15 @@ $.extend(Selectize.prototype, {
443445
self.setActiveItem(null);
444446
}
445447
if (!self.settings.openOnFocus) {
446-
if (self.isOpen && e.target === self.lastOpenTarget) {
448+
if (self.isOpen && $target === self.lastOpenTarget) {
447449
self.close();
448450
self.lastOpenTarget = false;
449451
} else if (!self.isOpen) {
450452
self.refreshOptions();
451453
self.open();
452-
self.lastOpenTarget = e.target;
454+
self.lastOpenTarget = $target;
453455
} else {
454-
self.lastOpenTarget = e.target;
456+
self.lastOpenTarget = $target;
455457
}
456458
}
457459
}
@@ -1286,7 +1288,7 @@ $.extend(Selectize.prototype, {
12861288
$active = $create;
12871289
}
12881290
self.setActiveOption($active);
1289-
if (triggerDropdown && !self.isOpen) { self.open(); }
1291+
if (triggerDropdown && !self.isOpen && !self.isFocused) { self.open(); }
12901292
} else {
12911293
self.setActiveOption(null);
12921294
if (triggerDropdown && self.isOpen) { self.close(); }
@@ -2013,22 +2015,22 @@ $.extend(Selectize.prototype, {
20132015
var $control = this.$control;
20142016
var offset = this.settings.dropdownParent === 'body' ? $control.offset() : $control.position();
20152017
offset.top += $control.outerHeight(true);
2016-
var w = this.$wrapper[0].style.width !== 'fit-content' ? '100%' : 'max-content';
2018+
var w = this.$wrapper[0].style.width !== 'fit-content' ? this.settings.dropdownParent === 'body' ? 'max-content' : '100%' : 'max-content';
20172019
if (this.settings.minWidth && this.settings.minWidth > w)
20182020
{
20192021
w = this.settings.minWidth;
20202022
}
2021-
this.$dropdown.css({
2023+
2024+
if (this.settings.dropdownParent !== 'body' && w === 'max-content' && $control.outerWidth(true) >= this.$dropdown.outerWidth(true)) {
2025+
w = '100%';
2026+
}
2027+
2028+
this.$dropdown.css({
20222029
width : w,
2030+
minWidth : $control.outerWidth(true),
20232031
top : offset.top,
20242032
left : offset.left
20252033
});
2026-
2027-
if (w === 'max-content' && $control[0].getBoundingClientRect().width >= this.$dropdown[0].getBoundingClientRect().width) {
2028-
this.$dropdown.css({
2029-
width : '100%'
2030-
});
2031-
}
20322034
},
20332035

20342036
setupDropdownHeight: function () {

0 commit comments

Comments
 (0)