-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make compatible with RN 49+, refactored and added onHide and onShow props #36
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,102 @@ | ||
'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 let isToolTipVisible = false; | ||
|
||
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); | ||
isToolTipVisible = true; | ||
this.props.onShow(); | ||
}; | ||
|
||
Object.keys(TouchableHighlight.propTypes).forEach((key) => props[key] = this.props[key]); | ||
hideMenu = () => { | ||
ToolTipMenu.hide(); | ||
isToolTipVisible = false; | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would put this in the component and expose via ref. this.refs.tooltip.isToolTipVisible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the idea is to use it as an import.
if you have multiple tooltip components and you want to know if there is at least one of those components showing the tooltip without looping though all of them.
does it make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds like a state that should live in your application then. It's little weird to keep this global export in a library.idk