From a6fc0898de990959d201b9665501deda215e41a4 Mon Sep 17 00:00:00 2001 From: Jorge Bernal Date: Tue, 5 Nov 2019 05:25:41 -0800 Subject: [PATCH] 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: https://github.com/facebook/react-native/pull/26966 Test Plan: Added relevant passing unit tests for Platform module. Differential Revision: D18323670 Pulled By: cpojer fbshipit-source-id: 7524c1914beab4f86041dcf8e60875380ebf7e02 --- Libraries/Utilities/Platform.android.js | 11 ++++++++--- Libraries/Utilities/Platform.ios.js | 7 ++++--- Libraries/Utilities/__tests__/Platform-test.js | 12 ++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Libraries/Utilities/Platform.android.js b/Libraries/Utilities/Platform.android.js index fd0cb70d49b..4a4de286938 100644 --- a/Libraries/Utilities/Platform.android.js +++ b/Libraries/Utilities/Platform.android.js @@ -12,8 +12,9 @@ import NativePlatformConstantsAndroid from './NativePlatformConstantsAndroid'; -export type PlatformSelectSpec = { +export type PlatformSelectSpec = { android?: A, + native?: N, default?: D, }; @@ -53,8 +54,12 @@ const Platform = { get isTV(): boolean { return this.constants.uiMode === 'tv'; }, - select: (spec: PlatformSelectSpec): A | D => - 'android' in spec ? spec.android : spec.default, + select: (spec: PlatformSelectSpec): A | N | D => + 'android' in spec + ? spec.android + : 'native' in spec + ? spec.native + : spec.default, }; module.exports = Platform; diff --git a/Libraries/Utilities/Platform.ios.js b/Libraries/Utilities/Platform.ios.js index 38666839daf..ed933099e6e 100644 --- a/Libraries/Utilities/Platform.ios.js +++ b/Libraries/Utilities/Platform.ios.js @@ -12,8 +12,9 @@ import NativePlatformConstantsIOS from './NativePlatformConstantsIOS'; -export type PlatformSelectSpec = { +export type PlatformSelectSpec = { default?: D, + native?: N, ios?: I, }; @@ -59,8 +60,8 @@ const Platform = { } return false; }, - select: (spec: PlatformSelectSpec): D | I => - 'ios' in spec ? spec.ios : spec.default, + select: (spec: PlatformSelectSpec): D | N | I => + 'ios' in spec ? spec.ios : 'native' in spec ? spec.native : spec.default, }; module.exports = Platform; diff --git a/Libraries/Utilities/__tests__/Platform-test.js b/Libraries/Utilities/__tests__/Platform-test.js index dd1a85caf5c..2dd11b0edcd 100644 --- a/Libraries/Utilities/__tests__/Platform-test.js +++ b/Libraries/Utilities/__tests__/Platform-test.js @@ -27,5 +27,17 @@ describe('Platform', () => { expect(PlatformIOS.select(obj)).toEqual(obj.ios); expect(PlatformAndroid.select(obj)).toEqual(obj.android); }); + + it('should return native value if no specific value was found', () => { + const obj = {native: 'native', default: 'default'}; + expect(PlatformIOS.select(obj)).toEqual(obj.native); + expect(PlatformAndroid.select(obj)).toEqual(obj.native); + }); + + it('should return default value if no specific value was found', () => { + const obj = {default: 'default'}; + expect(PlatformIOS.select(obj)).toEqual(obj.default); + expect(PlatformAndroid.select(obj)).toEqual(obj.default); + }); }); });