Skip to content

fix(link): support handling multiple click event listeners (fixes #2938) #2943

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

Merged
merged 6 commits into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/components/link/fixtures/link.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
disabled>Link</b-link>

<b-btn ref="href"
@click="handleBtnHrefClick"
:href="href">Link</b-btn>

<b-btn ref="to_string"
Expand Down
5 changes: 5 additions & 0 deletions src/components/link/fixtures/link.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const clickSpy = jest.fn()
const disabledClickSpy = jest.fn()
const btnHrefClick = jest.fn()

window.app = new Vue({
el: '#app',
data: {
href: 'https://bootstrap-vue.github.io/',
clickSpy,
disabledClickSpy,
btnHrefClick,
testData: {}
},
methods: {
Expand All @@ -16,6 +18,9 @@ window.app = new Vue({
handleDisabledClick(e) {
this.testData.disabled_event = e
disabledClickSpy.apply(this, arguments)
},
handleBtnHrefClick(e) {
btnHrefClick.apply(this, arguments)
}
}
})
10 changes: 7 additions & 3 deletions src/components/link/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,20 @@ function clickHandlerFactory({ disabled, tag, href, suppliedHandler, parent }) {
// Stop event from bubbling up.
evt.stopPropagation()
// Kill the event loop attached to this specific EventTarget.
// Needed to prevent vue-router for doing its thing
evt.stopImmediatePropagation()
} else {
if (isRouterLink(tag) && evt.target.__vue__) {
// Router links do not emit instance 'click' events, so we
// add in an $emit('click', evt) on it's vue instance
evt.target.__vue__.$emit('click', evt)
}
if (typeof suppliedHandler === 'function') {
suppliedHandler(...arguments)
}
// Call the suppliedHanlder(s), if any provided
concat(suppliedHandler)
.filter(h => typeof h === 'function')
.forEach(handler => {
handler(...arguments)
})
parent.$root.$emit('clicked::link', evt)
}

Expand Down
7 changes: 7 additions & 0 deletions src/components/link/link.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ describe('link', () => {
expect(firstCallArguments[0]).toBeInstanceOf(Event)
})

it('btn with href should invoke click handler when clicked on', async () => {
// https://github.com/bootstrap-vue/bootstrap-vue/issues/2938
const { app } = window
app.$refs.href.click()
expect(app.btnHrefClick).toHaveBeenCalled()
})

it("should emit 'clicked::link' on $root when clicked on", async () => {
const { app } = window
const spy = jest.fn()
Expand Down