From 311947ad5c161da11faae7a65bda41f802350a46 Mon Sep 17 00:00:00 2001 From: BeADre Date: Sun, 24 Apr 2022 23:06:56 +0800 Subject: [PATCH] fix(pull-refresh): close #509 --- .../src/pull-refresh/PullRefresh.vue | 34 +++++++++++++++---- .../__snapshots__/index.spec.js.snap | 12 +++---- .../src/pull-refresh/__tests__/index.spec.js | 2 +- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/varlet-ui/src/pull-refresh/PullRefresh.vue b/packages/varlet-ui/src/pull-refresh/PullRefresh.vue index cf48eb3f14a..294f853e4b3 100644 --- a/packages/varlet-ui/src/pull-refresh/PullRefresh.vue +++ b/packages/varlet-ui/src/pull-refresh/PullRefresh.vue @@ -10,8 +10,8 @@
@@ -33,8 +33,7 @@ const { n, classes } = createNamespace('pull-refresh') const MAX_DISTANCE = 100 const CONTROL_POSITION = -50 - -let scroller: HTMLElement | Window +const ICON_TRANSITION = 150 export default defineComponent({ name: 'VarPullRefresh', @@ -43,12 +42,16 @@ export default defineComponent({ }, props, setup(props) { + let scroller: HTMLElement | Window + let changeResult: Promise + const freshNode: Ref = ref(null) const startPosition: Ref = ref(0) const distance: Ref = ref(CONTROL_POSITION) const iconName: Ref = ref('arrow-down') const refreshStatus: Ref = ref('default') const isEnd: Ref = ref(false) + const iconHasChanged: Ref = ref(true) const isTouchable = computed( () => refreshStatus.value !== 'loading' && refreshStatus.value !== 'success' && !props.disabled @@ -63,6 +66,15 @@ export default defineComponent({ const isSuccess = computed(() => refreshStatus.value === 'success') + const changeIcon = (): Promise => { + return new Promise((resolve) => { + window.setTimeout(() => { + iconHasChanged.value = true + resolve() + }, ICON_TRANSITION) + }) + } + const touchStart = (event: TouchEvent) => { if (!isTouchable.value) return @@ -79,15 +91,23 @@ export default defineComponent({ const moveDistance = (event.touches[0].clientY - startPosition.value) / 2 + CONTROL_POSITION distance.value = moveDistance >= MAX_DISTANCE ? MAX_DISTANCE : moveDistance - iconName.value = distance.value >= MAX_DISTANCE * 0.2 ? 'refresh' : 'arrow-down' + + if (distance.value >= MAX_DISTANCE * 0.2) { + iconHasChanged.value = false + iconName.value = 'refresh' + changeResult = changeIcon() + } else { + iconName.value = 'arrow-down' + } } - const touchEnd = () => { + const touchEnd = async () => { if (!isTouchable.value) return isEnd.value = true if (distance.value >= MAX_DISTANCE * 0.2) { + await changeResult refreshStatus.value = 'loading' distance.value = MAX_DISTANCE * 0.3 @@ -134,6 +154,8 @@ export default defineComponent({ return { n, classes, + iconHasChanged, + ICON_TRANSITION, refreshStatus, freshNode, touchStart, diff --git a/packages/varlet-ui/src/pull-refresh/__tests__/__snapshots__/index.spec.js.snap b/packages/varlet-ui/src/pull-refresh/__tests__/__snapshots__/index.spec.js.snap index 108007a4026..9a5f8d3786c 100644 --- a/packages/varlet-ui/src/pull-refresh/__tests__/__snapshots__/index.spec.js.snap +++ b/packages/varlet-ui/src/pull-refresh/__tests__/__snapshots__/index.spec.js.snap @@ -2,21 +2,21 @@ exports[`test disabled prop 1`] = ` "
-
+
" `; exports[`test pull-refresh event 1`] = ` "
-
+
" `; exports[`test pullRefresh example 1`] = ` "
-
+
@@ -102,21 +102,21 @@ exports[`test pullRefresh example 1`] = ` exports[`test the different states during the drop-down process 1`] = ` "
-
+
" `; exports[`test the different states during the drop-down process 2`] = ` "
-
+
" `; exports[`test the different states during the drop-down process 3`] = ` "
-
+
" `; diff --git a/packages/varlet-ui/src/pull-refresh/__tests__/index.spec.js b/packages/varlet-ui/src/pull-refresh/__tests__/index.spec.js index efb9056c3cb..90fbc98ad77 100644 --- a/packages/varlet-ui/src/pull-refresh/__tests__/index.spec.js +++ b/packages/varlet-ui/src/pull-refresh/__tests__/index.spec.js @@ -108,7 +108,7 @@ test('test pull-refresh event', async () => { await trigger(el, 'touchmove', 0, 200) await trigger(el, 'touchend', 0, 150) - await delay(0) + await delay(200) expect(wrapper.vm.isRefresh).toBe(true) expect(refresh).toHaveBeenCalledTimes(1)