Skip to content

Commit 29a10d4

Browse files
committed
Possible solution for spatie#32
- Store the last tab name - If the tab name is the same, emit "clicked" and return. - Updated readme, added the events. Breaking: - If a user clicks the tab that is already active, it will no longer emit "changed" Suggested solution (already in place): - Emit "clicked" when an active tab is clicked, emit "changed" when the tab actually changed. - "clicked" can be used to update the content of the tab.
1 parent 88b969c commit 29a10d4

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `vue-tabs-component` will be documented in this file
44

5+
## 1.5.0 - 2018-06-06
6+
- Fixed bug #32 `changed` event fires twice on each change
7+
- Added `clicked` event, fires when active tab is clicked
8+
59
## 1.4.0 - 2017-11-06
610
- Added `isDisabled` prop to `Tab`
711

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# A Vue component to easily render tabs
1+
# A Vue component to easily render tabs
22

33
[![Latest Version on NPM](https://img.shields.io/npm/v/vue-tabs-component.svg?style=flat-square)](https://npmjs.com/package/vue-tabs-component)
44
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
@@ -8,10 +8,10 @@
88
The package contains a [Vue](https://vuejs.org/) component to easily display some tabs.
99

1010
This is how they can be used:
11-
11+
=
1212
```html
1313
<div>
14-
<tabs>
14+
<tabs :options="{ useUrlFragment: false }" @clicked="tabClicked" @changed="tabChanged">
1515
<tab name="First tab">
1616
This is the content of the first tab
1717
</tab>
@@ -122,6 +122,34 @@ When using with other libraries that use the url fragment, you can disable modif
122122
</tabs>
123123
```
124124

125+
### Callbacks
126+
Tabs has two events to which you can bind: `changed` and `clicked`
127+
128+
```html
129+
<tabs @clicked="tabClicked" @changed="tabChanged">
130+
...
131+
</tabs>
132+
```
133+
134+
```js
135+
export default {
136+
...
137+
methods: {
138+
...
139+
tabClicked (selectedTab) {
140+
console.log('Current tab re-clicked:' + selectedTab.tab.name);
141+
},
142+
tabChanged (selectedTab) {
143+
console.log('Tab changed to:' + selectedTab.tab.name);
144+
},
145+
...
146+
}
147+
}
148+
```
149+
150+
`changed` is emitted when the tab changes and can be used as handle to load data on request.
151+
`clicked` is emitted when an active tab is re-clicked and can be used to e.g. reload the data in the current tab.
152+
125153
### Adding a suffix and a prefix to the tab name
126154

127155
You can add a suffix and a prefix to the tab by using the `suffix` and `prefix` attributes.

src/components/Tabs.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
data: () => ({
4646
tabs: [],
4747
activeTabHash: '',
48+
lastActiveTabHash: '',
4849
}),
4950
5051
computed: {
@@ -98,13 +99,18 @@
9899
return;
99100
}
100101
102+
if (this.lastActiveTabHash === selectedTab.hash) {
103+
this.$emit('clicked', { tab: selectedTab });
104+
return;
105+
}
106+
101107
this.tabs.forEach(tab => {
102108
tab.isActive = (tab.hash === selectedTab.hash);
103109
});
104110
105111
this.$emit('changed', { tab: selectedTab });
106112
107-
this.activeTabHash = selectedTab.hash;
113+
this.lastActiveTabHash = this.activeTabHash = selectedTab.hash;
108114
109115
expiringStorage.set(this.storageKey, selectedTab.hash, this.cacheLifetime);
110116
},

0 commit comments

Comments
 (0)