feat(utils): improve add unit performance with wxs (#2550)

This commit is contained in:
rex 2019-12-18 00:31:29 +08:00 committed by GitHub
parent ee794b07fd
commit 4527c31c6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 61 additions and 158 deletions

View File

@ -1,5 +1,4 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { addUnit } from '../common/utils';
function emit(target: WechatMiniprogram.Component.TrivialInstance, value: boolean | any[]) { function emit(target: WechatMiniprogram.Component.TrivialInstance, value: boolean | any[]) {
target.$emit('input', value); target.$emit('input', value);
@ -35,14 +34,10 @@ VantComponent({
}, },
iconSize: { iconSize: {
type: null, type: null,
observer: 'setSizeWithUnit' value: 20
} }
}, },
data: {
sizeWithUnit: '20px'
},
methods: { methods: {
emitChange(value: boolean) { emitChange(value: boolean) {
if (this.parent) { if (this.parent) {
@ -87,12 +82,6 @@ VantComponent({
emit(parent, parentValue); emit(parent, parentValue);
} }
} }
}, }
setSizeWithUnit(size: string | number): void {
this.set({
sizeWithUnit: addUnit(size)
});
},
} }
}); });

View File

@ -8,7 +8,7 @@
name="success" name="success"
size="0.8em" size="0.8em"
class="{{ utils.bem('checkbox__icon', [shape, { disabled, checked: value }]) }}" class="{{ utils.bem('checkbox__icon', [shape, { disabled, checked: value }]) }}"
style="font-size: {{ sizeWithUnit }};{{ checkedColor && value && !disabled ? 'border-color:' + checkedColor + '; background-color:' + checkedColor : '' }}" style="font-size: {{ utils.addUnit(iconSize) }};{{ checkedColor && value && !disabled ? 'border-color:' + checkedColor + '; background-color:' + checkedColor : '' }}"
custom-class="icon-class" custom-class="icon-class"
custom-style="line-height: 1.25em;" custom-style="line-height: 1.25em;"
/> />

View File

@ -1,7 +1,6 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { button } from '../mixins/button'; import { button } from '../mixins/button';
import { openType } from '../mixins/open-type'; import { openType } from '../mixins/open-type';
import { addUnit } from '../common/utils';
import { GRAY, BLUE } from '../common/color'; import { GRAY, BLUE } from '../common/color';
type Action = 'confirm' | 'cancel' | 'overlay'; type Action = 'confirm' | 'cancel' | 'overlay';
@ -23,10 +22,7 @@ VantComponent({
showCancelButton: Boolean, showCancelButton: Boolean,
closeOnClickOverlay: Boolean, closeOnClickOverlay: Boolean,
confirmButtonOpenType: String, confirmButtonOpenType: String,
width: { width: null,
type: null,
observer: 'setWidthWithUnit'
},
zIndex: { zIndex: {
type: Number, type: Number,
value: 2000 value: 2000
@ -125,12 +121,6 @@ VantComponent({
if (callback) { if (callback) {
callback(this); callback(this);
} }
},
setWidthWithUnit(val) {
this.setData({
widthWithUnit: addUnit(val)
});
} }
} }
}); });

View File

@ -1,10 +1,12 @@
<wxs src="../wxs/utils.wxs" module="utils" />
<van-popup <van-popup
show="{{ show }}" show="{{ show }}"
z-index="{{ zIndex }}" z-index="{{ zIndex }}"
overlay="{{ overlay }}" overlay="{{ overlay }}"
transition="{{ transition }}" transition="{{ transition }}"
custom-class="van-dialog {{ className }}" custom-class="van-dialog {{ className }}"
custom-style="{{ widthWithUnit ? 'width: ' + widthWithUnit + ';' : '' }}{{ customStyle }}" custom-style="width: {{ utils.addUnit(width) }};{{ customStyle }}"
overlay-style="{{ overlayStyle }}" overlay-style="{{ overlayStyle }}"
close-on-click-overlay="{{ closeOnClickOverlay }}" close-on-click-overlay="{{ closeOnClickOverlay }}"
bind:close="onClickOverlay" bind:close="onClickOverlay"

