From 962a5a093536252c3fa9e2ac0ffa111bbfdc40f2 Mon Sep 17 00:00:00 2001 From: Brian Emil Hartz Date: Fri, 29 Nov 2019 13:56:53 -0700 Subject: [PATCH] add first to eventData (#162) --- README.md | 1 + .../__snapshots__/index.spec.js.snap | 20 +++++++++++++++++++ src/index.js | 5 +++-- types/index.d.ts | 1 + types/test.tsx | 12 +---------- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index cf40b0e2..66c20e4f 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ All Event Handlers are called with the below event data. { event, // source event initial, // initial swipe [x,y] + first, // true for first event deltaX, // x offset (initial.x - current.x) deltaY, // y offset (initial.y - current.y) absX, // absolute deltaX diff --git a/src/__tests__/__snapshots__/index.spec.js.snap b/src/__tests__/__snapshots__/index.spec.js.snap index dfb28378..397592ef 100644 --- a/src/__tests__/__snapshots__/index.spec.js.snap +++ b/src/__tests__/__snapshots__/index.spec.js.snap @@ -10,6 +10,7 @@ Array [ "deltaY": 0, "dir": "Right", "event": Object {}, + "first": false, "initial": Array [ 100, 100, @@ -35,6 +36,7 @@ Array [ "preventDefault": [MockFunction], "timeStamp": 1374825.199999963, }, + "first": true, "initial": Array [ 100, 100, @@ -55,6 +57,7 @@ Array [ "preventDefault": [MockFunction], "timeStamp": 1374841.3999999757, }, + "first": false, "initial": Array [ 100, 100, @@ -75,6 +78,7 @@ Array [ "preventDefault": [MockFunction], "timeStamp": 1374857.399999979, }, + "first": false, "initial": Array [ 100, 100, @@ -95,6 +99,7 @@ Array [ "preventDefault": [MockFunction], "timeStamp": 1374873.499999987, }, + "first": false, "initial": Array [ 100, 100, @@ -123,6 +128,7 @@ Array [ }, ], }, + "first": false, "initial": Array [ 100, 100, @@ -152,6 +158,7 @@ Array [ }, ], }, + "first": true, "initial": Array [ 100, 100, @@ -176,6 +183,7 @@ Array [ }, ], }, + "first": false, "initial": Array [ 100, 100, @@ -200,6 +208,7 @@ Array [ }, ], }, + "first": false, "initial": Array [ 100, 100, @@ -224,6 +233,7 @@ Array [ }, ], }, + "first": false, "initial": Array [ 100, 100, @@ -244,6 +254,7 @@ Array [ "deltaY": 0, "dir": "Right", "event": Object {}, + "first": false, "initial": Array [ 100, 100, @@ -269,6 +280,7 @@ Array [ "preventDefault": [MockFunction], "timeStamp": 1374825.199999963, }, + "first": true, "initial": Array [ 100, 100, @@ -289,6 +301,7 @@ Array [ "preventDefault": [MockFunction], "timeStamp": 1374841.3999999757, }, + "first": false, "initial": Array [ 100, 100, @@ -309,6 +322,7 @@ Array [ "preventDefault": [MockFunction], "timeStamp": 1374857.399999979, }, + "first": false, "initial": Array [ 100, 100, @@ -329,6 +343,7 @@ Array [ "preventDefault": [MockFunction], "timeStamp": 1374873.499999987, }, + "first": false, "initial": Array [ 100, 100, @@ -357,6 +372,7 @@ Array [ }, ], }, + "first": false, "initial": Array [ 100, 100, @@ -386,6 +402,7 @@ Array [ }, ], }, + "first": true, "initial": Array [ 100, 100, @@ -410,6 +427,7 @@ Array [ }, ], }, + "first": false, "initial": Array [ 100, 100, @@ -434,6 +452,7 @@ Array [ }, ], }, + "first": false, "initial": Array [ 100, 100, @@ -458,6 +477,7 @@ Array [ }, ], }, + "first": false, "initial": Array [ 100, 100, diff --git a/src/index.js b/src/index.js index f7d79d6e..0ffeb07e 100644 --- a/src/index.js +++ b/src/index.js @@ -61,7 +61,7 @@ function getHandlers(set, handlerProps) { return { ...state, ...initialState, - eventData: { initial: [...xy] }, + eventData: { initial: [...xy], first: true }, xy, start: event.timeStamp || 0 } @@ -105,7 +105,8 @@ function getHandlers(set, handlerProps) { ) event.preventDefault() - return { ...state, eventData, swiping: true } + // first is now always false + return { ...state, eventData: { ...eventData, first: false }, swiping: true } }) } diff --git a/types/index.d.ts b/types/index.d.ts index fd8f677a..aae9b4c5 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -9,6 +9,7 @@ export interface EventData { deltaY: number absX: number absY: number + first: boolean initial: Vector2 velocity: number dir: 'Left' | 'Right' | 'Up' | 'Down' diff --git a/types/test.tsx b/types/test.tsx index 93aac26a..40ce4b21 100644 --- a/types/test.tsx +++ b/types/test.tsx @@ -1,17 +1,6 @@ import * as React from 'react' import { Swipeable, SwipeableHandlers, SwipeableProps, SwipeCallback, useSwipeable } from 'react-swipeable' -interface CopyOfEventData { - event: MouseEvent | TouchEvent - deltaX: number - deltaY: number - absX: number - absY: number - // initial: Vector2 - velocity: number - dir: 'Left' | 'Right' | 'Up' | 'Down' -} - class SampleComponent extends React.PureComponent { private readonly handleSwiped: SwipeCallback = () => {} private readonly handleSwipedLeft: SwipeCallback = () => {} @@ -77,6 +66,7 @@ const handlers: SwipeableHandlers = useSwipeable({ deltaY, // $ExpectType number absX, // $ExpectType number absY, // $ExpectType number + first, // $ExpectType boolean initial, // $ExpectType [number, number] velocity, // $ExpectType number dir, // $ExpectType "Left" | "Right" | "Up" | "Down"