From 6d8ed4560894ae7aee00d492b3cdf8bc5b213b78 Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 4 Mar 2021 20:41:53 +0800 Subject: [PATCH] feat(PullRefresh): add pull-distance prop (#8280) --- src/pull-refresh/README.md | 1 + src/pull-refresh/README.zh-CN.md | 1 + src/pull-refresh/index.tsx | 14 ++++++++------ .../test/__snapshots__/index.spec.ts.snap | 14 ++++++++++++++ src/pull-refresh/test/index.spec.ts | 14 ++++++++++++++ 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/pull-refresh/README.md b/src/pull-refresh/README.md index 2cc49ba47..498e97c2d 100644 --- a/src/pull-refresh/README.md +++ b/src/pull-refresh/README.md @@ -110,6 +110,7 @@ Use slots to custom tips. | success-duration | Success text display duration(ms) | _number \| string_ | `500` | | animation-duration | Animation duration | _number \| string_ | `300` | | head-height | Height of head | _number \| string_ | `50` | +| pull-distance `v3.0.8` | The distance to trigger the pull refresh | _number \| string_ | same as `head-height` | | disabled | Whether to disable pull refresh | _boolean_ | `false` | ### Events diff --git a/src/pull-refresh/README.zh-CN.md b/src/pull-refresh/README.zh-CN.md index 43ff275bd..6fb723dbe 100644 --- a/src/pull-refresh/README.zh-CN.md +++ b/src/pull-refresh/README.zh-CN.md @@ -117,6 +117,7 @@ export default { | success-duration | 刷新成功提示展示时长(ms) | _number \| string_ | `500` | | animation-duration | 动画时长 | _number \| string_ | `300` | | head-height | 顶部内容高度 | _number \| string_ | `50` | +| pull-distance `v3.0.8` | 触发下拉刷新的距离 | _number \| string_ | 与 `head-height` 一致 | | disabled | 是否禁用下拉刷新 | _boolean_ | `false` | ### Events diff --git a/src/pull-refresh/index.tsx b/src/pull-refresh/index.tsx index 45b9b4c54..461864b61 100644 --- a/src/pull-refresh/index.tsx +++ b/src/pull-refresh/index.tsx @@ -29,6 +29,7 @@ export default createComponent({ pullingText: String, loosingText: String, loadingText: String, + pullDistance: [Number, String], modelValue: { type: Boolean, default: false, @@ -77,13 +78,13 @@ export default createComponent({ !props.disabled; const ease = (distance: number) => { - const headHeight = +props.headHeight; + const pullDistance = +(props.pullDistance || props.headHeight); - if (distance > headHeight) { - if (distance < headHeight * 2) { - distance = headHeight + (distance - headHeight) / 2; + if (distance > pullDistance) { + if (distance < pullDistance * 2) { + distance = pullDistance + (distance - pullDistance) / 2; } else { - distance = headHeight * 1.5 + (distance - headHeight * 2) / 4; + distance = pullDistance * 1.5 + (distance - pullDistance * 2) / 4; } } @@ -91,13 +92,14 @@ export default createComponent({ }; const setStatus = (distance: number, isLoading?: boolean) => { + const pullDistance = +(props.pullDistance || props.headHeight); state.distance = distance; if (isLoading) { state.status = 'loading'; } else if (distance === 0) { state.status = 'normal'; - } else if (distance < props.headHeight) { + } else if (distance < pullDistance) { state.status = 'pulling'; } else { state.status = 'loosing'; diff --git a/src/pull-refresh/test/__snapshots__/index.spec.ts.snap b/src/pull-refresh/test/__snapshots__/index.spec.ts.snap index 43009af82..43b18a3cb 100644 --- a/src/pull-refresh/test/__snapshots__/index.spec.ts.snap +++ b/src/pull-refresh/test/__snapshots__/index.spec.ts.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`should allow to custom pull distance 1`] = ` +
+
+
+
+ Pull to refresh... +
+
+
+
+`; + exports[`should render different head content in different pulling status 1`] = `
{ const head = wrapper.find('.van-pull-refresh__head'); expect(head.style.height).toEqual('100px'); }); + +test('should allow to custom pull distance', async () => { + const wrapper = mount(PullRefresh, { + props: { + pullDistance: 300, + }, + }); + const track = wrapper.find('.van-pull-refresh__track'); + + trigger(track, 'touchstart', 0, 0); + trigger(track, 'touchmove', 0, 100); + await later(); + expect(wrapper.html()).toMatchSnapshot(); +});