Skip to content

Commit

Permalink
fix(tab): line-width support string (#3628)
Browse files Browse the repository at this point in the history
fix #3620
  • Loading branch information
rex-zsd committed Sep 22, 2020
1 parent eca7dae commit 9bdfd34
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 76 deletions.
28 changes: 27 additions & 1 deletion packages/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function nextTick(fn: Function) {
}, 1000 / 30);
}

let systemInfo: WechatMiniprogram.GetSystemInfoSyncResult = null;
let systemInfo: WechatMiniprogram.GetSystemInfoSyncResult;
export function getSystemInfoSync() {
if (systemInfo == null) {
systemInfo = wx.getSystemInfoSync();
Expand Down Expand Up @@ -54,3 +54,29 @@ export function requestAnimationFrame(cb: Function) {
cb();
});
}

export function getRect(
this: WechatMiniprogram.Component.TrivialInstance,
selector: string
): Promise<WechatMiniprogram.BoundingClientRectCallbackResult> {
return new Promise((resolve) => {
wx.createSelectorQuery()
.in(this)
.select(selector)
.boundingClientRect()
.exec((rect = []) => resolve(rect[0]));
});
}

export function getAllRect(
this: WechatMiniprogram.Component.TrivialInstance,
selector: string
): Promise<WechatMiniprogram.BoundingClientRectCallbackResult[]> {
return new Promise((resolve) => {
wx.createSelectorQuery()
.in(this)
.selectAll(selector)
.boundingClientRect()
.exec((rect = []) => resolve(rect[0]));
});
}
109 changes: 41 additions & 68 deletions packages/tabs/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { VantComponent } from '../common/component';
import { touch } from '../mixins/touch';
import { Weapp } from 'definitions/weapp';
import { isDef, addUnit } from '../common/utils';
import { getAllRect, getRect, isDef } from '../common/utils';

type TrivialInstance = WechatMiniprogram.Component.TrivialInstance;

