segmented-control icon indicating copy to clipboard operation
segmented-control copied to clipboard

SegmentedControl onChange acts differently on iOS vs Android/Web

Open VNDRN opened this issue 1 year ago • 1 comments

Hi, We noticed that pressing the selected item in the segmented control triggers the onChange and onValueChange every time on android or web but it doesn't on iOS. Only pressing an unselected item triggers the onChange.

This behaviour should be the same on both android & iOS.

Tapping an active segment should never call the onChange orValueChange handlers, since there is no change in the state. Perhaps an onPress can be added that triggers the event on every press?

Reproducible example

https://snack.expo.dev/gfEAxSb7j4Pd5uGSENPVw

Notice the difference when running the snack on iOS vs on android

VNDRN avatar Sep 06 '24 08:09 VNDRN

diff --git a/node_modules/@react-native-segmented-control/segmented-control/js/SegmentedControl.js b/node_modules/@react-native-segmented-control/segmented-control/js/SegmentedControl.js
index 99bdc2e..488e1b4 100644
--- a/node_modules/@react-native-segmented-control/segmented-control/js/SegmentedControl.js
+++ b/node_modules/@react-native-segmented-control/segmented-control/js/SegmentedControl.js
@@ -44,6 +44,7 @@ const SegmentedControl = ({
   const animation = React.useRef(new Animated.Value(0)).current;

   const handleChange = (index: number) => {
+    if(index===selectedIndex) return;
     // mocks iOS's nativeEvent
     const event: any = {
       nativeEvent: {
``` /

VNDRN avatar Sep 06 '24 13:09 VNDRN