Skip to content

Commit fae3d25

Browse files
authored
fix(modal): use safeId() when comparing id received by hide/show handler (closes bootstrap-vue#3389 (bootstrap-vue#3394)
1 parent 004963d commit fae3d25

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

src/components/modal/modal.js

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,28 @@ export default Vue.extend({
361361
}
362362
},
363363
methods: {
364+
// Private method to update the v-model
364365
updateModel(val) {
365366
if (val !== this.visible) {
366367
this.$emit('change', val)
367368
}
368369
},
369-
// Public Methods
370+
// Private method to create a BvModalEvent object
371+
buildEvent(type, opts = {}) {
372+
return new BvModalEvent(type, {
373+
// Default options
374+
cancelable: false,
375+
target: this.$refs.modal || this.$el || null,
376+
relatedTarget: null,
377+
trigger: null,
378+
// Supplied options
379+
...opts,
380+
// Options that can't be overridden
381+
vueTarget: this,
382+
componentId: this.safeId()
383+
})
384+
},
385+
// Public method to show modal
370386
show() {
371387
if (this.isVisible || this.isOpening) {
372388
// If already open, on in the process of opening, do nothing
@@ -383,12 +399,8 @@ export default Vue.extend({
383399
this.isOpening = true
384400
// Set the element to return focus to when closed
385401
this.return_focus = this.return_focus || this.getActiveElement()
386-
const showEvt = new BvModalEvent('show', {
387-
cancelable: true,
388-
vueTarget: this,
389-
target: this.$refs.modal,
390-
relatedTarget: null,
391-
componentId: this.safeId()
402+
const showEvt = this.buildEvent('show', {
403+
cancelable: true
392404
})
393405
this.emitEvent(showEvt)
394406
// Don't show if canceled
@@ -401,18 +413,15 @@ export default Vue.extend({
401413
// Show the modal
402414
this.doShow()
403415
},
416+
// Public method to hide modal
404417
hide(trigger = '') {
405418
if (!this.isVisible || this.isClosing) {
406419
/* istanbul ignore next */
407420
return
408421
}
409422
this.isClosing = true
410-
const hideEvt = new BvModalEvent('hide', {
423+
const hideEvt = this.buildEvent('hide', {
411424
cancelable: trigger !== 'FORCE',
412-
vueTarget: this,
413-
target: this.$refs.modal,
414-
relatedTarget: null,
415-
componentId: this.safeId(),
416425
trigger: trigger || null
417426
})
418427
// We emit specific event for one of the three built-in buttons
@@ -513,14 +522,7 @@ export default Vue.extend({
513522
this.isShow = true
514523
this.isTransitioning = false
515524
this.$nextTick(() => {
516-
const shownEvt = new BvModalEvent('shown', {
517-
cancelable: false,
518-
vueTarget: this,
519-
target: this.$refs.modal,
520-
relatedTarget: null,
521-
componentId: this.safeId()
522-
})
523-
this.emitEvent(shownEvt)
525+
this.emitEvent(this.buildEvent('shown'))
524526
this.focusFirst()
525527
this.setEnforceFocus(true)
526528
})
@@ -546,14 +548,7 @@ export default Vue.extend({
546548
modalManager.unregisterModal(this)
547549
// TODO: Need to find a way to pass the `trigger` property
548550
// to the `hidden` event, not just only the `hide` event
549-
const hiddenEvt = new BvModalEvent('hidden', {
550-
cancelable: false,
551-
vueTarget: this,
552-
target: this.$el,
553-
relatedTarget: null,
554-
componentId: this.safeId()
555-
})
556-
this.emitEvent(hiddenEvt)
551+
this.emitEvent(this.buildEvent('hidden'))
557552
})
558553
},
559554
// Event emitter
@@ -639,18 +634,18 @@ export default Vue.extend({
639634
},
640635
// Root listener handlers
641636
showHandler(id, triggerEl) {
642-
if (id === this.id) {
637+
if (id === this.safeId()) {
643638
this.return_focus = triggerEl || this.getActiveElement()
644639
this.show()
645640
}
646641
},
647642
hideHandler(id) {
648-
if (id === this.id) {
643+
if (id === this.safeId()) {
649644
this.hide('event')
650645
}
651646
},
652647
toggleHandler(id, triggerEl) {
653-
if (id === this.id) {
648+
if (id === this.safeId()) {
654649
this.toggle(triggerEl)
655650
}
656651
},
@@ -662,8 +657,6 @@ export default Vue.extend({
662657
},
663658
// Focus control handlers
664659
focusFirst() {
665-
// TODO: Add support for finding input element with 'autofocus'
666-
// attribute set and focus that element
667660
// Don't try and focus if we are SSR
668661
if (isBrowser) {
669662
const modal = this.$refs.modal

0 commit comments

Comments
 (0)