Expand Down Expand Up @@ -35,10 +35,7 @@ VantComponent({
swipeable: Boolean,
titleActiveColor: String,
titleInactiveColor: String,
color: {
type: String,
observer: 'setLine',
},
color: String,
animated: {
type: Boolean,
observer() {
Expand All @@ -55,7 +52,6 @@ VantComponent({
lineHeight: {
type: [String, Number],
value: -1,
observer: 'setLine',
},
active: {
type: [String, Number],
Expand Down Expand Up @@ -102,13 +98,15 @@ VantComponent({
},

data: {
tabs: [],
tabs: [] as Record<string, unknown>[],
lineStyle: '',
scrollLeft: 0,
scrollable: false,
trackStyle: '',
currentIndex: null,
currentIndex: 0,
container: null,
skipTransition: true,
lineOffsetLeft: 0,
},

mounted() {
Expand Down Expand Up @@ -223,54 +221,34 @@ VantComponent({
}
},

setLine(skipTransition?: boolean) {
setLine(skipTransition = false) {
if (this.data.type !== 'line') {
return;
}

const {
color,
duration,
currentIndex,
lineWidth,
lineHeight,
} = this.data;

this.getRect('.van-tab', true).then(
(rects: WechatMiniprogram.BoundingClientRectCallbackResult[] = []) => {
const rect = rects[currentIndex];
if (rect == null) {
return;
}
const height =
lineHeight !== -1
? `height: ${addUnit(lineHeight)}; border-radius: ${addUnit(
lineHeight
)};`
: '';

let left = rects
.slice(0, currentIndex)
.reduce((prev, curr) => prev + curr.width, 0);

left += (rect.width - lineWidth) / 2;

const transition = skipTransition
? ''
: `transition-duration: ${duration}s; -webkit-transition-duration: ${duration}s;`;

this.setData({
lineStyle: `
${height}
width: ${addUnit(lineWidth)};
background-color: ${color};
-webkit-transform: translateX(${left}px);
transform: translateX(${left}px);
${transition}
`,
});
const { currentIndex } = this.data;

Promise.all([
getAllRect.call(this, '.van-tab'),
getRect.call(this, '.van-tabs__line'),
]).then(([rects = [], lineRect]) => {
const rect = rects[currentIndex];

if (rect == null) {
return;
}
);

let lineOffsetLeft = rects
.slice(0, currentIndex)
.reduce((prev, curr) => prev + curr.width, 0);

lineOffsetLeft += (rect.width - lineRect.width) / 2;

this.setData({
lineOffsetLeft,
skipTransition,
});
});
},

// scroll active tab into view
Expand All @@ -282,23 +260,18 @@ VantComponent({
}

Promise.all([
this.getRect('.van-tab', true),
this.getRect('.van-tabs__nav'),
]).then(
([tabRects, navRect]: [
WechatMiniprogram.BoundingClientRectCallbackResult[],
WechatMiniprogram.BoundingClientRectCallbackResult
]) => {
const tabRect = tabRects[currentIndex];
const offsetLeft = tabRects
.slice(0, currentIndex)
.reduce((prev, curr) => prev + curr.width, 0);

this.setData({
scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2,
});
}
);
getAllRect.call(this, '.van-tab'),
getRect.call(this, '.van-tabs__nav'),
]).then(([tabRects, navRect]) => {
const tabRect = tabRects[currentIndex];
const offsetLeft = tabRects
.slice(0, currentIndex)
.reduce((prev, curr) => prev + curr.width, 0);

this.setData({
scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2,
});
});
},

onTouchScroll(event: Weapp.TouchEvent) {
Expand Down
4 changes: 2 additions & 2 deletions packages/tabs/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
style="{{ color ? 'border-color: ' + color : '' }}"
>
<view class="{{ utils.bem('tabs__nav', [type, { complete: !ellipsis }]) }} nav-class" style="{{ getters.tabCardTypeBorderStyle(color, type) }}">
<view wx:if="{{ type === 'line' }}" class="van-tabs__line" style="{{ lineStyle }}" />
<view wx:if="{{ type === 'line' }}" class="van-tabs__line" style="{{ getters.lineStyle({ color, lineOffsetLeft, lineHeight, skipTransition, duration, lineWidth }) }}" />
<view
wx:for="{{ tabs }}"
wx:key="index"
Expand Down Expand Up @@ -60,4 +60,4 @@
<slot />
</view>
</view>
</view>
</view>
43 changes: 38 additions & 5 deletions packages/tabs/index.wxs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable */
var utils = require('../wxs/utils.wxs');

function tabClass(active, ellipsis) {
var classes = ['tab-class'];

Expand Down Expand Up @@ -68,11 +70,42 @@ function trackStyle(data) {
return [
'transform: translate3d(' + -100 * data.currentIndex + '%, 0, 0)',
'-webkit-transition-duration: ' + data.duration + 's',
'transition-duration: ' + data.duration + 's'
'transition-duration: ' + data.duration + 's',
].join(';');
}

module.exports.tabClass = tabClass;
module.exports.tabStyle = tabStyle;
module.exports.trackStyle = trackStyle;
module.exports.tabCardTypeBorderStyle = tabCardTypeBorderStyle;
function lineStyle(data) {
var styles = [
['width', utils.addUnit(data.lineWidth)],
['transform', 'translateX(' + data.lineOffsetLeft + 'px)'],
['-webkit-transform', 'translateX(' + data.lineOffsetLeft + 'px)'],
];

if (data.color) {
styles.push(['background-color', data.color]);
}

if (data.lineHeight !== -1) {
styles.push(['height', utils.addUnit(data.lineHeight)]);
styles.push(['border-radius', utils.addUnit(data.lineHeight)]);
}

if (!data.skipTransition) {
styles.push(['transition-duration', data.duration + 's']);
styles.push(['-webkit-transition-duration', data.duration + 's']);
}

return styles
.map(function (item) {
return item.join(':');
})
.join(';');
}

module.exports = {
tabClass: tabClass,
tabStyle: tabStyle,
trackStyle: trackStyle,
lineStyle: lineStyle,
tabCardTypeBorderStyle: tabCardTypeBorderStyle,
};

0 comments on commit 9bdfd34

Please sign in to comment.