Skip to content

Commit 897386c

Browse files
authored
force defaults for undefined configs (#296)
1 parent 270c63f commit 897386c

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

Diff for: CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# unreleased
2+
* Fix issue with some config properties being set to `undefined` breaking swipeable
3+
* [PR #296](https://github.com/formidablelabs/react-swipeable/pull/296)
4+
* explicitly set `undefined` config props to config defaults
5+
* Thank you [@simonflk](https://github.com/simonflk)
6+
17
# v6.2.0
28
* `delta` prop can now be an `object` specifying different values for each direction
39
* [PR #260](https://github.com/formidablelabs/react-swipeable/pull/260)

Diff for: __tests__/useSwipeable.spec.tsx

+28
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,34 @@ describe("useSwipeable", () => {
151151
expect(onTap).not.toHaveBeenCalled();
152152
});
153153

154+
it("handles touch events and fires correct props with undefined values for config", () => {
155+
const swipeFuncs = getMockedSwipeFunctions();
156+
const undefinedConfigOptions: SwipeableProps = {
157+
delta: undefined,
158+
preventDefaultTouchmoveEvent: undefined,
159+
rotationAngle: undefined,
160+
trackMouse: undefined,
161+
trackTouch: undefined,
162+
};
163+
const { getByText } = render(
164+
<SwipeableUsingHook {...swipeFuncs} {...undefinedConfigOptions} />
165+
);
166+
167+
const touchArea = getByText(TESTING_TEXT);
168+
169+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
170+
fireEvent[TM](touchArea, cte({ x: 100, y: 125 }));
171+
fireEvent[TM](touchArea, cte({ x: 100, y: 150 }));
172+
fireEvent[TE](touchArea, cte({}));
173+
174+
expect(swipeFuncs.onSwiped).toHaveBeenCalled();
175+
expect(swipeFuncs.onSwipedDown).toHaveBeenCalled();
176+
expect(swipeFuncs.onSwipedUp).not.toHaveBeenCalled();
177+
expect(swipeFuncs.onSwipedLeft).not.toHaveBeenCalled();
178+
expect(swipeFuncs.onSwipedRight).not.toHaveBeenCalled();
179+
expect(swipeFuncs.onSwiping).toHaveBeenCalledTimes(2);
180+
});
181+
154182
it("handles mouse events with trackMouse prop and fires correct props", () => {
155183
const swipeFuncs = getMockedSwipeFunctions();
156184
const { getByText } = render(

Diff for: src/index.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
LEFT,
1010
RIGHT,
1111
Setter,
12+
ConfigurationOptions,
1213
SwipeableCallbacks,
1314
SwipeableHandlers,
1415
SwipeableProps,
@@ -34,7 +35,7 @@ export {
3435
Vector2,
3536
};
3637

37-
const defaultProps = {
38+
const defaultProps: ConfigurationOptions = {
3839
delta: 10,
3940
preventDefaultTouchmoveEvent: false,
4041
rotationAngle: 0,
@@ -253,7 +254,7 @@ function getHandlers(
253254
// if new DOM el clean up old DOM and reset cleanUpTouch
254255
if (state.el && state.el !== el && state.cleanUpTouch) {
255256
state.cleanUpTouch();
256-
addState.cleanUpTouch = undefined;
257+
addState.cleanUpTouch = void 0;
257258
}
258259
// only attach if we want to track touch
259260
if (props.trackTouch && el) {
@@ -290,7 +291,7 @@ function updateTransientState(
290291
// clean up touch handlers if no longer tracking touches
291292
if (!props.trackTouch && state.cleanUpTouch) {
292293
state.cleanUpTouch();
293-
addState.cleanUpTouch = undefined;
294+
addState.cleanUpTouch = void 0;
294295
} else if (props.trackTouch && !state.cleanUpTouch) {
295296
// attach/re-attach touch handlers
296297
if (state.el) {
@@ -309,7 +310,20 @@ export function useSwipeable(options: SwipeableProps): SwipeableHandlers {
309310
const transientProps = React.useRef<SwipeablePropsWithDefaultOptions>({
310311
...defaultProps,
311312
});
312-
transientProps.current = { ...defaultProps, ...options };
313+
transientProps.current = {
314+
...defaultProps,
315+
...options,
316+
// Force defaults for config properties
317+
delta: options.delta === void 0 ? defaultProps.delta : options.delta,
318+
rotationAngle:
319+
options.rotationAngle === void 0
320+
? defaultProps.rotationAngle
321+
: options.rotationAngle,
322+
trackTouch:
323+
options.trackTouch === void 0
324+
? defaultProps.trackTouch
325+
: options.trackTouch,
326+
};
313327

314328
const [handlers, attachTouch] = React.useMemo(
315329
() =>

0 commit comments

Comments
 (0)