aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/twbs/bootstrap/js/src/dropdown.js
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/twbs/bootstrap/js/src/dropdown.js')
-rw-r--r--vendor/twbs/bootstrap/js/src/dropdown.js549
1 files changed, 255 insertions, 294 deletions
diff --git a/vendor/twbs/bootstrap/js/src/dropdown.js b/vendor/twbs/bootstrap/js/src/dropdown.js
index 76dda8f57..6fafdb116 100644
--- a/vendor/twbs/bootstrap/js/src/dropdown.js
+++ b/vendor/twbs/bootstrap/js/src/dropdown.js
@@ -1,13 +1,28 @@
/**
* --------------------------------------------------------------------------
- * Bootstrap (v4.6.0): dropdown.js
+ * Bootstrap (v5.1.3): dropdown.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/
-import $ from 'jquery'
-import Popper from 'popper.js'
-import Util from './util'
+import * as Popper from '@popperjs/core'
+
+import {
+ defineJQueryPlugin,
+ getElement,
+ getElementFromSelector,
+ getNextActiveElement,
+ isDisabled,
+ isElement,
+ isRTL,
+ isVisible,
+ noop,
+ typeCheckConfig
+} from './util/index'
+import EventHandler from './dom/event-handler'
+import Manipulator from './dom/manipulator'
+import SelectorEngine from './dom/selector-engine'
+import BaseComponent from './base-component'
/**
* ------------------------------------------------------------------------
@@ -16,65 +31,61 @@ import Util from './util'
*/
const NAME = 'dropdown'
-const VERSION = '4.6.0'
const DATA_KEY = 'bs.dropdown'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
-const JQUERY_NO_CONFLICT = $.fn[NAME]
-const ESCAPE_KEYCODE = 27 // KeyboardEvent.which value for Escape (Esc) key
-const SPACE_KEYCODE = 32 // KeyboardEvent.which value for space key
-const TAB_KEYCODE = 9 // KeyboardEvent.which value for tab key
-const ARROW_UP_KEYCODE = 38 // KeyboardEvent.which value for up arrow key
-const ARROW_DOWN_KEYCODE = 40 // KeyboardEvent.which value for down arrow key
-const RIGHT_MOUSE_BUTTON_WHICH = 3 // MouseEvent.which value for the right button (assuming a right-handed mouse)
-const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEYCODE}|${ARROW_DOWN_KEYCODE}|${ESCAPE_KEYCODE}`)
+
+const ESCAPE_KEY = 'Escape'
+const SPACE_KEY = 'Space'
+const TAB_KEY = 'Tab'
+const ARROW_UP_KEY = 'ArrowUp'
+const ARROW_DOWN_KEY = 'ArrowDown'
+const RIGHT_MOUSE_BUTTON = 2 // MouseEvent.button value for the secondary button, usually the right button
+
+const REGEXP_KEYDOWN = new RegExp(`${ARROW_UP_KEY}|${ARROW_DOWN_KEY}|${ESCAPE_KEY}`)
const EVENT_HIDE = `hide${EVENT_KEY}`
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
const EVENT_SHOW = `show${EVENT_KEY}`
const EVENT_SHOWN = `shown${EVENT_KEY}`
-const EVENT_CLICK = `click${EVENT_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
const EVENT_KEYDOWN_DATA_API = `keydown${EVENT_KEY}${DATA_API_KEY}`
const EVENT_KEYUP_DATA_API = `keyup${EVENT_KEY}${DATA_API_KEY}`
-const CLASS_NAME_DISABLED = 'disabled'
const CLASS_NAME_SHOW = 'show'
const CLASS_NAME_DROPUP = 'dropup'
-const CLASS_NAME_DROPRIGHT = 'dropright'
-const CLASS_NAME_DROPLEFT = 'dropleft'
-const CLASS_NAME_MENURIGHT = 'dropdown-menu-right'
-const CLASS_NAME_POSITION_STATIC = 'position-static'
+const CLASS_NAME_DROPEND = 'dropend'
+const CLASS_NAME_DROPSTART = 'dropstart'
+const CLASS_NAME_NAVBAR = 'navbar'
-const SELECTOR_DATA_TOGGLE = '[data-toggle="dropdown"]'
-const SELECTOR_FORM_CHILD = '.dropdown form'
+const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="dropdown"]'
const SELECTOR_MENU = '.dropdown-menu'
const SELECTOR_NAVBAR_NAV = '.navbar-nav'
const SELECTOR_VISIBLE_ITEMS = '.dropdown-menu .dropdown-item:not(.disabled):not(:disabled)'
-const PLACEMENT_TOP = 'top-start'
-const PLACEMENT_TOPEND = 'top-end'
-const PLACEMENT_BOTTOM = 'bottom-start'
-const PLACEMENT_BOTTOMEND = 'bottom-end'
-const PLACEMENT_RIGHT = 'right-start'
-const PLACEMENT_LEFT = 'left-start'
+const PLACEMENT_TOP = isRTL() ? 'top-end' : 'top-start'
+const PLACEMENT_TOPEND = isRTL() ? 'top-start' : 'top-end'
+const PLACEMENT_BOTTOM = isRTL() ? 'bottom-end' : 'bottom-start'
+const PLACEMENT_BOTTOMEND = isRTL() ? 'bottom-start' : 'bottom-end'
+const PLACEMENT_RIGHT = isRTL() ? 'left-start' : 'right-start'
+const PLACEMENT_LEFT = isRTL() ? 'right-start' : 'left-start'
const Default = {
- offset: 0,
- flip: true,
- boundary: 'scrollParent',
+ offset: [0, 2],
+ boundary: 'clippingParents',
reference: 'toggle',
display: 'dynamic',
- popperConfig: null
+ popperConfig: null,
+ autoClose: true
}
const DefaultType = {
- offset: '(number|string|function)',
- flip: 'boolean',
+ offset: '(array|string|function)',
boundary: '(string|element)',
- reference: '(string|element)',
+ reference: '(string|element|object)',
display: 'string',
- popperConfig: '(null|object)'
+ popperConfig: '(null|object|function)',
+ autoClose: '(boolean|string)'
}
/**
@@ -83,23 +94,18 @@ const DefaultType = {
* ------------------------------------------------------------------------
*/
-class Dropdown {
+class Dropdown extends BaseComponent {
constructor(element, config) {
- this._element = element
+ super(element)
+
this._popper = null
this._config = this._getConfig(config)
this._menu = this._getMenuElement()
this._inNavbar = this._detectNavbar()
-
- this._addEventListeners()
}
// Getters
- static get VERSION() {
- return VERSION
- }
-
static get Default() {
return Default
}
@@ -108,72 +114,37 @@ class Dropdown {
return DefaultType
}
+ static get NAME() {
+ return NAME
+ }
+
// Public
toggle() {
- if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED)) {
- return
- }
-
- const isActive = $(this._menu).hasClass(CLASS_NAME_SHOW)
-
- Dropdown._clearMenus()
-
- if (isActive) {
- return
- }
-
- this.show(true)
+ return this._isShown() ? this.hide() : this.show()
}
- show(usePopper = false) {
- if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED) || $(this._menu).hasClass(CLASS_NAME_SHOW)) {
+ show() {
+ if (isDisabled(this._element) || this._isShown(this._menu)) {
return
}
const relatedTarget = {
relatedTarget: this._element
}
- const showEvent = $.Event(EVENT_SHOW, relatedTarget)
- const parent = Dropdown._getParentFromElement(this._element)
- $(parent).trigger(showEvent)
+ const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, relatedTarget)
- if (showEvent.isDefaultPrevented()) {
+ if (showEvent.defaultPrevented) {
return
}
+ const parent = Dropdown.getParentFromElement(this._element)
// Totally disable Popper for Dropdowns in Navbar
- if (!this._inNavbar && usePopper) {
- /**
- * Check for Popper dependency
- * Popper - https://popper.js.org
- */
- if (typeof Popper === 'undefined') {
- throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)')
- }
-
- let referenceElement = this._element
-
- if (this._config.reference === 'parent') {
- referenceElement = parent
- } else if (Util.isElement(this._config.reference)) {
- referenceElement = this._config.reference
-
- // Check if it's jQuery element
- if (typeof this._config.reference.jquery !== 'undefined') {
- referenceElement = this._config.reference[0]
- }
- }
-
- // If boundary is not `scrollParent`, then set position to `static`
- // to allow the menu to "escape" the scroll parent's boundaries
- // https://github.com/twbs/bootstrap/issues/24251
- if (this._config.boundary !== 'scrollParent') {
- $(parent).addClass(CLASS_NAME_POSITION_STATIC)
- }
-
- this._popper = new Popper(referenceElement, this._menu, this._getPopperConfig())
+ if (this._inNavbar) {
+ Manipulator.setDataAttribute(this._menu, 'popper', 'none')
+ } else {
+ this._createPopper(parent)
}
// If this is a touch-enabled device we add extra
@@ -181,266 +152,275 @@ class Dropdown {
// only needed because of broken event delegation on iOS
// https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
if ('ontouchstart' in document.documentElement &&
- $(parent).closest(SELECTOR_NAVBAR_NAV).length === 0) {
- $(document.body).children().on('mouseover', null, $.noop)
+ !parent.closest(SELECTOR_NAVBAR_NAV)) {
+ [].concat(...document.body.children)
+ .forEach(elem => EventHandler.on(elem, 'mouseover', noop))
}
this._element.focus()
this._element.setAttribute('aria-expanded', true)
- $(this._menu).toggleClass(CLASS_NAME_SHOW)
- $(parent)
- .toggleClass(CLASS_NAME_SHOW)
- .trigger($.Event(EVENT_SHOWN, relatedTarget))
+ this._menu.classList.add(CLASS_NAME_SHOW)
+ this._element.classList.add(CLASS_NAME_SHOW)
+ EventHandler.trigger(this._element, EVENT_SHOWN, relatedTarget)
}
hide() {
- if (this._element.disabled || $(this._element).hasClass(CLASS_NAME_DISABLED) || !$(this._menu).hasClass(CLASS_NAME_SHOW)) {
+ if (isDisabled(this._element) || !this._isShown(this._menu)) {
return
}
const relatedTarget = {
relatedTarget: this._element
}
- const hideEvent = $.Event(EVENT_HIDE, relatedTarget)
- const parent = Dropdown._getParentFromElement(this._element)
-
- $(parent).trigger(hideEvent)
-
- if (hideEvent.isDefaultPrevented()) {
- return
- }
-
- if (this._popper) {
- this._popper.destroy()
- }
- $(this._menu).toggleClass(CLASS_NAME_SHOW)
- $(parent)
- .toggleClass(CLASS_NAME_SHOW)
- .trigger($.Event(EVENT_HIDDEN, relatedTarget))
+ this._completeHide(relatedTarget)
}
dispose() {
- $.removeData(this._element, DATA_KEY)
- $(this._element).off(EVENT_KEY)
- this._element = null
- this._menu = null
- if (this._popper !== null) {
+ if (this._popper) {
this._popper.destroy()
- this._popper = null
}
+
+ super.dispose()
}
update() {
this._inNavbar = this._detectNavbar()
- if (this._popper !== null) {
- this._popper.scheduleUpdate()
+ if (this._popper) {
+ this._popper.update()
}
}
// Private
- _addEventListeners() {
- $(this._element).on(EVENT_CLICK, event => {
- event.preventDefault()
- event.stopPropagation()
- this.toggle()
- })
+ _completeHide(relatedTarget) {
+ const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE, relatedTarget)
+ if (hideEvent.defaultPrevented) {
+ return
+ }
+
+ // If this is a touch-enabled device we remove the extra
+ // empty mouseover listeners we added for iOS support
+ if ('ontouchstart' in document.documentElement) {
+ [].concat(...document.body.children)
+ .forEach(elem => EventHandler.off(elem, 'mouseover', noop))
+ }
+
+ if (this._popper) {
+ this._popper.destroy()
+ }
+
+ this._menu.classList.remove(CLASS_NAME_SHOW)
+ this._element.classList.remove(CLASS_NAME_SHOW)
+ this._element.setAttribute('aria-expanded', 'false')
+ Manipulator.removeDataAttribute(this._menu, 'popper')
+ EventHandler.trigger(this._element, EVENT_HIDDEN, relatedTarget)
}
_getConfig(config) {
config = {
...this.constructor.Default,
- ...$(this._element).data(),
+ ...Manipulator.getDataAttributes(this._element),
...config
}
- Util.typeCheckConfig(
- NAME,
- config,
- this.constructor.DefaultType
- )
+ typeCheckConfig(NAME, config, this.constructor.DefaultType)
+
+ if (typeof config.reference === 'object' && !isElement(config.reference) &&
+ typeof config.reference.getBoundingClientRect !== 'function'
+ ) {
+ // Popper virtual elements require a getBoundingClientRect method
+ throw new TypeError(`${NAME.toUpperCase()}: Option "reference" provided type "object" without a required "getBoundingClientRect" method.`)
+ }
return config
}
- _getMenuElement() {
- if (!this._menu) {
- const parent = Dropdown._getParentFromElement(this._element)
+ _createPopper(parent) {
+ if (typeof Popper === 'undefined') {
+ throw new TypeError('Bootstrap\'s dropdowns require Popper (https://popper.js.org)')
+ }
- if (parent) {
- this._menu = parent.querySelector(SELECTOR_MENU)
- }
+ let referenceElement = this._element
+
+ if (this._config.reference === 'parent') {
+ referenceElement = parent
+ } else if (isElement(this._config.reference)) {
+ referenceElement = getElement(this._config.reference)
+ } else if (typeof this._config.reference === 'object') {
+ referenceElement = this._config.reference
}
- return this._menu
+ const popperConfig = this._getPopperConfig()
+ const isDisplayStatic = popperConfig.modifiers.find(modifier => modifier.name === 'applyStyles' && modifier.enabled === false)
+
+ this._popper = Popper.createPopper(referenceElement, this._menu, popperConfig)
+
+ if (isDisplayStatic) {
+ Manipulator.setDataAttribute(this._menu, 'popper', 'static')
+ }
+ }
+
+ _isShown(element = this._element) {
+ return element.classList.contains(CLASS_NAME_SHOW)
+ }
+
+ _getMenuElement() {
+ return SelectorEngine.next(this._element, SELECTOR_MENU)[0]
}
_getPlacement() {
- const $parentDropdown = $(this._element.parentNode)
- let placement = PLACEMENT_BOTTOM
-
- // Handle dropup
- if ($parentDropdown.hasClass(CLASS_NAME_DROPUP)) {
- placement = $(this._menu).hasClass(CLASS_NAME_MENURIGHT) ?
- PLACEMENT_TOPEND :
- PLACEMENT_TOP
- } else if ($parentDropdown.hasClass(CLASS_NAME_DROPRIGHT)) {
- placement = PLACEMENT_RIGHT
- } else if ($parentDropdown.hasClass(CLASS_NAME_DROPLEFT)) {
- placement = PLACEMENT_LEFT
- } else if ($(this._menu).hasClass(CLASS_NAME_MENURIGHT)) {
- placement = PLACEMENT_BOTTOMEND
- }
-
- return placement
+ const parentDropdown = this._element.parentNode
+
+ if (parentDropdown.classList.contains(CLASS_NAME_DROPEND)) {
+ return PLACEMENT_RIGHT
+ }
+
+ if (parentDropdown.classList.contains(CLASS_NAME_DROPSTART)) {
+ return PLACEMENT_LEFT
+ }
+
+ // We need to trim the value because custom properties can also include spaces
+ const isEnd = getComputedStyle(this._menu).getPropertyValue('--bs-position').trim() === 'end'
+
+ if (parentDropdown.classList.contains(CLASS_NAME_DROPUP)) {
+ return isEnd ? PLACEMENT_TOPEND : PLACEMENT_TOP
+ }
+
+ return isEnd ? PLACEMENT_BOTTOMEND : PLACEMENT_BOTTOM
}
_detectNavbar() {
- return $(this._element).closest('.navbar').length > 0
+ return this._element.closest(`.${CLASS_NAME_NAVBAR}`) !== null
}
_getOffset() {
- const offset = {}
+ const { offset } = this._config
- if (typeof this._config.offset === 'function') {
- offset.fn = data => {
- data.offsets = {
- ...data.offsets,
- ...(this._config.offset(data.offsets, this._element) || {})
- }
+ if (typeof offset === 'string') {
+ return offset.split(',').map(val => Number.parseInt(val, 10))
+ }
- return data
- }
- } else {
- offset.offset = this._config.offset
+ if (typeof offset === 'function') {
+ return popperData => offset(popperData, this._element)
}
return offset
}
_getPopperConfig() {
- const popperConfig = {
+ const defaultBsPopperConfig = {
placement: this._getPlacement(),
- modifiers: {
- offset: this._getOffset(),
- flip: {
- enabled: this._config.flip
- },
- preventOverflow: {
- boundariesElement: this._config.boundary
+ modifiers: [{
+ name: 'preventOverflow',
+ options: {
+ boundary: this._config.boundary
}
- }
+ },
+ {
+ name: 'offset',
+ options: {
+ offset: this._getOffset()
+ }
+ }]
}
// Disable Popper if we have a static display
if (this._config.display === 'static') {
- popperConfig.modifiers.applyStyle = {
+ defaultBsPopperConfig.modifiers = [{
+ name: 'applyStyles',
enabled: false
- }
+ }]
}
return {
- ...popperConfig,
- ...this._config.popperConfig
+ ...defaultBsPopperConfig,
+ ...(typeof this._config.popperConfig === 'function' ? this._config.popperConfig(defaultBsPopperConfig) : this._config.popperConfig)
+ }
+ }
+
+ _selectMenuItem({ key, target }) {
+ const items = SelectorEngine.find(SELECTOR_VISIBLE_ITEMS, this._menu).filter(isVisible)
+
+ if (!items.length) {
+ return
}
+
+ // if target isn't included in items (e.g. when expanding the dropdown)
+ // allow cycling to get the last item in case key equals ARROW_UP_KEY
+ getNextActiveElement(items, target, key === ARROW_DOWN_KEY, !items.includes(target)).focus()
}
// Static
- static _jQueryInterface(config) {
+ static jQueryInterface(config) {
return this.each(function () {
- let data = $(this).data(DATA_KEY)
- const _config = typeof config === 'object' ? config : null
+ const data = Dropdown.getOrCreateInstance(this, config)
- if (!data) {
- data = new Dropdown(this, _config)
- $(this).data(DATA_KEY, data)
+ if (typeof config !== 'string') {
+ return
}
- if (typeof config === 'string') {
- if (typeof data[config] === 'undefined') {
- throw new TypeError(`No method named "${config}"`)
- }
-
- data[config]()
+ if (typeof data[config] === 'undefined') {
+ throw new TypeError(`No method named "${config}"`)
}
+
+ data[config]()
})
}
- static _clearMenus(event) {
- if (event && (event.which === RIGHT_MOUSE_BUTTON_WHICH ||
- event.type === 'keyup' && event.which !== TAB_KEYCODE)) {
+ static clearMenus(event) {
+ if (event && (event.button === RIGHT_MOUSE_BUTTON || (event.type === 'keyup' && event.key !== TAB_KEY))) {
return
}
- const toggles = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE))
+ const toggles = SelectorEngine.find(SELECTOR_DATA_TOGGLE)
for (let i = 0, len = toggles.length; i < len; i++) {
- const parent = Dropdown._getParentFromElement(toggles[i])
- const context = $(toggles[i]).data(DATA_KEY)
- const relatedTarget = {
- relatedTarget: toggles[i]
- }
-
- if (event && event.type === 'click') {
- relatedTarget.clickEvent = event
- }
-
- if (!context) {
+ const context = Dropdown.getInstance(toggles[i])
+ if (!context || context._config.autoClose === false) {
continue
}
- const dropdownMenu = context._menu
- if (!$(parent).hasClass(CLASS_NAME_SHOW)) {
+ if (!context._isShown()) {
continue
}
- if (event && (event.type === 'click' &&
- /input|textarea/i.test(event.target.tagName) || event.type === 'keyup' && event.which === TAB_KEYCODE) &&
- $.contains(parent, event.target)) {
- continue
- }
-
- const hideEvent = $.Event(EVENT_HIDE, relatedTarget)
- $(parent).trigger(hideEvent)
- if (hideEvent.isDefaultPrevented()) {
- continue
+ const relatedTarget = {
+ relatedTarget: context._element
}
- // If this is a touch-enabled device we remove the extra
- // empty mouseover listeners we added for iOS support
- if ('ontouchstart' in document.documentElement) {
- $(document.body).children().off('mouseover', null, $.noop)
- }
+ if (event) {
+ const composedPath = event.composedPath()
+ const isMenuTarget = composedPath.includes(context._menu)
+ if (
+ composedPath.includes(context._element) ||
+ (context._config.autoClose === 'inside' && !isMenuTarget) ||
+ (context._config.autoClose === 'outside' && isMenuTarget)
+ ) {
+ continue
+ }
- toggles[i].setAttribute('aria-expanded', 'false')
+ // Tab navigation through the dropdown menu or events from contained inputs shouldn't close the menu
+ if (context._menu.contains(event.target) && ((event.type === 'keyup' && event.key === TAB_KEY) || /input|select|option|textarea|form/i.test(event.target.tagName))) {
+ continue
+ }
- if (context._popper) {
- context._popper.destroy()
+ if (event.type === 'click') {
+ relatedTarget.clickEvent = event
+ }
}
- $(dropdownMenu).removeClass(CLASS_NAME_SHOW)
- $(parent)
- .removeClass(CLASS_NAME_SHOW)
- .trigger($.Event(EVENT_HIDDEN, relatedTarget))
+ context._completeHide(relatedTarget)
}
}
- static _getParentFromElement(element) {
- let parent
- const selector = Util.getSelectorFromElement(element)
-
- if (selector) {
- parent = document.querySelector(selector)
- }
-
- return parent || element.parentNode
+ static getParentFromElement(element) {
+ return getElementFromSelector(element) || element.parentNode
}
- // eslint-disable-next-line complexity
- static _dataApiKeydownHandler(event) {
+ static dataApiKeydownHandler(event) {
// If not input/textarea:
// - And not a key in REGEXP_KEYDOWN => not a dropdown command
// If input/textarea:
@@ -449,57 +429,46 @@ class Dropdown {
// - If key is not up or down => not a dropdown command
// - If trigger inside the menu => not a dropdown command
if (/input|textarea/i.test(event.target.tagName) ?
- event.which === SPACE_KEYCODE || event.which !== ESCAPE_KEYCODE &&
- (event.which !== ARROW_DOWN_KEYCODE && event.which !== ARROW_UP_KEYCODE ||
- $(event.target).closest(SELECTOR_MENU).length) : !REGEXP_KEYDOWN.test(event.which)) {
- return
- }
-
- if (this.disabled || $(this).hasClass(CLASS_NAME_DISABLED)) {
+ event.key === SPACE_KEY || (event.key !== ESCAPE_KEY &&
+ ((event.key !== ARROW_DOWN_KEY && event.key !== ARROW_UP_KEY) ||
+ event.target.closest(SELECTOR_MENU))) :
+ !REGEXP_KEYDOWN.test(event.key)) {
return
}
- const parent = Dropdown._getParentFromElement(this)
- const isActive = $(parent).hasClass(CLASS_NAME_SHOW)
+ const isActive = this.classList.contains(CLASS_NAME_SHOW)
- if (!isActive && event.which === ESCAPE_KEYCODE) {
+ if (!isActive && event.key === ESCAPE_KEY) {
return
}
event.preventDefault()
event.stopPropagation()
- if (!isActive || (event.which === ESCAPE_KEYCODE || event.which === SPACE_KEYCODE)) {
- if (event.which === ESCAPE_KEYCODE) {
- $(parent.querySelector(SELECTOR_DATA_TOGGLE)).trigger('focus')
- }
-
- $(this).trigger('click')
+ if (isDisabled(this)) {
return
}
- const items = [].slice.call(parent.querySelectorAll(SELECTOR_VISIBLE_ITEMS))
- .filter(item => $(item).is(':visible'))
+ const getToggleButton = this.matches(SELECTOR_DATA_TOGGLE) ? this : SelectorEngine.prev(this, SELECTOR_DATA_TOGGLE)[0]
+ const instance = Dropdown.getOrCreateInstance(getToggleButton)
- if (items.length === 0) {
+ if (event.key === ESCAPE_KEY) {
+ instance.hide()
return
}
- let index = items.indexOf(event.target)
-
- if (event.which === ARROW_UP_KEYCODE && index > 0) { // Up
- index--
- }
+ if (event.key === ARROW_UP_KEY || event.key === ARROW_DOWN_KEY) {
+ if (!isActive) {
+ instance.show()
+ }
- if (event.which === ARROW_DOWN_KEYCODE && index < items.length - 1) { // Down
- index++
+ instance._selectMenuItem(event)
+ return
}
- if (index < 0) {
- index = 0
+ if (!isActive || event.key === SPACE_KEY) {
+ Dropdown.clearMenus()
}
-
- items[index].focus()
}
}
@@ -509,30 +478,22 @@ class Dropdown {
* ------------------------------------------------------------------------
*/
-$(document)
- .on(EVENT_KEYDOWN_DATA_API, SELECTOR_DATA_TOGGLE, Dropdown._dataApiKeydownHandler)
- .on(EVENT_KEYDOWN_DATA_API, SELECTOR_MENU, Dropdown._dataApiKeydownHandler)
- .on(`${EVENT_CLICK_DATA_API} ${EVENT_KEYUP_DATA_API}`, Dropdown._clearMenus)
- .on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
- event.preventDefault()
- event.stopPropagation()
- Dropdown._jQueryInterface.call($(this), 'toggle')
- })
- .on(EVENT_CLICK_DATA_API, SELECTOR_FORM_CHILD, e => {
- e.stopPropagation()
- })
+EventHandler.on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_DATA_TOGGLE, Dropdown.dataApiKeydownHandler)
+EventHandler.on(document, EVENT_KEYDOWN_DATA_API, SELECTOR_MENU, Dropdown.dataApiKeydownHandler)
+EventHandler.on(document, EVENT_CLICK_DATA_API, Dropdown.clearMenus)
+EventHandler.on(document, EVENT_KEYUP_DATA_API, Dropdown.clearMenus)
+EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
+ event.preventDefault()
+ Dropdown.getOrCreateInstance(this).toggle()
+})
/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
+ * add .Dropdown to jQuery only if jQuery is present
*/
-$.fn[NAME] = Dropdown._jQueryInterface
-$.fn[NAME].Constructor = Dropdown
-$.fn[NAME].noConflict = () => {
- $.fn[NAME] = JQUERY_NO_CONFLICT
- return Dropdown._jQueryInterface
-}
+defineJQueryPlugin(Dropdown)
export default Dropdown