From e9f039d1fe3ecbd52c46c127a184b4d0d6f1c17f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laussel=20Lo=C3=AFc?= Date: Wed, 10 Jun 2020 14:45:31 +0200 Subject: [PATCH] avoid preventing input event onclick (#30992) * instead of stopping event if onclick is triggered on input, call toggle method only if its not on checkbox inside a label * add unit test * add a dedicated test to ensure click event is forward to label Co-authored-by: XhmikosR --- js/src/button.js | 5 ++--- js/tests/unit/button.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/js/src/button.js b/js/src/button.js index 0da2545119..08654d558e 100644 --- a/js/src/button.js +++ b/js/src/button.js @@ -153,10 +153,9 @@ $(document) return } - if (initialButton.tagName === 'LABEL' && inputBtn && inputBtn.type === 'checkbox') { - event.preventDefault() // work around event sent to label and input + if (initialButton.tagName !== 'LABEL' || inputBtn && inputBtn.type !== 'checkbox') { + Button._jQueryInterface.call($(button), 'toggle') } - Button._jQueryInterface.call($(button), 'toggle') } }) .on(EVENT_FOCUS_BLUR_DATA_API, SELECTOR_DATA_TOGGLE_CARROT, (event) => { diff --git a/js/tests/unit/button.js b/js/tests/unit/button.js index e92d229e6d..2adffedd9f 100644 --- a/js/tests/unit/button.js +++ b/js/tests/unit/button.js @@ -222,6 +222,46 @@ $(function () { assert.ok(!$btn2.find('input')[0].checked, 'btn2 is not checked') }) + QUnit.test('should fire click event on input', function (assert) { + assert.expect(1) + var done = assert.async() + var groupHTML = '
' + + '' + + '
' + var $group = $(groupHTML).appendTo('#qunit-fixture') + + var $btn = $group.children().eq(0) + $group.find('input').on('click', function (e) { + e.preventDefault() + assert.ok(true, 'click event fired') + done() + }) + + $btn[0].click() + }) + + QUnit.test('should fire click event on label', function (assert) { + assert.expect(1) + var done = assert.async() + var groupHTML = '
' + + '' + + '
' + var $group = $(groupHTML).appendTo('#qunit-fixture') + + var $btn = $group.children().eq(0) + $group.find('label').on('click', function (e) { + e.preventDefault() + assert.ok(true, 'click event fired') + done() + }) + + $btn[0].click() + }) + QUnit.test('should not add aria-pressed on labels for radio/checkbox inputs in a data-toggle="buttons" group', function (assert) { assert.expect(2) var groupHTML = '
' +