Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit 9b79bc6

Browse files
committed
feat: added value input prop
This change allows you to set a `value` that you can use to forcibly update the UI's displayed selected item. However, this does not introduce a breaking change. This means that `onSelect` _will_ visually update the UI, THEN the `value` update will take over and update the UI. Keep in mind that we _will_ be changing this behavior in the next version release. If you are not currently using `value`, I suggest updating to it, as it will be required in the next major semver update. Closes #1
1 parent 711f7b0 commit 9b79bc6

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { Component } from "react";
12
import { StyleProp, ViewStyle, TextStyle } from "react-native";
23

34
export interface ButtonToggleGroupProps {
45
values: string[];
6+
value: string;
57
onSelect: (val: string) => void;
68
style?: StyleProp<ViewStyle>;
79
highlightBackgroundColor: string;
@@ -11,7 +13,7 @@ export interface ButtonToggleGroupProps {
1113
textStyle?: StyleProp<TextStyle>;
1214
}
1315

14-
export default class ButtonToggleGroup extends React.Component<
16+
export default class ButtonToggleGroup extends Component<
1517
ButtonToggleGroupProps,
1618
any
1719
> {}

index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import MaskedView from "@react-native-community/masked-view";
55

66
const ButtonToggleGroup = ({
77
values,
8+
value,
89
onSelect,
910
style,
1011
highlightBackgroundColor,
@@ -39,6 +40,14 @@ const ButtonToggleGroup = ({
3940
});
4041
}, [widthSize, selectedPanelLeft, selectedIndex]);
4142

43+
React.useEffect(() => {
44+
const newIndex = values.findIndex((v) => v === value);
45+
setPrevSelectedIndex(selectedIndex);
46+
setSelectedIndex(newIndex);
47+
}, [values, value, selectedIndex]);
48+
49+
// This allows the text to render under the related animation while the mask is gliding across
50+
// Notice the `.start(setPrevIndex)` to reset the previous index once the animation has stabilized
4251
const maxIndex =
4352
selectedIndex > prevSelectedIndex ? selectedIndex : prevSelectedIndex;
4453
const minIndex =
@@ -66,7 +75,7 @@ const ButtonToggleGroup = ({
6675
* the ripple effect continues to work on Android. As such, we conditionally
6776
* apply the logic for Android vs iOS
6877
*/
69-
const inactiveContainerIOS = Platform.OS === 'ios' ? {zIndex: -1} : {};
78+
const inactiveContainerIOS = Platform.OS === "ios" ? { zIndex: -1 } : {};
7079

7180
return (
7281
<View style={[styles.container, style]}>
@@ -114,7 +123,11 @@ const ButtonToggleGroup = ({
114123
</View>
115124
</MaskedView>
116125
<View
117-
style={[styles.baseButtonContainer, styles.inactiveButtonContainer, inactiveContainerIOS]}
126+
style={[
127+
styles.baseButtonContainer,
128+
styles.inactiveButtonContainer,
129+
inactiveContainerIOS,
130+
]}
118131
>
119132
{values.map((value, i) => (
120133
<TouchableRipple

0 commit comments

Comments
 (0)