import { watch, computed, nextTick, onMounted, reactive } from 'vue'; // Utils import { createNamespace } from '../utils'; // Composition import { useWindowSize } from '@vant/use'; import { useRefs } from '../composables/use-refs'; // Components import Tab from '../tab'; import Tabs from '../tabs'; import Field from '../field'; import Button from '../button'; import Coupon from '../coupon'; const [createComponent, bem, t] = createNamespace('coupon-list'); const EMPTY_IMAGE = 'https://img.yzcdn.cn/vant/coupon-empty.png'; export default createComponent({ props: { code: String, enabledTitle: String, disabledTitle: String, closeButtonText: String, inputPlaceholder: 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, }, showCount: { type: Boolean, default: true, }, currency: { type: String, default: '¥', }, emptyImage: { type: String, default: EMPTY_IMAGE, }, }, emits: ['change', 'exchange', 'update:code'], setup(props, { emit }) { const [couponRefs, setCouponRefs] = useRefs(); const state = reactive({ tab: 0, code: props.code || '', }); const { height: windowHeight } = useWindowSize(); const buttonDisabled = computed( () => !props.exchangeButtonLoading && (props.exchangeButtonDisabled || !state.code || state.code.length < props.exchangeMinLength) ); const listStyle = computed(() => ({ height: windowHeight.value - (props.showExchangeBar ? 140 : 94) + 'px', })); const onExchange = () => { emit('exchange', state.code); // auto clear currentCode when not use vModel if (!props.code) { state.code = ''; } }; const scrollToCoupon = (index) => { nextTick(() => { if (couponRefs.value[index]) { couponRefs.value[index].scrollIntoView(); } }); }; const renderEmpty = () => (
{t('empty')}