From daa56aa5c6e7f42fd05ccb76690499a0027f05d3 Mon Sep 17 00:00:00 2001 From: zhongnan Date: Thu, 17 Dec 2020 17:24:20 +0800 Subject: [PATCH] perf(wxs): improve performance with wxs --- packages/checkbox/index.wxs | 20 ++++----- packages/grid-item/index.ts | 35 +-------------- packages/grid-item/index.wxml | 9 +++- packages/grid-item/index.wxs | 32 ++++++++++++++ packages/grid/index.ts | 14 ------ packages/grid/index.wxml | 7 ++- packages/grid/index.wxs | 13 ++++++ packages/icon/index.wxml | 5 +-- packages/icon/{computed.wxs => index.wxs} | 29 ++++--------- packages/image/index.ts | 52 +---------------------- packages/image/index.wxml | 5 ++- packages/image/index.wxs | 32 ++++++++++++++ packages/slider/index.wxs | 18 +++----- 13 files changed, 124 insertions(+), 147 deletions(-) create mode 100644 packages/grid-item/index.wxs create mode 100644 packages/grid/index.wxs rename packages/icon/{computed.wxs => index.wxs} (59%) create mode 100644 packages/image/index.wxs diff --git a/packages/checkbox/index.wxs b/packages/checkbox/index.wxs index 927eb55d..eb9c7726 100644 --- a/packages/checkbox/index.wxs +++ b/packages/checkbox/index.wxs @@ -1,20 +1,20 @@ /* eslint-disable */ -var utils = require('../wxs/utils.wxs'); +var style = require('../wxs/style.wxs'); +var addUnit = require('../wxs/add-unit.wxs'); function iconStyle(checkedColor, value, disabled, parentDisabled, iconSize) { - var styles = [['font-size', utils.addUnit(iconSize)]]; + var styles = { + 'font-size': addUnit(iconSize), + }; + if (checkedColor && value && !disabled && !parentDisabled) { - styles.push(['border-color', checkedColor]); - styles.push(['background-color', checkedColor]); + styles['border-color'] = checkedColor; + styles['background-color'] = checkedColor; } - return styles - .map(function(item) { - return item.join(':'); - }) - .join(';'); + return style(styles); } module.exports = { - iconStyle: iconStyle + iconStyle: iconStyle, }; diff --git a/packages/grid-item/index.ts b/packages/grid-item/index.ts index db112b47..4754b18b 100644 --- a/packages/grid-item/index.ts +++ b/packages/grid-item/index.ts @@ -1,6 +1,5 @@ import { link } from '../mixins/link'; import { VantComponent } from '../common/component'; -import { addUnit } from '../common/utils'; VantComponent({ relation: { @@ -48,40 +47,8 @@ VantComponent({ direction, iconSize, } = data; - const width = `${100 / columnNum}%`; - - const styleWrapper: string[] = []; - styleWrapper.push(`width: ${width}`); - - if (square) { - styleWrapper.push(`padding-top: ${width}`); - } - - if (gutter) { - const gutterValue = addUnit(gutter); - styleWrapper.push(`padding-right: ${gutterValue}`); - - const index = children.indexOf(this); - if (index >= columnNum && !square) { - styleWrapper.push(`margin-top: ${gutterValue}`); - } - } - - let contentStyle = ''; - - if (square && gutter) { - const gutterValue = addUnit(gutter); - - contentStyle = ` - right: ${gutterValue}; - bottom: ${gutterValue}; - height: auto; - `; - } this.setData({ - viewStyle: styleWrapper.join('; '), - contentStyle, center, border, square, @@ -89,6 +56,8 @@ VantComponent({ clickable, direction, iconSize, + index: children.indexOf(this), + columnNum, }); }, diff --git a/packages/grid-item/index.wxml b/packages/grid-item/index.wxml index ede970ca..0070a2bb 100644 --- a/packages/grid-item/index.wxml +++ b/packages/grid-item/index.wxml @@ -1,9 +1,14 @@ + - + diff --git a/packages/grid-item/index.wxs b/packages/grid-item/index.wxs new file mode 100644 index 00000000..2cfe37d0 --- /dev/null +++ b/packages/grid-item/index.wxs @@ -0,0 +1,32 @@ +/* eslint-disable */ +var style = require('../wxs/style.wxs'); +var addUnit = require('../wxs/add-unit.wxs'); + +function wrapperStyle(data) { + var width = 100 / data.columnNum + '%'; + + return style({ + width: width, + 'padding-top': data.square ? width : null, + 'padding-right': addUnit(data.gutter), + 'margin-top': + data.index >= data.columnNum && !data.square + ? addUnit(data.gutter) + : null, + }); +} + +function contentStyle(data) { + return data.square + ? style({ + right: addUnit(data.gutter), + bottom: addUnit(data.gutter), + height: 'auto', + }) + : ''; +} + +module.exports = { + wrapperStyle: wrapperStyle, + contentStyle: contentStyle, +}; diff --git a/packages/grid/index.ts b/packages/grid/index.ts index d81436c4..4c898f02 100644 --- a/packages/grid/index.ts +++ b/packages/grid/index.ts @@ -1,5 +1,4 @@ import { VantComponent } from '../common/component'; -import { addUnit } from '../common/utils'; VantComponent({ relation: { @@ -47,19 +46,6 @@ VantComponent({ }, }, - data: { - viewStyle: '', - }, - - created() { - const { gutter } = this.data; - if (gutter) { - this.setData({ - viewStyle: `padding-left: ${addUnit(gutter)}`, - }); - } - }, - methods: { updateChildren() { this.children.forEach( diff --git a/packages/grid/index.wxml b/packages/grid/index.wxml index c5a545f5..2e4118f3 100644 --- a/packages/grid/index.wxml +++ b/packages/grid/index.wxml @@ -1,3 +1,8 @@ - + + + diff --git a/packages/grid/index.wxs b/packages/grid/index.wxs new file mode 100644 index 00000000..cd3b1bd5 --- /dev/null +++ b/packages/grid/index.wxs @@ -0,0 +1,13 @@ +/* eslint-disable */ +var style = require('../wxs/style.wxs'); +var addUnit = require('../wxs/add-unit.wxs'); + +function rootStyle(data) { + return style({ + 'padding-left': addUnit(data.gutter), + }); +} + +module.exports = { + rootStyle: rootStyle, +}; diff --git a/packages/icon/index.wxml b/packages/icon/index.wxml index 62eff360..3c701745 100644 --- a/packages/icon/index.wxml +++ b/packages/icon/index.wxml @@ -1,10 +1,9 @@ - - + +