diff --git a/packages/tab/test/__snapshots__/index.spec.js.snap b/packages/tab/test/__snapshots__/index.spec.js.snap index 97884f61d..b3f0cb330 100644 --- a/packages/tab/test/__snapshots__/index.spec.js.snap +++ b/packages/tab/test/__snapshots__/index.spec.js.snap @@ -57,11 +57,11 @@ exports[`change tabs data 2`] = ` exports[`click to switch tab 1`] = `
-
+
-
+
@@ -79,11 +79,11 @@ exports[`click to switch tab 1`] = ` exports[`click to switch tab 2`] = `
-
+
-
+
@@ -160,11 +160,11 @@ exports[`render nav-left & nav-right slot 1`] = ` exports[`swipe to switch tab 1`] = `
-
+
-
+
@@ -182,11 +182,11 @@ exports[`swipe to switch tab 1`] = ` exports[`swipe to switch tab 2`] = `
-
+
-
+
@@ -202,11 +202,11 @@ exports[`swipe to switch tab 2`] = ` exports[`swipe to switch tab 3`] = `
-
+
-
+
@@ -218,23 +218,3 @@ exports[`swipe to switch tab 3`] = `
`; - -exports[`swipe to switch tab 4`] = ` -
-
-
- - - -
-
-
-
-
Text
- - -
-
-`; diff --git a/packages/tab/test/index.spec.js b/packages/tab/test/index.spec.js index 39e2f60ba..d72138a69 100644 --- a/packages/tab/test/index.spec.js +++ b/packages/tab/test/index.spec.js @@ -13,7 +13,6 @@ function createWrapper(options = {}) { :color="color" :type="type" :sticky="sticky" - :swipeable="swipeable" :line-width="lineWidth" :lazy-render="lazyRender" @change="onChange" @@ -28,7 +27,6 @@ function createWrapper(options = {}) { return { color: '#f44', type: 'line', - swipeable: true, sticky: true, lineWidth: 2, lazyRender: true @@ -40,7 +38,14 @@ function createWrapper(options = {}) { test('click to switch tab', async () => { const onChange = jest.fn(); - const wrapper = createWrapper({ + const wrapper = mount({ + template: ` + + Text + Text + Text + + `, methods: { onChange } @@ -59,7 +64,14 @@ test('click to switch tab', async () => { test('swipe to switch tab', async () => { const onChange = jest.fn(); - const wrapper = createWrapper({ + const wrapper = mount({ + template: ` + + Text + Text + Text + + `, methods: { onChange } @@ -68,13 +80,14 @@ test('swipe to switch tab', async () => { const content = wrapper.find('.van-tabs__content'); await later(); expect(wrapper).toMatchSnapshot(); + triggerDrag(content, -100, 0); expect(wrapper).toMatchSnapshot(); expect(onChange).toHaveBeenCalledTimes(1); + expect(onChange).toHaveBeenCalledWith(1, 'title2'); + triggerDrag(content, -100, 0); - expect(wrapper).toMatchSnapshot(); - triggerDrag(content, 100, 0); - triggerDrag(content, 100, 0); + expect(onChange).toHaveBeenCalledTimes(1); expect(wrapper).toMatchSnapshot(); await later(); diff --git a/packages/tabs/Content.js b/packages/tabs/Content.js new file mode 100644 index 000000000..f859ee5f3 --- /dev/null +++ b/packages/tabs/Content.js @@ -0,0 +1,80 @@ +import { use } from '../utils'; +import { TouchMixin } from '../mixins/touch'; + +const [sfc, bem] = use('tabs'); +const MIN_SWIPE_DISTANCE = 50; + +export default sfc({ + mixins: [TouchMixin], + + props: { + count: Number, + active: Number, + duration: Number, + animated: Boolean, + swipeable: Boolean + }, + + computed: { + style() { + if (this.animated) { + return { + transform: `translate3d(${-1 * this.active * 100}%, 0, 0)`, + transitionDuration: `${this.duration}s` + }; + } + }, + + listeners() { + if (this.swipeable) { + return { + touchstart: this.touchStart, + touchmove: this.touchMove, + touchend: this.onTouchEnd, + touchcancel: this.onTouchEnd + }; + } + } + }, + + methods: { + // watch swipe touch end + onTouchEnd() { + const { direction, deltaX, active } = this; + + /* istanbul ignore else */ + if (direction === 'horizontal' && this.offsetX >= MIN_SWIPE_DISTANCE) { + console.log('on touchend', active, deltaX); + /* istanbul ignore else */ + if (deltaX > 0 && active !== 0) { + this.$emit('change', active - 1); + } else if (deltaX < 0 && active !== this.count - 1) { + this.$emit('change', active + 1); + } + } + }, + + renderChildren() { + if (this.animated) { + return ( +
+ {this.slots()} +
+ ); + } + + return this.slots(); + } + }, + + render(h) { + return ( +
+ {this.renderChildren()} +
+ ); + } +}); diff --git a/packages/tabs/index.js b/packages/tabs/index.js index 49318fe10..36885d946 100644 --- a/packages/tabs/index.js +++ b/packages/tabs/index.js @@ -1,7 +1,6 @@ import { use, isDef, suffixPx } from '../utils'; import { scrollLeftTo } from './utils'; import { on, off } from '../utils/dom/event'; -import { TouchMixin } from '../mixins/touch'; import { ParentMixin } from '../mixins/relation'; import { BindEventMixin } from '../mixins/bind-event'; import { @@ -11,12 +10,12 @@ import { getScrollEventTarget } from '../utils/dom/scroll'; import Title from './Title'; +import Content from './Content'; const [sfc, bem] = use('tabs'); export default sfc({ mixins: [ - TouchMixin, ParentMixin('vanTabs'), BindEventMixin(function (bind, isBind) { this.bindScrollEvent(isBind); @@ -109,15 +108,6 @@ export default sfc({ borderColor: this.color, background: this.background }; - }, - - trackStyle() { - if (this.animated) { - return { - transform: `translate3d(${-1 * this.curActive * 100}%, 0, 0)`, - transitionDuration: `${this.duration}s` - }; - } } }, @@ -181,22 +171,6 @@ export default sfc({ } }, - // watch swipe touch end - onTouchEnd() { - const { direction, deltaX, curActive } = this; - const minSwipeDistance = 50; - - /* istanbul ignore else */ - if (direction === 'horizontal' && this.offsetX >= minSwipeDistance) { - /* istanbul ignore else */ - if (deltaX > 0 && curActive !== 0) { - this.setCurActive(curActive - 1); - } else if (deltaX < 0 && curActive !== this.children.length - 1) { - this.setCurActive(curActive + 1); - } - } - }, - // adjust tab position onScroll() { const scrollTop = getScrollTop(window) + this.offsetTop; @@ -272,6 +246,7 @@ export default sfc({ if (this.curActive !== null) { this.$emit('change', active, this.children[active].title); } + this.curActive = active; } }, @@ -345,16 +320,6 @@ export default sfc({ /> )); - let contentListeners; - if (this.swipeable) { - contentListeners = { - touchstart: this.touchStart, - touchmove: this.touchMove, - touchend: this.onTouchEnd, - touchcancel: this.onTouchEnd - }; - } - return (
-
- {animated ? ( -
- {this.slots()} -
- ) : ( - this.slots() - )} -
+ + {this.slots()} +
); }