perf(tab): set container once after mounted (#3875)

This commit is contained in:
rex 2020-12-18 15:04:31 +08:00 committed by GitHub
parent f321e202f4
commit eaf513bf2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 18 deletions

View File

@ -1,14 +1,18 @@
import { isDef, isNumber, isPlainObject, isPromise } from './validator'; import { isDef, isNumber, isPlainObject, isPromise } from './validator';
import { canIUseGroupSetData } from './version'; import { canIUseGroupSetData, canIUseNextTick } from './version';
export function range(num: number, min: number, max: number) { export function range(num: number, min: number, max: number) {
return Math.min(Math.max(num, min), max); return Math.min(Math.max(num, min), max);
} }
export function nextTick(fn: Function) { export function nextTick(cb: (...args: any[]) => void) {
setTimeout(() => { if (canIUseNextTick()) {
fn(); wx.nextTick(cb);
}, 1000 / 30); } else {
setTimeout(() => {
cb();
}, 1000 / 30);
}
} }
let systemInfo: WechatMiniprogram.GetSystemInfoSyncResult; let systemInfo: WechatMiniprogram.GetSystemInfoSyncResult;

View File

@ -46,3 +46,7 @@ export function canIUseGroupSetData() {
const system = getSystemInfoSync(); const system = getSystemInfoSync();
return compareVersion(system.SDKVersion, '2.4.0') >= 0; return compareVersion(system.SDKVersion, '2.4.0') >= 0;
} }
export function canIUseNextTick() {
return wx.canIUse('nextTick');
}

View File

@ -1,6 +1,12 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { touch } from '../mixins/touch'; import { touch } from '../mixins/touch';
import { getAllRect, getRect, groupSetData } from '../common/utils'; import {
getAllRect,
getRect,
groupSetData,
nextTick,
requestAnimationFrame,
} from '../common/utils';
import { isDef } from '../common/validator'; import { isDef } from '../common/validator';
type TrivialInstance = WechatMiniprogram.Component.TrivialInstance; type TrivialInstance = WechatMiniprogram.Component.TrivialInstance;
@ -99,10 +105,8 @@ VantComponent({
data: { data: {
tabs: [] as Record<string, unknown>[], tabs: [] as Record<string, unknown>[],
lineStyle: '',
scrollLeft: 0, scrollLeft: 0,
scrollable: false, scrollable: false,
trackStyle: '',
currentIndex: 0, currentIndex: 0,
container: null, container: null,
skipTransition: true, skipTransition: true,
@ -110,19 +114,17 @@ VantComponent({
}, },
mounted() { mounted() {
wx.nextTick(() => { requestAnimationFrame(() => {
this.setData({
container: () => this.createSelectorQuery().select('.van-tabs'),
});
this.resize(true); this.resize(true);
this.scrollIntoView(); this.scrollIntoView();
}); });
}, },
methods: { methods: {
updateContainer() {
this.setData({
container: () => this.createSelectorQuery().select('.van-tabs'),
});
},
updateTabs() { updateTabs() {
const { children = [], data } = this; const { children = [], data } = this;
this.setData({ this.setData({
@ -158,7 +160,7 @@ VantComponent({
this.trigger('disabled', child); this.trigger('disabled', child);
} else { } else {
this.setCurrentIndex(index); this.setCurrentIndex(index);
wx.nextTick(() => { nextTick(() => {
this.trigger('click'); this.trigger('click');
}); });
} }
@ -203,10 +205,9 @@ VantComponent({
const shouldEmitChange = data.currentIndex !== null; const shouldEmitChange = data.currentIndex !== null;
this.setData({ currentIndex }); this.setData({ currentIndex });
wx.nextTick(() => { nextTick(() => {
this.resize(); this.resize();
this.scrollIntoView(); this.scrollIntoView();
this.updateContainer();
this.trigger('input'); this.trigger('input');
if (shouldEmitChange) { if (shouldEmitChange) {