Skip to content

Commit

Permalink
feat(focus): Add functionality to imperatively focus webview (#567)
Browse files Browse the repository at this point in the history
*  - add focus functionality for devices without touch screen
 (faced problem while developing for android TV, cause there only remote controller for device)

* Reimplement as a ref method.

*  - remove redundant requestFocus
  • Loading branch information
marcupan authored and Titozzz committed Aug 6, 2019
1 parent 9db55a5 commit 6f053ba
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
Expand Up @@ -110,6 +110,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
public static final int COMMAND_POST_MESSAGE = 5;
public static final int COMMAND_INJECT_JAVASCRIPT = 6;
public static final int COMMAND_LOAD_URL = 7;
public static final int COMMAND_FOCUS = 8;
protected static final String REACT_CLASS = "RNCWebView";
protected static final String HTML_ENCODING = "UTF-8";
protected static final String HTML_MIME_TYPE = "text/html";
Expand Down Expand Up @@ -511,7 +512,7 @@ public Map getExportedCustomDirectEventTypeConstants() {
@Override
public @Nullable
Map<String, Integer> getCommandsMap() {
return MapBuilder.of(
Map map = MapBuilder.of(
"goBack", COMMAND_GO_BACK,
"goForward", COMMAND_GO_FORWARD,
"reload", COMMAND_RELOAD,
Expand All @@ -520,6 +521,8 @@ Map<String, Integer> getCommandsMap() {
"injectJavaScript", COMMAND_INJECT_JAVASCRIPT,
"loadUrl", COMMAND_LOAD_URL
);
map.put("requestFocus", COMMAND_FOCUS);
return map;
}

@Override
Expand Down Expand Up @@ -567,6 +570,9 @@ public void receiveCommand(WebView root, int commandId, @Nullable ReadableArray
}
root.loadUrl(args.getString(0));
break;
case COMMAND_FOCUS:
root.requestFocus();
break;
}
}

Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Expand Up @@ -34,6 +34,11 @@ declare class WebView extends Component<WebViewProps> {
* Executes the JavaScript string.
*/
injectJavaScript: (script: string) => void;

/**
* Focuses on WebView redered page.
*/
requestFocus: () => void;
}

export {WebView};
Expand Down
8 changes: 8 additions & 0 deletions src/WebView.android.tsx
Expand Up @@ -105,6 +105,14 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
);
};

requestFocus = () => {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
this.getCommands().requestFocus,
null,
);
};

postMessage = (data: string) => {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
Expand Down
11 changes: 11 additions & 0 deletions src/WebView.ios.tsx
Expand Up @@ -164,6 +164,17 @@ class WebView extends React.Component<IOSWebViewProps, State> {
);
};

/**
* Request focus on WebView rendered page.
*/
requestFocus = () => {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
this.getCommands().requestFocus,
null,
);
};

/**
* Posts a message to the web view, which will emit a `message` event.
* Accepts one argument, `data`, which must be a string.
Expand Down
1 change: 1 addition & 0 deletions src/WebViewTypes.ts
Expand Up @@ -18,6 +18,7 @@ export interface WebViewCommands {
postMessage: Function;
injectJavaScript: Function;
loadUrl: Function;
requestFocus: Function;
}

export interface CustomUIManager extends UIManagerStatic {
Expand Down

0 comments on commit 6f053ba

Please sign in to comment.