Add a way to dismiss PopupMenu elements

Summary:
In native Android apps, like the YouTube app, context menus are closed when the device orientation changes.

In React Native apps instead, when having a [PopupMenu](https://developer.android.com/reference/android/widget/PopupMenu.html) open and rotating the device, the PopupMenu is not dismissed and appears in a wrong position on the screen.

This PR exposes a `dismissPopupMenu` method to allow the application to dismiss any open PopupMenu:

```(javascript)
UIManager.dismissPopupMenu()
```
Closes https://github.com/facebook/react-native/pull/15636

Differential Revision: D6837663

Pulled By: hramos

fbshipit-source-id: 7b0f4f04341129ad45c703a50897e17d93651974
This commit is contained in:
miguelsm
2018-03-16 16:59:49 -07:00
committed by Facebook Github Bot
parent 6426735e82
commit 353c070be9
5 changed files with 57 additions and 5 deletions

View File

@@ -33,6 +33,7 @@ import javax.annotation.Nullable;
public class ReactToolbarManager extends ViewGroupManager<ReactToolbar> {
private static final String REACT_CLASS = "ToolbarAndroid";
private static final int COMMAND_DISMISS_POPUP_MENUS = 1;
@Override
public String getName() {
@@ -157,6 +158,27 @@ public class ReactToolbarManager extends ViewGroupManager<ReactToolbar> {
return true;
}
@Nullable
@Override
public Map<String, Integer> getCommandsMap() {
return MapBuilder.of("dismissPopupMenus", COMMAND_DISMISS_POPUP_MENUS);
}
@Override
public void receiveCommand(ReactToolbar view, int commandType, @Nullable ReadableArray args) {
switch (commandType) {
case COMMAND_DISMISS_POPUP_MENUS: {
view.dismissPopupMenus();
return;
}
default:
throw new IllegalArgumentException(String.format(
"Unsupported command %d received by %s.",
commandType,
getClass().getSimpleName()));
}
}
private int[] getDefaultContentInsets(Context context) {
Resources.Theme theme = context.getTheme();
TypedArray toolbarStyle = null;