@react-native-segmented-control/segmented-control 2.5.8 → 2.5.9
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.
- package/js/RNCSegmentedControlNativeComponent.js +4 -5
- package/js/SegmentedControl.ios.js +12 -13
- package/js/SegmentedControl.js +5 -3
- package/js/SegmentedControlTab.js +5 -4
- package/js/SegmentsSeparators.js +1 -1
- package/js/types.js +16 -13
- package/package.json +18 -26
- package/js/__tests__/SegmentedControl.test.js +0 -99
|
@@ -9,11 +9,10 @@
|
|
|
9
9
|
'use strict';
|
|
10
10
|
|
|
11
11
|
import {requireNativeComponent} from 'react-native';
|
|
12
|
-
import type {HostComponent} from 'react-native
|
|
12
|
+
import type {HostComponent} from 'react-native';
|
|
13
13
|
import type {SegmentedControlProps} from './types';
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
const RNCSegmentedControlNativeComponent: HostComponent<SegmentedControlProps> =
|
|
16
|
+
requireNativeComponent<SegmentedControlProps>('RNCSegmentedControl');
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
'RNCSegmentedControl',
|
|
19
|
-
): any): NativeSegmentedControlIOS);
|
|
18
|
+
export default RNCSegmentedControlNativeComponent;
|
|
@@ -16,9 +16,11 @@ import {Image, StyleSheet, processColor} from 'react-native';
|
|
|
16
16
|
|
|
17
17
|
import RNCSegmentedControlNativeComponent from './RNCSegmentedControlNativeComponent';
|
|
18
18
|
|
|
19
|
-
type
|
|
19
|
+
type NativeRef = React.ElementRef<typeof RNCSegmentedControlNativeComponent>;
|
|
20
|
+
|
|
21
|
+
type Props = Readonly<{|
|
|
20
22
|
...SegmentedControlProps,
|
|
21
|
-
forwardedRef: ?React.
|
|
23
|
+
forwardedRef: ?React.RefSetter<NativeRef>,
|
|
22
24
|
|}>;
|
|
23
25
|
|
|
24
26
|
/**
|
|
@@ -44,7 +46,7 @@ type Props = $ReadOnly<{|
|
|
|
44
46
|
|
|
45
47
|
class SegmentedControlIOS extends React.Component<Props> {
|
|
46
48
|
static defaultProps: {|
|
|
47
|
-
values:
|
|
49
|
+
values: ReadonlyArray<string | number>,
|
|
48
50
|
enabled: boolean,
|
|
49
51
|
|} = {
|
|
50
52
|
values: [],
|
|
@@ -67,8 +69,7 @@ class SegmentedControlIOS extends React.Component<Props> {
|
|
|
67
69
|
? {
|
|
68
70
|
...fontStyle,
|
|
69
71
|
color: fontStyle.color
|
|
70
|
-
?
|
|
71
|
-
processColor(fontStyle.color)
|
|
72
|
+
? processColor(fontStyle.color)
|
|
72
73
|
: undefined,
|
|
73
74
|
}
|
|
74
75
|
: undefined
|
|
@@ -78,8 +79,7 @@ class SegmentedControlIOS extends React.Component<Props> {
|
|
|
78
79
|
? {
|
|
79
80
|
...activeFontStyle,
|
|
80
81
|
color: activeFontStyle.color
|
|
81
|
-
?
|
|
82
|
-
processColor(activeFontStyle.color)
|
|
82
|
+
? processColor(activeFontStyle.color)
|
|
83
83
|
: undefined,
|
|
84
84
|
}
|
|
85
85
|
: undefined
|
|
@@ -102,12 +102,11 @@ const styles = StyleSheet.create({
|
|
|
102
102
|
},
|
|
103
103
|
});
|
|
104
104
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
) => {
|
|
105
|
+
const SegmentedControlIOSWithRef: React.ComponentType<{
|
|
106
|
+
...SegmentedControlProps,
|
|
107
|
+
ref?: React.RefSetter<NativeRef>,
|
|
108
|
+
}> = React.forwardRef(
|
|
109
|
+
(props: SegmentedControlProps, forwardedRef: ?React.RefSetter<NativeRef>) => {
|
|
111
110
|
return <SegmentedControlIOS {...props} forwardedRef={forwardedRef} />;
|
|
112
111
|
},
|
|
113
112
|
);
|
package/js/SegmentedControl.js
CHANGED
|
@@ -87,9 +87,11 @@ const SegmentedControl = ({
|
|
|
87
87
|
|
|
88
88
|
React.useEffect(() => {
|
|
89
89
|
if (ref.current) {
|
|
90
|
-
ref.current.measure((_x: number, _y: number, width
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
ref.current.measure((_x: number, _y: number, width?: number) => {
|
|
91
|
+
if (width !== undefined) {
|
|
92
|
+
updateSegmentWidth(width);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
93
95
|
}
|
|
94
96
|
}, [values, updateSegmentWidth]);
|
|
95
97
|
|
|
@@ -16,9 +16,10 @@ import {
|
|
|
16
16
|
Platform,
|
|
17
17
|
} from 'react-native';
|
|
18
18
|
|
|
19
|
+
import type {TextStyle} from 'react-native';
|
|
19
20
|
import type {FontStyle, ViewStyle} from './types';
|
|
20
21
|
|
|
21
|
-
type Props =
|
|
22
|
+
type Props = Readonly<{|
|
|
22
23
|
value: string | number | Object,
|
|
23
24
|
tintColor?: ?string,
|
|
24
25
|
onSelect: () => void,
|
|
@@ -32,7 +33,7 @@ type Props = $ReadOnly<{|
|
|
|
32
33
|
testID?: string,
|
|
33
34
|
|}>;
|
|
34
35
|
|
|
35
|
-
function isBase64(str:
|
|
36
|
+
function isBase64(str: unknown): boolean {
|
|
36
37
|
const regex =
|
|
37
38
|
/^data:image\/(?:gif|png|jpeg|bmp|webp)(?:;charset=utf-8)?;base64,(?:[A-Za-z0-9]|[+/])+={0,2}/;
|
|
38
39
|
return typeof str === 'string' && regex.test(str);
|
|
@@ -73,7 +74,7 @@ export const SegmentedControlTab = ({
|
|
|
73
74
|
};
|
|
74
75
|
const color = getColor();
|
|
75
76
|
|
|
76
|
-
const activeStyle = {
|
|
77
|
+
const activeStyle: TextStyle = {
|
|
77
78
|
...styles.activeText,
|
|
78
79
|
fontFamily: activeFontFamily || fontFamily,
|
|
79
80
|
fontSize: activeFontSize || fontSize,
|
|
@@ -81,7 +82,7 @@ export const SegmentedControlTab = ({
|
|
|
81
82
|
fontWeight: activeFontWeight || fontWeight || styles.activeText.fontWeight,
|
|
82
83
|
};
|
|
83
84
|
|
|
84
|
-
const idleStyle = {
|
|
85
|
+
const idleStyle: TextStyle = {
|
|
85
86
|
color,
|
|
86
87
|
fontSize: fontSize,
|
|
87
88
|
fontFamily: fontFamily,
|
package/js/SegmentsSeparators.js
CHANGED
package/js/types.js
CHANGED
|
@@ -2,21 +2,24 @@
|
|
|
2
2
|
* @flow
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import type {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
import type {
|
|
6
|
+
ColorValue,
|
|
7
|
+
NativeSyntheticEvent,
|
|
8
|
+
StyleProp,
|
|
9
|
+
ViewProps,
|
|
10
|
+
ViewStyle as RNViewStyle,
|
|
11
|
+
} from 'react-native';
|
|
9
12
|
|
|
10
|
-
export type Event =
|
|
11
|
-
|
|
13
|
+
export type Event = NativeSyntheticEvent<
|
|
14
|
+
Readonly<{|
|
|
12
15
|
value: string,
|
|
13
16
|
selectedSegmentIndex: number,
|
|
14
17
|
|}>,
|
|
15
18
|
>;
|
|
16
19
|
|
|
17
|
-
export type ViewStyle =
|
|
20
|
+
export type ViewStyle = StyleProp<RNViewStyle>;
|
|
18
21
|
|
|
19
|
-
export type FontStyle =
|
|
22
|
+
export type FontStyle = Readonly<{|
|
|
20
23
|
/**
|
|
21
24
|
* Font Color of Segmented Control
|
|
22
25
|
*/
|
|
@@ -46,12 +49,12 @@ export type FontStyle = $ReadOnly<{|
|
|
|
46
49
|
| '900',
|
|
47
50
|
|}>;
|
|
48
51
|
|
|
49
|
-
export type SegmentedControlProps =
|
|
52
|
+
export type SegmentedControlProps = Readonly<{|
|
|
50
53
|
...ViewProps,
|
|
51
54
|
/**
|
|
52
55
|
* The labels for the control's segment buttons, in order.
|
|
53
56
|
*/
|
|
54
|
-
values:
|
|
57
|
+
values: ReadonlyArray<string | number | Object>,
|
|
55
58
|
/**
|
|
56
59
|
* The index in `props.values` of the segment to be (pre)selected.
|
|
57
60
|
*/
|
|
@@ -60,12 +63,12 @@ export type SegmentedControlProps = $ReadOnly<{|
|
|
|
60
63
|
* Callback that is called when the user taps a segment;
|
|
61
64
|
* passes the segment's value as an argument
|
|
62
65
|
*/
|
|
63
|
-
onValueChange?: ?(value: string | number | Object) =>
|
|
66
|
+
onValueChange?: ?(value: string | number | Object) => unknown,
|
|
64
67
|
/**
|
|
65
68
|
* Callback that is called when the user taps a segment;
|
|
66
69
|
* passes the event as an argument
|
|
67
70
|
*/
|
|
68
|
-
onChange?: ?(event: Event) =>
|
|
71
|
+
onChange?: ?(event: Event) => unknown,
|
|
69
72
|
/**
|
|
70
73
|
* If false the user won't be able to interact with the control.
|
|
71
74
|
* Default value is true.
|
|
@@ -111,7 +114,7 @@ export type SegmentedControlProps = $ReadOnly<{|
|
|
|
111
114
|
/**
|
|
112
115
|
* array testID to each segment button
|
|
113
116
|
*/
|
|
114
|
-
testIDS:
|
|
117
|
+
testIDS: ReadonlyArray<string>,
|
|
115
118
|
/**
|
|
116
119
|
* Style properties for the slider component (Animated.View)
|
|
117
120
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-native-segmented-control/segmented-control",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.9",
|
|
4
4
|
"description": "React Native SegmentedControlIOS library",
|
|
5
5
|
"main": "js/index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -8,14 +8,15 @@
|
|
|
8
8
|
"index.d.ts",
|
|
9
9
|
"ios",
|
|
10
10
|
"js",
|
|
11
|
+
"!js/__tests__",
|
|
11
12
|
"react-native-segmented-control.podspec"
|
|
12
13
|
],
|
|
13
14
|
"scripts": {
|
|
14
15
|
"start": "react-native start",
|
|
15
16
|
"test": "pnpm flow && pnpm lint",
|
|
16
17
|
"ios": "cd example && react-native run-ios",
|
|
17
|
-
"web": "expo web",
|
|
18
18
|
"android": "react-native run-android",
|
|
19
|
+
"web": "pnpm --filter SegmentedControlExampleWeb start",
|
|
19
20
|
"flow": "flow",
|
|
20
21
|
"lint": "eslint . --cache",
|
|
21
22
|
"tsc": "tsc --noEmit",
|
|
@@ -26,38 +27,29 @@
|
|
|
26
27
|
"react-native": ">=0.62"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
|
-
"@babel/core": "^7.
|
|
30
|
-
"@babel/
|
|
31
|
-
"@
|
|
32
|
-
"@
|
|
30
|
+
"@babel/core": "^7.29.13",
|
|
31
|
+
"@babel/preset-env": "^7.29.0",
|
|
32
|
+
"@babel/preset-flow": "^7.27.0",
|
|
33
|
+
"@babel/preset-react": "^7.27.0",
|
|
34
|
+
"@babel/runtime": "^7.29.14",
|
|
35
|
+
"@react-native/babel-preset": "0.88.1",
|
|
36
|
+
"@react-native/eslint-config": "0.88.1",
|
|
37
|
+
"@react-native/jest-preset": "0.88.1",
|
|
33
38
|
"@release-it/conventional-changelog": "^12.0.2",
|
|
34
|
-
"@types/react": "^
|
|
35
|
-
"@types/react-native": "^0.64.4",
|
|
36
|
-
"babel-eslint": "^10.1.0",
|
|
39
|
+
"@types/react": "^19.3.0",
|
|
37
40
|
"babel-jest": "^30.2.0",
|
|
38
|
-
"babel-preset-expo": "^9.2.0",
|
|
39
41
|
"eslint": "8",
|
|
40
|
-
"
|
|
41
|
-
"eslint-plugin-prettier": "^5.0.0",
|
|
42
|
-
"expo": "^46.0.16",
|
|
43
|
-
"flow-bin": "^0.237.2",
|
|
42
|
+
"flow-bin": "^0.331.0",
|
|
44
43
|
"husky": "^9.1.7",
|
|
45
|
-
"jest": "^
|
|
46
|
-
"metro-react-native-babel-preset": "0.77.0",
|
|
44
|
+
"jest": "^30.5.2",
|
|
47
45
|
"prettier": "^3.0.0",
|
|
48
|
-
"react": "
|
|
49
|
-
"react-
|
|
50
|
-
"react-
|
|
51
|
-
"react-native-test-app": "1.4.6",
|
|
52
|
-
"react-native-web": "^0.14.10",
|
|
53
|
-
"react-test-renderer": "17.0.2",
|
|
46
|
+
"react": "19.3.0",
|
|
47
|
+
"react-native": "0.88.1",
|
|
48
|
+
"react-test-renderer": "19.3.0",
|
|
54
49
|
"release-it": "^21.1.2",
|
|
55
50
|
"rimraf": "^3.0.2",
|
|
56
51
|
"typescript": "^5.9.2"
|
|
57
52
|
},
|
|
58
|
-
"resolutions": {
|
|
59
|
-
"@react-native-community/cli": "4.10.0"
|
|
60
|
-
},
|
|
61
53
|
"release-it": {
|
|
62
54
|
"git": {
|
|
63
55
|
"commitMessage": "chore: release ${version}",
|
|
@@ -77,7 +69,7 @@
|
|
|
77
69
|
}
|
|
78
70
|
},
|
|
79
71
|
"jest": {
|
|
80
|
-
"preset": "react-native",
|
|
72
|
+
"preset": "@react-native/jest-preset",
|
|
81
73
|
"setupFilesAfterEnv": [
|
|
82
74
|
"<rootDir>/jest.setup.js"
|
|
83
75
|
]
|
|
@@ -1,99 +0,0 @@
|
|
|
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
|
-
});
|