-
title1
-
title2
+
title2
+
title3
+
-
title3
-
-
`;
diff --git a/packages/tab/test/index.spec.js b/packages/tab/test/index.spec.js
index ad8c8273c..39e2f60ba 100644
--- a/packages/tab/test/index.spec.js
+++ b/packages/tab/test/index.spec.js
@@ -19,17 +19,13 @@ function createWrapper(options = {}) {
@change="onChange"
>
${options.extraTemplate || ''}
-
Text
-
- title2
- Text
-
+
Text
+
Text
Text
`,
data() {
return {
- title1: 'title1',
color: '#f44',
type: 'line',
swipeable: true,
@@ -96,8 +92,7 @@ test('change tabs data', async () => {
swipeable: false,
sticky: false,
type: 'card',
- color: 'blue',
- title1: 'new title1'
+ color: 'blue'
});
await later();
diff --git a/packages/tabs/Title.js b/packages/tabs/Title.js
new file mode 100644
index 000000000..7862ad64e
--- /dev/null
+++ b/packages/tabs/Title.js
@@ -0,0 +1,82 @@
+import { use } from '../utils';
+
+const bem = use('tab')[1];
+
+export default {
+ props: {
+ type: String,
+ color: String,
+ title: String,
+ active: Boolean,
+ ellipsis: Boolean,
+ disabled: Boolean,
+ scrollable: Boolean,
+ activeColor: String,
+ inactiveColor: String,
+ swipeThreshold: Number
+ },
+
+ computed: {
+ style() {
+ const style = {};
+ const { color, active } = this;
+ const isCard = this.type === 'card';
+
+ // card theme color
+ if (color && isCard) {
+ style.borderColor = color;
+
+ if (!this.disabled) {
+ if (active) {
+ style.backgroundColor = color;
+ } else {
+ style.color = color;
+ }
+ }
+ }
+
+ const titleColor = active ? this.activeColor : this.inactiveColor;
+ if (titleColor) {
+ style.color = titleColor;
+ }
+
+ if (this.scrollable && this.ellipsis) {
+ style.flexBasis = `${88 / this.swipeThreshold}%`;
+ }
+
+ return style;
+ }
+ },
+
+ methods: {
+ onClick() {
+ this.$emit('click');
+ },
+
+ renderTitle(el) {
+ const { title } = this.$refs;
+ title.innerHTML = '';
+ title.appendChild(el);
+ }
+ },
+
+ render(h) {
+ return (
+
+
+ {this.title}
+
+
+ );
+ }
+};
diff --git a/packages/tabs/index.js b/packages/tabs/index.js
index 5b448b17c..49318fe10 100644
--- a/packages/tabs/index.js
+++ b/packages/tabs/index.js
@@ -10,9 +10,9 @@ import {
getElementTop,
getScrollEventTarget
} from '../utils/dom/scroll';
+import Title from './Title';
const [sfc, bem] = use('tabs');
-const tabBem = use('tab')[1];
export default sfc({
mixins: [
@@ -225,16 +225,16 @@ export default sfc({
const shouldAnimate = this.inited;
this.$nextTick(() => {
- const { tabs } = this.$refs;
+ const { titles } = this.$refs;
- if (!tabs || !tabs[this.curActive] || this.type !== 'line') {
+ if (!titles || !titles[this.curActive] || this.type !== 'line') {
return;
}
- const tab = tabs[this.curActive];
+ const title = titles[this.curActive].$el;
const { lineWidth, lineHeight } = this;
- const width = isDef(lineWidth) ? lineWidth : tab.offsetWidth / 2;
- const left = tab.offsetLeft + tab.offsetWidth / 2;
+ const width = isDef(lineWidth) ? lineWidth : title.offsetWidth / 2;
+ const left = title.offsetLeft + title.offsetWidth / 2;
const lineStyle = {
width: suffixPx(width),
@@ -301,15 +301,15 @@ export default sfc({
// scroll active tab into view
scrollIntoView(immediate) {
- const { tabs } = this.$refs;
+ const { titles } = this.$refs;
- if (!this.scrollable || !tabs || !tabs[this.curActive]) {
+ if (!this.scrollable || !titles || !titles[this.curActive]) {
return;
}
const { nav } = this.$refs;
- const active = tabs[this.curActive];
- const to = active.offsetLeft - (nav.offsetWidth - active.offsetWidth) / 2;
+ const title = titles[this.curActive].$el;
+ const to = title.offsetLeft - (nav.offsetWidth - title.offsetWidth) / 2;
scrollLeftTo(nav, to, immediate ? 0 : this.duration);
},
@@ -317,67 +317,32 @@ export default sfc({
// render title slot of child tab
renderTitle(el, index) {
this.$nextTick(() => {
- const title = this.$refs.title[index];
- title.innerHTML = '';
- title.appendChild(el);
+ this.$refs.titles[index].renderTitle(el);
});
- },
-
- getTabStyle(item, index) {
- const style = {};
- const { color } = this;
- const active = index === this.curActive;
- const isCard = this.type === 'card';
-
- // theme color
- if (color) {
- if (!item.disabled && isCard && !active) {
- style.color = color;
- }
- if (!item.disabled && isCard && active) {
- style.backgroundColor = color;
- }
- if (isCard) {
- style.borderColor = color;
- }
- }
-
- const titleColor = active ? this.titleActiveColor : this.titleInactiveColor;
- if (titleColor) {
- style.color = titleColor;
- }
-
- if (this.scrollable && this.ellipsis) {
- style.flexBasis = 88 / this.swipeThreshold + '%';
- }
-
- return style;
}
},
render(h) {
const { type, ellipsis, animated, scrollable } = this;
- const Nav = this.children.map((tab, index) => (
-
(
+
{
this.onClick(index);
}}
- >
-
- {tab.title}
-
-
+ />
));
let contentListeners;
@@ -402,8 +367,8 @@ export default sfc({
>
{this.slots('nav-left')}
- {type === 'line' &&
}
{Nav}
+ {type === 'line' &&
}
{this.slots('nav-right')}