-
Notifications
You must be signed in to change notification settings - Fork 159
React Router v4 support #201
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
Changes from 6 commits
2c28b9d
e986ece
593b055
016eb9d
946e8c6
94062a0
32f9e44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,61 +1,45 @@ | ||
| // This is largely taken from react-router/lib/Link. | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| function isLeftClickEvent(event) { | ||
| return event.button === 0; | ||
| } | ||
|
|
||
| function isModifiedEvent(event) { | ||
| return !!( | ||
| event.metaKey || | ||
| event.altKey || | ||
| event.ctrlKey || | ||
| event.shiftKey | ||
| ); | ||
| } | ||
|
|
||
| function createLocationDescriptor(to, query, hash, state) { | ||
| if (query || hash || state) { | ||
| return { pathname: to, query, hash, state }; | ||
| } | ||
|
|
||
| return to; | ||
| } | ||
|
|
||
| const propTypes = { | ||
| onlyActiveOnIndex: React.PropTypes.bool.isRequired, | ||
| to: React.PropTypes.oneOfType([ | ||
| React.PropTypes.string, | ||
| React.PropTypes.object, | ||
| ]).isRequired, | ||
| query: React.PropTypes.string, | ||
| hash: React.PropTypes.string, | ||
| state: React.PropTypes.object, | ||
| action: React.PropTypes.oneOf([ | ||
| 'push', | ||
| 'replace', | ||
| ]).isRequired, | ||
| onClick: React.PropTypes.func, | ||
| active: React.PropTypes.bool, | ||
| target: React.PropTypes.string, | ||
| children: React.PropTypes.node.isRequired, | ||
| }; | ||
| import React, { Component, PropTypes } from 'react'; | ||
| import { Route } from 'react-router-dom'; | ||
|
|
||
| const isModifiedEvent = (event) => | ||
| !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey); | ||
|
|
||
| export default class LinkContainer extends Component { | ||
| static contextTypes = { | ||
| router: PropTypes.shape({ | ||
| history: PropTypes.shape({ | ||
| push: PropTypes.func.isRequired, | ||
| replace: PropTypes.func.isRequired, | ||
| createHref: PropTypes.func.isRequired, | ||
| }).isRequired, | ||
| }).isRequired, | ||
| }; | ||
|
|
||
| const contextTypes = { | ||
| router: React.PropTypes.object, | ||
| }; | ||
| static propTypes = { | ||
| children: PropTypes.element.isRequired, | ||
| onClick: PropTypes.func, | ||
| target: PropTypes.string, | ||
| replace: PropTypes.bool, | ||
| to: PropTypes.oneOfType([ | ||
| PropTypes.string, | ||
| PropTypes.object, | ||
| ]).isRequired, | ||
| exact: PropTypes.bool, | ||
| strict: PropTypes.bool, | ||
| className: PropTypes.string, | ||
| activeClassName: PropTypes.string, | ||
| style: PropTypes.object, | ||
| activeStyle: PropTypes.object, | ||
| isActive: PropTypes.func, | ||
| }; | ||
|
|
||
| const defaultProps = { | ||
| onlyActiveOnIndex: false, | ||
| action: 'push', | ||
| }; | ||
| static defaultProps = { | ||
| replace: false, | ||
| activeClassName: 'active', | ||
| }; | ||
|
|
||
| class LinkContainer extends React.Component { | ||
| onClick = (event) => { | ||
| const { | ||
| to, query, hash, state, children, onClick, target, action, | ||
| } = this.props; | ||
| handleClick = (event) => { | ||
| const { children, onClick } = this.props; | ||
|
|
||
| if (children.props.onClick) { | ||
| children.props.onClick(event); | ||
|
|
@@ -66,42 +50,62 @@ class LinkContainer extends React.Component { | |
| } | ||
|
|
||
| if ( | ||
| target || | ||
| event.defaultPrevented || | ||
| isModifiedEvent(event) || | ||
| !isLeftClickEvent(event) | ||
| !event.defaultPrevented && // onClick prevented default | ||
| event.button === 0 && // ignore right clicks | ||
| !isModifiedEvent(event) // ignore clicks with modifier keys | ||
| ) { | ||
| return; | ||
| } | ||
| event.preventDefault(); | ||
|
|
||
| event.preventDefault(); | ||
| const { history } = this.context.router; | ||
| const { replace, to } = this.props; | ||
|
|
||
| this.context.router[action]( | ||
| createLocationDescriptor(to, query, hash, state) | ||
| ); | ||
| }; | ||
| if (replace) { | ||
| history.replace(to); | ||
| } else { | ||
| history.push(to); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| render() { | ||
| const { router } = this.context; | ||
| const { onlyActiveOnIndex, to, children, ...props } = this.props; | ||
|
|
||
| props.onClick = this.onClick; | ||
|
|
||
| // Ignore if rendered outside Router context; simplifies unit testing. | ||
| if (router) { | ||
| props.href = router.createHref(to); | ||
| const { | ||
| children, | ||
| replace, // eslint-disable-line no-unused-vars | ||
| to, | ||
| exact, | ||
| strict, | ||
| activeClassName, | ||
| className, | ||
| activeStyle, | ||
| style, | ||
| isActive: getIsActive, | ||
| ...props, | ||
| } = this.props; | ||
|
|
||
| if (props.active == null) { | ||
| props.active = router.isActive(to, onlyActiveOnIndex); | ||
| } | ||
| } | ||
| const href = this.context.router.history.createHref( | ||
| typeof to === 'string' ? { pathname: to } : to | ||
| ); | ||
|
|
||
| return React.cloneElement(React.Children.only(children), props); | ||
| return ( | ||
| <Route | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on official example.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that is...wonky
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, sorry it wasn't a criticism of the code, just the RR api choice |
||
| path={typeof to === 'object' ? to.pathname : to} | ||
| exact={exact} | ||
| strict={strict} | ||
| children={({ location, match }) => { | ||
| const isActive = !!(getIsActive ? getIsActive(match, location) : match); | ||
|
|
||
| return React.cloneElement( | ||
| React.Children.only(children), | ||
| { | ||
| ...props, | ||
| className: isActive ? [className, activeClassName].join(' ') : className, | ||
| style: isActive ? { ...style, ...activeStyle } : style, | ||
| href, | ||
| onClick: this.handleClick, | ||
| } | ||
| ); | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| LinkContainer.propTypes = propTypes; | ||
| LinkContainer.contextTypes = contextTypes; | ||
| LinkContainer.defaultProps = defaultProps; | ||
|
|
||
| export default LinkContainer; | ||
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.
Can you give this a default? Same question for anything else that's not required.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yes, I think it's worth to add default values for
exactandstrictprops that relate to how React Router detects if route is active. However, I don't think it's worth to add default values for other non-required props just because it does not add any value and those may not be defined, so there is no need to pass them down to underlying component.Obviously, I can set
undefinedfor them indefaultPropsbut does it make any sense? Seems to just increase amount of code since these props are undefined anyway.