drawer router key (#3925)

This commit is contained in:
Eric Vicenti 2018-04-06 15:13:55 -07:00 committed by Brent Vatne
parent 6cde6e2558
commit f70a25a6a8
2 changed files with 38 additions and 7 deletions

View File

@ -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,

View File

@ -70,4 +70,24 @@ describe('DrawerRouter', () => {
);
expect(state4.isDrawerOpen).toEqual(true);
});
test('Drawer opens closes with key targeted', () => {
const ScreenA = () => <div />;
const ScreenB = () => <div />;
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);
});
});