@react-native-segmented-control/segmented-control 2.5.7 → 2.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -43,7 +43,10 @@ type Props = $ReadOnly<{|
43
43
  */
44
44
 
45
45
  class SegmentedControlIOS extends React.Component<Props> {
46
- static defaultProps = {
46
+ static defaultProps: {|
47
+ values: $ReadOnlyArray<string | number>,
48
+ enabled: boolean,
49
+ |} = {
47
50
  values: [],
48
51
  enabled: true,
49
52
  };
@@ -54,7 +57,7 @@ class SegmentedControlIOS extends React.Component<Props> {
54
57
  this.props.onValueChange(event.nativeEvent.value);
55
58
  };
56
59
 
57
- render() {
60
+ render(): React.Node {
58
61
  const {forwardedRef, fontStyle, activeFontStyle, values, ...props} =
59
62
  this.props;
60
63
  return (
@@ -44,9 +44,13 @@ const SegmentedControl = ({
44
44
  const colorScheme = appearance || colorSchemeHook;
45
45
  const [segmentWidth, setSegmentWidth] = React.useState(0);
46
46
  const animation = React.useRef(new Animated.Value(0)).current;
47
- const ref = React.useRef();
47
+ const ref = React.useRef<?React.ElementRef<typeof View>>(null);
48
48
 
49
49
  const handleChange = (index: number) => {
50
+ if (index === selectedIndex) {
51
+ return;
52
+ }
53
+
50
54
  // mocks iOS's nativeEvent
51
55
  const event: any = {
52
56
  nativeEvent: {
@@ -75,7 +79,7 @@ const SegmentedControl = ({
75
79
  Animated.timing(animation, {
76
80
  toValue: isRTL * (selectedIndex || 0),
77
81
  duration: 300,
78
- easing: Easing.out(Easing.quad),
82
+ easing: Easing.out(Easing.quad.bind(Easing)),
79
83
  useNativeDriver: true,
80
84
  }).start();
81
85
  }
@@ -83,7 +87,9 @@ const SegmentedControl = ({
83
87
 
84
88
  React.useEffect(() => {
85
89
  if (ref.current) {
86
- ref.current.measure((_x, _y, width) => updateSegmentWidth(width));
90
+ ref.current.measure((_x: number, _y: number, width: number) =>
91
+ updateSegmentWidth(width),
92
+ );
87
93
  }
88
94
  }, [values, updateSegmentWidth]);
89
95
 
@@ -32,10 +32,10 @@ type Props = $ReadOnly<{|
32
32
  testID?: string,
33
33
  |}>;
34
34
 
35
- function isBase64(str) {
35
+ function isBase64(str: mixed): boolean {
36
36
  const regex =
37
37
  /^data:image\/(?:gif|png|jpeg|bmp|webp)(?:;charset=utf-8)?;base64,(?:[A-Za-z0-9]|[+/])+={0,2}/;
38
- return str && regex.test(str);
38
+ return typeof str === 'string' && regex.test(str);
39
39
  }
40
40
 
41
41
  export const SegmentedControlTab = ({
@@ -18,7 +18,7 @@ export const SegmentsSeparators = ({
18
18
  selectedIndex,
19
19
  }: Props): React.Node => {
20
20
  const colorScheme = useColorScheme();
21
- const hide = (val) => {
21
+ const hide = (val: number) => {
22
22
  return selectedIndex === val || selectedIndex === val + 1;
23
23
  };
24
24
 
@@ -0,0 +1,99 @@
1
+ /* eslint-env jest */
2
+
3
+ import React from 'react';
4
+ import {TouchableOpacity} from 'react-native';
5
+ import renderer, {act} from 'react-test-renderer';
6
+ import SegmentedControl from '../SegmentedControl.js';
7
+
8
+ describe('Android and web selection callbacks', () => {
9
+ let tree;
10
+ let props;
11
+
12
+ beforeEach(() => {
13
+ props = {
14
+ values: ['One', 'Two'],
15
+ selectedIndex: 0,
16
+ onChange: jest.fn(),
17
+ onValueChange: jest.fn(),
18
+ };
19
+ });
20
+
21
+ afterEach(() => {
22
+ act(() => tree?.unmount());
23
+ });
24
+
25
+ function render(overrides = {}) {
26
+ props = {...props, ...overrides};
27
+ act(() => {
28
+ tree = renderer.create(<SegmentedControl {...props} />);
29
+ });
30
+ }
31
+
32
+ function press(index) {
33
+ act(() => {
34
+ tree.root.findAllByType(TouchableOpacity)[index].props.onPress();
35
+ });
36
+ }
37
+
38
+ it.each([0, 1])('does not notify for selected index %i', (selectedIndex) => {
39
+ render({selectedIndex});
40
+ press(selectedIndex);
41
+ expect(props.onChange).not.toHaveBeenCalled();
42
+ expect(props.onValueChange).not.toHaveBeenCalled();
43
+ });
44
+
45
+ it('notifies both callbacks once with the newly selected segment', () => {
46
+ render();
47
+ press(1);
48
+ expect(props.onChange).toHaveBeenCalledTimes(1);
49
+ expect(props.onChange).toHaveBeenCalledWith({
50
+ nativeEvent: {value: 'Two', selectedSegmentIndex: 1},
51
+ });
52
+ expect(props.onValueChange).toHaveBeenCalledTimes(1);
53
+ expect(props.onValueChange).toHaveBeenCalledWith('Two');
54
+ });
55
+
56
+ it('uses the latest controlled selection after a prop update', () => {
57
+ render();
58
+ act(() => {
59
+ tree.update(<SegmentedControl {...props} selectedIndex={1} />);
60
+ });
61
+ press(1);
62
+ expect(props.onChange).not.toHaveBeenCalled();
63
+ expect(props.onValueChange).not.toHaveBeenCalled();
64
+ press(0);
65
+ expect(props.onChange).toHaveBeenCalledTimes(1);
66
+ expect(props.onChange).toHaveBeenCalledWith({
67
+ nativeEvent: {value: 'One', selectedSegmentIndex: 0},
68
+ });
69
+ expect(props.onValueChange).toHaveBeenCalledTimes(1);
70
+ expect(props.onValueChange).toHaveBeenCalledWith('One');
71
+ });
72
+
73
+ it('compares indexes when two segments have the same value', () => {
74
+ render({values: ['Same', 'Same']});
75
+ press(1);
76
+ expect(props.onChange).toHaveBeenCalledWith({
77
+ nativeEvent: {value: 'Same', selectedSegmentIndex: 1},
78
+ });
79
+ expect(props.onValueChange).toHaveBeenCalledWith('Same');
80
+ });
81
+
82
+ it.each([undefined, null, -1])(
83
+ 'allows a first selection when selectedIndex is %s',
84
+ (selectedIndex) => {
85
+ render({selectedIndex});
86
+ press(0);
87
+ expect(props.onChange).toHaveBeenCalledTimes(1);
88
+ expect(props.onValueChange).toHaveBeenCalledWith('One');
89
+ },
90
+ );
91
+
92
+ it('allows optional callbacks to be omitted', () => {
93
+ render({onChange: undefined, onValueChange: undefined});
94
+ expect(() => {
95
+ press(0);
96
+ press(1);
97
+ }).not.toThrow();
98
+ });
99
+ });
package/js/types.js CHANGED
@@ -116,4 +116,8 @@ export type SegmentedControlProps = $ReadOnly<{|
116
116
  * Style properties for the slider component (Animated.View)
117
117
  */
118
118
  sliderStyle?: ViewStyle,
119
+ /**
120
+ * Accessibility hint separator text
121
+ */
122
+ accessibilityHintSeperator?: string,
119
123
  |}>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native-segmented-control/segmented-control",
3
- "version": "2.5.7",
3
+ "version": "2.5.8",
4
4
  "description": "React Native SegmentedControlIOS library",
5
5
  "main": "js/index.js",
6
6
  "types": "index.d.ts",
@@ -19,7 +19,7 @@
19
19
  "flow": "flow",
20
20
  "lint": "eslint . --cache",
21
21
  "tsc": "tsc --noEmit",
22
- "ci:publish": "pnpm semantic-release"
22
+ "release": "release-it"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "react": ">=16.0",
@@ -30,33 +30,52 @@
30
30
  "@babel/runtime": "^7.10.2",
31
31
  "@expo/webpack-config": "^18.0.4",
32
32
  "@react-native-community/eslint-config": "^3.2.0",
33
- "@semantic-release/git": "^10.0.1",
33
+ "@release-it/conventional-changelog": "^12.0.2",
34
34
  "@types/react": "^17.0.0",
35
35
  "@types/react-native": "^0.64.4",
36
36
  "babel-eslint": "^10.1.0",
37
- "babel-jest": "^29.0.3",
37
+ "babel-jest": "^30.2.0",
38
38
  "babel-preset-expo": "^9.2.0",
39
39
  "eslint": "8",
40
+ "eslint-plugin-ft-flow": "^3.0.11",
40
41
  "eslint-plugin-prettier": "^5.0.0",
41
- "prettier": "^3.0.0",
42
42
  "expo": "^46.0.16",
43
- "flow-bin": "^0.142.0",
44
- "husky": "^8.0.1",
43
+ "flow-bin": "^0.237.2",
44
+ "husky": "^9.1.7",
45
45
  "jest": "^29.2.1",
46
46
  "metro-react-native-babel-preset": "0.77.0",
47
+ "prettier": "^3.0.0",
47
48
  "react": "17.0.2",
48
49
  "react-dom": "17.0.2",
49
50
  "react-native": "0.63.4",
50
51
  "react-native-test-app": "1.4.6",
51
52
  "react-native-web": "^0.14.10",
52
53
  "react-test-renderer": "17.0.2",
54
+ "release-it": "^21.1.2",
53
55
  "rimraf": "^3.0.2",
54
- "semantic-release": "^23.0.6",
55
- "typescript": "^4"
56
+ "typescript": "^5.9.2"
56
57
  },
57
58
  "resolutions": {
58
59
  "@react-native-community/cli": "4.10.0"
59
60
  },
61
+ "release-it": {
62
+ "git": {
63
+ "commitMessage": "chore: release ${version}",
64
+ "tagName": "v${version}"
65
+ },
66
+ "npm": {
67
+ "publish": true,
68
+ "skipChecks": true
69
+ },
70
+ "github": {
71
+ "release": true
72
+ },
73
+ "plugins": {
74
+ "@release-it/conventional-changelog": {
75
+ "preset": "conventionalcommits"
76
+ }
77
+ }
78
+ },
60
79
  "jest": {
61
80
  "preset": "react-native",
62
81
  "setupFilesAfterEnv": [