Skip to content

Commit

Permalink
feat(dropdown): add original click event
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubhonisek authored and Johann-S committed Jun 25, 2018
1 parent bca4cea commit 49e0946
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/4.1/components/dropdowns.md
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ Note when `boundary` is set to any value other than `'scrollParent'`, the style
### Events

All dropdown events are fired at the `.dropdown-menu`'s parent element and have a `relatedTarget` property, whose value is the toggling anchor element.
`hide.bs.dropdown` and `hidden.bs.dropdown` events have a `clickEvent` property (only when the original event type is `click`) that contains an Event Object for the click event.

| Event | Description |
| --- | --- |
Expand Down
4 changes: 4 additions & 0 deletions js/src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ const Dropdown = (($) => {
relatedTarget: toggles[i]
}

if (event && event.type === 'click') {
relatedTarget.clickEvent = event
}

if (!context) {
continue
}
Expand Down
68 changes: 68 additions & 0 deletions js/tests/unit/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,74 @@ $(function () {
$dropdown.trigger('click')
})

QUnit.test('should fire hide and hidden event with a clickEvent', function (assert) {
assert.expect(3)
var dropdownHTML = '<div class="tabs">' +
'<div class="dropdown">' +
'<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' +
'<div class="dropdown-menu">' +
'<a class="dropdown-item" href="#">Secondary link</a>' +
'<a class="dropdown-item" href="#">Something else here</a>' +
'<div class="divider"/>' +
'<a class="dropdown-item" href="#">Another link</a>' +
'</div>' +
'</div>' +
'</div>'
var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]')
.bootstrapDropdown()

$dropdown.parent('.dropdown')
.on('hide.bs.dropdown', function (e) {
assert.ok(e.clickEvent)
})
.on('hidden.bs.dropdown', function (e) {
assert.ok(e.clickEvent)
})
.on('shown.bs.dropdown', function () {
assert.ok(true, 'shown was fired')
$(document.body).trigger('click')
})

$dropdown.trigger('click')
})

QUnit.test('should fire hide and hidden event without a clickEvent if event type is not click', function (assert) {
assert.expect(3)
var dropdownHTML = '<div class="tabs">' +
'<div class="dropdown">' +
'<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>' +
'<div class="dropdown-menu">' +
'<a class="dropdown-item" href="#">Secondary link</a>' +
'<a class="dropdown-item" href="#">Something else here</a>' +
'<div class="divider"/>' +
'<a class="dropdown-item" href="#">Another link</a>' +
'</div>' +
'</div>' +
'</div>'
var $dropdown = $(dropdownHTML)
.appendTo('#qunit-fixture')
.find('[data-toggle="dropdown"]')
.bootstrapDropdown()

$dropdown.parent('.dropdown')
.on('hide.bs.dropdown', function (e) {
assert.notOk(e.clickEvent)
})
.on('hidden.bs.dropdown', function (e) {
assert.notOk(e.clickEvent)
})
.on('shown.bs.dropdown', function () {
assert.ok(true, 'shown was fired')
$dropdown.trigger($.Event('keydown', {
which: 27
}))
})

$dropdown.trigger('click')
})

QUnit.test('should ignore keyboard events within <input>s and <textarea>s', function (assert) {
assert.expect(3)
var done = assert.async()
Expand Down

0 comments on commit 49e0946

Please sign in to comment.