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)
$(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)
$(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
264 changes: 264 additions & 0 deletions js/tests/unit/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -1097,4 +1097,268 @@ $(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()
})

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

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')
$dropdown.trigger('click')
dropdown.show()

assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is still shown')
})

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

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')
dropdown.hide()
assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is still hidden')
})

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 prevent default event on show method call', function (assert) {
assert.expect(1)

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 (event) {
event.preventDefault()
done()
})

dropdown.show()
assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is hidden')
})

QUnit.test('should prevent default event on hide method call', function (assert) {
assert.expect(1)

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 (event) {
event.preventDefault()
done()
})

dropdown.hide()
assert.ok($dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu is shown')
})

QUnit.test('should not open dropdown via show method if target is disabled via attribute', function (assert) {
assert.expect(1)
var dropdownHTML =
'<div class="dropdown">' +
' <button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>' +
' <div class="dropdown-menu">' +
' <a class="dropdown-item" href="#">Another link</a>' +
' </div>' +
'</div>'
$(dropdownHTML).appendTo('#qunit-fixture')
var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown()
$dropdown.show()
assert.ok(!$dropdown.parent('.dropdown').hasClass('show'))
})

QUnit.test('should not open dropdown via show method if target is disabled via class', function (assert) {
assert.expect(1)
var dropdownHTML =
'<div class="dropdown">' +
' <button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>' +
' <div class="dropdown-menu">' +
' <a class="dropdown-item" href="#">Another link</a>' +
' </div>' +
'</div>'

$(dropdownHTML).appendTo('#qunit-fixture')
var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown()
$dropdown.show()
assert.ok(!$dropdown.parent('.dropdown').hasClass('show'))
})

QUnit.test('should not hide dropdown via hide method if target is disabled via attribute', function (assert) {
assert.expect(1)
var dropdownHTML =
'<div class="dropdown show">' +
' <button disabled href="#" class="btn dropdown-toggle" data-toggle="dropdown">Dropdown</button>' +
' <div class="dropdown-menu">' +
' <a class="dropdown-item" href="#">Another link</a>' +
' </div>' +
'</div>'
$(dropdownHTML).appendTo('#qunit-fixture')
var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown()
$dropdown.hide()
assert.ok($dropdown.parent('.dropdown').hasClass('show'))
})

QUnit.test('should not hide dropdown via hide method if target is disabled via class', function (assert) {
assert.expect(1)
var dropdownHTML =
'<div class="dropdown show">' +
' <button href="#" class="btn dropdown-toggle disabled" data-toggle="dropdown">Dropdown</button>' +
' <div class="dropdown-menu">' +
' <a class="dropdown-item" href="#">Another link</a>' +
' </div>' +
'</div>'

$(dropdownHTML).appendTo('#qunit-fixture')
var $dropdown = $('#qunit-fixture').find('[data-toggle="dropdown"]').bootstrapDropdown()
$dropdown.hide()
assert.ok($dropdown.parent('.dropdown').hasClass('show'))
})
})