diff --git a/packages/docs/src/pages/docs/apis/linking.md b/packages/docs/src/pages/docs/apis/linking.md index 492ed6d08..de82754d5 100644 --- a/packages/docs/src/pages/docs/apis/linking.md +++ b/packages/docs/src/pages/docs/apis/linking.md @@ -31,8 +31,8 @@ Returns a `Promise` that resolves to a boolean indicating whether the app can op Returns a `Promise` that resolves to the string of the URL that initially loaded the app. {% endcall %} -{% call macro.prop('openURL', '(url) => Promise<>') %} -Try to open the given url in a secure fashion. The method returns a Promise object. If the url opens, the promise is resolved. If not, the promise is rejected. +{% call macro.prop('openURL', '(url, target) => Promise<>') %} +Try to open the given url in a secure fashion. The provided target (including `undefined`) will be passed as the window target, or "_blank" if no target included. The method returns a Promise object. If the url opens, the promise is resolved. If not, the promise is rejected. {% endcall %} --- diff --git a/packages/react-native-web/src/exports/Linking/__tests__/index-test.js b/packages/react-native-web/src/exports/Linking/__tests__/index-test.js new file mode 100644 index 000000000..c9094cf8b --- /dev/null +++ b/packages/react-native-web/src/exports/Linking/__tests__/index-test.js @@ -0,0 +1,37 @@ +/* eslint-env jasmine, jest */ + +import Linking from '..'; + +describe('apis/Linking', () => { + describe('openURL', () => { + test('calls open with a url and target', done => { + jest.spyOn(window, 'open').mockImplementationOnce((url, target, opener) => { + expect(url).toBe('http://foo.com/'); + expect(target).toBe('target_name'); + expect(opener).toBe('noopener'); + done(); + }); + Linking.openURL('http://foo.com', 'target_name'); + }); + + test('defaults target to _blank if not provided', done => { + jest.spyOn(window, 'open').mockImplementationOnce((url, target, opener) => { + expect(url).toBe('http://foo.com/'); + expect(target).toBe('_blank'); + expect(opener).toBe('noopener'); + done(); + }); + Linking.openURL('http://foo.com'); + }); + + test('accepts undefined as a target', done => { + jest.spyOn(window, 'open').mockImplementationOnce((url, target, opener) => { + expect(url).toBe('http://foo.com/'); + expect(target).toBe(undefined); + expect(opener).toBe('noopener'); + done(); + }); + Linking.openURL('http://foo.com', undefined); + }); + }); +}); diff --git a/packages/react-native-web/src/exports/Linking/index.js b/packages/react-native-web/src/exports/Linking/index.js index 4cd1b254e..a704fac0b 100644 --- a/packages/react-native-web/src/exports/Linking/index.js +++ b/packages/react-native-web/src/exports/Linking/index.js @@ -66,12 +66,16 @@ class Linking { /** * Try to open the given url in a secure fashion. The method returns a Promise object. + * If a target is passed (including undefined) that target will be used, otherwise '_blank'. * If the url opens, the promise is resolved. If not, the promise is rejected. - * Dispatches the `onOpen` event if `url` is opened successfully + * Dispatches the `onOpen` event if `url` is opened successfully. */ - openURL(url: string): Promise { + openURL(url: string, target?: string): Promise { + if (arguments.length == 1) { + target = '_blank'; + } try { - open(url); + open(url, target); this._dispatchEvent('onOpen', url); return Promise.resolve(); } catch (e) { @@ -85,13 +89,13 @@ class Linking { } } -const open = (url) => { +const open = (url, string) => { if (canUseDOM) { const urlToOpen = new URL(url, window.location).toString(); if (urlToOpen.indexOf('tel:') === 0) { window.location = urlToOpen; } else { - window.open(urlToOpen, '_blank', 'noopener'); + window.open(urlToOpen, string, 'noopener'); } } };