From 6e026e18c3a71f6509fba9e0ddd83906a5674ff1 Mon Sep 17 00:00:00 2001 From: neverland Date: Sat, 13 Mar 2021 18:19:42 +0800 Subject: [PATCH] fix(Swipe): incorrect item width after scaled (#8330) --- src/swipe/Swipe.tsx | 8 +- .../test/__snapshots__/demo.spec.ts.snap | 82 ++++++------------- .../test/__snapshots__/index.spec.js.snap | 48 +++++++---- src/swipe/test/demo.spec.ts | 15 +--- src/swipe/test/index.spec.js | 40 +++------ 5 files changed, 72 insertions(+), 121 deletions(-) diff --git a/src/swipe/Swipe.tsx b/src/swipe/Swipe.tsx index b552d1be0..70d2408b3 100644 --- a/src/swipe/Swipe.tsx +++ b/src/swipe/Swipe.tsx @@ -23,7 +23,6 @@ import { // Composables import { - useRect, doubleRaf, useChildren, useWindowSize, @@ -95,7 +94,7 @@ export default defineComponent({ setup(props, { emit, slots }) { const root = ref(); const state = reactive({ - rect: null as DOMRect | null, + rect: null as { width: number; height: number } | null, width: 0, height: 0, offset: 0, @@ -283,7 +282,10 @@ export default defineComponent({ stopAutoplay(); - const rect = useRect(root); + const rect = { + width: root.value.offsetWidth, + height: root.value.offsetHeight, + }; if (count.value) { active = Math.min(count.value - 1, active); diff --git a/src/swipe/test/__snapshots__/demo.spec.ts.snap b/src/swipe/test/__snapshots__/demo.spec.ts.snap index 7454a27da..038471e3f 100644 --- a/src/swipe/test/__snapshots__/demo.spec.ts.snap +++ b/src/swipe/test/__snapshots__/demo.spec.ts.snap @@ -3,27 +3,19 @@ exports[`should render demo and match snapshot 1`] = `
-
-
+
1
-
+
2
-
+
3
-
+
4
@@ -43,26 +35,18 @@ exports[`should render demo and match snapshot 1`] = `
-
-
+
-
+
-
+
-
+
@@ -80,27 +64,19 @@ exports[`should render demo and match snapshot 1`] = `
-
-
+
1
-
+
2
-
+
3
-
+
4
@@ -122,26 +98,26 @@ exports[`should render demo and match snapshot 1`] = `
-
1
2
3
4
@@ -202,27 +178,19 @@ exports[`should render demo and match snapshot 1`] = `
-
-
+
1
-
+
2
-
+
3
-
+
4
diff --git a/src/swipe/test/__snapshots__/index.spec.js.snap b/src/swipe/test/__snapshots__/index.spec.js.snap index 68f97cce8..729711266 100644 --- a/src/swipe/test/__snapshots__/index.spec.js.snap +++ b/src/swipe/test/__snapshots__/index.spec.js.snap @@ -1,22 +1,24 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`should not allow to drag swipe when touchable is false 1`] = ` -
+
1
2
3
@@ -33,7 +35,9 @@ exports[`should not allow to drag swipe when touchable is false 1`] = ` `; exports[`should render dynamic SwipeItem correctly 1`] = ` -
+
@@ -42,7 +46,9 @@ exports[`should render dynamic SwipeItem correctly 1`] = ` `; exports[`should render dynamic SwipeItem correctly 2`] = ` -
+
@@ -71,22 +77,24 @@ exports[`should render dynamic SwipeItem correctly 2`] = ` `; exports[`should render initial swipe correctly 1`] = ` -
+
1
2
3
@@ -103,22 +111,24 @@ exports[`should render initial swipe correctly 1`] = ` `; exports[`should render initial swipe correctly 2`] = ` -
+
1
2
3
@@ -135,22 +145,24 @@ exports[`should render initial swipe correctly 2`] = ` `; exports[`should render vertical swipe when using vertical prop 1`] = ` -
+
1
2
3
diff --git a/src/swipe/test/demo.spec.ts b/src/swipe/test/demo.spec.ts index fc2544d84..c0e0c95b9 100644 --- a/src/swipe/test/demo.spec.ts +++ b/src/swipe/test/demo.spec.ts @@ -1,17 +1,4 @@ import Demo from '../demo/index.vue'; import { snapshotDemo } from '../../../test/demo'; -import { mockGetBoundingClientRect } from '../../../test'; -let restore: () => void; - -snapshotDemo(Demo, { - beforeTest: () => { - restore = mockGetBoundingClientRect({ - width: 100, - height: 100, - }); - }, - afterTest: () => { - restore(); - }, -}); +snapshotDemo(Demo); diff --git a/src/swipe/test/index.spec.js b/src/swipe/test/index.spec.js index ecece9c4c..55aa0227c 100644 --- a/src/swipe/test/index.spec.js +++ b/src/swipe/test/index.spec.js @@ -1,27 +1,8 @@ import { ref, onMounted } from 'vue'; -import { - mount, - later, - trigger, - triggerDrag, - mockGetBoundingClientRect, -} from '../../../test'; +import { mount, later, trigger, triggerDrag } from '../../../test'; import { Swipe } from '..'; import { SwipeItem } from '../../swipe-item'; -let restore; - -beforeEach(() => { - restore = mockGetBoundingClientRect({ - width: 100, - height: 100, - }); -}); - -afterEach(() => { - restore(); -}); - function mockPageHidden() { let hidden = true; @@ -33,6 +14,11 @@ function mockPageHidden() { hidden = false; } +const swipeStyle = { + width: '100px', + height: '100px', +}; + const Component = { props: { vertical: Boolean, @@ -54,15 +40,11 @@ const Component = { }, }, render() { - const style = { - width: '100px', - height: '100px', - }; return ( - - 1 - 2 - 3 + + 1 + 2 + 3 ); }, @@ -309,7 +291,7 @@ test('should render dynamic SwipeItem correctly', async () => { }, render() { return ( - + {this.render && [ 1