mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-08-15 18:45:06 +00:00
committed by
Nicolas Gallagher
parent
c4223a6a57
commit
ffd300ebfd
@@ -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 %}
|
||||
|
||||
---
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
+9
-5
@@ -67,12 +67,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<Object | void> {
|
||||
openURL(url: string, target?: string): Promise<Object | void> {
|
||||
if (arguments.length === 1) {
|
||||
target = '_blank';
|
||||
}
|
||||
try {
|
||||
open(url);
|
||||
open(url, target);
|
||||
this._dispatchEvent('onOpen', url);
|
||||
return Promise.resolve();
|
||||
} catch (e) {
|
||||
@@ -86,13 +90,13 @@ class Linking {
|
||||
}
|
||||
}
|
||||
|
||||
const open = (url) => {
|
||||
const open = (url, target) => {
|
||||
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, target, 'noopener');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user