Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for native pseudo-OS to Platform.select #26966

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Libraries/Utilities/Platform.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

import NativePlatformConstantsAndroid from './NativePlatformConstantsAndroid';

export type PlatformSelectSpec<A, D> = {
export type PlatformSelectSpec<A, N, D> = {
android?: A,
native?: N,
default?: D,
};

Expand Down Expand Up @@ -53,8 +54,12 @@ const Platform = {
get isTV(): boolean {
return this.constants.uiMode === 'tv';
},
select: <A, D>(spec: PlatformSelectSpec<A, D>): A | D =>
'android' in spec ? spec.android : spec.default,
select: <A, N, D>(spec: PlatformSelectSpec<A, N, D>): A | N | D =>
koke marked this conversation as resolved.
Show resolved Hide resolved
'android' in spec
? spec.android
: 'native' in spec
? spec.native
: spec.default,
};

module.exports = Platform;
7 changes: 4 additions & 3 deletions Libraries/Utilities/Platform.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

import NativePlatformConstantsIOS from './NativePlatformConstantsIOS';

export type PlatformSelectSpec<D, I> = {
export type PlatformSelectSpec<D, N, I> = {
default?: D,
native?: N,
ios?: I,
};

Expand Down Expand Up @@ -59,8 +60,8 @@ const Platform = {
}
return false;
},
select: <D, I>(spec: PlatformSelectSpec<D, I>): D | I =>
'ios' in spec ? spec.ios : spec.default,
select: <D, N, I>(spec: PlatformSelectSpec<D, N, I>): D | N | I =>
'ios' in spec ? spec.ios : 'native' in spec ? spec.native : spec.default,
};

module.exports = Platform;
12 changes: 12 additions & 0 deletions Libraries/Utilities/__tests__/Platform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});