From f70a25a6a8299b372cca281200c1eb20b9dd721b Mon Sep 17 00:00:00 2001 From: Eric Vicenti Date: Fri, 6 Apr 2018 15:13:55 -0700 Subject: [PATCH] drawer router key (#3925) --- src/routers/DrawerRouter.js | 25 ++++++++++++++++------ src/routers/__tests__/DrawerRouter-test.js | 20 +++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/routers/DrawerRouter.js b/src/routers/DrawerRouter.js index c3b1264..03716a8 100644 --- a/src/routers/DrawerRouter.js +++ b/src/routers/DrawerRouter.js @@ -18,9 +18,9 @@ export default (routeConfigs, config = {}) => { getActionCreators(route, navStateKey) { return { - openDrawer: () => ({ type: DrawerActions.OPEN_DRAWER }), - closeDrawer: () => ({ type: DrawerActions.CLOSE_DRAWER }), - toggleDrawer: () => ({ type: DrawerActions.TOGGLE_DRAWER }), + openDrawer: () => DrawerActions.openDrawer({ key: navStateKey }), + closeDrawer: () => DrawerActions.closeDrawer({ key: navStateKey }), + toggleDrawer: () => DrawerActions.toggleDrawer({ key: navStateKey }), ...switchRouter.getActionCreators(route, navStateKey), }; }, @@ -31,20 +31,31 @@ export default (routeConfigs, config = {}) => { isDrawerOpen: false, }; - // Handle explicit drawer actions - if (state.isDrawerOpen && action.type === DrawerActions.CLOSE_DRAWER) { + const isRouterTargeted = action.key == null || action.key === state.key; + + if ( + isRouterTargeted && + action.type === DrawerActions.CLOSE_DRAWER && + state.isDrawerOpen + ) { return { ...state, isDrawerOpen: false, }; } - if (!state.isDrawerOpen && action.type === DrawerActions.OPEN_DRAWER) { + + if ( + isRouterTargeted && + action.type === DrawerActions.OPEN_DRAWER && + !state.isDrawerOpen + ) { return { ...state, isDrawerOpen: true, }; } - if (action.type === DrawerActions.TOGGLE_DRAWER) { + + if (isRouterTargeted && action.type === DrawerActions.TOGGLE_DRAWER) { return { ...state, isDrawerOpen: !state.isDrawerOpen, diff --git a/src/routers/__tests__/DrawerRouter-test.js b/src/routers/__tests__/DrawerRouter-test.js index 753c771..f1a588c 100644 --- a/src/routers/__tests__/DrawerRouter-test.js +++ b/src/routers/__tests__/DrawerRouter-test.js @@ -70,4 +70,24 @@ describe('DrawerRouter', () => { ); expect(state4.isDrawerOpen).toEqual(true); }); + + test('Drawer opens closes with key targeted', () => { + const ScreenA = () =>
; + const ScreenB = () =>
; + const router = DrawerRouter({ + Foo: { screen: ScreenA }, + Bar: { screen: ScreenB }, + }); + const state = router.getStateForAction(INIT_ACTION); + const state2 = router.getStateForAction( + { type: DrawerActions.OPEN_DRAWER, key: 'wrong' }, + state + ); + expect(state2.isDrawerOpen).toEqual(false); + const state3 = router.getStateForAction( + { type: DrawerActions.OPEN_DRAWER, key: state.key }, + state2 + ); + expect(state3.isDrawerOpen).toEqual(true); + }); });