Skip to content
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

---

Expand Down
6 changes: 5 additions & 1 deletion js/SegmentedControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
onValueChange,
enabled = true,
disabledIndices,
selectedIndex,
momentary = false,
selectedIndex: selectedIndexProp,
values,
tintColor,
backgroundColor,
Expand All @@ -41,6 +42,9 @@
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);
Expand Down Expand Up @@ -121,7 +125,7 @@
<Animated.View
style={[
styles.slider,
{

Check warning on line 128 in js/SegmentedControl.js

View workflow job for this annotation

GitHub Actions / lint

Inline style: { zIndex: -1 }
transform: [{translateX: animation}],
width: segmentWidth - 4,
zIndex: -1,
Expand Down
79 changes: 78 additions & 1 deletion js/__tests__/SegmentedControl.test.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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(<SegmentedControl {...props} />);
});
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(<SegmentedControl {...props} momentary={false} />);
});
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']]);
});
});
Loading