Skip to content

Commit

Permalink
allow focusing of select element, then redirect focus to button. allo…
Browse files Browse the repository at this point in the history
…ws tabbing to selectpickers on Safari (Safari doesn't supporting tabbing to buttons by default) (snapappointments#478)
  • Loading branch information
caseyjhol committed Apr 29, 2020
1 parent ff79469 commit 0866192
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions js/bootstrap-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@

drop =
'<div class="dropdown bootstrap-select' + showTick + inputGroup + '">' +
'<button type="button" class="' + this.options.styleBase + ' dropdown-toggle" ' + (this.options.display === 'static' ? 'data-display="static"' : '') + 'data-toggle="dropdown"' + autofocus + ' role="combobox" aria-owns="' + this.selectId + '" aria-haspopup="listbox" aria-expanded="false">' +
'<button type="button" tabindex="-1" class="' + this.options.styleBase + ' dropdown-toggle" ' + (this.options.display === 'static' ? 'data-display="static"' : '') + 'data-toggle="dropdown"' + autofocus + ' role="combobox" aria-owns="' + this.selectId + '" aria-haspopup="listbox" aria-expanded="false">' +
'<div class="filter-option">' +
'<div class="filter-option-inner">' +
'<div class="filter-option-inner-inner"></div>' +
Expand Down Expand Up @@ -1681,8 +1681,6 @@

button.classList.toggle('bs-placeholder', that.multiple ? !selectedCount : !getSelectValues(element, selectedOptions));

this.tabIndex();

if (this.options.selectedTextFormat === 'static') {
titleFragment = generateOption.text.call(this, { text: this.options.title }, true);
} else {
Expand Down Expand Up @@ -2329,27 +2327,13 @@
checkDisabled: function () {
if (this.isDisabled()) {
this.$newElement[0].classList.add(classNames.DISABLED);
this.$button.addClass(classNames.DISABLED).attr('tabindex', -1).attr('aria-disabled', true);
this.$button.addClass(classNames.DISABLED).attr('aria-disabled', true);
} else {
if (this.$button[0].classList.contains(classNames.DISABLED)) {
this.$newElement[0].classList.remove(classNames.DISABLED);
this.$button.removeClass(classNames.DISABLED).attr('aria-disabled', false);
}

if (this.$button.attr('tabindex') == -1 && !this.$element.data('tabindex')) {
this.$button.removeAttr('tabindex');
}
}
},

tabIndex: function () {
if (this.$element.data('tabindex') !== this.$element.attr('tabindex') &&
(this.$element.attr('tabindex') !== -98 && this.$element.attr('tabindex') !== '-98')) {
this.$element.data('tabindex', this.$element.attr('tabindex'));
this.$button.attr('tabindex', this.$element.data('tabindex'));
}

this.$element.attr('tabindex', -98);
},

clickListener: function () {
Expand Down Expand Up @@ -2587,6 +2571,28 @@
}
});

this.$button
.on('focus' + EVENT_KEY, function (e) {
var tabindex = that.$element[0].getAttribute('tabindex');

// only change when button is actually focused
if (tabindex !== undefined && e.originalEvent && e.originalEvent.isTrusted) {
// apply select element's tabindex to ensure correct order is followed when tabbing to the next element
this.setAttribute('tabindex', tabindex);
// set element's tabindex to -1 to allow for reverse tabbing
that.$element[0].setAttribute('tabindex', -1);
that.selectpicker.view.tabindex = tabindex;
}
})
.on('blur' + EVENT_KEY, function (e) {
// revert everything to original tabindex
if (that.selectpicker.view.tabindex !== undefined && e.originalEvent && e.originalEvent.isTrusted) {
that.$element[0].setAttribute('tabindex', that.selectpicker.view.tabindex);
this.setAttribute('tabindex', -1);
that.selectpicker.view.tabindex = undefined;
}
});

this.$element
.on('change' + EVENT_KEY, function () {
that.render();
Expand Down

0 comments on commit 0866192

Please sign in to comment.