[new feature] IndexBar: add sticky prop (#3402)

This commit is contained in:
neverland 2019-05-30 11:50:09 +08:00 committed by GitHub
parent 74124246f6
commit 82134fe402
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 263 additions and 60 deletions

View File

@ -2,6 +2,10 @@
### [v2.0.0-beta.3](https://github.com/youzan/vant/tree/v2.0.0-beta.3) ### [v2.0.0-beta.3](https://github.com/youzan/vant/tree/v2.0.0-beta.3)
##### IndexBar
- 新增`sticky`参数
##### Rate ##### Rate
- 新增`gutter`属性 - 新增`gutter`属性

View File

@ -10,6 +10,32 @@ export default sfc({
index: [String, Number] index: [String, Number]
}, },
data() {
return {
top: 0,
active: false
};
},
computed: {
sticky() {
return this.active && this.parent.sticky;
},
anchorStyle() {
if (this.sticky) {
return {
top: `${this.top}px`,
zIndex: `${this.parent.zIndex}`
};
}
}
},
mounted() {
this.height = this.$el.offsetHeight;
},
methods: { methods: {
scrollIntoView() { scrollIntoView() {
this.$el.scrollIntoView(); this.$el.scrollIntoView();
@ -17,9 +43,16 @@ export default sfc({
}, },
render(h) { render(h) {
const { sticky } = this;
return ( return (
<div class={bem()}> <div style={{ height: sticky ? `${this.height}px` : null }}>
{this.slots('default') ? this.slots('default') : this.index} <div
style={this.anchorStyle}
class={[bem({ sticky }), { 'van-hairline--bottom': sticky }]}
>
{this.slots('default') || this.index}
</div>
</div> </div>
); );
} }

View File

@ -5,4 +5,15 @@
font-weight: @index-anchor-font-weight; font-weight: @index-anchor-font-weight;
font-size: @index-anchor-font-size; font-size: @index-anchor-font-size;
line-height: @index-anchor-line-height; line-height: @index-anchor-line-height;
background-color: transparent;
transition: background-color .2s;
&--sticky {
position: fixed;
top: 0;
right: 0;
left: 0;
z-index: 1;
background-color: #fff;
}
} }

View File

@ -64,6 +64,7 @@ export default {
|------|------|------|------| |------|------|------|------|
| index-list | Index List | `Array` | `A-Z` | | index-list | Index List | `Array` | `A-Z` |
| z-index | z-index | `Number` | `1` | | z-index | z-index | `Number` | `1` |
| sticky | Whether to enable anchor sticky top | `Boolean` | `true` |
### IndexAnchor Props ### IndexAnchor Props

View File

@ -1,6 +1,8 @@
import { use } from '../utils'; import { use } from '../utils';
import { TouchMixin } from '../mixins/touch'; import { TouchMixin } from '../mixins/touch';
import { ParentMixin } from '../mixins/relation'; import { ParentMixin } from '../mixins/relation';
import { on, off } from '../utils/event';
import { getScrollTop, getElementTop, getScrollEventTarget } from '../utils/scroll';
const [sfc, bem] = use('index-bar'); const [sfc, bem] = use('index-bar');
@ -8,6 +10,10 @@ export default sfc({
mixins: [TouchMixin, ParentMixin('vanIndexBar')], mixins: [TouchMixin, ParentMixin('vanIndexBar')],
props: { props: {
sticky: {
type: Boolean,
default: true
},
zIndex: { zIndex: {
type: Number, type: Number,
default: 1 default: 1
@ -27,13 +33,72 @@ export default sfc({
} }
}, },
mounted() {
this.scroller = getScrollEventTarget(this.$el);
this.handler(true);
},
destroyed() {
this.handler(false);
},
activated() {
this.handler(true);
},
deactivated() {
this.handler(false);
},
methods: { methods: {
onClick(event) { handler(bind) {
this.scrollToElement(event.target); /* istanbul ignore else */
if (this.binded !== bind) {
this.binded = bind;
(bind ? on : off)(this.scroller, 'scroll', this.onScroll);
}
}, },
onTouchStart(event) { onScroll() {
this.touchStart(event); if (!this.sticky) {
return;
}
const scrollTop = getScrollTop(this.scroller);
const rects = this.children.map(item => ({
height: item.height,
top: getElementTop(item.$el)
}));
const active = this.getActiveAnchorIndex(scrollTop, rects);
this.children.forEach((item, index) => {
if (index === active) {
item.active = true;
item.top = Math.max(0, rects[index].top - scrollTop);
} else if (index === active - 1) {
const activeItemTop = rects[active].top - scrollTop;
item.active = activeItemTop > 0;
item.top = activeItemTop - rects[active].height;
} else {
item.active = false;
}
});
},
getActiveAnchorIndex(scrollTop, rects) {
for (let i = this.children.length - 1; i >= 0; i--) {
const prevHeight = i > 0 ? rects[i - 1].height : 0;
if (scrollTop + prevHeight >= rects[i].top) {
return i;
}
}
return -1;
},
onClick(event) {
this.scrollToElement(event.target);
}, },
onTouchMove(event) { onTouchMove(event) {
@ -80,7 +145,7 @@ export default sfc({
class={bem('sidebar')} class={bem('sidebar')}
style={{ zIndex: this.zIndex }} style={{ zIndex: this.zIndex }}
onClick={this.onClick} onClick={this.onClick}
onTouchstart={this.onTouchStart} onTouchstart={this.touchStart}
onTouchmove={this.onTouchMove} onTouchmove={this.onTouchMove}
onTouchend={this.onTouchEnd} onTouchend={this.onTouchEnd}
onTouchcancel={this.onTouchEnd} onTouchcancel={this.onTouchEnd}

View File

@ -15,7 +15,9 @@ exports[`renders demo correctly 1`] = `
<div class="van-index-bar"> <div class="van-index-bar">
<div class="van-index-bar__sidebar" style="z-index: 1;"><span data-index="A" class="van-index-bar__index">A</span><span data-index="B" class="van-index-bar__index">B</span><span data-index="C" class="van-index-bar__index">C</span><span data-index="D" class="van-index-bar__index">D</span><span data-index="E" class="van-index-bar__index">E</span><span data-index="F" class="van-index-bar__index">F</span><span data-index="G" class="van-index-bar__index">G</span><span data-index="H" class="van-index-bar__index">H</span><span data-index="I" class="van-index-bar__index">I</span><span data-index="J" class="van-index-bar__index">J</span><span data-index="K" class="van-index-bar__index">K</span><span data-index="L" class="van-index-bar__index">L</span><span data-index="M" class="van-index-bar__index">M</span><span data-index="N" class="van-index-bar__index">N</span><span data-index="O" class="van-index-bar__index">O</span><span data-index="P" class="van-index-bar__index">P</span><span data-index="Q" class="van-index-bar__index">Q</span><span data-index="R" class="van-index-bar__index">R</span><span data-index="S" class="van-index-bar__index">S</span><span data-index="T" class="van-index-bar__index">T</span><span data-index="U" class="van-index-bar__index">U</span><span data-index="V" class="van-index-bar__index">V</span><span data-index="W" class="van-index-bar__index">W</span><span data-index="X" class="van-index-bar__index">X</span><span data-index="Y" class="van-index-bar__index">Y</span><span data-index="Z" class="van-index-bar__index">Z</span></div> <div class="van-index-bar__sidebar" style="z-index: 1;"><span data-index="A" class="van-index-bar__index">A</span><span data-index="B" class="van-index-bar__index">B</span><span data-index="C" class="van-index-bar__index">C</span><span data-index="D" class="van-index-bar__index">D</span><span data-index="E" class="van-index-bar__index">E</span><span data-index="F" class="van-index-bar__index">F</span><span data-index="G" class="van-index-bar__index">G</span><span data-index="H" class="van-index-bar__index">H</span><span data-index="I" class="van-index-bar__index">I</span><span data-index="J" class="van-index-bar__index">J</span><span data-index="K" class="van-index-bar__index">K</span><span data-index="L" class="van-index-bar__index">L</span><span data-index="M" class="van-index-bar__index">M</span><span data-index="N" class="van-index-bar__index">N</span><span data-index="O" class="van-index-bar__index">O</span><span data-index="P" class="van-index-bar__index">P</span><span data-index="Q" class="van-index-bar__index">Q</span><span data-index="R" class="van-index-bar__index">R</span><span data-index="S" class="van-index-bar__index">S</span><span data-index="T" class="van-index-bar__index">T</span><span data-index="U" class="van-index-bar__index">U</span><span data-index="V" class="van-index-bar__index">V</span><span data-index="W" class="van-index-bar__index">W</span><span data-index="X" class="van-index-bar__index">X</span><span data-index="Y" class="van-index-bar__index">Y</span><span data-index="Z" class="van-index-bar__index">Z</span></div>
<div> <div>
<div class="van-index-anchor">A</div> <div>
<div class="van-index-anchor">A</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -27,7 +29,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">B</div> <div>
<div class="van-index-anchor">B</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -39,7 +43,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">C</div> <div>
<div class="van-index-anchor">C</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -51,7 +57,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">D</div> <div>
<div class="van-index-anchor">D</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -63,7 +71,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">E</div> <div>
<div class="van-index-anchor">E</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -75,7 +85,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">F</div> <div>
<div class="van-index-anchor">F</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -87,7 +99,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">G</div> <div>
<div class="van-index-anchor">G</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -99,7 +113,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">H</div> <div>
<div class="van-index-anchor">H</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -111,7 +127,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">I</div> <div>
<div class="van-index-anchor">I</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -123,7 +141,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">J</div> <div>
<div class="van-index-anchor">J</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -135,7 +155,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">K</div> <div>
<div class="van-index-anchor">K</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -147,7 +169,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">L</div> <div>
<div class="van-index-anchor">L</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -159,7 +183,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">M</div> <div>
<div class="van-index-anchor">M</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -171,7 +197,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">N</div> <div>
<div class="van-index-anchor">N</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -183,7 +211,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">O</div> <div>
<div class="van-index-anchor">O</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -195,7 +225,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">P</div> <div>
<div class="van-index-anchor">P</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -207,7 +239,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">Q</div> <div>
<div class="van-index-anchor">Q</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -219,7 +253,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">R</div> <div>
<div class="van-index-anchor">R</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -231,7 +267,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">S</div> <div>
<div class="van-index-anchor">S</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -243,7 +281,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">T</div> <div>
<div class="van-index-anchor">T</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -255,7 +295,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">U</div> <div>
<div class="van-index-anchor">U</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -267,7 +309,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">V</div> <div>
<div class="van-index-anchor">V</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -279,7 +323,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">W</div> <div>
<div class="van-index-anchor">W</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -291,7 +337,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">X</div> <div>
<div class="van-index-anchor">X</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -303,7 +351,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">Y</div> <div>
<div class="van-index-anchor">Y</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>
@ -315,7 +365,9 @@ exports[`renders demo correctly 1`] = `
</div> </div>
</div> </div>
<div> <div>
<div class="van-index-anchor">Z</div> <div>
<div class="van-index-anchor">Z</div>
</div>
<div class="van-cell"> <div class="van-cell">
<div class="van-cell__title"><span>文本</span></div> <div class="van-cell__title"><span>文本</span></div>
</div> </div>

View File

@ -3,7 +3,11 @@
exports[`custom anchor text 1`] = ` exports[`custom anchor text 1`] = `
<div class="van-index-bar"> <div class="van-index-bar">
<div class="van-index-bar__sidebar" style="z-index: 1;"><span data-index="A" class="van-index-bar__index">A</span><span data-index="B" class="van-index-bar__index">B</span><span data-index="C" class="van-index-bar__index">C</span><span data-index="D" class="van-index-bar__index">D</span><span data-index="E" class="van-index-bar__index">E</span><span data-index="F" class="van-index-bar__index">F</span><span data-index="G" class="van-index-bar__index">G</span><span data-index="H" class="van-index-bar__index">H</span><span data-index="I" class="van-index-bar__index">I</span><span data-index="J" class="van-index-bar__index">J</span><span data-index="K" class="van-index-bar__index">K</span><span data-index="L" class="van-index-bar__index">L</span><span data-index="M" class="van-index-bar__index">M</span><span data-index="N" class="van-index-bar__index">N</span><span data-index="O" class="van-index-bar__index">O</span><span data-index="P" class="van-index-bar__index">P</span><span data-index="Q" class="van-index-bar__index">Q</span><span data-index="R" class="van-index-bar__index">R</span><span data-index="S" class="van-index-bar__index">S</span><span data-index="T" class="van-index-bar__index">T</span><span data-index="U" class="van-index-bar__index">U</span><span data-index="V" class="van-index-bar__index">V</span><span data-index="W" class="van-index-bar__index">W</span><span data-index="X" class="van-index-bar__index">X</span><span data-index="Y" class="van-index-bar__index">Y</span><span data-index="Z" class="van-index-bar__index">Z</span></div> <div class="van-index-bar__sidebar" style="z-index: 1;"><span data-index="A" class="van-index-bar__index">A</span><span data-index="B" class="van-index-bar__index">B</span><span data-index="C" class="van-index-bar__index">C</span><span data-index="D" class="van-index-bar__index">D</span><span data-index="E" class="van-index-bar__index">E</span><span data-index="F" class="van-index-bar__index">F</span><span data-index="G" class="van-index-bar__index">G</span><span data-index="H" class="van-index-bar__index">H</span><span data-index="I" class="van-index-bar__index">I</span><span data-index="J" class="van-index-bar__index">J</span><span data-index="K" class="van-index-bar__index">K</span><span data-index="L" class="van-index-bar__index">L</span><span data-index="M" class="van-index-bar__index">M</span><span data-index="N" class="van-index-bar__index">N</span><span data-index="O" class="van-index-bar__index">O</span><span data-index="P" class="van-index-bar__index">P</span><span data-index="Q" class="van-index-bar__index">Q</span><span data-index="R" class="van-index-bar__index">R</span><span data-index="S" class="van-index-bar__index">S</span><span data-index="T" class="van-index-bar__index">T</span><span data-index="U" class="van-index-bar__index">U</span><span data-index="V" class="van-index-bar__index">V</span><span data-index="W" class="van-index-bar__index">W</span><span data-index="X" class="van-index-bar__index">X</span><span data-index="Y" class="van-index-bar__index">Y</span><span data-index="Z" class="van-index-bar__index">Z</span></div>
<div class="van-index-anchor">Title A</div> <div>
<div class="van-index-anchor">Title B</div> <div class="van-index-anchor">Title A</div>
</div>
<div>
<div class="van-index-anchor">Title B</div>
</div>
</div> </div>
`; `;

View File

@ -68,6 +68,7 @@ export default {
|------|------|------|------|------| |------|------|------|------|------|
| index-list | 索引字符列表 | `Array` | `A-Z` | - | | index-list | 索引字符列表 | `Array` | `A-Z` | - |
| z-index | z-index 层级 | `Number` | `1` | - | | z-index | z-index 层级 | `Number` | `1` | - |
| sticky | 是否开启锚点自动吸顶 | `Boolean` | `true` | - |
### IndexAnchor Props ### IndexAnchor Props

View File

@ -1,17 +1,17 @@
import Slider from '..'; import Slider from '..';
import { mount, trigger, triggerDrag } from '../../../test/utils'; import { mount, trigger, triggerDrag, mockGetBoundingClientRect } from '../../../test/utils';
function mockGetBoundingClientRect(vertical) { function mockRect(vertical) {
Element.prototype.getBoundingClientRect = jest.fn(() => ({ return mockGetBoundingClientRect({
width: vertical ? 0 : 100, width: vertical ? 0 : 100,
height: vertical ? 100 : 0, height: vertical ? 100 : 0,
top: vertical ? 0 : 100, top: vertical ? 0 : 100,
left: vertical ? 100 : 0 left: vertical ? 100 : 0
})); });
} }
test('drag button', () => { test('drag button', () => {
mockGetBoundingClientRect(); const restoreMock = mockRect();
const wrapper = mount(Slider, { const wrapper = mount(Slider, {
propsData: { propsData: {
@ -37,10 +37,12 @@ test('drag button', () => {
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
expect(wrapper.emitted('drag-start')).toBeTruthy(); expect(wrapper.emitted('drag-start')).toBeTruthy();
expect(wrapper.emitted('drag-end')).toBeTruthy(); expect(wrapper.emitted('drag-end')).toBeTruthy();
restoreMock();
}); });
it('click bar', () => { it('click bar', () => {
mockGetBoundingClientRect(); const restoreMock = mockRect();
const wrapper = mount(Slider, { const wrapper = mount(Slider, {
propsData: { propsData: {
@ -59,10 +61,12 @@ it('click bar', () => {
wrapper.setData({ disabled: false }); wrapper.setData({ disabled: false });
trigger(wrapper, 'click', 100, 0); trigger(wrapper, 'click', 100, 0);
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
restoreMock();
}); });
test('drag button vertical', () => { test('drag button vertical', () => {
mockGetBoundingClientRect(true); const restoreMock = mockRect(true);
const wrapper = mount(Slider, { const wrapper = mount(Slider, {
propsData: { propsData: {
@ -78,10 +82,12 @@ test('drag button vertical', () => {
const button = wrapper.find('.van-slider__button'); const button = wrapper.find('.van-slider__button');
triggerDrag(button, 0, 50); triggerDrag(button, 0, 50);
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
restoreMock();
}); });
it('click vertical', () => { it('click vertical', () => {
mockGetBoundingClientRect(true); const restoreMock = mockRect(true);
const wrapper = mount(Slider, { const wrapper = mount(Slider, {
propsData: { propsData: {
@ -96,4 +102,6 @@ it('click vertical', () => {
trigger(wrapper, 'click', 0, 100); trigger(wrapper, 'click', 0, 100);
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
restoreMock();
}); });

View File

@ -1,5 +1,5 @@
import SwipeCell from '..'; import SwipeCell from '..';
import { mount, triggerDrag, later } from '../../../test/utils'; import { mount, triggerDrag, later, mockGetBoundingClientRect } from '../../../test/utils';
const THRESHOLD = 0.15; const THRESHOLD = 0.15;
const defaultProps = { const defaultProps = {
@ -13,12 +13,6 @@ const defaultProps = {
} }
}; };
function mockGetBoundingClientRect(vertical) {
Element.prototype.getBoundingClientRect = jest.fn(() => ({
width: 50
}));
}
it('drag and show left part', () => { it('drag and show left part', () => {
const wrapper = mount(SwipeCell, defaultProps); const wrapper = mount(SwipeCell, defaultProps);
@ -98,7 +92,9 @@ it('disabled prop', () => {
}); });
it('auto calc width', async () => { it('auto calc width', async () => {
mockGetBoundingClientRect(); const restoreMock = mockGetBoundingClientRect({
width: 50
});
const wrapper = mount(SwipeCell, { const wrapper = mount(SwipeCell, {
scopedSlots: defaultProps.scopedSlots scopedSlots: defaultProps.scopedSlots
@ -107,4 +103,6 @@ it('auto calc width', async () => {
await later(); await later();
triggerDrag(wrapper, 100, 0); triggerDrag(wrapper, 100, 0);
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
restoreMock();
}); });

View File

@ -1,12 +1,17 @@
import Demo from '../demo'; import Demo from '../demo';
import demoTest from '../../../test/demo-test'; import demoTest from '../../../test/demo-test';
import { mockGetBoundingClientRect } from '../../../test/utils';
function mockGetBoundingClientRect(vertical) { let restore;
Element.prototype.getBoundingClientRect = jest.fn(() => ({
width: 100,
height: 100
}));
}
mockGetBoundingClientRect(); demoTest(Demo, {
demoTest(Demo); hookBeforeTest: () => {
restore = mockGetBoundingClientRect({
width: 100,
height: 100
});
},
hookAfterTest: () => {
restore();
}
});

View File

@ -239,6 +239,7 @@ export default sfc({
const elTopToPageTop = getElementTop(this.$el); const elTopToPageTop = getElementTop(this.$el);
const elBottomToPageTop = const elBottomToPageTop =
elTopToPageTop + this.$el.offsetHeight - this.$refs.wrap.offsetHeight; elTopToPageTop + this.$el.offsetHeight - this.$refs.wrap.offsetHeight;
if (scrollTop > elBottomToPageTop) { if (scrollTop > elBottomToPageTop) {
this.position = 'bottom'; this.position = 'bottom';
} else if (scrollTop > elTopToPageTop) { } else if (scrollTop > elTopToPageTop) {
@ -246,10 +247,12 @@ export default sfc({
} else { } else {
this.position = ''; this.position = '';
} }
const scrollParams = { const scrollParams = {
scrollTop, scrollTop,
isFixed: this.position === 'top' isFixed: this.position === 'top'
}; };
this.$emit('scroll', scrollParams); this.$emit('scroll', scrollParams);
}, },

View File

@ -13,8 +13,12 @@ const empty = {
Vue.component('demo-block', empty); Vue.component('demo-block', empty);
Vue.component('demo-section', empty); Vue.component('demo-section', empty);
export default function (Demo: any) { export default function (Demo: any, option: any = {}) {
test('renders demo correctly', async () => { test('renders demo correctly', async () => {
if (option.hookBeforeTest) {
option.hookBeforeTest();
}
if (Demo.i18n) { if (Demo.i18n) {
Locale.add(Demo.i18n); Locale.add(Demo.i18n);
} }
@ -24,5 +28,9 @@ export default function (Demo: any) {
await later(); await later();
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
if (option.hookAfterTest) {
option.hookAfterTest();
}
}); });
} }

View File

@ -70,3 +70,13 @@ export function later(delay: number = 0): Promise<void> {
export function transitionStub(): void { export function transitionStub(): void {
Vue.component('transition', TransitionStub as any); Vue.component('transition', TransitionStub as any);
} }
export function mockGetBoundingClientRect(rect: ClientRect | DOMRect): Function {
const originMethod = Element.prototype.getBoundingClientRect;
Element.prototype.getBoundingClientRect = <any> jest.fn(() => rect);
return function () {
Element.prototype.getBoundingClientRect = originMethod;
};
}