Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'show' and 'hide' methods to dropdown #27370

Merged
merged 8 commits into from
Oct 20, 2018
55 changes: 55 additions & 0 deletions js/src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,61 @@ class Dropdown {
.trigger($.Event(Event.SHOWN, relatedTarget))
}

show() {
if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED)) {
return
}
const isActive = $(this._menu).hasClass(ClassName.SHOW)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe skip isActive here and later one since they are used only once.

if (isActive) {
return
}

const relatedTarget = {
relatedTarget: this._element
}
const showEvent = $.Event(Event.SHOW, relatedTarget)

const parent = Dropdown._getParentFromElement(this._element)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing spaces

$(parent).trigger(showEvent)

if (showEvent.isDefaultPrevented()) {
return
}

$(this._menu).toggleClass(ClassName.SHOW)
$(parent)
.toggleClass(ClassName.SHOW)
.trigger($.Event(Event.SHOWN, relatedTarget))
}

hide() {
if (this._element.disabled || $(this._element).hasClass(ClassName.DISABLED)) {
return
}

const isActive = $(this._menu).hasClass(ClassName.SHOW)
if (!isActive) {
return
}

const relatedTarget = {
relatedTarget: this._element
}
const hideEvent = $.Event(Event.HIDE, relatedTarget)

const parent = Dropdown._getParentFromElement(this._element)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing spaces

$(parent).trigger(hideEvent)

if (hideEvent.isDefaultPrevented()) {
return
}

$(this._menu).toggleClass(ClassName.SHOW)
$(parent)
.toggleClass(ClassName.SHOW)
.trigger($.Event(Event.HIDDEN, relatedTarget))
}

dispose() {
$.removeData(this._element, DATA_KEY)
$(this._element).off(EVENT_KEY)
Expand Down
65 changes: 65 additions & 0 deletions js/tests/unit/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -1097,4 +1097,69 @@ $(function () {
assert.ok(dropdown._menu === null)
assert.ok(dropdown._element === null)
})

QUnit.test('should show dropdown', function (assert) {
assert.expect(2)

var dropdownHTML =
'<div class="dropdown">' +
' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' +
' <div class="dropdown-menu">' +
' <a class="dropdown-item" href="#">Another link</a>' +
' </div>' +
'</div>'

var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]')
.bootstrapDropdown()

var dropdown = $dropdown.data('bs.dropdown')
var done = assert.async()

$dropdown
.parent('.dropdown')
.on('show.bs.dropdown', function () {
assert.ok(true, 'show was fired')
})
.on('shown.bs.dropdown', function () {
assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is shown')
done()
})

dropdown.show()
})

QUnit.test('should hide dropdown', function (assert) {
assert.expect(2)

var dropdownHTML =
'<div class="dropdown">' +
' <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' +
' <div class="dropdown-menu">' +
' <a class="dropdown-item" href="#">Another link</a>' +
' </div>' +
'</div>'

var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]')
.bootstrapDropdown()

var dropdown = $dropdown.data('bs.dropdown')
var done = assert.async()
$dropdown.trigger('click')

$dropdown
.parent('.dropdown')
.on('hide.bs.dropdown', function () {
assert.ok(true, 'hide was fired')
})
.on('hidden.bs.dropdown', function () {
assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is hidden')
done()
})

dropdown.hide()
})
})