1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-08 06:36:33 +02:00

Dropdown — Add option to make the dropdown menu clickable (#33389)

This commit is contained in:
Rohit Sharma
2021-04-20 10:49:57 +05:30
committed by GitHub
parent a22f4d3cfd
commit b59b75bc55
3 changed files with 175 additions and 12 deletions

View File

@@ -75,7 +75,8 @@ const Default = {
boundary: 'clippingParents',
reference: 'toggle',
display: 'dynamic',
popperConfig: null
popperConfig: null,
autoClose: true
}
const DefaultType = {
@@ -83,7 +84,8 @@ const DefaultType = {
boundary: '(string|element)',
reference: '(string|element|object)',
display: 'string',
popperConfig: '(null|object|function)'
popperConfig: '(null|object|function)',
autoClose: '(boolean|string)'
}
/**
@@ -127,9 +129,8 @@ class Dropdown extends BaseComponent {
const isActive = this._element.classList.contains(CLASS_NAME_SHOW)
Dropdown.clearMenus()
if (isActive) {
this.hide()
return
}
@@ -424,7 +425,7 @@ class Dropdown extends BaseComponent {
for (let i = 0, len = toggles.length; i < len; i++) {
const context = Data.get(toggles[i], DATA_KEY)
if (!context) {
if (!context || context._config.autoClose === false) {
continue
}
@@ -437,8 +438,13 @@ class Dropdown extends BaseComponent {
}
if (event) {
// Don't close the menu if the clicked element or one of its parents is the dropdown button
if ([context._element].some(element => event.composedPath().includes(element))) {
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
}