Skip to content
This repository was archived by the owner on Oct 24, 2018. It is now read-only.

Commit 3ece2ba

Browse files
Merge branch 'master' into 69-default-tab
2 parents 5022bbb + e2aad1e commit 3ece2ba

File tree

5 files changed

+75
-7
lines changed

5 files changed

+75
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

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

5-
## 1.5.0 - 2018-07-02
5+
## 1.5.0 - 2018-XX-XX
66
- Added `defaultTabHash` option
7+
- Fixed bug #32 `changed` event fires twice on each change
8+
- Added `clicked` event, fires when active tab is clicked
79

810
## 1.4.0 - 2017-11-06
911
- Added `isDisabled` prop to `Tab`

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.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-tabs-component",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "A Vue component to easily render tabs",
55
"main": "dist/index.js",
66
"jsnext:main": "src/index.js",
@@ -42,7 +42,7 @@
4242
"vue": "^2.3.0",
4343
"vue-loader": "^12.0.3",
4444
"vue-template-compiler": "^2.3.0",
45-
"webpack": "^2.3.3",
45+
"webpack": "^3.12.0",
4646
"webpack-merge": "^4.1.0"
4747
},
4848
"jest": {

src/components/Tabs.vue

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
data: () => ({
4747
tabs: [],
4848
activeTabHash: '',
49+
activeTabIndex: 0,
50+
lastActiveTabHash: '',
4951
}),
5052
5153
computed: {
@@ -104,13 +106,21 @@
104106
return;
105107
}
106108
109+
if (this.lastActiveTabHash === selectedTab.hash) {
110+
this.$emit('clicked', { tab: selectedTab });
111+
return;
112+
}
113+
107114
this.tabs.forEach(tab => {
108115
tab.isActive = (tab.hash === selectedTab.hash);
109116
});
110117
111118
this.$emit('changed', { tab: selectedTab });
112119
113120
this.activeTabHash = selectedTab.hash;
121+
this.activeTabIndex = this.getTabIndex(selectedTabHash);
122+
123+
this.lastActiveTabHash = this.activeTabHash = selectedTab.hash;
114124
115125
expiringStorage.set(this.storageKey, selectedTab.hash, this.cacheLifetime);
116126
},
@@ -139,6 +149,30 @@
139149
});
140150
}
141151
},
152+
153+
getTabIndex(hash){
154+
const tab = this.findTab(hash);
155+
156+
return this.tabs.indexOf(tab);
157+
},
158+
159+
getTabHash(index){
160+
const tab = this.tabs.find(tab => this.tabs.indexOf(tab) === index);
161+
162+
if (!tab) {
163+
return;
164+
}
165+
166+
return tab.hash;
167+
},
168+
169+
getActiveTab(){
170+
return this.findTab(this.activeTabHash);
171+
},
172+
173+
getActiveTabIndex() {
174+
return this.getTabIndex(this.activeTabHash);
175+
},
142176
},
143177
};
144178
</script>

webpack.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ module.exports = merge(require('./webpack.base'), {
99
output: {
1010
path: path.resolve(__dirname, 'dist'),
1111
filename: 'index.js',
12-
library: 'vue-tabs',
12+
library: {
13+
root: 'VueTabs',
14+
amd: 'vue-tabs',
15+
commonjs: 'vue-tabs'
16+
},
1317
libraryTarget: 'umd',
1418
},
1519

0 commit comments

Comments
 (0)