vant/packages/sku/components/SkuRowItem.vue
2017-09-16 21:46:39 +08:00

50 lines
1.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<span v-if="isChoosable"
@click="onSkuSelected"
:class="{ 'van-sku-row__item': true, 'van-sku-row__item--active': isChoosed }">
{{ skuValue.name }}
</span>
<span v-else class="van-sku-row__item van-sku-row__item--disabled">{{ skuValue.name }}</span>
</template>
<script>
export default {
name: 'van-sku-row-item',
props: {
skuEventBus: Object,
skuValue: Object,
skuList: Array,
selectedSku: Object,
skuKeyStr: String
},
computed: {
isChoosed() {
return this.skuValue.id === this.selectedSku[this.skuKeyStr];
},
isChoosable() {
const matchedSku = Object.assign({}, this.selectedSku, {
[this.skuKeyStr]: this.skuValue.id
});
const skusToCheck = Object.keys(matchedSku).filter(skuKey => matchedSku[skuKey] !== '');
const filteredSku = this.skuList.filter(sku => {
return skusToCheck.every(skuKey => {
// 后端给的skuValue.id有时候是数字有时候是字符串全等会匹配不上
return matchedSku[skuKey] == sku[skuKey]; // eslint-disable-line
});
});
const stock = filteredSku.reduce((total, sku) => (total += sku.stock_num), 0);
return stock > 0;
}
},
methods: {
onSkuSelected() {
this.skuEventBus.$emit('sku:select', Object.assign({}, this.skuValue, { skuKeyStr: this.skuKeyStr }));
}
}
};
</script>