From 932a1d15dd6336d2c20f84354943b12032504756 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sat, 28 Nov 2026 05:02:38 +0000 Subject: [PATCH] feat: support the momentary prop on Android and web `momentary` was only honoured on iOS, where UISegmentedControl's `isMomentary` stops a tapped segment from staying selected. The JavaScript implementation used on Android and web ignored it, so the control kept showing the slider and the bold active label, and tapping the selected segment again did nothing. When `momentary` is set, the JavaScript control now ignores `selectedIndex` for display purposes: - the slider is not rendered and every separator stays visible; - no segment gets the active label style or `accessibilityState.selected`; - every tap calls `onChange`/`onValueChange`, including repeated taps on the same segment, matching the native iOS behaviour. The README now lists the prop for iOS, Android and Web, and unit tests cover the rendering and callback behaviour. Fixes #868 --- README.md | 6 +- js/SegmentedControl.js | 6 +- js/__tests__/SegmentedControl.test.js | 79 ++++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index dc2fdcd4..39e2924b 100644 --- a/README.md +++ b/README.md @@ -141,9 +141,9 @@ Indexes of the segments (positions in `values`) the user can't interact with, wh If true, then selecting a segment won't persist visually. The `onValueChange` callback will still work as expected. -| Type | Required | Platform | -| ---- | -------- | -------- | -| bool | No | iOS | +| Type | Required | Platform | +| ---- | -------- | ----------------- | +| bool | No | iOS, Android, Web | --- diff --git a/js/SegmentedControl.js b/js/SegmentedControl.js index ff490aaa..67f4bd3b 100644 --- a/js/SegmentedControl.js +++ b/js/SegmentedControl.js @@ -29,7 +29,8 @@ const SegmentedControl = ({ onValueChange, enabled = true, disabledIndices, - selectedIndex, + momentary = false, + selectedIndex: selectedIndexProp, values, tintColor, backgroundColor, @@ -41,6 +42,9 @@ const SegmentedControl = ({ testIDS, sliderStyle, }: SegmentedControlProps): React.Node => { + // A momentary control never shows a persistent selection, mirroring + // UISegmentedControl's `isMomentary` on iOS. + const selectedIndex = momentary ? null : selectedIndexProp; const colorSchemeHook = useColorScheme(); const colorScheme = appearance || colorSchemeHook; const [segmentWidth, setSegmentWidth] = React.useState(0); diff --git a/js/__tests__/SegmentedControl.test.js b/js/__tests__/SegmentedControl.test.js index b98299be..9c3ccc03 100644 --- a/js/__tests__/SegmentedControl.test.js +++ b/js/__tests__/SegmentedControl.test.js @@ -1,7 +1,7 @@ /* eslint-env jest */ import React from 'react'; -import {TouchableOpacity} from 'react-native'; +import {Animated, StyleSheet, TouchableOpacity, View} from 'react-native'; import renderer, {act} from 'react-test-renderer'; import SegmentedControl from '../SegmentedControl.js'; @@ -152,3 +152,80 @@ describe('Android and web disabled segments', () => { ).toEqual([false, false, false]); }); }); + +describe('Android and web momentary control', () => { + let tree; + let props; + + beforeEach(() => { + props = { + values: ['One', 'Two'], + selectedIndex: 0, + momentary: true, + onChange: jest.fn(), + onValueChange: jest.fn(), + }; + }); + + afterEach(() => { + act(() => tree?.unmount()); + }); + + function render(overrides = {}) { + props = {...props, ...overrides}; + act(() => { + tree = renderer.create(); + }); + return tree.root.findAllByType(TouchableOpacity); + } + + function sliders() { + return tree.root + .findAllByType(Animated.View) + .filter( + (node) => StyleSheet.flatten(node.props.style).position === 'absolute', + ); + } + + function layout(width) { + act(() => { + tree.root.findAllByType(View)[0].props.onLayout({ + nativeEvent: {layout: {width}}, + }); + }); + } + + it('does not mark any segment as selected', () => { + const tabs = render(); + expect(tabs.map((tab) => tab.props.accessibilityState.selected)).toEqual([ + false, + false, + ]); + }); + + it('does not render the selection slider', () => { + render(); + layout(200); + expect(sliders()).toHaveLength(0); + + act(() => { + tree.update(); + }); + expect(sliders()).toHaveLength(1); + }); + + it('notifies every tap, including repeated taps on the same segment', () => { + const tabs = render(); + act(() => tabs[0].props.onPress()); + act(() => tabs[0].props.onPress()); + act(() => tabs[1].props.onPress()); + expect(props.onChange).toHaveBeenCalledTimes(3); + expect(props.onChange).toHaveBeenNthCalledWith(1, { + nativeEvent: {value: 'One', selectedSegmentIndex: 0}, + }); + expect(props.onChange).toHaveBeenNthCalledWith(3, { + nativeEvent: {value: 'Two', selectedSegmentIndex: 1}, + }); + expect(props.onValueChange.mock.calls).toEqual([['One'], ['One'], ['Two']]); + }); +});