From 4260907b90708ab1b21d0e62e7db3134496f0fad Mon Sep 17 00:00:00 2001 From: Emily Janzer Date: Tue, 12 Feb 2019 10:14:22 -0800 Subject: [PATCH] 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 --- Libraries/Components/Switch/Switch.js | 2 ++ .../com/facebook/react/views/switchview/ReactSwitch.java | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Libraries/Components/Switch/Switch.js b/Libraries/Components/Switch/Switch.js index 9bd00aaf1..ad9286397 100644 --- a/Libraries/Components/Switch/Switch.js +++ b/Libraries/Components/Switch/Switch.js @@ -137,6 +137,8 @@ class Switch extends React.Component { on: value === true, style, thumbTintColor: _thumbColor, + trackColorForFalse: _trackColorForFalse, + trackColorForTrue: _trackColorForTrue, trackTintColor: value === true ? _trackColorForTrue : _trackColorForFalse, }: NativeAndroidProps) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitch.java b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitch.java index 64d0d4448..4dd299cca 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitch.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitch.java @@ -59,8 +59,12 @@ import javax.annotation.Nullable; // If the switch has a different value than the value sent by JS, we must change it. if (isChecked() != on) { super.setChecked(on); - Integer currentTrackColor = on ? mTrackColorForTrue : mTrackColorForFalse; - setTrackColor(currentTrackColor); + if (mTrackColorForTrue != null || mTrackColorForFalse != null) { + // Update the track color to reflect the new value. We only want to do this if these + // props were actually set from JS; otherwise we'll just reset the color to the default. + Integer currentTrackColor = on ? mTrackColorForTrue : mTrackColorForFalse; + setTrackColor(currentTrackColor); + } } mAllowChange = true; }