|
@@ -4,69 +4,84 @@
|
|
|
|
|
|
props: {
|
|
|
type: String,
|
|
|
- tabPosition: String,
|
|
|
activeName: String,
|
|
|
- closable: false,
|
|
|
- tabWidth: 0
|
|
|
+ closable: {
|
|
|
+ type: Boolean,
|
|
|
+ default: false
|
|
|
+ },
|
|
|
+ value: {}
|
|
|
},
|
|
|
|
|
|
data() {
|
|
|
return {
|
|
|
children: null,
|
|
|
- activeTab: null,
|
|
|
- currentName: 0,
|
|
|
- panes: []
|
|
|
+ currentName: this.value || this.activeName
|
|
|
};
|
|
|
},
|
|
|
|
|
|
watch: {
|
|
|
- activeName: {
|
|
|
- handler(val) {
|
|
|
- this.currentName = val;
|
|
|
- }
|
|
|
+ activeName(val) {
|
|
|
+ this.currentName = val;
|
|
|
+ },
|
|
|
+ value(val) {
|
|
|
+ this.currentName = val;
|
|
|
+ },
|
|
|
+ currentName(val) {
|
|
|
+ this.$emit('input', val);
|
|
|
}
|
|
|
},
|
|
|
|
|
|
methods: {
|
|
|
handleTabRemove(tab, event) {
|
|
|
event.stopPropagation();
|
|
|
- let tabs = this.$children;
|
|
|
+ const tabs = this.$children;
|
|
|
|
|
|
- var index = tabs.indexOf(tab);
|
|
|
- tab.$destroy(true);
|
|
|
-
|
|
|
- if (tab.index === this.currentName) {
|
|
|
- let nextChild = tabs[index];
|
|
|
- let prevChild = tabs[index - 1];
|
|
|
-
|
|
|
- while (prevChild && prevChild.disabled) {
|
|
|
- prevChild = tabs[tabs.indexOf(prevChild) - 1];
|
|
|
- }
|
|
|
+ let index = tabs.indexOf(tab);
|
|
|
+ tab.$destroy();
|
|
|
|
|
|
- this.currentName = nextChild
|
|
|
- ? nextChild.index
|
|
|
- : prevChild
|
|
|
- ? prevChild.index
|
|
|
- : '-1';
|
|
|
- }
|
|
|
this.$emit('tab-remove', tab);
|
|
|
this.$forceUpdate();
|
|
|
+
|
|
|
+ this.$nextTick(_ => {
|
|
|
+ if (tab.active) {
|
|
|
+ let nextChild = tabs[index];
|
|
|
+ let prevChild = tabs[index - 1];
|
|
|
+ let nextActiveTab = nextChild || prevChild || null;
|
|
|
+
|
|
|
+ if (nextActiveTab) {
|
|
|
+ this.currentName = nextActiveTab.name || nextActiveTab.index;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
},
|
|
|
- handleTabClick(tab, event) {
|
|
|
+ handleTabClick(tab, tabName, event) {
|
|
|
if (tab.disabled) return;
|
|
|
- this.currentName = tab.index;
|
|
|
+ this.currentName = tabName;
|
|
|
this.$emit('tab-click', tab, event);
|
|
|
- },
|
|
|
- calcBarStyle() {
|
|
|
+ }
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ this.$forceUpdate();
|
|
|
+ },
|
|
|
+ render(h) {
|
|
|
+ let {
|
|
|
+ type,
|
|
|
+ handleTabRemove,
|
|
|
+ handleTabClick,
|
|
|
+ currentName
|
|
|
+ } = this;
|
|
|
+
|
|
|
+ const getBarStyle = () => {
|
|
|
if (this.type || !this.$refs.tabs) return {};
|
|
|
- var style = {};
|
|
|
- var offset = 0;
|
|
|
- var tabWidth = 0;
|
|
|
+ let style = {};
|
|
|
+ let offset = 0;
|
|
|
+ let tabWidth = 0;
|
|
|
|
|
|
- this.$children.every((panel, index) => {
|
|
|
+ this.$children.every((tab, index) => {
|
|
|
let $el = this.$refs.tabs[index];
|
|
|
if (!$el) { return false; }
|
|
|
- if (panel.index !== this.currentName) {
|
|
|
+
|
|
|
+ if (!tab.active) {
|
|
|
offset += $el.clientWidth;
|
|
|
return true;
|
|
|
} else {
|
|
@@ -79,51 +94,45 @@
|
|
|
style.transform = `translateX(${offset}px)`;
|
|
|
|
|
|
return style;
|
|
|
- }
|
|
|
- },
|
|
|
- mounted() {
|
|
|
- this.currentName = this.activeName || this.$children[0] && this.$children[0].index || '1';
|
|
|
- this.$nextTick(() => {
|
|
|
- this.$forceUpdate();
|
|
|
- });
|
|
|
- },
|
|
|
- render(h) {
|
|
|
- let {
|
|
|
- type,
|
|
|
- panes, // eslint-disable-line
|
|
|
- handleTabRemove,
|
|
|
- handleTabClick,
|
|
|
- currentName
|
|
|
- } = this;
|
|
|
-
|
|
|
- const barStyle = this.calcBarStyle();
|
|
|
- const activeBar = !type
|
|
|
- ? <div class="el-tabs__active-bar" style={barStyle}></div>
|
|
|
- : null;
|
|
|
+ };
|
|
|
|
|
|
const tabs = this.$children.map((tab, index) => {
|
|
|
- let btnClose = h('span', {
|
|
|
- class: {
|
|
|
- 'el-icon-close': true
|
|
|
- },
|
|
|
- on: { click: (ev) => { handleTabRemove(tab, ev); } }
|
|
|
- });
|
|
|
- const _tab = h('div', {
|
|
|
- class: {
|
|
|
- 'el-tabs__item': true,
|
|
|
- 'is-active': currentName === tab.index,
|
|
|
- 'is-disabled': tab.disabled,
|
|
|
- 'is-closable': tab.isClosable
|
|
|
- },
|
|
|
- ref: 'tabs',
|
|
|
- refInFor: true,
|
|
|
- on: { click: (ev) => { handleTabClick(tab, ev); } }
|
|
|
- }, [
|
|
|
- tab.labelContent ? tab.labelContent.call(this._renderProxy, h, tab) : tab.label,
|
|
|
- tab.isClosable ? btnClose : null,
|
|
|
- index === 0 ? activeBar : null
|
|
|
- ]);
|
|
|
- return _tab;
|
|
|
+ let tabName = tab.name || tab.index || index;
|
|
|
+ if (currentName === undefined && index === 0) {
|
|
|
+ this.currentName = tabName;
|
|
|
+ }
|
|
|
+
|
|
|
+ tab.index = index;
|
|
|
+
|
|
|
+ const activeBar = !type && index === 0
|
|
|
+ ? <div class="el-tabs__active-bar" style={getBarStyle()}></div>
|
|
|
+ : null;
|
|
|
+
|
|
|
+ const btnClose = tab.isClosable
|
|
|
+ ? <span class="el-icon-close" on-click={(ev) => { handleTabRemove(tab, ev); }}></span>
|
|
|
+ : null;
|
|
|
+
|
|
|
+ const tabLabelContent = tab.labelContent
|
|
|
+ ? tab.labelContent.call(this._renderProxy, h, tab)
|
|
|
+ : tab.label;
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div
|
|
|
+ class={{
|
|
|
+ 'el-tabs__item': true,
|
|
|
+ 'is-active': tab.active,
|
|
|
+ 'is-disabled': tab.disabled,
|
|
|
+ 'is-closable': tab.isClosable
|
|
|
+ }}
|
|
|
+ ref="tabs"
|
|
|
+ refInFor
|
|
|
+ on-click={(ev) => { handleTabClick(tab, tabName, ev); }}
|
|
|
+ >
|
|
|
+ {tabLabelContent}
|
|
|
+ {btnClose}
|
|
|
+ {activeBar}
|
|
|
+ </div>
|
|
|
+ );
|
|
|
});
|
|
|
return (
|
|
|
<div class={{
|