Skip to content

Commit a254782

Browse files
committed
Merge pull request twbs#16404 from patrickhlauke/radio-checkbox-kbd-toggle-fix
Fix radio and checkbox keyboard handling in .btn-group
2 parents 7b9f204 + 0c1daaf commit a254782

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
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))

js/tests/unit/button.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,6 @@ $(function () {
130130
assert.strictEqual($btn.attr('aria-pressed'), 'true', 'btn aria-pressed state is true')
131131
})
132132

133-
QUnit.test('should toggle active when btn children are clicked within btn-group', function (assert) {
134-
assert.expect(2)
135-
var $btngroup = $('<div class="btn-group" data-toggle="buttons"/>')
136-
var $btn = $('<button class="btn">fat</button>')
137-
var $inner = $('<i/>')
138-
$btngroup
139-
.append($btn.append($inner))
140-
.appendTo('#qunit-fixture')
141-
assert.ok(!$btn.hasClass('active'), 'btn does not have active class')
142-
$inner.trigger('click')
143-
assert.ok($btn.hasClass('active'), 'btn has class active')
144-
})
145-
146133
QUnit.test('should check for closest matching toggle', function (assert) {
147134
assert.expect(12)
148135
var groupHTML = '<div class="btn-group" data-toggle="buttons">'

0 commit comments

Comments
 (0)