Skip to content

Commit

Permalink
Implement Text onLongPress
Browse files Browse the repository at this point in the history
Summary:Fixes facebook#5855

Tested with `UIExplorer`, first <Text> is the `onPress`, second is the `onLongPress`.
Closes facebook#7099

Differential Revision: D3212436

Pulled By: mkonicek

fb-gh-sync-id: 0a1cbcd4eaf39ad4fe67d861c3be2e042e1acb27
fbshipit-source-id: 0a1cbcd4eaf39ad4fe67d861c3be2e042e1acb27
  • Loading branch information
grabbou authored and zebulgar committed Jun 18, 2016
1 parent e9f1c05 commit 46f4510
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Libraries/Text/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ const Text = React.createClass({
* This function is called on press.
*/
onPress: React.PropTypes.func,
/**
* This function is called on long press.
*/
onLongPress: React.PropTypes.func,
/**
* When true, no visual change is made when text is pressed down. By
* default, a gray oval highlights the text on press down.
Expand Down Expand Up @@ -128,23 +132,27 @@ const Text = React.createClass({
* Only assigned if touch is needed.
*/
_handlers: (null: ?Object),
_hasPressHandler(): boolean {
return !!this.props.onPress || !!this.props.onLongPress;
},
/**
* These are assigned lazily the first time the responder is set to make plain
* text nodes as cheap as possible.
*/
touchableHandleActivePressIn: (null: ?Function),
touchableHandleActivePressOut: (null: ?Function),
touchableHandlePress: (null: ?Function),
touchableHandleLongPress: (null: ?Function),
touchableGetPressRectOffset: (null: ?Function),
render(): ReactElement {
let newProps = this.props;
if (this.props.onStartShouldSetResponder || this.props.onPress) {
if (this.props.onStartShouldSetResponder || this._hasPressHandler()) {
if (!this._handlers) {
this._handlers = {
onStartShouldSetResponder: (): bool => {
const shouldSetFromProps = this.props.onStartShouldSetResponder &&
this.props.onStartShouldSetResponder();
const setResponder = shouldSetFromProps || !!this.props.onPress;
const setResponder = shouldSetFromProps || this._hasPressHandler();
if (setResponder && !this.touchableHandleActivePressIn) {
// Attach and bind all the other handlers only the first time a touch
// actually happens.
Expand All @@ -154,7 +162,7 @@ const Text = React.createClass({
}
}
this.touchableHandleActivePressIn = () => {
if (this.props.suppressHighlighting || !this.props.onPress) {
if (this.props.suppressHighlighting || !this._hasPressHandler()) {
return;
}
this.setState({
Expand All @@ -163,7 +171,7 @@ const Text = React.createClass({
};

this.touchableHandleActivePressOut = () => {
if (this.props.suppressHighlighting || !this.props.onPress) {
if (this.props.suppressHighlighting || !this._hasPressHandler()) {
return;
}
this.setState({
Expand All @@ -175,6 +183,10 @@ const Text = React.createClass({
this.props.onPress && this.props.onPress();
};

this.touchableHandleLongPress = () => {
this.props.onLongPress && this.props.onLongPress();
};

this.touchableGetPressRectOffset = function(): RectOffset {
return PRESS_RECT_OFFSET;
};
Expand Down

0 comments on commit 46f4510

Please sign in to comment.