Skip to content

Commit 97e8ece

Browse files
tmorehousejacobmllr95
authored andcommitted
fix(link): support handling multiple click event listeners (fixes #2938) (#2943)
* Update link.js * Update link.html * Update link.js * Update link.js * Update link.spec.js
1 parent b7f8879 commit 97e8ece

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

src/components/link/fixtures/link.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
disabled>Link</b-link>
2323

2424
<b-btn ref="href"
25+
@click="handleBtnHrefClick"
2526
:href="href">Link</b-btn>
2627

2728
<b-btn ref="to_string"

src/components/link/fixtures/link.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const clickSpy = jest.fn()
22
const disabledClickSpy = jest.fn()
3+
const btnHrefClick = jest.fn()
34

45
window.app = new Vue({
56
el: '#app',
67
data: {
78
href: 'https://bootstrap-vue.github.io/',
89
clickSpy,
910
disabledClickSpy,
11+
btnHrefClick,
1012
testData: {}
1113
},
1214
methods: {
@@ -16,6 +18,9 @@ window.app = new Vue({
1618
handleDisabledClick(e) {
1719
this.testData.disabled_event = e
1820
disabledClickSpy.apply(this, arguments)
21+
},
22+
handleBtnHrefClick(e) {
23+
btnHrefClick.apply(this, arguments)
1924
}
2025
}
2126
})

src/components/link/link.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,20 @@ function clickHandlerFactory({ disabled, tag, href, suppliedHandler, parent }) {
114114
// Stop event from bubbling up.
115115
evt.stopPropagation()
116116
// Kill the event loop attached to this specific EventTarget.
117+
// Needed to prevent vue-router for doing its thing
117118
evt.stopImmediatePropagation()
118119
} else {
119120
if (isRouterLink(tag) && evt.target.__vue__) {
120121
// Router links do not emit instance 'click' events, so we
121122
// add in an $emit('click', evt) on it's vue instance
122123
evt.target.__vue__.$emit('click', evt)
123124
}
124-
if (typeof suppliedHandler === 'function') {
125-
suppliedHandler(...arguments)
126-
}
125+
// Call the suppliedHanlder(s), if any provided
126+
concat(suppliedHandler)
127+
.filter(h => typeof h === 'function')
128+
.forEach(handler => {
129+
handler(...arguments)
130+
})
127131
parent.$root.$emit('clicked::link', evt)
128132
}
129133

src/components/link/link.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ describe('link', () => {
9090
expect(firstCallArguments[0]).toBeInstanceOf(Event)
9191
})
9292

93+
it('btn with href should invoke click handler when clicked on', async () => {
94+
// https://github.com/bootstrap-vue/bootstrap-vue/issues/2938
95+
const { app } = window
96+
app.$refs.href.click()
97+
expect(app.btnHrefClick).toHaveBeenCalled()
98+
})
99+
93100
it("should emit 'clicked::link' on $root when clicked on", async () => {
94101
const { app } = window
95102
const spy = jest.fn()

0 commit comments

Comments
 (0)