Skip to content

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

Merged
merged 7 commits into from
Apr 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@
"lodash": "^4.14.0",
"mocha": "^2.5.3",
"react": "^15.2.1",
"react-addons-test-utils": "^15.4.2",
"react-bootstrap": "^0.30.0",
"react-dom": "^15.2.1",
"react-router": "^2.6.0",
"react-router": "4.0.0-beta.5",
"release-script": "^1.0.2",
"rimraf": "^2.5.4",
"shelljs": "^0.7.2",
Expand Down
2 changes: 1 addition & 1 deletion src/IndexLinkContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import LinkContainer from './LinkContainer';
export default class IndexLinkContainer extends React.Component {
render() {
return (
<LinkContainer {...this.props} onlyActiveOnIndex />
<LinkContainer {...this.props} exact />
);
}
}
Expand Down
173 changes: 88 additions & 85 deletions src/LinkContainer.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,43 @@
// 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';

const isModifiedEvent = (event) =>
!!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);

export default class LinkContainer extends Component {
static contextTypes = {
router: PropTypes.shape({
push: PropTypes.func.isRequired,
replace: PropTypes.func.isRequired,
createHref: PropTypes.func.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,
Copy link

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.

Copy link
Member Author

@v12 v12 Apr 11, 2017

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 exact and strict props 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 undefined for them in defaultProps but does it make any sense? Seems to just increase amount of code since these props are undefined anyway.

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);
Expand All @@ -66,42 +48,63 @@ class LinkContainer extends React.Component {
}

if (
target ||
event.defaultPrevented ||
isModifiedEvent(event) ||
!isLeftClickEvent(event)
!event.defaultPrevented && // onClick prevented default
event.button === 0 && // ignore right clicks
!this.props.target && // let browser handle "target=_blank" etc.
!isModifiedEvent(event) // ignore clicks with modifier keys
) {
return;
}
event.preventDefault();

event.preventDefault();
const { router } = this.context;
const { replace, to } = this.props;

this.context.router[action](
createLocationDescriptor(to, query, hash, state)
);
};
if (replace) {
router.replace(to);
} else {
router.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.createHref(
typeof to === 'string' ? { pathname: to } : to
);

return React.cloneElement(React.Children.only(children), props);
return (
<Route
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on official example. children function is called even if current URL doesn't match. It's called with an object that contains a match and a history properties but match will be null if there was no match. Thus it becomes possible to find out if activeClassName and/or activeStyle should be applied to the container.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is...wonky

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jquense We have no choice here - it's the way RRv4 works. I could've used matchPath here but it'd be reinventing the wheel because <Route> already does the job.

Copy link
Member

Choose a reason for hiding this comment

The 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: (className || '') + (isActive ? activeClassName : ''),
style: isActive ? { ...style, ...activeStyle } : style,
href,
onClick: this.handleClick,
}
);
}}
/>
);
}
}

LinkContainer.propTypes = propTypes;
LinkContainer.contextTypes = contextTypes;
LinkContainer.defaultProps = defaultProps;

export default LinkContainer;
31 changes: 15 additions & 16 deletions test/IndexLinkContainer.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import ReactTestUtils from 'react/lib/ReactTestUtils';
import ReactTestUtils from 'react-addons-test-utils';
import * as ReactBootstrap from 'react-bootstrap';
import ReactDOM from 'react-dom';
import { createMemoryHistory, IndexRoute, Route, Router } from 'react-router';
import { findDOMNode } from 'react-dom';
import { Route, MemoryRouter as Router } from 'react-router';

import IndexLinkContainer from '../src/IndexLinkContainer';

Expand All @@ -19,25 +19,24 @@ describe('IndexLinkContainer', () => {
describe('active state', () => {
function renderComponent(location) {
const router = ReactTestUtils.renderIntoDocument(
<Router history={createMemoryHistory(location)}>
<Route
path="/"
component={() => (
<IndexLinkContainer to="/">
<Component>Root</Component>
</IndexLinkContainer>
)}
>
<IndexRoute />
<Route path="foo" />
</Route>
<Router initialEntries={[location]}>
<div>
<Route
path="/"
render={() => (
<IndexLinkContainer to="/">
<Component>Root</Component>
</IndexLinkContainer>
)}
/>
</div>
</Router>
);

const component = ReactTestUtils.findRenderedComponentWithType(
router, Component
);
return ReactDOM.findDOMNode(component);
return findDOMNode(component);
}

it('should be active on the index route', () => {
Expand Down
Loading