diff --git a/README.md b/README.md index dc2fdcd..39e2924 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 ff490aa..67f4bd3 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 b98299b..9c3ccc0 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']]); + }); +});