1
0
mirror of https://gitee.com/vant-contrib/vant-weapp.git synced 2025-04-06 03:58:05 +08:00

perf(icon): use wxs ()

This commit is contained in:
rex 2020-11-29 18:24:26 +08:00 committed by GitHub
parent 35b2c1d99d
commit 166ba61c0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 13 deletions
example/pages/icon
packages/icon

@ -10,9 +10,8 @@ page {
} }
.icon { .icon {
display: block;
margin: 16px 0; margin: 16px 0;
color: #323233; color: #323233 !important;
} }
.text { .text {

@ -0,0 +1,50 @@
/* eslint-disable */
var utils = require('../wxs/utils.wxs');
function isImage(name) {
return name.indexOf('/') !== -1;
}
function rootClass(data) {
var classes = ['custom-class'];
if (data.classPrefix != null) {
classes.push(data.classPrefix);
}
if (isImage(data.name)) {
classes.push('van-icon--image');
} else if (data.classPrefix != null) {
classes.push(data.classPrefix + '-' + data.name);
}
return classes.join(' ');
}
function rootStyle(data) {
var styles = [];
if (data.color) {
styles.push(['color', data.color]);
}
if (data.size) {
styles.push(['font-size', utils.addUnit(data.size)]);
}
if (data.customStyle) {
styles.push([data.customStyle]);
}
return styles
.map(function (pair) {
return pair.join(':');
})
.join(';');
}
module.exports = {
isImage: isImage,
rootClass: rootClass,
rootStyle: rootStyle,
};

@ -11,14 +11,7 @@ VantComponent({
type: String, type: String,
value: 'van-icon', value: 'van-icon',
}, },
name: { name: String,
type: String,
observer(val) {
this.setData({
isImageName: val.indexOf('/') !== -1,
});
},
},
}, },
methods: { methods: {

@ -1,8 +1,9 @@
<wxs src="../wxs/utils.wxs" module="utils" /> <wxs src="../wxs/utils.wxs" module="utils" />
<wxs src="./computed.wxs" module="computed" />
<view <view
class="custom-class {{ classPrefix }} {{ isImageName ? 'van-icon--image' : classPrefix + '-' + name }}" class="{{ computed.rootClass({ classPrefix, name }) }}"
style="color: {{ color }};font-size: {{ utils.addUnit(size) }};{{ customStyle }}" style="{{ computed.rootStyle({ customStyle, color, size }) }}"
bind:tap="onClick" bind:tap="onClick"
> >
<van-info <van-info
@ -12,7 +13,7 @@
custom-class="van-icon__info" custom-class="van-icon__info"
/> />
<image <image
wx:if="{{ isImageName }}" wx:if="{{ computed.isImage(name) }}"
src="{{ name }}" src="{{ name }}"
mode="aspectFit" mode="aspectFit"
class="van-icon__image" class="van-icon__image"