chore(Sku): prettier code

This commit is contained in:
chenjiahan 2020-06-23 11:57:07 +08:00
parent 3350120cba
commit 92a0e632c4
5 changed files with 68 additions and 64 deletions

View File

@ -45,7 +45,7 @@ export default {
}; };
``` ```
### Custom Stepper Config ### Custom Stepper
```html ```html
<van-sku <van-sku

View File

@ -28,12 +28,14 @@ const { QUOTA_LIMIT } = LIMIT_TYPE;
export default createComponent({ export default createComponent({
props: { props: {
sku: Object, sku: Object,
priceTag: String,
goods: Object, goods: Object,
value: Boolean, value: Boolean,
buyText: String, buyText: String,
goodsId: [Number, String], goodsId: [Number, String],
priceTag: String,
lazyLoad: Boolean,
hideStock: Boolean, hideStock: Boolean,
properties: Array,
addCartText: String, addCartText: String,
stepperTitle: String, stepperTitle: String,
getContainer: [String, Function], getContainer: [String, Function],
@ -44,7 +46,6 @@ export default createComponent({
closeOnClickOverlay: Boolean, closeOnClickOverlay: Boolean,
disableStepperInput: Boolean, disableStepperInput: Boolean,
resetSelectedSkuOnHide: Boolean, resetSelectedSkuOnHide: Boolean,
properties: Array,
quota: { quota: {
type: Number, type: Number,
default: 0, default: 0,
@ -102,7 +103,6 @@ export default createComponent({
type: Boolean, type: Boolean,
default: true, default: true,
}, },
lazyLoad: Boolean,
}, },
data() { data() {
@ -117,6 +117,7 @@ export default createComponent({
watch: { watch: {
show(val) { show(val) {
this.$emit('input', val); this.$emit('input', val);
if (!val) { if (!val) {
this.$emit('sku-close', { this.$emit('sku-close', {
selectedSkuValues: this.selectedSkuValues, selectedSkuValues: this.selectedSkuValues,
@ -161,7 +162,6 @@ export default createComponent({
return; return;
} }
// header高度82px, sku actions高度50px如果改动了样式自己传下bodyOffsetTop调整下
const maxHeight = window.innerHeight - this.bodyOffsetTop; const maxHeight = window.innerHeight - this.bodyOffsetTop;
return { return {
@ -174,15 +174,11 @@ export default createComponent({
if (this.hasSku && !isAllSelected(this.skuTree, this.selectedSku)) { if (this.hasSku && !isAllSelected(this.skuTree, this.selectedSku)) {
return false; return false;
} }
// 属性未全选 // 属性未全选
if ( return !this.propList.some(
this.propList.some(
(it) => (this.selectedProp[it.k_id] || []).length < 1 (it) => (this.selectedProp[it.k_id] || []).length < 1
) );
) {
return false;
}
return true;
}, },
isSkuEmpty() { isSkuEmpty() {
@ -614,6 +610,7 @@ export default createComponent({
sku, sku,
goods, goods,
price, price,
lazyLoad,
originPrice, originPrice,
skuEventBus, skuEventBus,
selectedSku, selectedSku,
@ -622,8 +619,8 @@ export default createComponent({
stepperTitle, stepperTitle,
selectedSkuComb, selectedSkuComb,
showHeaderImage, showHeaderImage,
lazyLoad = false,
} = this; } = this;
const slotsProps = { const slotsProps = {
price, price,
originPrice, originPrice,
@ -632,6 +629,7 @@ export default createComponent({
selectedSku, selectedSku,
selectedSkuComb, selectedSkuComb,
}; };
const slots = (name) => this.slots(name, slotsProps); const slots = (name) => this.slots(name, slotsProps);
const Header = slots('sku-header') || ( const Header = slots('sku-header') || (
@ -694,12 +692,12 @@ export default createComponent({
> >
<SkuRowItem <SkuRowItem
skuList={sku.list} skuList={sku.list}
lazyLoad={lazyLoad}
skuValue={skuValue} skuValue={skuValue}
skuKeyStr={skuTreeItem.k_s}
selectedSku={selectedSku} selectedSku={selectedSku}
skuEventBus={skuEventBus} skuEventBus={skuEventBus}
skuKeyStr={skuTreeItem.k_s}
largePicturePreview={skuTreeItem.large_picture_preview} largePicturePreview={skuTreeItem.large_picture_preview}
lazyLoad={lazyLoad}
></SkuRowItem> ></SkuRowItem>
</template> </template>
))} ))}

View File

@ -5,19 +5,23 @@ const [createComponent] = createNamespace('sku-row-item');
export default createComponent({ export default createComponent({
props: { props: {
lazyLoad: Boolean,
skuValue: Object, skuValue: Object,
skuKeyStr: String, skuKeyStr: String,
skuEventBus: Object, skuEventBus: Object,
selectedSku: Object, selectedSku: Object,
largePicturePreview: Boolean,
skuList: { skuList: {
type: Array, type: Array,
default: () => [], default: () => [],
}, },
largePicturePreview: Boolean,
lazyLoad: Boolean,
}, },
computed: { computed: {
imgUrl() {
return this.skuValue.imgUrl || this.skuValue.img_url;
},
choosable() { choosable() {
return isSkuChoosable(this.skuList, this.selectedSku, { return isSkuChoosable(this.skuList, this.selectedSku, {
key: this.skuKeyStr, key: this.skuKeyStr,
@ -35,45 +39,51 @@ export default createComponent({
}); });
} }
}, },
onPreviewImg(event) { onPreviewImg(event) {
event.stopPropagation(); event.stopPropagation();
this.skuEventBus.$emit( this.skuEventBus.$emit('sku:previewImage', this.imgUrl);
'sku:previewImage', },
this.skuValue.imgUrl || this.skuValue.img_url
genImage(classPrefix) {
const { imgUrl } = this;
if (imgUrl && this.largePicturePreview) {
if (this.lazyLoad) {
return (
<img class={`${classPrefix}-img`} src={imgUrl} vLazy={imgUrl} />
); );
}
return <img class={`${classPrefix}-img`} src={imgUrl} />;
}
}, },
}, },
render() { render() {
const choosed = this.skuValue.id === this.selectedSku[this.skuKeyStr]; const choosed = this.skuValue.id === this.selectedSku[this.skuKeyStr];
const imgUrl = this.skuValue.imgUrl || this.skuValue.img_url; const classPrefix = this.largePicturePreview
const BEM_NAME = this.largePicturePreview
? 'van-sku-row__picture-item' ? 'van-sku-row__picture-item'
: 'van-sku-row__item'; : 'van-sku-row__item';
return ( return (
<span <span
class={[ class={[
`${BEM_NAME}`, classPrefix,
choosed ? `${BEM_NAME}--active` : '', choosed ? `${classPrefix}--active` : '',
!this.choosable ? `${BEM_NAME}--disabled` : '', !this.choosable ? `${classPrefix}--disabled` : '',
]} ]}
onClick={this.onSelect} onClick={this.onSelect}
> >
{this.largePicturePreview && ( {this.largePicturePreview && (
<img <img
class={`${BEM_NAME}-img-icon`} class={`${classPrefix}-img-icon`}
src="https://img.yzcdn.cn/upload_files/2020/06/18/Fn6Qf0fGRFyuD8xh0Gi1w2ng59G1.png" src="https://img.yzcdn.cn/upload_files/2020/06/18/Fn6Qf0fGRFyuD8xh0Gi1w2ng59G1.png"
onClick={this.onPreviewImg} onClick={this.onPreviewImg}
/> />
)} )}
{imgUrl && {this.genImage(classPrefix)}
(this.largePicturePreview && this.lazyLoad ? ( <span class={`${classPrefix}-name`}>{this.skuValue.name}</span>
<img class={`${BEM_NAME}-img`} src={imgUrl} vLazy={imgUrl} />
) : (
<img class={`${BEM_NAME}-img`} src={imgUrl} />
))}
<span class={`${BEM_NAME}-name`}>{this.skuValue.name}</span>
</span> </span>
); );
}, },

View File

@ -1,21 +1,20 @@
<template> <template>
<demo-section> <demo-section>
<!-- 基础用法 -->
<demo-block :title="t('basicUsage')"> <demo-block :title="t('basicUsage')">
<div class="sku-container"> <div class="sku-container">
<van-sku <van-sku
v-model="showBase" v-model="showBase"
:sku="skuData.sku" :sku="skuData.sku"
:quota="skuData.quota"
:goods="skuData.goods_info" :goods="skuData.goods_info"
:goods-id="skuData.goods_id" :goods-id="skuData.goods_id"
:hide-stock="skuData.sku.hide_stock"
:quota="skuData.quota"
:quota-used="skuData.quota_used" :quota-used="skuData.quota_used"
:properties="skuData.properties"
:hide-stock="skuData.sku.hide_stock"
:message-config="messageConfig"
:start-sale-num="skuData.start_sale_num" :start-sale-num="skuData.start_sale_num"
:close-on-click-overlay="closeOnClickOverlay" :close-on-click-overlay="closeOnClickOverlay"
:message-config="messageConfig"
:custom-sku-validator="customSkuValidator" :custom-sku-validator="customSkuValidator"
:properties="skuData.properties"
disable-stepper-input disable-stepper-input
reset-stepper-on-hide reset-stepper-on-hide
safe-area-inset-bottom safe-area-inset-bottom
@ -29,21 +28,20 @@
</div> </div>
</demo-block> </demo-block>
<!-- 自定义步进器 --> <demo-block :title="t('customStepper')">
<demo-block :title="t('title2')">
<div class="sku-container"> <div class="sku-container">
<van-sku <van-sku
v-model="showStepper" v-model="showStepper"
:sku="skuData.sku" :sku="skuData.sku"
:quota="skuData.quota"
:goods="skuData.goods_info" :goods="skuData.goods_info"
:goods-id="skuData.goods_id" :goods-id="skuData.goods_id"
:hide-stock="skuData.sku.hide_stock"
:quota="skuData.quota"
:quota-used="skuData.quota_used" :quota-used="skuData.quota_used"
:start-sale-num="skuData.start_sale_num"
:custom-stepper-config="customStepperConfig"
:message-config="messageConfig"
:properties="skuData.properties" :properties="skuData.properties"
:hide-stock="skuData.sku.hide_stock"
:start-sale-num="skuData.start_sale_num"
:message-config="messageConfig"
:custom-stepper-config="customStepperConfig"
hide-quota-text hide-quota-text
safe-area-inset-bottom safe-area-inset-bottom
@buy-clicked="onBuyClicked" @buy-clicked="onBuyClicked"
@ -55,22 +53,21 @@
</div> </div>
</demo-block> </demo-block>
<!-- 隐藏售罄sku -->
<demo-block :title="t('hideSoldoutSku')"> <demo-block :title="t('hideSoldoutSku')">
<div class="sku-container"> <div class="sku-container">
<van-sku <van-sku
v-model="showSoldout" v-model="showSoldout"
:sku="skuData.sku" :sku="skuData.sku"
:quota="skuData.quota"
:goods="skuData.goods_info" :goods="skuData.goods_info"
:goods-id="skuData.goods_id" :goods-id="skuData.goods_id"
:hide-stock="skuData.sku.hide_stock"
:quota="skuData.quota"
:quota-used="skuData.quota_used" :quota-used="skuData.quota_used"
:start-sale-num="skuData.start_sale_num"
:custom-stepper-config="customStepperConfig"
:message-config="messageConfig"
:show-soldout-sku="false"
:properties="skuData.properties" :properties="skuData.properties"
:hide-stock="skuData.sku.hide_stock"
:message-config="messageConfig"
:start-sale-num="skuData.start_sale_num"
:show-soldout-sku="false"
:custom-stepper-config="customStepperConfig"
hide-quota-text hide-quota-text
safe-area-inset-bottom safe-area-inset-bottom
@buy-clicked="onBuyClicked" @buy-clicked="onBuyClicked"
@ -82,23 +79,22 @@
</div> </div>
</demo-block> </demo-block>
<!-- 大图模式 -->
<demo-block :title="t('largePicturePreview')"> <demo-block :title="t('largePicturePreview')">
<div class="sku-container"> <div class="sku-container">
<van-sku <van-sku
v-model="showLargePicturePreview" v-model="showLargePicturePreview"
:sku="skuData.sku" :sku="skuData.sku"
:quota="skuData.quota"
:goods="skuData.goods_info" :goods="skuData.goods_info"
:goods-id="skuData.goods_id" :goods-id="skuData.goods_id"
:hide-stock="skuData.sku.hide_stock" :hide-stock="skuData.sku.hide_stock"
:quota="skuData.quota"
:quota-used="skuData.quota_used"
:start-sale-num="skuData.start_sale_num"
:close-on-click-overlay="closeOnClickOverlay"
:message-config="messageConfig"
:custom-sku-validator="customSkuValidator"
:properties="skuData.properties" :properties="skuData.properties"
:quota-used="skuData.quota_used"
:message-config="messageConfig"
:start-sale-num="skuData.start_sale_num"
:show-header-image="false" :show-header-image="false"
:custom-sku-validator="customSkuValidator"
:close-on-click-overlay="closeOnClickOverlay"
disable-stepper-input disable-stepper-input
reset-stepper-on-hide reset-stepper-on-hide
safe-area-inset-bottom safe-area-inset-bottom
@ -185,22 +181,22 @@ import { LIMIT_TYPE } from '../constants';
export default { export default {
i18n: { i18n: {
'zh-CN': { 'zh-CN': {
title2: '自定义步进器',
button1: '积分兑换', button1: '积分兑换',
button2: '买买买', button2: '买买买',
actionsTop: '商品不多,赶快购买吧', actionsTop: '商品不多,赶快购买吧',
stepperTitle: '我要买', stepperTitle: '我要买',
customBySlot: '通过插槽定制', customBySlot: '通过插槽定制',
customStepper: '自定义步进器',
hideSoldoutSku: '隐藏售罄规格', hideSoldoutSku: '隐藏售罄规格',
largePicturePreview: '大图预览模式', largePicturePreview: '大图预览模式',
}, },
'en-US': { 'en-US': {
title2: 'Custom Stepper Related Config',
button1: 'Button', button1: 'Button',
button2: 'Button', button2: 'Button',
actionsTop: 'Action top info', actionsTop: 'Action top info',
customBySlot: 'Custom By Slot', customBySlot: 'Custom By Slot',
stepperTitle: 'Stepper title', stepperTitle: 'Stepper title',
customStepper: 'Custom Stepper',
hideSoldoutSku: 'Hide Soldout Sku', hideSoldoutSku: 'Hide Soldout Sku',
largePicturePreview: 'Large Picture Preview', largePicturePreview: 'Large Picture Preview',
}, },

View File

@ -14,7 +14,7 @@ exports[`renders demo correctly 1`] = `
<div class="sku-container"> <div class="sku-container">
<!----> <button class="van-button van-button--primary van-button--normal van-button--block"> <!----> <button class="van-button van-button--primary van-button--normal van-button--block">
<div class="van-button__content"><span class="van-button__text"> <div class="van-button__content"><span class="van-button__text">
自定义步进器
</span></div> </span></div>
</button></div> </button></div>
</div> </div>