Skip to content

Commit f6d5ff3

Browse files
author
Emil Hartz
committed
force defaults for undefined configs
1 parent 95ca939 commit f6d5ff3

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

__tests__/useSwipeable.spec.tsx

+33
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,39 @@ 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 onTap = jest.fn();
156+
const swipeFuncs = getMockedSwipeFunctions();
157+
const undefinedConfigOptions: SwipeableProps = {
158+
delta: undefined,
159+
preventDefaultTouchmoveEvent: undefined,
160+
rotationAngle: undefined,
161+
trackMouse: undefined,
162+
trackTouch: undefined,
163+
};
164+
const { getByText } = render(
165+
<SwipeableUsingHook
166+
{...swipeFuncs}
167+
onTap={onTap}
168+
{...undefinedConfigOptions}
169+
/>
170+
);
171+
172+
const touchArea = getByText(TESTING_TEXT);
173+
174+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
175+
fireEvent[TM](touchArea, cte({ x: 100, y: 125 }));
176+
fireEvent[TM](touchArea, cte({ x: 100, y: 150 }));
177+
fireEvent[TE](touchArea, cte({}));
178+
179+
expect(swipeFuncs.onSwiped).toHaveBeenCalled();
180+
expect(swipeFuncs.onSwipedDown).toHaveBeenCalled();
181+
expect(swipeFuncs.onSwipedUp).not.toHaveBeenCalled();
182+
expect(swipeFuncs.onSwipedLeft).not.toHaveBeenCalled();
183+
expect(swipeFuncs.onSwipedRight).not.toHaveBeenCalled();
184+
expect(swipeFuncs.onSwiping).toHaveBeenCalledTimes(2);
185+
});
186+
154187
it("handles mouse events with trackMouse prop and fires correct props", () => {
155188
const swipeFuncs = getMockedSwipeFunctions();
156189
const { getByText } = render(

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)