[improvement] Button: use wxs to optimize performance (#1049)

This commit is contained in:
neverland 2018-12-11 14:57:29 +08:00 committed by GitHub
parent aebcb213a9
commit 20928347e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 60 additions and 19 deletions

View File

@ -34,5 +34,4 @@
<van-button size="small" class="demo-margin-right">小型按钮</van-button> <van-button size="small" class="demo-margin-right">小型按钮</van-button>
<van-button size="mini">迷你按钮</van-button> <van-button size="mini">迷你按钮</van-button>
</demo-block> </demo-block>
</demo-section> </demo-section>

View File

@ -24,21 +24,6 @@ VantComponent({
} }
}, },
computed: {
classes(): string {
const { type, size, block, plain, round, square, loading, disabled } = this.data;
return this.classNames('van-button', `van-button--${type}`, `van-button--${size}`, {
'van-button--block': block,
'van-button--round': round,
'van-button--plain': plain,
'van-button--square': square,
'van-button--loading': loading,
'van-button--disabled': disabled,
'van-button--unclickable': disabled || loading
});
}
},
methods: { methods: {
onClick() { onClick() {
if (!this.data.disabled && !this.data.loading) { if (!this.data.disabled && !this.data.loading) {

View File

@ -1,7 +1,9 @@
<wxs src="../wxs/utils.wxs" module="utils" />
<button <button
id="{{ id }}" id="{{ id }}"
lang="{{ lang }}" lang="{{ lang }}"
class="custom-class {{ classes }}" class="custom-class {{ utils.bem('button', [type, size, { block, round, plain, square, loading, disabled, unclickable: disabled || loading }]) }}"
open-type="{{ openType }}" open-type="{{ openType }}"
session-from="{{ sessionFrom }}" session-from="{{ sessionFrom }}"
app-parameter="{{ appParameter }}" app-parameter="{{ appParameter }}"

View File

@ -1,3 +1,5 @@
<wxs src="../wxs/utils.wxs" module="utils" />
<view <view
class="custom-class {{ classPrefix }} {{ utils.isSrc(name) ? 'van-icon--image' : classPrefix + '-' + name }}" class="custom-class {{ classPrefix }} {{ utils.isSrc(name) ? 'van-icon--image' : classPrefix + '-' + name }}"
style="{{ color ? 'color: ' + color + ';' : '' }}{{ size ? 'font-size: ' + size + ';' : '' }}{{ customStyle }}" style="{{ color ? 'color: ' + color + ';' : '' }}{{ size ? 'font-size: ' + size + ';' : '' }}{{ customStyle }}"
@ -14,5 +16,3 @@
src="{{ name }}" src="{{ name }}"
/> />
</view> </view>
<wxs src="../common/utils.wxs" module="utils" />

5
packages/wxs/array.wxs Normal file
View File

@ -0,0 +1,5 @@
function isArray(obj) {
return obj && obj.constructor === 'Array';
}
module.exports.isArray = isArray;

34
packages/wxs/bem.wxs Normal file
View File

@ -0,0 +1,34 @@
var array = require('./array.wxs');
var object = require('./object.wxs');
var PREFIX = 'van-';
function join(name, mods) {
name = PREFIX + name;
mods = mods.map(function(mod) {
return name + '--' + mod;
});
mods.unshift(name);
return mods.join(' ');
}
function traversing(mods, conf) {
if (typeof conf === 'string') {
mods.push(conf);
} else if (array.isArray(conf)) {
conf.forEach(function(item) {
traversing(mods, item);
});
} else if (typeof conf === 'object') {
object.keys(conf).forEach(function(key) {
conf[key] && mods.push(key);
});
}
}
function bem(name, conf) {
var mods = [];
traversing(mods, conf);
return join(name, mods);
}
module.exports.bem = bem;

13
packages/wxs/object.wxs Normal file
View File

@ -0,0 +1,13 @@
/* eslint-disable */
var REGEXP = getRegExp('{|}|"', 'g');
function keys(obj) {
return JSON.stringify(obj)
.replace(REGEXP, '')
.split(',')
.map(function(item) {
return item.split(':')[0];
});
}
module.exports.keys = keys;

View File

@ -1,5 +1,8 @@
var bem = require('./bem.wxs').bem;
function isSrc(url) { function isSrc(url) {
return url.indexOf('http') === 0 || url.indexOf('data:image') === 0; return url.indexOf('http') === 0 || url.indexOf('data:image') === 0;
} }
module.exports.bem = bem;
module.exports.isSrc = isSrc; module.exports.isSrc = isSrc;