-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from chirag04/update
Make compatible with RN 49+, refactored and added onHide and onShow props
- Loading branch information
Showing
6 changed files
with
151 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,98 @@ | ||
'use strict'; | ||
|
||
var { | ||
import React, {PureComponent} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
requireNativeComponent, | ||
TouchableHighlight, | ||
View, | ||
NativeModules, | ||
findNodeHandle, | ||
} = require('react-native'); | ||
var React = require('react'); | ||
var ToolTipMenu = NativeModules.ToolTipMenu; | ||
var RCTToolTipText = requireNativeComponent('RCTToolTipText', null); | ||
|
||
var propTypes = { | ||
actions: React.PropTypes.arrayOf(React.PropTypes.shape({ | ||
text: React.PropTypes.string.isRequired, | ||
onPress: React.PropTypes.func, | ||
})), | ||
arrowDirection: React.PropTypes.oneOf(['up', 'down', 'left', 'right']), | ||
longPress: React.PropTypes.bool, | ||
...TouchableHighlight.propTypes, | ||
}; | ||
|
||
var ViewClass = React.createClass({ | ||
getDefaultProps: function() { | ||
return { | ||
arrowDirection: 'down' | ||
} from 'react-native'; | ||
|
||
const ToolTipMenu = NativeModules.ToolTipMenu; | ||
const RCTToolTipText = requireNativeComponent('RCTToolTipText', null); | ||
|
||
export default class ToolTip extends PureComponent { | ||
static propTypes = { | ||
actions: PropTypes.arrayOf(PropTypes.shape({ | ||
text: PropTypes.string.isRequired, | ||
onPress: PropTypes.func | ||
})), | ||
arrowDirection: PropTypes.oneOf(['up', 'down', 'left', 'right']), | ||
longPress: PropTypes.bool, | ||
onHide: PropTypes.func, | ||
onShow: PropTypes.func, | ||
...TouchableHighlight.propTypes | ||
}; | ||
}, | ||
|
||
showMenu: function() { | ||
ToolTipMenu.show(findNodeHandle(this.refs.toolTipText), this.getOptionTexts(), this.props.arrowDirection); | ||
}, | ||
hideMenu: function() { | ||
ToolTipMenu.hide(); | ||
}, | ||
|
||
getOptionTexts: function() { | ||
return this.props.actions.map((option) => option.text); | ||
}, | ||
|
||
// Assuming there is no actions with the same text | ||
getCallback: function(optionText) { | ||
var selectedOption = this.props.actions.find((option) => option.text === optionText); | ||
|
||
if (selectedOption) { | ||
return selectedOption.onPress; | ||
} | ||
|
||
return null; | ||
}, | ||
static defaultProps = { | ||
arrowDirection: 'down', | ||
onHide: () => true, | ||
onShow: () => true | ||
}; | ||
|
||
getTouchableHighlightProps: function() { | ||
var props = {}; | ||
showMenu = () => { | ||
ToolTipMenu.show(findNodeHandle(this.refs.toolTipText), this.getOptionTexts(), this.props.arrowDirection); | ||
this.props.onShow(); | ||
}; | ||
|
||
Object.keys(TouchableHighlight.propTypes).forEach((key) => props[key] = this.props[key]); | ||
hideMenu = () => { | ||
ToolTipMenu.hide(); | ||
this.props.onHide(); | ||
}; | ||
|
||
if (this.props.longPress) { | ||
props.onLongPress = this.showMenu; | ||
} else { | ||
props.onPress = this.showMenu; | ||
} | ||
getOptionTexts = () => { | ||
return this.props.actions.map((option) => option.text); | ||
}; | ||
|
||
// Assuming there is no actions with the same text | ||
getCallback = (optionText) => { | ||
const selectedOption = this.props.actions.find((option) => option.text === optionText); | ||
|
||
return props; | ||
}, | ||
if (selectedOption) { | ||
return selectedOption.onPress; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
handleToolTipTextChange: function(event) { | ||
var callback = this.getCallback(event.nativeEvent.text); | ||
getTouchableHighlightProps = () => { | ||
const props = {}; | ||
|
||
Object.keys(TouchableHighlight.propTypes).forEach((key) => props[key] = this.props[key]); | ||
|
||
if (this.props.longPress) { | ||
props.onLongPress = this.showMenu; | ||
} else { | ||
props.onPress = this.showMenu; | ||
} | ||
|
||
return props; | ||
}; | ||
|
||
handleToolTipTextChange = (event) => { | ||
const callback = this.getCallback(event.nativeEvent.text); | ||
if (callback) { | ||
callback(event); | ||
} | ||
}; | ||
|
||
handleBlurToolTip = () => { | ||
this.hideMenu(); | ||
}; | ||
|
||
if (callback) { | ||
callback(event); | ||
render() { | ||
return ( | ||
<RCTToolTipText ref='toolTipText' onChange={this.handleToolTipTextChange} onBlur={this.handleBlurToolTip}> | ||
<TouchableHighlight | ||
{...this.getTouchableHighlightProps()} | ||
> | ||
<View> | ||
{this.props.children} | ||
</View> | ||
</TouchableHighlight> | ||
</RCTToolTipText> | ||
); | ||
} | ||
}, | ||
|
||
render: function() { | ||
return ( | ||
<RCTToolTipText ref='toolTipText' onChange={this.handleToolTipTextChange}> | ||
<TouchableHighlight | ||
{...this.getTouchableHighlightProps()} | ||
> | ||
<View> | ||
{this.props.children} | ||
</View> | ||
</TouchableHighlight> | ||
</RCTToolTipText> | ||
); | ||
} | ||
}); | ||
|
||
ViewClass.propTypes = propTypes; | ||
|
||
module.exports = ViewClass; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters