Skip to content

Commit

Permalink
fix: focus called twice / dropdown width
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienwnklr authored and risadams committed Dec 23, 2022
1 parent 20beb5f commit 525f530
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
13 changes: 6 additions & 7 deletions src/plugins/auto_position/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ Selectize.define("auto_position", function () {
controlPosBottom - dropdownHeight - wrapperHeight >= 0 ?
POSITION.top :
POSITION.bottom;
const w = this.$wrapper[0].style.width !== 'fit-content' ? '100%' : 'max-content';
const w = this.$wrapper[0].style.width !== 'fit-content' ? this.settings.dropdownParent === 'body' ? 'max-content' : '100%' : 'max-content';
const styles = {
width: w,
minWidth : $control.outerWidth(true),
left: offset.left
};

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

this.$dropdown.css(styles);

if (w === 'max-content' && $control[0].getBoundingClientRect().width >= this.$dropdown[0].getBoundingClientRect().width) {
this.$dropdown.css({
width : '100%'
});
if (this.settings.dropdownParent !== 'body' && w === 'max-content' && $control.outerWidth(true) >= this.$dropdown.outerWidth(true)) {
w = '100%';
}

this.$dropdown.css(styles);
};
}());
});
34 changes: 18 additions & 16 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,15 +426,17 @@ $.extend(Selectize.prototype, {
if (!self.isFocused) {
// give control focus
if (!defaultPrevented) {
window.setTimeout(function() {
self.focus();
}, 0);
window.setTimeout(function () {
if (!self.isOpen) {
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 ($target !== self.$control_input[0] || self.$control_input.val() === '') {
if (self.settings.mode === 'single') {
// toggle dropdown
self.isOpen ? self.close() : self.open();
Expand All @@ -443,15 +445,15 @@ $.extend(Selectize.prototype, {
self.setActiveItem(null);
}
if (!self.settings.openOnFocus) {
if (self.isOpen && e.target === self.lastOpenTarget) {
if (self.isOpen && $target === self.lastOpenTarget) {
self.close();
self.lastOpenTarget = false;
} else if (!self.isOpen) {
self.refreshOptions();
self.open();
self.lastOpenTarget = e.target;
self.lastOpenTarget = $target;
} else {
self.lastOpenTarget = e.target;
self.lastOpenTarget = $target;
}
}
}
Expand Down Expand Up @@ -1286,7 +1288,7 @@ $.extend(Selectize.prototype, {
$active = $create;
}
self.setActiveOption($active);
if (triggerDropdown && !self.isOpen) { self.open(); }
if (triggerDropdown && !self.isOpen && !self.isFocused) { self.open(); }
} else {
self.setActiveOption(null);
if (triggerDropdown && self.isOpen) { self.close(); }
Expand Down Expand Up @@ -2013,22 +2015,22 @@ $.extend(Selectize.prototype, {
var $control = this.$control;
var offset = this.settings.dropdownParent === 'body' ? $control.offset() : $control.position();
offset.top += $control.outerHeight(true);
var w = this.$wrapper[0].style.width !== 'fit-content' ? '100%' : 'max-content';
var w = this.$wrapper[0].style.width !== 'fit-content' ? this.settings.dropdownParent === 'body' ? 'max-content' : '100%' : 'max-content';
if (this.settings.minWidth && this.settings.minWidth > w)
{
w = this.settings.minWidth;
}
this.$dropdown.css({

if (this.settings.dropdownParent !== 'body' && w === 'max-content' && $control.outerWidth(true) >= this.$dropdown.outerWidth(true)) {
w = '100%';
}

this.$dropdown.css({
width : w,
minWidth : $control.outerWidth(true),
top : offset.top,
left : offset.left
});

if (w === 'max-content' && $control[0].getBoundingClientRect().width >= this.$dropdown[0].getBoundingClientRect().width) {
this.$dropdown.css({
width : '100%'
});
}
},

setupDropdownHeight: function () {
Expand Down

0 comments on commit 525f530

Please sign in to comment.