Skip to content

Commit

Permalink
Destroy modals on setRoot (#5302)
Browse files Browse the repository at this point in the history
When setRoot is called, modals should be destroyed, while Overlay components can remain on screen.
  • Loading branch information
guyca authored Jul 21, 2019
1 parent 43a86bb commit f06787d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
6 changes: 6 additions & 0 deletions e2e/Modals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ describe('modal', () => {
await expect(elementByLabel('Modal Stack Position: 1')).toBeVisible();
});

it('setRoot dismisses modals', async () => {
await elementById(TestIDs.SET_ROOT).tap();
await expect(elementById(TestIDs.MODAL_SCREEN_HEADER)).toBeNotVisible();
await expect(elementById(TestIDs.PUSHED_SCREEN_HEADER)).toBeVisible();
});

it(':android: override hardware back button in modal with stack', async () => {
await elementById(TestIDs.PUSH_BTN).tap();
await elementById(TestIDs.ADD_BACK_HANDLER).tap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Options getDefaultOptions() {
return defaultOptions;
}

public FrameLayout getRootLayout() {
FrameLayout getRootLayout() {
return rootLayout;
}

Expand Down Expand Up @@ -129,6 +129,7 @@ public void sendOnNavigationButtonPressed(String buttonId) {

public void setRoot(final ViewController viewController, CommandListener commandListener, ReactInstanceManager reactInstanceManager) {
destroyRoot();
modalStack.destroy();
final boolean removeSplashView = isRootNotCreated();
if (isRootNotCreated()) getView();
root = viewController;
Expand All @@ -138,11 +139,11 @@ public void onSuccess(String childId) {
if (removeSplashView) removePreviousContentView();
super.onSuccess(childId);
}
}, reactInstanceManager);
}

private void removePreviousContentView() {
contentLayout.removeViewAt(0);
private void removePreviousContentView() {
contentLayout.removeViewAt(0);
}
}, reactInstanceManager);
}

public void mergeOptions(final String componentId, Options options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.List;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -166,6 +167,13 @@ public void setRoot_ReplacesExistingChildControllerViews() {
assertIsChild(uut.getRootLayout(), child2.getView());
}

@Test
public void setRoot_destroysModals() {
uut.showModal(child1, new CommandListenerAdapter());
uut.setRoot(child2, new CommandListenerAdapter(), reactInstanceManager);
assertTrue(child1.isDestroyed());
}

@Test
public void hasUniqueId() {
assertThat(uut.getId()).startsWith("navigator");
Expand Down
5 changes: 5 additions & 0 deletions playground/src/screens/ModalScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const React = require('react');
const Root = require('../components/Root');
const Button = require('../components/Button')
const Navigation = require('./../services/Navigation');
const { stack } = require('../commons/Layouts');
const Screens = require('./Screens');
const {
PUSH_BTN,
Expand All @@ -15,6 +16,7 @@ const {
DISMISS_ALL_PREVIOUS_MODAL_BTN,
DISMISS_ALL_MODALS_BTN,
DISMISS_FIRST_MODAL_BTN,
SET_ROOT
} = require('../testIDs');

class ModalScreen extends React.Component {
Expand Down Expand Up @@ -46,6 +48,7 @@ class ModalScreen extends React.Component {
<Button label='Dismiss All Modals' testID={DISMISS_ALL_MODALS_BTN} onPress={this.dismissAllModals} />
{this.props.previousModalIds && (<Button label='Dismiss First Modal' testID={DISMISS_FIRST_MODAL_BTN} onPress={this.dismissFirstModal} />)}
<Button label='Push screen' testID={PUSH_BTN} onPress={this.pushScreen} />
<Button label='Set Root' testID={SET_ROOT} onPress={this.setRoot} />
</Root>
);
}
Expand Down Expand Up @@ -83,6 +86,8 @@ class ModalScreen extends React.Component {

pushScreen = () => Navigation.push(this, Screens.Pushed);

setRoot = () => Navigation.setRoot(stack(Screens.Pushed));

getModalPosition = () => this.props.modalPosition || 1;

getPreviousModalId = () => _.last(this.props.previousModalIds);
Expand Down
6 changes: 4 additions & 2 deletions playground/src/services/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const popToRoot = (self) => Navigation.popToRoot(self.props.componentId);

const mergeOptions = (selfOrCompId, options) => Navigation.mergeOptions(compId(selfOrCompId), options);

const setStackRoot = (self, root) => Navigation.setStackRoot(self.props.componentId, root)
const setStackRoot = (self, root) => Navigation.setStackRoot(self.props.componentId, root);

const setRoot = (root) => Navigation.setRoot(root.root ? root : { root: component(root, {}) });

const compId = (selfOrCompId) => {
return get(selfOrCompId, 'props.componentId', selfOrCompId);
Expand All @@ -49,7 +51,7 @@ module.exports = {
events: Navigation.events.bind(Navigation),
popTo: Navigation.popTo.bind(Navigation),
setDefaultOptions: Navigation.setDefaultOptions.bind(Navigation),
setRoot: Navigation.setRoot.bind(Navigation),
setRoot,
TouchablePreview: Navigation.TouchablePreview,
setStackRoot,
constants
Expand Down
1 change: 1 addition & 0 deletions playground/src/testIDs.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ module.exports = {
SET_INTERCEPT_TOUCH: `SET_INTERCEPT_TOUCH`,
PUSH_BOTTOM_TABS_BUTTON: `PUSH_BOTTOM_TABS_BUTTON`,
SET_STACK_ROOT_BUTTON: `SET_STACK_ROOT_BUTTON`,
SET_ROOT:'SET_ROOT',

// Elements
SCROLLVIEW_ELEMENT: `SCROLLVIEW_ELEMENT`,
Expand Down

0 comments on commit f06787d

Please sign in to comment.