Skip to content

Commit 2220c16

Browse files
committed
Merge pull request acdlite#56 from acdlite/isActive
Add isActive()
2 parents 0200874 + c910f5d commit 2220c16

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/__tests__/reduxReactRouter-test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {
22
reduxReactRouter,
33
routerStateReducer,
44
pushState,
5-
replaceState
5+
replaceState,
6+
isActive
67
} from '../';
78

89
import { createStore, combineReducers } from 'redux';
@@ -191,5 +192,25 @@ describe('reduxRouter()', () => {
191192
expect(store.getState().router.location.pathname)
192193
.to.equal('/login');
193194
});
195+
196+
describe('isActive', () => {
197+
it('creates a selector for whether a pathname/query pair is active', () => {
198+
const reducer = combineReducers({
199+
router: routerStateReducer
200+
});
201+
202+
const history = createHistory();
203+
204+
const store = reduxReactRouter({
205+
history,
206+
routes
207+
})(createStore)(reducer);
208+
209+
const activeSelector = isActive('/parent', { key: 'value' });
210+
expect(activeSelector(store.getState().router)).to.be.false;
211+
history.pushState(null, '/parent?key=value');
212+
expect(activeSelector(store.getState().router)).to.be.true;
213+
});
214+
});
194215
});
195216
});

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export routerStateReducer from './routerStateReducer';
22
export ReduxRouter from './ReduxRouter';
33
export reduxReactRouter from './client';
4+
export isActive from './isActive';
45

56
export {
67
historyAPI,

src/isActive.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import _isActive from 'react-router/lib/isActive';
2+
3+
/**
4+
* Creates a router state selector that returns whether or not the given
5+
* pathname and query are active.
6+
* @param {String} pathname
7+
* @param {Object} query
8+
* @param {Boolean} indexOnly
9+
* @return {Boolean}
10+
*/
11+
export default function isActive(pathname, query, indexOnly = false) {
12+
return state => {
13+
if (!state) return false;
14+
const { location, params, routes } = state;
15+
return _isActive(pathname, query, indexOnly, location, routes, params);
16+
};
17+
}

0 commit comments

Comments
 (0)