lulouis-vant
Version:
Lightweight Mobile UI Components built on Vue
228 lines (219 loc) • 5.67 kB
JavaScript
import { use } from '../utils';
import Tab from '../tab';
import Tabs from '../tabs';
import Field from '../field';
import Button from '../button';
import Coupon from '../coupon';
var _use = use('coupon-list'),
sfc = _use[0],
bem = _use[1],
t = _use[2];
var EMPTY_IMAGE = 'https://img.yzcdn.cn/v2/image/wap/trade/new_order/empty@2x.png';
export default sfc({
model: {
prop: 'code'
},
props: {
code: String,
coupons: Array,
disabledCoupons: Array,
closeButtonText: String,
inputPlaceholder: String,
exchangeButtonText: String,
exchangeButtonLoading: Boolean,
exchangeButtonDisabled: Boolean,
exchangeMinLength: {
type: Number,
default: 1
},
chosenCoupon: {
type: Number,
default: -1
},
displayedCouponIndex: {
type: Number,
default: -1
},
showExchangeBar: {
type: Boolean,
default: true
},
showCloseButton: {
type: Boolean,
default: true
},
currency: {
type: String,
default: '¥'
}
},
data: function data() {
return {
tab: 0,
winHeight: window.innerHeight,
currentCode: this.code || ''
};
},
computed: {
buttonDisabled: function buttonDisabled() {
return !this.exchangeButtonLoading && (this.exchangeButtonDisabled || !this.currentCode || this.currentCode.length < this.exchangeMinLength);
},
title: function title() {
return t('enable') + " (" + this.coupons.length + ")";
},
disabledTitle: function disabledTitle() {
return t('disabled') + " (" + this.disabledCoupons.length + ")";
},
listStyle: function listStyle() {
return {
height: this.winHeight - (this.showExchangeBar ? 140 : 94) + 'px'
};
}
},
watch: {
code: function code(_code) {
this.currentCode = _code;
},
currentCode: function currentCode(code) {
this.$emit('input', code);
},
displayedCouponIndex: function displayedCouponIndex(val) {
this.scrollToShowCoupon(val);
}
},
mounted: function mounted() {
this.scrollToShowCoupon(this.displayedCouponIndex);
},
methods: {
onClickExchangeButton: function 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: function scrollToShowCoupon(index) {
var _this = this;
if (index === -1) {
return;
}
this.$nextTick(function () {
var _this$$refs = _this.$refs,
card = _this$$refs.card,
list = _this$$refs.list;
/* istanbul ignore next */
if (list && card && card[index]) {
list.scrollTop = card[index].$el.offsetTop - 100;
}
});
}
},
render: function render(h) {
var _this2 = this;
var ExchangeBar = this.showExchangeBar && h(Field, {
"attrs": {
"clearable": true,
"border": false,
"placeholder": this.inputPlaceholder || t('placeholder'),
"maxlength": "20"
},
"class": bem('field'),
"model": {
value: _this2.currentCode,
callback: function callback($$v) {
_this2.currentCode = $$v;
}
}
}, [h(Button, {
"slot": "button",
"attrs": {
"size": "small",
"type": "danger",
"text": this.exchangeButtonText || t('exchange'),
"loading": this.exchangeButtonLoading,
"disabled": this.buttonDisabled
},
"class": bem('exchange'),
"on": {
"click": this.onClickExchangeButton
}
})]);
var Empty = h("div", {
"class": bem('empty')
}, [h("img", {
"attrs": {
"src": EMPTY_IMAGE
}
}), h("p", [t('empty')])]);
var onChange = function onChange(index) {
return function () {
return _this2.$emit('change', index);
};
};
var CouponTab = h(Tab, {
"attrs": {
"title": this.title
}
}, [h("div", {
"class": bem('list'),
"style": this.listStyle
}, [this.coupons.map(function (coupon, index) {
return h(Coupon, {
"ref": "card",
"key": coupon.id,
"attrs": {
"coupon": coupon,
"currency": _this2.currency,
"chosen": index === _this2.chosenCoupon
},
"nativeOn": {
"click": onChange(index)
}
});
}), !this.coupons.length && Empty])]);
var DisabledCouponTab = h(Tab, {
"attrs": {
"title": this.disabledTitle
}
}, [h("div", {
"class": bem('list'),
"style": this.listStyle
}, [this.disabledCoupons.map(function (coupon) {
return h(Coupon, {
"attrs": {
"disabled": true,
"coupon": coupon,
"currency": _this2.currency
},
"key": coupon.id
});
}), !this.disabledCoupons.length && Empty])]);
return h("div", {
"class": bem()
}, [ExchangeBar, h(Tabs, {
"class": bem('tab'),
"attrs": {
"line-width": 120
},
"model": {
value: _this2.tab,
callback: function callback($$v) {
_this2.tab = $$v;
}
}
}, [CouponTab, DisabledCouponTab]), h(Button, {
"directives": [{
name: "show",
value: this.showCloseButton
}],
"attrs": {
"size": "large",
"text": this.closeButtonText || t('close')
},
"class": bem('close'),
"on": {
"click": onChange(-1)
}
})]);
}
});