Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RNTester/js/SwitchExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class BasicSwitchExample extends React.Component<
<Switch
testID="on-off-initial-off"
onValueChange={value => this.setState({falseSwitchIsOn: value})}
trackColor={{
true: 'yellow',
false: 'purple',
}}
value={this.state.falseSwitchIsOn}
/>
<OnOffIndicator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void setChecked(boolean checked) {
if (mAllowChange && isChecked() != checked) {
mAllowChange = false;
super.setChecked(checked);
setTrackColor(checked);
}
}

Expand All @@ -59,12 +60,7 @@ 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);
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);
}
setTrackColor(on);
}
mAllowChange = true;
}
Expand All @@ -90,4 +86,13 @@ public void setTrackColorForFalse(@Nullable Integer color) {
setTrackColor(mTrackColorForFalse);
}
}

private void setTrackColor(boolean checked) {
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 = checked ? mTrackColorForTrue : mTrackColorForFalse;
setTrackColor(currentTrackColor);
}
}
}