Skip to content

Commit c0904f9

Browse files
committed
Add onLongPress and tapTimeCutoff to api
1 parent dfc4a3a commit c0904f9

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

src/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const knownProps = {
9393
onTapTwo: true,
9494
onTapThree: true,
9595
onTapFour: true,
96+
onLongPress: true,
9697
onMouseEnter: true,
9798
onMouseLeave: true,
9899
onMouseMove: true,
@@ -123,6 +124,8 @@ export const knownProps = {
123124
// ms to allow for the browser to add subsequent event to the queue in setTimeouts
124125
export const queueTime = 600;
125126

127+
export const defaultTapTimeCutoff = 500;
128+
126129
export function dummyEvent(type) {
127130
return {
128131
type,

src/index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import objectAssign from 'object-assign';
3-
import propTypes from './propTypes';
3+
import { propTypes, defaultProps } from './propTypes';
44
import compareProps from './compareProps';
55
import mergeAndExtractProps from './mergeAndExtractProps';
66
import {
@@ -32,6 +32,7 @@ import {
3232

3333
class Interactive extends React.Component {
3434
static propTypes = propTypes;
35+
static defaultProps = defaultProps;
3536

3637
constructor(props) {
3738
super(props);
@@ -285,7 +286,9 @@ class Interactive extends React.Component {
285286
// add onTouchMove handler to passThroughProps if it's required
286287
if (
287288
deviceHasTouch &&
288-
(mergedProps.touchActiveTapOnly || mergedProps.onTouchMove)
289+
(mergedProps.touchActiveTapOnly ||
290+
mergedProps.onLongPress ||
291+
mergedProps.onTouchMove)
289292
) {
290293
passThroughProps.onTouchMove = this.handleEvent;
291294
}
@@ -950,14 +953,17 @@ class Interactive extends React.Component {
950953

951954
// if going from no touch to touch, set touchTapTimer
952955
if (newTouchDown) {
956+
e.persist();
953957
this.manageSetTimeout(
954958
'touchTapTimer',
955959
() => {
956-
// if the timer finishes then fire a touchtapcancel event to cancel the tap,
960+
// if the timer finishes then call onLongPress callback and
961+
// fire a touchtapcancel event to cancel the tap,
957962
// because this goes through handleEvent, updateState will be called if needed
963+
this.p.props.onLongPress && this.p.props.onLongPress(e);
958964
this.handleEvent(dummyEvent('touchtapcancel'));
959965
},
960-
600,
966+
this.p.props.tapTimeCutoff,
961967
);
962968
}
963969

@@ -973,8 +979,9 @@ class Interactive extends React.Component {
973979
if (extraTouches())
974980
return this.handleTouchEvent({ type: 'touchtapcancel' });
975981

976-
// if touchActiveTapOnly prop, check to see if the touch moved enough to cancel tap
977-
if (this.p.props.touchActiveTapOnly) {
982+
// if touchActiveTapOnly or onLongPress prop,
983+
// check to see if the touch moved enough to cancel tap
984+
if (this.p.props.touchActiveTapOnly || this.p.props.onLongPress) {
978985
for (let i = 0; i < e.changedTouches.length; i++) {
979986
const touch = this.track.touches.points[
980987
e.changedTouches[i].identifier

src/propTypes.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import PropTypes from 'prop-types';
2-
import { iStates, stateProps } from './constants';
2+
import { iStates, stateProps, defaultTapTimeCutoff } from './constants';
33

44
function statePropsExcept(state) {
55
const statePropsCopy = {
@@ -74,6 +74,7 @@ const propTypes = {
7474
onTapTwo: PropTypes.func,
7575
onTapThree: PropTypes.func,
7676
onTapFour: PropTypes.func,
77+
tapTimeCutoff: PropTypes.number,
7778

7879
onMouseEnter: PropTypes.func,
7980
onMouseLeave: PropTypes.func,
@@ -102,4 +103,8 @@ const propTypes = {
102103
interactiveChild: PropTypes.bool,
103104
};
104105

105-
export default propTypes;
106+
const defaultProps = {
107+
tapTimeCutoff: defaultTapTimeCutoff,
108+
};
109+
110+
export { propTypes, defaultProps };

0 commit comments

Comments
 (0)