Skip to content

Commit d6ee448

Browse files
dulmandakhfacebook-github-bot
authored andcommitted
fix switch trackColor on Android. fixes #23962 (#23977)
Summary: fixes #23962, where trackColor is reset when value changed. This PR will set trackColor corresponding trackColor every-time value changes. [Android] [Changed] - Fix Switch trackColor Pull Request resolved: #23977 Differential Revision: D14495206 Pulled By: hramos fbshipit-source-id: d712f540cd3f8359d6e85f79c12732689870a112
1 parent 508daf2 commit d6ee448

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

RNTester/js/SwitchExample.js

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ class BasicSwitchExample extends React.Component<
5454
<Switch
5555
testID="on-off-initial-off"
5656
onValueChange={value => this.setState({falseSwitchIsOn: value})}
57+
trackColor={{
58+
true: 'yellow',
59+
false: 'purple',
60+
}}
5761
value={this.state.falseSwitchIsOn}
5862
/>
5963
<OnOffIndicator

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public void setChecked(boolean checked) {
3636
if (mAllowChange && isChecked() != checked) {
3737
mAllowChange = false;
3838
super.setChecked(checked);
39+
setTrackColor(checked);
3940
}
4041
}
4142

@@ -59,12 +60,7 @@ public void setThumbColor(@Nullable Integer color) {
5960
// If the switch has a different value than the value sent by JS, we must change it.
6061
if (isChecked() != on) {
6162
super.setChecked(on);
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-
}
63+
setTrackColor(on);
6864
}
6965
mAllowChange = true;
7066
}
@@ -90,4 +86,13 @@ public void setTrackColorForFalse(@Nullable Integer color) {
9086
setTrackColor(mTrackColorForFalse);
9187
}
9288
}
89+
90+
private void setTrackColor(boolean checked) {
91+
if (mTrackColorForTrue != null || mTrackColorForFalse != null) {
92+
// Update the track color to reflect the new value. We only want to do this if these
93+
// props were actually set from JS; otherwise we'll just reset the color to the default.
94+
Integer currentTrackColor = checked ? mTrackColorForTrue : mTrackColorForFalse;
95+
setTrackColor(currentTrackColor);
96+
}
97+
}
9398
}

0 commit comments

Comments
 (0)