View File

@ -1,14 +1,10 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { addUnit } from '../common/utils';
VantComponent({ VantComponent({
props: { props: {
dot: Boolean, dot: Boolean,
info: null, info: null,
size: { size: null,
type: null,
observer: 'setSizeWithUnit'
},
color: String, color: String,
customStyle: String, customStyle: String,
classPrefix: { classPrefix: {
@ -25,19 +21,9 @@ VantComponent({
} }
}, },
data: {
sizeWithUnit: null,
},
methods: { methods: {
onClick() { onClick() {
this.$emit('click'); this.$emit('click');
},
setSizeWithUnit(size: string | number): void {
this.setData({
sizeWithUnit: addUnit(size)
});
} }
} }
}); });

View File

@ -1,6 +1,8 @@
<wxs src="../wxs/utils.wxs" module="utils" />
<view <view
class="custom-class {{ classPrefix }} {{ isImageName ? 'van-icon--image' : classPrefix + '-' + name }}" class="custom-class {{ classPrefix }} {{ isImageName ? 'van-icon--image' : classPrefix + '-' + name }}"
style="{{ color ? 'color: ' + color + ';' : '' }}{{ size ? 'font-size: ' + sizeWithUnit + ';' : '' }}{{ customStyle }}" style="color: {{ color }};font-size: {{ utils.addUnit(size) }};{{ customStyle }}"
bind:tap="onClick" bind:tap="onClick"
> >
<van-info <van-info

View File

@ -1,5 +1,4 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { addUnit } from '../common/utils';
VantComponent({ VantComponent({
props: { props: {
@ -9,27 +8,7 @@ VantComponent({
type: String, type: String,
value: 'circular' value: 'circular'
}, },
size: { size: String,
type: String, textSize: String
observer: 'setSizeWithUnit'
},
textSize: {
type: String,
observer: 'setTextSizeWithUnit'
}
},
methods: {
setSizeWithUnit(size: string | number): void {
this.setData({
sizeWithUnit: addUnit(size)
});
},
setTextSizeWithUnit(size: string | number): void {
this.set({
textSizeWithUnit: addUnit(size)
});
}
} }
}); });

View File

@ -1,7 +1,9 @@
<wxs src="../wxs/utils.wxs" module="utils" />
<view class="custom-class van-loading {{ vertical ? 'van-loading--vertical' : '' }}"> <view class="custom-class van-loading {{ vertical ? 'van-loading--vertical' : '' }}">
<view <view
class="van-loading__spinner van-loading__spinner--{{ type }}" class="van-loading__spinner van-loading__spinner--{{ type }}"
style="color: {{ color }}; width: {{ sizeWithUnit }}; height: {{ sizeWithUnit }}" style="color: {{ color }}; width: {{ utils.addUnit(size) }}; height: {{ utils.addUnit(size) }}"
> >
<view <view
wx:if="{{ type === 'spinner' }}" wx:if="{{ type === 'spinner' }}"
@ -10,7 +12,7 @@
class="van-loading__dot" class="van-loading__dot"
/> />
</view> </view>
<view class="van-loading__text" style="font-size: {{ textSizeWithUnit }};"> <view class="van-loading__text" style="font-size: {{ utils.addUnit(textSize) }};">
<slot /> <slot />
</view> </view>
</view> </view>

View File

@ -1,6 +1,5 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { BLUE } from '../common/color'; import { BLUE } from '../common/color';
import { addUnit } from '../common/utils';
VantComponent({ VantComponent({
props: { props: {
@ -23,19 +22,7 @@ VantComponent({
}, },
strokeWidth: { strokeWidth: {
type: null, type: null,
observer: 'setStrokeWidthUnit' value: 4
}
},
data: {
strokeWidthUnit: '4px'
},
methods: {
setStrokeWidthUnit(val) {
this.setData({
strokeWidthUnit: addUnit(val)
});
} }
} }
}); });

View File

@ -1,8 +1,9 @@
<wxs src="../wxs/utils.wxs" module="utils" />
<wxs src="./index.wxs" module="getters" /> <wxs src="./index.wxs" module="getters" />
<view <view
class="van-progress custom-class" class="van-progress custom-class"
style="height: {{ strokeWidthUnit }}; {{ trackColor ? 'background: ' + trackColor : '' }}" style="height: {{ utils.addUnit(strokeWidth) }}; {{ trackColor ? 'background: ' + trackColor : '' }}"
> >
<view <view
class="van-progress__portion" class="van-progress__portion"

View File

@ -1,6 +1,5 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { Weapp } from 'definitions/weapp'; import { Weapp } from 'definitions/weapp';
import { addUnit } from '../common/utils';
VantComponent({ VantComponent({
field: true, field: true,
@ -34,21 +33,11 @@ VantComponent({
}, },
iconSize: { iconSize: {
type: null, type: null,
observer: 'setIconSizeUnit' value: 20
} }
}, },
data: {
iconSizeWithUnit: '20px'
},
methods: { methods: {
setIconSizeUnit(val) {
this.setData({
iconSizeWithUnit: addUnit(val)
});
},
emitChange(value: boolean) { emitChange(value: boolean) {
const instance = this.parent || this; const instance = this.parent || this;
instance.$emit('input', value); instance.$emit('input', value);

View File

@ -8,7 +8,7 @@
> >
<slot /> <slot />
</view> </view>
<view class="van-radio__icon-wrap" style="font-size: {{ iconSizeWithUnit }};" bindtap="onChange"> <view class="van-radio__icon-wrap" style="font-size: {{ utils.addUnit(iconSize) }};" bindtap="onChange">
<slot wx:if="{{ useIconSlot }}" name="icon" /> <slot wx:if="{{ useIconSlot }}" name="icon" />
<van-icon <van-icon
wx:else wx:else
@ -16,7 +16,7 @@
class="{{ utils.bem('radio__icon', [shape, { disabled, checked: value === name }]) }}" class="{{ utils.bem('radio__icon', [shape, { disabled, checked: value === name }]) }}"
style="{{ checkedColor && !disabled && value === name ? 'border-color:' + checkedColor + '; background-color:' + checkedColor + ';' : '' }}" style="{{ checkedColor && !disabled && value === name ? 'border-color:' + checkedColor + '; background-color:' + checkedColor + ';' : '' }}"
custom-class="icon-class" custom-class="icon-class"
custom-style="line-height: {{ iconSizeWithUnit }};font-size: .8em;display: block;" custom-style="line-height: {{ utils.addUnit(iconSize) }};font-size: .8em;display: block;"
/> />
</view> </view>
<view <view

View File

@ -1,6 +1,5 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { Weapp } from 'definitions/weapp'; import { Weapp } from 'definitions/weapp';
import { addUnit } from '../common/utils';
VantComponent({ VantComponent({
field: true, field: true,
@ -8,14 +7,18 @@ VantComponent({
classes: ['icon-class'], classes: ['icon-class'],
props: { props: {
value: Number, value: {
type: Number,
observer(value: number) {
if (value !== this.data.innerValue) {
this.setData({ innerValue: value });
}
}
},
readonly: Boolean, readonly: Boolean,
disabled: Boolean, disabled: Boolean,
allowHalf: Boolean, allowHalf: Boolean,
size: { size: null,
type: null,
observer: 'setSizeWithUnit'
},
icon: { icon: {
type: String, type: String,
value: 'star' value: 'star'
@ -40,10 +43,7 @@ VantComponent({
type: Number, type: Number,
value: 5 value: 5
}, },
gutter: { gutter: null,
type: null,
observer: 'setGutterWithUnit'
},
touchable: { touchable: {
type: Boolean, type: Boolean,
value: true value: true
@ -51,31 +51,10 @@ VantComponent({
}, },
data: { data: {
innerValue: 0, innerValue: 0
gutterWithUnit: undefined,
sizeWithUnit: null
},
watch: {
value(value: number) {
if (value !== this.data.innerValue) {
this.setData({ innerValue: value });
}
}
}, },
methods: { methods: {
setGutterWithUnit(val) {
this.setData({
gutterWithUnit: addUnit(val)
});
},
setSizeWithUnit(size: string | number): void {
this.setData({
sizeWithUnit: addUnit(size)
});
},
onSelect(event: Weapp.Event) { onSelect(event: Weapp.Event) {
const { data } = this; const { data } = this;
const { score } = event.currentTarget.dataset; const { score } = event.currentTarget.dataset;

View File

@ -8,12 +8,12 @@
class="van-rate__item" class="van-rate__item"
wx:for="{{ count }}" wx:for="{{ count }}"
wx:key="index" wx:key="index"
style="padding-right: {{ index !== count - 1 ? gutterWithUnit : '' }}" style="padding-right: {{ index !== count - 1 ? utils.addUnit(gutter) : '' }}"
> >
<van-icon <van-icon
name="{{ index + 1 <= innerValue ? icon : voidIcon }}" name="{{ index + 1 <= innerValue ? icon : voidIcon }}"
class="van-rate__icon" class="van-rate__icon"
style="font-size :{{ size? sizeWithUnit : ''}}" style="font-size: {{ utils.addUnit(size) }}"
custom-class="icon-class" custom-class="icon-class"
data-score="{{ index }}" data-score="{{ index }}"
color="{{ disabled ? disabledColor : index + 1 <= innerValue ? color : voidColor }}" color="{{ disabled ? disabledColor : index + 1 <= innerValue ? color : voidColor }}"
@ -24,7 +24,7 @@
wx:if="{{ allowHalf }}" wx:if="{{ allowHalf }}"
name="{{ index + 0.5 <= innerValue ? icon : voidIcon }}" name="{{ index + 0.5 <= innerValue ? icon : voidIcon }}"
class="{{ utils.bem('rate__icon', ['half']) }}" class="{{ utils.bem('rate__icon', ['half']) }}"
style="font-size :{{ size? sizeWithUnit : ''}}" style="font-size: {{ utils.addUnit(size) }}"
custom-class="icon-class" custom-class="icon-class"
data-score="{{ index - 0.5 }}" data-score="{{ index - 0.5 }}"
color="{{ disabled ? disabledColor : index + 0.5 <= innerValue ? color : voidColor }}" color="{{ disabled ? disabledColor : index + 0.5 <= innerValue ? color : voidColor }}"

View File

@ -1,6 +1,5 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { Weapp } from 'definitions/weapp'; import { Weapp } from 'definitions/weapp';
import { addUnit } from '../common/utils';
VantComponent({ VantComponent({
classes: [ classes: [
@ -25,8 +24,7 @@ VantComponent({
}, },
height: { height: {
type: [Number, String], type: [Number, String],
value: 300, value: 300
observer: 'updateHeight'
}, },
max: { max: {
type: Number, type: Number,
@ -38,10 +36,6 @@ VantComponent({
subItems: [] subItems: []
}, },
created() {
this.updateHeight();
},
methods: { methods: {
// 当一个子项被选择时 // 当一个子项被选择时
onSelectItem(event: Weapp.Event) { onSelectItem(event: Weapp.Event) {
@ -74,12 +68,6 @@ VantComponent({
const { children = [] } = items[mainActiveIndex] || {}; const { children = [] } = items[mainActiveIndex] || {};
return this.set({ subItems: children }); return this.set({ subItems: children });
},
updateHeight() {
this.setData({
innerHeight: addUnit(this.data.height)
});
} }
} }
}); });

View File

@ -3,7 +3,7 @@
<view <view
class="van-tree-select" class="van-tree-select"
style="height: {{ innerHeight }}" style="height: {{ utils.addUnit(height) }}"
> >
<scroll-view scroll-y class="van-tree-select__nav"> <scroll-view scroll-y class="van-tree-select__nav">
<van-sidebar active-key="{{ mainActiveIndex }}" bind:change="onClickNav" custom-class="van-tree-select__nav__inner"> <van-sidebar active-key="{{ mainActiveIndex }}" bind:change="onClickNav" custom-class="van-tree-select__nav__inner">

View File

@ -1,6 +1,5 @@
import { VantComponent } from '../common/component'; import { VantComponent } from '../common/component';
import { isImageFile } from './utils'; import { isImageFile } from './utils';
import { addUnit } from '../common/utils';
VantComponent({ VantComponent({
props: { props: {
@ -11,8 +10,7 @@ VantComponent({
useBeforeRead: Boolean, useBeforeRead: Boolean,
previewSize: { previewSize: {
type: null, type: null,
value: 90, value: 90
observer: 'setComputedPreviewSize'
}, },
name: { name: {
type: [Number, String], type: [Number, String],
@ -70,12 +68,6 @@ VantComponent({
this.setData({ lists, isInCount: lists.length < maxCount }); this.setData({ lists, isInCount: lists.length < maxCount });
}, },
setComputedPreviewSize(val) {
this.setData({
computedPreviewSize: addUnit(val)
});
},
startUpload() { startUpload() {
if (this.data.disabled) return; if (this.data.disabled) return;
const { const {

View File

@ -15,14 +15,14 @@
src="{{ item.url || item.path }}" src="{{ item.url || item.path }}"
alt="{{ item.name || ('图片' + index) }}" alt="{{ item.name || ('图片' + index) }}"
class="van-uploader__preview-image" class="van-uploader__preview-image"
style="width: {{ computedPreviewSize }}; height: {{ computedPreviewSize }};" style="width: {{ utils.addUnit(previewSize) }}; height: {{ utils.addUnit(previewSize) }};"
data-url="{{ item.url || item.path }}" data-url="{{ item.url || item.path }}"
bind:tap="doPreviewImage" bind:tap="doPreviewImage"
/> />
<view <view
wx:else wx:else
class="van-uploader__file" class="van-uploader__file"
style="width: {{ computedPreviewSize }}; height: {{ computedPreviewSize }};" style="width: {{ utils.addUnit(previewSize) }}; height: {{ utils.addUnit(previewSize) }};"
> >
<van-icon name="description" class="van-uploader__file-icon" /> <van-icon name="description" class="van-uploader__file-icon" />
<view class="van-uploader__file-name van-ellipsis">{{ item.name || item.url || item.path }}</view> <view class="van-uploader__file-name van-ellipsis">{{ item.name || item.url || item.path }}</view>
@ -46,7 +46,7 @@
<view <view
wx:else wx:else
class="van-uploader__upload" class="van-uploader__upload"
style="width: {{ computedPreviewSize }}; height: {{ computedPreviewSize }};" style="width: {{ utils.addUnit(previewSize) }}; height: {{ utils.addUnit(previewSize) }};"
bind:tap="startUpload" bind:tap="startUpload"
> >
<van-icon name="plus" class="van-uploader__upload-icon" /> <van-icon name="plus" class="van-uploader__upload-icon" />

14
packages/wxs/add-unit.wxs Normal file
View File

@ -0,0 +1,14 @@
/* eslint-disable */
var REGEXP = getRegExp('^\d+(\.\d+)?$');
function addUnit(value) {
if (value == null) {
return undefined;
}
return REGEXP.test('' + value) ? value + 'px' : value;
}
module.exports = {
addUnit: addUnit
};

View File

@ -1,7 +1,10 @@
/* eslint-disable */
var bem = require('./bem.wxs').bem; var bem = require('./bem.wxs').bem;
var memoize = require('./memoize.wxs').memoize; var memoize = require('./memoize.wxs').memoize;
var addUnit = require('./add-unit.wxs').addUnit;
module.exports = { module.exports = {
bem: memoize(bem), bem: memoize(bem),
memoize: memoize memoize: memoize,
addUnit: addUnit
}; };