Skip to content

Commit a6fc089

Browse files
kokefacebook-github-bot
authored andcommitted
Add support for native pseudo-OS to Platform.select (#26966)
Summary: When you write platform-specific code using [file extensions](https://facebook.github.io/react-native/docs/platform-specific-code#platform-specific-extensions), you can specify `.ios.js`, `.android.js`, or the catch-all `.native.js` when you are sharing code with a web project. This `native` shortcut is missing for the `Platform.select` method, and this PR is adding support for that. ## Changelog [General] [Added] - Platform.select now supports native as an option. Pull Request resolved: #26966 Test Plan: Added relevant passing unit tests for Platform module. Differential Revision: D18323670 Pulled By: cpojer fbshipit-source-id: 7524c1914beab4f86041dcf8e60875380ebf7e02
1 parent e1d03b4 commit a6fc089

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

Libraries/Utilities/Platform.android.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
import NativePlatformConstantsAndroid from './NativePlatformConstantsAndroid';
1414

15-
export type PlatformSelectSpec<A, D> = {
15+
export type PlatformSelectSpec<A, N, D> = {
1616
android?: A,
17+
native?: N,
1718
default?: D,
1819
};
1920

@@ -53,8 +54,12 @@ const Platform = {
5354
get isTV(): boolean {
5455
return this.constants.uiMode === 'tv';
5556
},
56-
select: <A, D>(spec: PlatformSelectSpec<A, D>): A | D =>
57-
'android' in spec ? spec.android : spec.default,
57+
select: <A, N, D>(spec: PlatformSelectSpec<A, N, D>): A | N | D =>
58+
'android' in spec
59+
? spec.android
60+
: 'native' in spec
61+
? spec.native
62+
: spec.default,
5863
};
5964

6065
module.exports = Platform;

Libraries/Utilities/Platform.ios.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
import NativePlatformConstantsIOS from './NativePlatformConstantsIOS';
1414

15-
export type PlatformSelectSpec<D, I> = {
15+
export type PlatformSelectSpec<D, N, I> = {
1616
default?: D,
17+
native?: N,
1718
ios?: I,
1819
};
1920

@@ -59,8 +60,8 @@ const Platform = {
5960
}
6061
return false;
6162
},
62-
select: <D, I>(spec: PlatformSelectSpec<D, I>): D | I =>
63-
'ios' in spec ? spec.ios : spec.default,
63+
select: <D, N, I>(spec: PlatformSelectSpec<D, N, I>): D | N | I =>
64+
'ios' in spec ? spec.ios : 'native' in spec ? spec.native : spec.default,
6465
};
6566

6667
module.exports = Platform;

Libraries/Utilities/__tests__/Platform-test.js

+12
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,17 @@ describe('Platform', () => {
2727
expect(PlatformIOS.select(obj)).toEqual(obj.ios);
2828
expect(PlatformAndroid.select(obj)).toEqual(obj.android);
2929
});
30+
31+
it('should return native value if no specific value was found', () => {
32+
const obj = {native: 'native', default: 'default'};
33+
expect(PlatformIOS.select(obj)).toEqual(obj.native);
34+
expect(PlatformAndroid.select(obj)).toEqual(obj.native);
35+
});
36+
37+
it('should return default value if no specific value was found', () => {
38+
const obj = {default: 'default'};
39+
expect(PlatformIOS.select(obj)).toEqual(obj.default);
40+
expect(PlatformAndroid.select(obj)).toEqual(obj.default);
41+
});
3042
});
3143
});

0 commit comments

Comments
 (0)