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

Hide and hidden events on tabs #14772

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/_includes/js/tabs.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ <h3>Events</h3>
<td>shown.bs.tab</td>
<td>This event fires on tab show after a tab has been shown. Use <code>event.target</code> and <code>event.relatedTarget</code> to target the active tab and the previous active tab (if available) respectively.</td>
</tr>
<tr>
<td>hide.bs.tab</td>
<td>This event fires immediately when a new tab is to be shown. Use <code>event.relatedTarget</code> to target the new tab.</td>
</tr>
<tr>
<td>hidden.bs.tab</td>
<td>This event fires after a new tab is shown.. Use <code>event.relatedTarget</code> to target the new tab.</td>
Copy link
Collaborator

Choose a reason for hiding this comment

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

"shown.." => "shown."

</tr>
</tbody>
</table>
</div><!-- /.table-responsive -->
Expand Down
20 changes: 14 additions & 6 deletions js/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,30 @@

if ($this.parent('li').hasClass('active')) return

var previous = $ul.find('.active:last a')[0]
var e = $.Event('show.bs.tab', {
relatedTarget: previous
var $previous = $ul.find('.active:last a')
var hideEvent = $.Event('hide.bs.tab', {
relatedTarget: $this[0]
})
var showEvent = $.Event('show.bs.tab', {
relatedTarget: $previous[0]
})

$this.trigger(e)
$previous.trigger(hideEvent)
$this.trigger(showEvent)

if (e.isDefaultPrevented()) return
if (showEvent.isDefaultPrevented() || hideEvent.isDefaultPrevented()) return

var $target = $(selector)

this.activate($this.closest('li'), $ul)
this.activate($target, $target.parent(), function () {
$previous.trigger({
type: 'hidden.bs.tab',
relatedTarget: $this[0]
})
$this.trigger({
type: 'shown.bs.tab',
relatedTarget: previous
relatedTarget: $previous[0]
})
})
}
Expand Down
77 changes: 77 additions & 0 deletions js/tests/unit/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,81 @@ $(function () {
.bootstrapTab('show')
})

test('should fire hide and hidden events', function () {
stop()

var tabsHTML = '<ul class="tabs">'
+ '<li><a href="#home">Home</a></li>'
+ '<li><a href="#profile">Profile</a></li>'
+ '</ul>'

$(tabsHTML)
.find('li:first a')
.on('hide.bs.tab', function () {
ok(true, 'hide event fired')
})
.bootstrapTab('show')
.end()
.find('li:last a')
.bootstrapTab('show')

$(tabsHTML)
.find('li:first a')
.on('hidden.bs.tab', function () {
ok(true, 'hidden event fired')
start()
})
.bootstrapTab('show')
.end()
.find('li:last a')
.bootstrapTab('show')
})

test('should not fire hidden when hide is prevented', function () {
stop()

var tabsHTML = '<ul class="tabs">'
+ '<li><a href="#home">Home</a></li>'
+ '<li><a href="#profile">Profile</a></li>'
+ '</ul>'

$(tabsHTML)
.find('li:first a')
.on('hide.bs.tab', function (e) {
e.preventDefault()
ok(true, 'hide event fired')
start()
})
.on('hidden.bs.tab', function () {
ok(false, 'hidden event fired')
})
.bootstrapTab('show')
.end()
.find('li:last a')
.bootstrapTab('show')
})

test('hide and hidden events contain correct relatedTarget', function () {
stop()

var tabsHTML = '<ul class="tabs">'
+ '<li><a href="#home">Home</a></li>'
+ '<li><a href="#profile">Profile</a></li>'
+ '</ul>'

$(tabsHTML)
.find('li:first a')
.on('hide.bs.tab', function (e) {
equal(e.relatedTarget.hash, '#profile', 'references correct element as relatedTarget')
})
.on('hidden.bs.tab', function (e) {
equal(e.relatedTarget.hash, '#profile', 'references correct element as relatedTarget')
start()
})
.bootstrapTab('show')
.end()
.find('li:last a')
.bootstrapTab('show')
})

})