Last active
August 25, 2016 09:14
-
-
Save slorber/d23296fdc0f375f0ed363e7e40bc2de5 to your computer and use it in GitHub Desktop.
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 is highly inspired by React-Router Link, but with some differences: | |
// - It's complex to put Router available in context for the whole tree (because of cursors legacy), so ReactRouter Link can't be used safely everywhere... | |
// - We need support for touch events on links | |
const DefaultLink = React.createClass({ | |
propTypes: { | |
to: PropTypes.string.isRequired | |
}, | |
getUrl() { | |
return this.props.to; | |
}, | |
doPush() { | |
const url = this.getUrl(); | |
const { query, hash, state } = this.props; | |
const location = createLocationDescriptor(url, { query: preserveDevQueryParams(query), hash, state }); | |
AppHistory.push(location); | |
}, | |
handleClick(event) { | |
// In these cases we let default browser behavior | |
if (isModifiedEvent(event) || !isLeftClickEvent(event) || this.props.target) return; | |
event.preventDefault(); | |
event.stopPropagation(); | |
this.doPush(); | |
}, | |
handleTap(event) { | |
event.preventDefault(); | |
event.stopPropagation(); | |
this.doPush(); | |
}, | |
render() { | |
return <Tappable | |
{...this.props} | |
component="a" | |
href={AppHistory.createHref(this.getUrl())} | |
onClick={this.handleClick} | |
onTap={this.handleTap}/> | |
} | |
}); | |
exports.DefaultLink = DefaultLink; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment