Skip to content

Commit 521325c

Browse files
committed
feat: CNav, CNavItem refactor:
- add possibility for disabled CNavItem item to be active, - remove side effects from CNav, provide active nav instead, - add posibility to use CNavItem without CNav - simplify CNav children activation mechanism
1 parent f185194 commit 521325c

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

src/components/nav/CNav.vue

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,36 @@ export default {
1717
vertical: Boolean,
1818
inCard: Boolean
1919
},
20+
provide () {
21+
const nav = {}
22+
Object.defineProperty(nav, 'active', {
23+
get: () => this.activeItemInstance
24+
})
25+
return { nav }
26+
},
27+
data () {
28+
return {
29+
activeItemInstance: null
30+
}
31+
},
2032
computed: {
2133
navClasses () {
2234
return {
2335
'nav': true,
24-
'nav-tabs': this.variant === 'tabs',
25-
'nav-pills': this.variant === 'pills',
36+
[`nav-${this.variant}`]: this.variant,
2637
'flex-column': this.vertical,
2738
'nav-fill': this.fill,
2839
'nav-justified': this.justified,
29-
'card-header-tabs': this.inCard && this.variant === 'tabs',
30-
'card-header-pills': this.inCard && this.variant === 'pills'
40+
[`card-header-${this.variant}`]: this.inCard && this.variant
3141
}
3242
}
3343
},
3444
methods: {
3545
onClick (e) {
36-
const clickedItem = this.getClickedItem(e)
37-
clickedItem ? this.activateItem(clickedItem) : null
38-
},
39-
getClickedItem (e) {
40-
return this.$children.filter(item => this.itemWasActivated(item, e))[0]
41-
},
42-
itemWasActivated (item, e) {
43-
return item.$el.contains(e.target) &&
44-
!item.disabled &&
45-
item.isActive !== undefined
46-
},
47-
activateItem (itemToActivate) {
48-
// Works on CNavItem, CTab, CDropdown and every component that have
49-
// 'isActive' data prop that determines active state
5046
this.$children.forEach(item => {
51-
return item.isActive = item === itemToActivate ? true : false
47+
if (item.$el.contains(e.target) && !item.disabled) {
48+
this.activeItemInstance = item
49+
}
5250
})
5351
}
5452
}

src/components/nav/CNavItem.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ export default {
1717
components: {
1818
CLink
1919
},
20+
inject: { nav: { default: {} }},
2021
props,
21-
data () {
22-
return {
23-
isActive: this.disabled ? null : this.active
22+
computed: {
23+
isActive () {
24+
return this.nav.active ? this.nav.active === this : this.active
2425
}
2526
}
2627
}

0 commit comments

Comments
 (0)