mirror of
https://github.com/twbs/bootstrap.git
synced 2025-10-03 00:31:55 +02:00
- adds more defensive checks to make sure no unnecessary toggling happens on disabled buttons; this also fixes an up-to-now undiscovered bug where a toggle button with `.disabled` class would still have its `aria-pressed` toggled - adds a set of explicit tests for the above case of disabled buttons and `aria-pressed` - remove a now irrelevant (or at least very nonsensical) test for `<label>` containing both an actionable and a `hidden` `<input>` - expand the test for disabled checkbox to also explicitly test starting conditions (used mainly in my debugging) - ensure that `$btn[0].click()` is used to click checkboxes in tests, rather than the `click()` on the jquery object which is simply a shorthand for `trigger('click')` and does not actually trigger the browser default behavior - remove the `preventDefault()` from the button handling, which was preventing correct keyboard functionality for checkboxes/radio buttons - add extra logic to the button.js code to handle checkboxes correctly and avoid double-triggering as a result of mouse interactions (which saw the checkboxes being toggled twice, thus returning them to their original state) - add logic that prevents the `checked` property from being added incorrectly for any inputs other than radio buttons and checkboxes - added more tests (including the most basic test for a properly triggered fake checkbox button) - work around Firefox bug #1540995 (which this code was hitting after removing the `preventDefault()`, due to Firefox's incorrect toggling of disabled checkboxes when programmatically (but not manually) activated with a `click()` event
184 lines
5.0 KiB
JavaScript
184 lines
5.0 KiB
JavaScript
/**
|
|
* --------------------------------------------------------------------------
|
|
* Bootstrap (v4.3.1): button.js
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
|
* --------------------------------------------------------------------------
|
|
*/
|
|
|
|
import $ from 'jquery'
|
|
|
|
/**
|
|
* ------------------------------------------------------------------------
|
|
* Constants
|
|
* ------------------------------------------------------------------------
|
|
*/
|
|
|
|
const NAME = 'button'
|
|
const VERSION = '4.3.1'
|
|
const DATA_KEY = 'bs.button'
|
|
const EVENT_KEY = `.${DATA_KEY}`
|
|
const DATA_API_KEY = '.data-api'
|
|
const JQUERY_NO_CONFLICT = $.fn[NAME]
|
|
|
|
const ClassName = {
|
|
ACTIVE : 'active',
|
|
BUTTON : 'btn',
|
|
FOCUS : 'focus'
|
|
}
|
|
|
|
const Selector = {
|
|
DATA_TOGGLE_CARROT : '[data-toggle^="button"]',
|
|
DATA_TOGGLE : '[data-toggle="buttons"]',
|
|
INPUT : 'input:not([type="hidden"])',
|
|
ACTIVE : '.active',
|
|
BUTTON : '.btn'
|
|
}
|
|
|
|
const Event = {
|
|
CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`,
|
|
FOCUS_BLUR_DATA_API : `focus${EVENT_KEY}${DATA_API_KEY} ` +
|
|
`blur${EVENT_KEY}${DATA_API_KEY}`
|
|
}
|
|
|
|
/**
|
|
* ------------------------------------------------------------------------
|
|
* Class Definition
|
|
* ------------------------------------------------------------------------
|
|
*/
|
|
|
|
class Button {
|
|
constructor(element) {
|
|
this._element = element
|
|
}
|
|
|
|
// Getters
|
|
|
|
static get VERSION() {
|
|
return VERSION
|
|
}
|
|
|
|
// Public
|
|
|
|
toggle() {
|
|
let triggerChangeEvent = true
|
|
let addAriaPressed = true
|
|
const rootElement = $(this._element).closest(
|
|
Selector.DATA_TOGGLE
|
|
)[0]
|
|
|
|
if (rootElement) {
|
|
const input = this._element.querySelector(Selector.INPUT)
|
|
|
|
if (input) {
|
|
if (input.type === 'radio') {
|
|
if (input.checked &&
|
|
this._element.classList.contains(ClassName.ACTIVE)) {
|
|
triggerChangeEvent = false
|
|
} else {
|
|
const activeElement = rootElement.querySelector(Selector.ACTIVE)
|
|
|
|
if (activeElement) {
|
|
$(activeElement).removeClass(ClassName.ACTIVE)
|
|
}
|
|
}
|
|
} else if (input.type === 'checkbox') {
|
|
if (this._element.tagName === 'LABEL' && input.checked === this._element.classList.contains(ClassName.ACTIVE)) {
|
|
triggerChangeEvent = false
|
|
}
|
|
} else {
|
|
// if it's not a radio button or checkbox don't add a pointless/invalid checked property to the input
|
|
triggerChangeEvent = false
|
|
}
|
|
|
|
if (triggerChangeEvent) {
|
|
input.checked = !this._element.classList.contains(ClassName.ACTIVE)
|
|
$(input).trigger('change')
|
|
}
|
|
|
|
input.focus()
|
|
addAriaPressed = false
|
|
}
|
|
}
|
|
|
|
if (!(this._element.hasAttribute('disabled') || this._element.classList.contains('disabled'))) {
|
|
if (addAriaPressed) {
|
|
this._element.setAttribute('aria-pressed',
|
|
!this._element.classList.contains(ClassName.ACTIVE))
|
|
}
|
|
|
|
if (triggerChangeEvent) {
|
|
$(this._element).toggleClass(ClassName.ACTIVE)
|
|
}
|
|
}
|
|
}
|
|
|
|
dispose() {
|
|
$.removeData(this._element, DATA_KEY)
|
|
this._element = null
|
|
}
|
|
|
|
// Static
|
|
|
|
static _jQueryInterface(config) {
|
|
return this.each(function () {
|
|
let data = $(this).data(DATA_KEY)
|
|
|
|
if (!data) {
|
|
data = new Button(this)
|
|
$(this).data(DATA_KEY, data)
|
|
}
|
|
|
|
if (config === 'toggle') {
|
|
data[config]()
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
/**
|
|
* ------------------------------------------------------------------------
|
|
* Data Api implementation
|
|
* ------------------------------------------------------------------------
|
|
*/
|
|
|
|
$(document)
|
|
.on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
|
|
let button = event.target
|
|
|
|
if (!$(button).hasClass(ClassName.BUTTON)) {
|
|
button = $(button).closest(Selector.BUTTON)[0]
|
|
}
|
|
|
|
if (!button || button.hasAttribute('disabled') || button.classList.contains('disabled')) {
|
|
event.preventDefault() // work around Firefox bug #1540995
|
|
} else {
|
|
const inputBtn = button.querySelector(Selector.INPUT)
|
|
|
|
if (inputBtn && (inputBtn.hasAttribute('disabled') || inputBtn.classList.contains('disabled'))) {
|
|
event.preventDefault() // work around Firefox bug #1540995
|
|
return
|
|
}
|
|
|
|
Button._jQueryInterface.call($(button), 'toggle')
|
|
}
|
|
})
|
|
.on(Event.FOCUS_BLUR_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => {
|
|
const button = $(event.target).closest(Selector.BUTTON)[0]
|
|
$(button).toggleClass(ClassName.FOCUS, /^focus(in)?$/.test(event.type))
|
|
})
|
|
|
|
/**
|
|
* ------------------------------------------------------------------------
|
|
* jQuery
|
|
* ------------------------------------------------------------------------
|
|
*/
|
|
|
|
$.fn[NAME] = Button._jQueryInterface
|
|
$.fn[NAME].Constructor = Button
|
|
$.fn[NAME].noConflict = () => {
|
|
$.fn[NAME] = JQUERY_NO_CONFLICT
|
|
return Button._jQueryInterface
|
|
}
|
|
|
|
export default Button
|