Skip to content

Commit f4e3aae

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

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

__tests__/useSwipeable.spec.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,37 @@ 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
165+
{...swipeFuncs}
166+
{...undefinedConfigOptions}
167+
/>
168+
);
169+
170+
const touchArea = getByText(TESTING_TEXT);
171+
172+
fireEvent[TS](touchArea, cte({ x: 100, y: 100 }));
173+
fireEvent[TM](touchArea, cte({ x: 100, y: 125 }));
174+
fireEvent[TM](touchArea, cte({ x: 100, y: 150 }));
175+
fireEvent[TE](touchArea, cte({}));
176+
177+
expect(swipeFuncs.onSwiped).toHaveBeenCalled();
178+
expect(swipeFuncs.onSwipedDown).toHaveBeenCalled();
179+
expect(swipeFuncs.onSwipedUp).not.toHaveBeenCalled();
180+
expect(swipeFuncs.onSwipedLeft).not.toHaveBeenCalled();
181+
expect(swipeFuncs.onSwipedRight).not.toHaveBeenCalled();
182+
expect(swipeFuncs.onSwiping).toHaveBeenCalledTimes(2);
183+
});
184+
154185
it("handles mouse events with trackMouse prop and fires correct props", () => {
155186
const swipeFuncs = getMockedSwipeFunctions();
156187
const { getByText } = render(

src/index.ts

Lines changed: 18 additions & 4 deletions
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)