Skip to content

Commit 4aa7add

Browse files
committed
fix: use new Switch API from RN >= 0.57. fixes #571
1 parent dcc5bf1 commit 4aa7add

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/components/Switch.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
/* @flow */
2-
import * as React from 'react';
32

3+
import * as React from 'react';
44
import { grey400, grey800, grey50, white, black } from '../styles/colors';
55
import { Switch as NativeSwitch, Platform } from 'react-native';
66
import setColor from 'color';
77
import { withTheme } from '../core/theming';
88
import type { Theme } from '../types';
99

10+
// eslint-disable-next-line import/no-unresolved
11+
const { version } = require('ReactNativeVersion');
12+
1013
type Props = React.ElementProps<NativeSwitch> & {
1114
/**
1215
* Disable toggling the switch.
@@ -85,12 +88,12 @@ class Switch extends React.Component<Props> {
8588
onValueChange,
8689
color,
8790
theme,
88-
...props
91+
...rest
8992
} = this.props;
9093

9194
const checkedColor = color || theme.colors.accent;
9295

93-
const trackTintColor =
96+
const onTintColor =
9497
Platform.OS === 'ios'
9598
? checkedColor
9699
: disabled
@@ -121,14 +124,26 @@ class Switch extends React.Component<Props> {
121124
? grey400
122125
: grey50;
123126

127+
const props =
128+
version.major === 0 && version.minor <= 56
129+
? {
130+
onTintColor,
131+
thumbTintColor,
132+
}
133+
: {
134+
thumbColor: thumbTintColor,
135+
trackColor: {
136+
true: onTintColor,
137+
},
138+
};
139+
124140
return (
125141
<NativeSwitch
126-
{...props}
127142
value={value}
128143
disabled={disabled}
129-
onTintColor={trackTintColor}
130-
thumbTintColor={thumbTintColor}
131144
onValueChange={disabled ? undefined : onValueChange}
145+
{...props}
146+
{...rest}
132147
/>
133148
);
134149
}

0 commit comments

Comments
 (0)