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
This commit is contained in:
Emily Janzer 2019-02-12 10:14:22 -08:00 committed by Mike Grabowski
parent 392b0845ce
commit 4260907b90
2 changed files with 8 additions and 2 deletions

View File

@ -137,6 +137,8 @@ class Switch extends React.Component<Props> {
on: value === true,
style,
thumbTintColor: _thumbColor,
trackColorForFalse: _trackColorForFalse,
trackColorForTrue: _trackColorForTrue,
trackTintColor:
value === true ? _trackColorForTrue : _trackColorForFalse,
}: NativeAndroidProps)

View File

@ -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;
}