Skip to content

Commit 4260907

Browse files
Emily Janzergrabbou
authored andcommittedMar 22, 2019
Pass through track color values for true/false to native component
Summary: There's a bug in the OSS Switch component where the track color value is reset to the default value when the switch is toggled. It looks like the Java class resets the track color value in `setOn` (which fires in a press event): https://fburl.com/vmugfzja but these values aren't actually initialized from JS - in Switch.js we only pass through the current track color: https://fburl.com/vytekd0o. The React component already has an API for defining both true/false track colors. However, we should also make sure not to reset these values for people using the old API of `tintColor`/`onTintColor`, so I'm changing it to only reset the value when both of those props are null. Reviewed By: mdvacca Differential Revision: D14035007 fbshipit-source-id: 12d968076bd47d54deedbfc15b12ff3cd77e2fd0
1 parent 392b084 commit 4260907

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed
 

‎Libraries/Components/Switch/Switch.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class Switch extends React.Component<Props> {
137137
on: value === true,
138138
style,
139139
thumbTintColor: _thumbColor,
140+
trackColorForFalse: _trackColorForFalse,
141+
trackColorForTrue: _trackColorForTrue,
140142
trackTintColor:
141143
value === true ? _trackColorForTrue : _trackColorForFalse,
142144
}: NativeAndroidProps)

‎ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitch.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ public void setThumbColor(@Nullable Integer color) {
5959
// If the switch has a different value than the value sent by JS, we must change it.
6060
if (isChecked() != on) {
6161
super.setChecked(on);
62-
Integer currentTrackColor = on ? mTrackColorForTrue : mTrackColorForFalse;
63-
setTrackColor(currentTrackColor);
62+
if (mTrackColorForTrue != null || mTrackColorForFalse != null) {
63+
// Update the track color to reflect the new value. We only want to do this if these
64+
// props were actually set from JS; otherwise we'll just reset the color to the default.
65+
Integer currentTrackColor = on ? mTrackColorForTrue : mTrackColorForFalse;
66+
setTrackColor(currentTrackColor);
67+
}
6468
}
6569
mAllowChange = true;
6670
}

0 commit comments

Comments
 (0)
Please sign in to comment.