2018-03-19 20:49:35 +08:00

168 lines
3.7 KiB
Vue

<template>
<div class="van-coupon-list">
<cell-group class="van-coupon-list__top" v-if="showExchangeBar">
<field
class="van-coupon-list__filed van-hairline--surround"
v-model="currentCode"
:placeholder="inputPlaceholder || $t('placeholder')"
:maxlength="20"
/>
<van-button
size="small"
type="danger"
class="van-coupon-list__exchange"
:text="exchangeButtonText || $t('exchange')"
:loading="exchangeButtonLoading"
:disabled="buttonDisabled"
@click="onClickExchangeButton"
/>
</cell-group>
<div class="van-coupon-list__list" :class="{ 'van-coupon-list--with-exchange': showExchangeBar }" ref="list">
<coupon-item
ref="card"
v-for="(item, index) in coupons"
:key="item.id || item.name"
:data="item"
:chosen="index === chosenCoupon"
@click.native="$emit('change', index)"
/>
<h3 v-if="disabledCoupons.length">{{ disabledListTitle || $t('disabled') }}</h3>
<coupon-item
disabled
v-for="item in disabledCoupons"
:key="item.id || item.name"
:data="item"
/>
<div class="van-coupon-list__empty" v-if="!coupons.length && !disabledCoupons.length">
<img src="https://img.yzcdn.cn/v2/image/wap/trade/new_order/empty@2x.png" >
<p>{{ $t('empty') }}</p>
</div>
</div>
<div
v-show="showCloseButton"
v-text="closeButtonText || $t('close')"
class="van-coupon-list__close van-hairline--top"
@click="$emit('change', -1)"
/>
</div>
</template>
<script>
import create from '../utils/create';
import CouponItem from './Item';
import Field from '../field';
import VanButton from '../button';
export default create({
name: 'coupon-list',
components: {
VanButton,
Field,
CouponItem
},
model: {
prop: 'code'
},
props: {
code: String,
closeButtonText: String,
inputPlaceholder: String,
disabledListTitle: String,
exchangeButtonText: String,
exchangeButtonLoading: Boolean,
exchangeButtonDisabled: Boolean,
exchangeMinLength: {
type: Number,
default: 1
},
chosenCoupon: {
type: Number,
default: -1
},
coupons: {
type: Array,
default: () => []
},
disabledCoupons: {
type: Array,
default: () => []
},
displayedCouponIndex: {
type: Number,
default: -1
},
showExchangeBar: {
type: Boolean,
default: true
},
showCloseButton: {
type: Boolean,
default: true
}
},
data() {
return {
currentCode: this.code || ''
};
},
computed: {
buttonDisabled() {
return (
!this.exchangeButtonLoading &&
(this.exchangeButtonDisabled ||
this.currentCode.length < this.exchangeMinLength)
);
}
},
watch: {
code(code) {
this.currentCode = code;
},
currentCode(code) {
this.$emit('input', code);
},
displayedCouponIndex(val) {
this.scrollToShowCoupon(val);
}
},
mounted() {
this.scrollToShowCoupon(this.displayedCouponIndex);
},
methods: {
onClickExchangeButton() {
this.$emit('exchange', this.currentCode);
// auto clear currentCode when not use v-model
if (!this.code) {
this.currentCode = '';
}
},
// scroll to show specific coupon
scrollToShowCoupon(index) {
if (index === -1) {
return;
}
this.$nextTick(() => {
const { card, list } = this.$refs;
if (list && card && card[index]) {
list.scrollTop = card[index].$el.offsetTop - 100;
}
});
}
}
});
</script>