Skip to content

Commit 130f8ff

Browse files
authored
fix(tabs): add detection of when registered tabs change order (#3513)
1 parent ccba87f commit 130f8ff

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/components/tabs/tabs.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Vue from '../../utils/vue'
22
import KeyCodes from '../../utils/key-codes'
3+
import observeDom from '../../utils/observe-dom'
34
import stableSort from '../../utils/stable-sort'
45
import { requestAF, selectAll } from '../../utils/dom'
56
import { arrayIncludes, concat } from '../../utils/array'
@@ -302,11 +303,14 @@ export const BTabs = /*#__PURE__*/ Vue.extend({
302303
this.updateTabs()
303304
})
304305
}
306+
// Enable or disable the observer
307+
this.setObserver(newVal)
305308
}
306309
},
307310
created() {
308311
let tabIdx = parseInt(this.value, 10)
309312
this.currentTab = isNaN(tabIdx) ? -1 : tabIdx
313+
this._bvObserver = null
310314
// For SSR and to make sure only a single tab is shown on mount
311315
// We wrap this in a `$nextTick()` to ensure the child tabs have been created
312316
this.$nextTick(() => {
@@ -335,6 +339,9 @@ export const BTabs = /*#__PURE__*/ Vue.extend({
335339
this.isMounted = true
336340
})
337341
},
342+
beforeDestroy() {
343+
this.isMounted = false
344+
},
338345
destroyed() {
339346
// Ensure no references to child instances exist
340347
this.tabs = []
@@ -351,6 +358,24 @@ export const BTabs = /*#__PURE__*/ Vue.extend({
351358
unregisterTab(tab) {
352359
this.registeredTabs = this.registeredTabs.slice().filter(t => t !== tab)
353360
},
361+
setObserver(on) {
362+
// DOM observer is needed to detect changes in order of tabs
363+
if (on) {
364+
// Make sure no existing observer running
365+
this.setObserver(false)
366+
// Watch for changes to <b-tab> sub components
367+
this._bvObserver = observeDom(this.$refs.tabsContainer, this.updateTabs.bind(this), {
368+
childList: true,
369+
subtree: false,
370+
attributes: false
371+
})
372+
} else {
373+
if (this._bvObserver && this._bvObserver.disconnect) {
374+
this._bvObserver.disconnect()
375+
}
376+
this._bvObserver = null
377+
}
378+
},
354379
getTabs() {
355380
// We use registeredTabs as the shouce of truth for child tab components. And we
356381
// filter out any BTab components that are extended BTab with a root child BTab.

0 commit comments

Comments
 (0)