Skip to content

Commit

Permalink
[added] IndexLink
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Aug 28, 2015
1 parent 304302b commit 94509e7
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 9 deletions.
13 changes: 13 additions & 0 deletions modules/IndexLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import { component } from './PropTypes';
import Link from './Link';

var IndexLink = React.createClass({

render() {
return <Link {...this.props} onlyActiveOnIndex={true} />
}

});

export default IndexLink;
8 changes: 5 additions & 3 deletions modules/Link.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import warning from 'warning';

var { object, string, func } = React.PropTypes;
var { bool, object, string, func } = React.PropTypes;

function isLeftClickEvent(event) {
return event.button === 0;
Expand Down Expand Up @@ -38,6 +38,7 @@ var Link = React.createClass({
propTypes: {
activeStyle: object,
activeClassName: string,
onlyActiveOnIndex: bool.isRequired,
to: string.isRequired,
query: object,
state: object,
Expand All @@ -48,6 +49,7 @@ var Link = React.createClass({
return {
className: '',
activeClassName: 'active',
onlyActiveOnIndex: false,
style: {}
};
},
Expand Down Expand Up @@ -81,7 +83,7 @@ var Link = React.createClass({
},

render() {
var { to, query } = this.props;
var { to, query, onlyActiveOnIndex } = this.props;

var props = {
...this.props,
Expand All @@ -95,7 +97,7 @@ var Link = React.createClass({
if (history) {
props.href = history.createHref(to, query);

if (history.isActive(to, query)) {
if (history.isActive(to, query, onlyActiveOnIndex)) {
if (props.activeClassName)
props.className += props.className !== '' ? ` ${props.activeClassName}` : props.activeClassName;

Expand Down
4 changes: 2 additions & 2 deletions modules/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ var State = {
history: object.isRequired
},

isActive(pathname, query) {
return this.context.history.isActive(pathname, query);
isActive(pathname, query, indexOnly) {
return this.context.history.isActive(pathname, query, indexOnly);
}

};
Expand Down
54 changes: 54 additions & 0 deletions modules/__tests__/isActive-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import expect from 'expect';
import React from 'react';
import createHistory from 'history/lib/createMemoryHistory';
import IndexRoute from '../IndexRoute';
import Router from '../Router';
import Route from '../Route';

Expand Down Expand Up @@ -67,6 +68,7 @@ describe('isActive', function () {
</Router>
), node, function () {
expect(this.history.isActive('/home')).toBe(true);
expect(this.history.isActive('/home', null, true)).toBe(false);
done();
});
});
Expand All @@ -82,6 +84,7 @@ describe('isActive', function () {
</Router>
), node, function () {
expect(this.history.isActive('/home', { the: 'query' })).toBe(true);
expect(this.history.isActive('/home', { the: 'query' }, true)).toBe(false);
done();
});
});
Expand All @@ -97,6 +100,57 @@ describe('isActive', function () {
</Router>
), node, function () {
expect(this.history.isActive('/home', { something: 'else' })).toBe(false);
expect(this.history.isActive('/home', { something: 'else' }, true)).toBe(false);
done();
});
});
});
});

describe('a pathname that matches an index URL', function () {
describe('with no query', function () {
it('is active', function (done) {
React.render((
<Router history={createHistory('/home')}>
<Route path="/home">
<IndexRoute />
</Route>
</Router>
), node, function () {
expect(this.history.isActive('/home', null)).toBe(true);
expect(this.history.isActive('/home', null, true)).toBe(true);
done();
});
});
});

describe('with a query that also matches', function () {
it('is active', function (done) {
React.render((
<Router history={createHistory('/home?the=query')}>
<Route path="/home">
<IndexRoute />
</Route>
</Router>
), node, function () {
expect(this.history.isActive('/home', { the: 'query' })).toBe(true);
expect(this.history.isActive('/home', { the: 'query' }, true)).toBe(true);
done();
});
});
});

describe('with a query that does not match', function () {
it('is not active', function (done) {
React.render((
<Router history={createHistory('/home?the=query')}>
<Route path="/home">
<IndexRoute />
</Route>
</Router>
), node, function () {
expect(this.history.isActive('/home', { something: 'else' })).toBe(false);
expect(this.history.isActive('/home', { something: 'else' }, true)).toBe(false);
done();
});
});
Expand Down
5 changes: 4 additions & 1 deletion modules/isActive.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ function queryIsActive(query, activeQuery) {
* Returns true if a <Link> to the given pathname/query combination is
* currently active.
*/
function isActive(pathname, query, location, routes, params) {
function isActive(pathname, query, indexOnly, location, routes, params) {
if (location == null)
return false;

if (indexOnly && (routes.length < 2 || routes[routes.length - 2].indexRoute !== routes[routes.length - 1]))
return false;

return pathnameIsActive(pathname, location.pathname, routes, params) &&
queryIsActive(query, location.query);
}
Expand Down
6 changes: 3 additions & 3 deletions modules/useRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ function useRoutes(createHistory) {
return function (options={}) {
var { routes, ...historyOptions } = options;
var history = useQueries(createHistory)(historyOptions);
var state;
var state = {};

function isActive(pathname, query) {
return _isActive(pathname, query, state.location, state.routes, state.params);
function isActive(pathname, query, indexOnly=false) {
return _isActive(pathname, query, indexOnly, state.location, state.routes, state.params);
}

function matchRoutesWithWarning(routes, location, callback) {
Expand Down

0 comments on commit 94509e7

Please sign in to comment.