Skip to content

Commit b486091

Browse files
mufengfangafc163
andauthored
fix: startPos may be undefined (#194)
* fix: startPos may be undefined * feat: add multi-touch test case Co-authored-by: afc163 <[email protected]>
1 parent 8af7caf commit b486091

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/DrawerChild.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ interface IState {
2525
prevProps?: IDrawerChildProps;
2626
}
2727

28+
interface Point {
29+
x: number;
30+
y: number;
31+
}
32+
2833
class DrawerChild extends React.Component<IDrawerChildProps, IState> {
2934
public static getDerivedStateFromProps(
3035
props: IDrawerChildProps,
@@ -64,10 +69,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
6469

6570
private passive: { passive: boolean } | boolean;
6671

67-
private startPos: {
68-
x: number;
69-
y: number;
70-
};
72+
private startPos: Point | null;
7173

7274
constructor(props: IDrawerChildProps) {
7375
super(props);
@@ -158,6 +160,8 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
158160

159161
private removeStartHandler = (e: React.TouchEvent | TouchEvent) => {
160162
if (e.touches.length > 1) {
163+
// need clear the startPos when another touch event happens
164+
this.startPos = null;
161165
return;
162166
}
163167
this.startPos = {
@@ -167,6 +171,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
167171
};
168172

169173
private removeMoveHandler = (e: React.TouchEvent | TouchEvent) => {
174+
// the startPos may be null or undefined
170175
if (e.changedTouches.length > 1 || !this.startPos) {
171176
return;
172177
}

tests/index.spec.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import * as React from 'react';
33
import toJson from 'enzyme-to-json';
44
import Drawer from '../src';
55

6+
interface Point {
7+
x: number;
8+
y: number;
9+
}
10+
611
function Div(props) {
712
const { show, ...otherProps } = props;
813
return (
@@ -44,6 +49,18 @@ function createMoveTouchEventObject({ x = 0, y = 0 }) {
4449
};
4550
}
4651

52+
function createMultiStartTouchEventObject(points: Point[]) {
53+
return { touches: points.map(point => createClientXY(point.x, point.y)) };
54+
}
55+
56+
function createMultiMoveTouchEventObject(points: Point[]) {
57+
const touches = points.map(point => createClientXY(point.x, point.y));
58+
return {
59+
touches,
60+
changedTouches: touches,
61+
};
62+
}
63+
4764
describe('rc-drawer-menu', () => {
4865
let instance;
4966
it('single drawer', () => {
@@ -90,6 +107,27 @@ describe('rc-drawer-menu', () => {
90107
content.simulate('touchStart', createStartTouchEventObject({ x: 0, y: 0 }));
91108
content.simulate('touchMove', createMoveTouchEventObject({ x: 0, y: 10 }));
92109
content.simulate('touchEnd', createMoveTouchEventObject({ x: 0, y: 10 }));
110+
content.simulate(
111+
'touchStart',
112+
createMultiStartTouchEventObject([
113+
{ x: 0, y: 0 },
114+
{ x: 100, y: 100 },
115+
]),
116+
);
117+
content.simulate(
118+
'touchMove',
119+
createMultiMoveTouchEventObject([
120+
{ x: 0, y: 10 },
121+
{ x: 100, y: 120 },
122+
]),
123+
);
124+
content.simulate(
125+
'touchEnd',
126+
createMultiMoveTouchEventObject([
127+
{ x: 0, y: 10 },
128+
{ x: 100, y: 120 },
129+
]),
130+
);
93131
console.log('transform is empty:', drawer.style.transform);
94132
expect(drawer.style.transform).toEqual('');
95133
});

0 commit comments

Comments
 (0)