Skip to content

Commit

Permalink
Pass through track color values for true/false to native component
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Emily Janzer authored and facebook-github-bot committed Feb 12, 2019
1 parent 2d86d38 commit 7652e31
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Libraries/Components/Switch/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ public void setThumbColor(@Nullable Integer color) {
// 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;
}
Expand Down

0 comments on commit 7652e31

Please sign in to comment.