From be3f2cbce9b543e5abd475c9cad2b4e03f35af53 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Mon, 14 Sep 2020 19:22:09 +0800 Subject: [PATCH] chore(CouponList): refactor with composition api --- package.json | 2 +- src/coupon-list/index.js | 284 +++++++++++++++++++-------------------- yarn.lock | 8 +- 3 files changed, 145 insertions(+), 149 deletions(-) diff --git a/package.json b/package.json index 8a43ddfe0..27236e84c 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "dependencies": { "@babel/runtime": "7.x", "@vant/icons": "1.3.0", - "@vant/use": "^0.0.1", + "@vant/use": "^0.0.2", "vue-lazyload": "1.2.3" }, "peerDependencies": { diff --git a/src/coupon-list/index.js b/src/coupon-list/index.js index c909246b8..5a6d09207 100644 --- a/src/coupon-list/index.js +++ b/src/coupon-list/index.js @@ -1,6 +1,12 @@ +import { watch, computed, nextTick, onMounted, reactive } from 'vue'; + // Utils import { createNamespace } from '../utils'; +// Composition +import { useWindowSize } from '@vant/use'; +import { useRefs } from '../composition/use-refs'; + // Components import Tab from '../tab'; import Tabs from '../tabs'; @@ -65,178 +71,168 @@ export default createComponent({ emits: ['change', 'exchange', 'update:code'], - data() { - return { + setup(props, { emit }) { + const [couponRefs, setCouponRefs] = useRefs(); + + const state = reactive({ tab: 0, - winHeight: window.innerHeight, - currentCode: this.code || '', - }; - }, + code: props.code || '', + }); - computed: { - buttonDisabled() { - return ( - !this.exchangeButtonLoading && - (this.exchangeButtonDisabled || - !this.currentCode || - this.currentCode.length < this.exchangeMinLength) - ); - }, + const { height: windowHeight } = useWindowSize(); - listStyle() { - return { - height: this.winHeight - (this.showExchangeBar ? 140 : 94) + 'px', - }; - }, - }, + const buttonDisabled = computed( + () => + !props.exchangeButtonLoading && + (props.exchangeButtonDisabled || + !state.code || + state.code.length < props.exchangeMinLength) + ); - watch: { - code(code) { - this.currentCode = code; - }, + const listStyle = computed(() => ({ + height: windowHeight.value - (props.showExchangeBar ? 140 : 94) + 'px', + })); - currentCode(code) { - this.$emit('update:code', code); - }, - - displayedCouponIndex: 'scrollToShowCoupon', - }, - - mounted() { - this.scrollToShowCoupon(this.displayedCouponIndex); - }, - - methods: { - onClickExchangeButton() { - this.$emit('exchange', this.currentCode); + const onExchange = () => { + emit('exchange', state.code); // auto clear currentCode when not use vModel - if (!this.code) { - this.currentCode = ''; + if (!props.code) { + state.code = ''; } - }, + }; - // scroll to show specific coupon - scrollToShowCoupon(index) { - if (index === -1) { - return; - } - - this.$nextTick(() => { - const { card, list } = this.$refs; - - /* istanbul ignore next */ - if (list && card && card[index]) { - list.scrollTop = card[index].$el.offsetTop - 100; + const scrollToCoupon = (index) => { + nextTick(() => { + if (couponRefs.value[index]) { + couponRefs.value[index].scrollIntoView(); } }); - }, + }; - genEmpty() { - return ( -
- -

{t('empty')}

-
- ); - }, - - genExchangeButton() { - return ( -