Skip to content

Commit f9cd88e

Browse files
committed
Fix radio and checkbox keyboard handling
Fix for problem that emerged from twbs#16226 (`.active` class not actually being applied) and expansion of the script to also correctly handle keyboard interaction with checkboxes in `data-toggle="button"` groups
1 parent 45c798a commit f9cd88e

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

js/button.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,18 @@
5757
var $input = this.$element.find('input')
5858
if ($input.prop('type') == 'radio') {
5959
if ($input.prop('checked')) changed = false
60-
if (!$input.prop('checked') || !this.$element.hasClass('active')) $parent.find('.active').removeClass('active')
60+
$parent.find('.active').removeClass('active')
61+
this.$element.addClass('active')
62+
} else if ($input.prop('type') == 'checkbox') {
63+
if (($input.prop('checked')) !== this.$element.hasClass('active')) changed = false
64+
this.$element.toggleClass('active')
6165
}
62-
if (changed) $input.prop('checked', !this.$element.hasClass('active')).trigger('change')
66+
$input.prop('checked', this.$element.hasClass('active'))
67+
if (changed) $input.trigger('change')
6368
} else {
6469
this.$element.attr('aria-pressed', !this.$element.hasClass('active'))
70+
this.$element.toggleClass('active')
6571
}
66-
67-
if (changed) this.$element.toggleClass('active')
6872
}
6973

7074

@@ -107,7 +111,7 @@
107111
var $btn = $(e.target)
108112
if (!$btn.hasClass('btn')) $btn = $btn.closest('.btn')
109113
Plugin.call($btn, 'toggle')
110-
if (!$(e.target).is('input[type="radio"]')) e.preventDefault()
114+
if (!($(e.target).is('input[type="radio"]') || $(e.target).is('input[type="checkbox"]'))) e.preventDefault()
111115
})
112116
.on('focus.bs.button.data-api blur.bs.button.data-api', '[data-toggle^="button"]', function (e) {
113117
$(e.target).closest('.btn').toggleClass('focus', /^focus(in)?$/.test(e.type))

0 commit comments

Comments
 (0)