@react-native-segmented-control/segmented-control 2.5.6 → 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.
- package/README.md +9 -0
- package/index.d.ts +5 -0
- package/js/SegmentedControl.ios.js +5 -2
- package/js/SegmentedControl.js +11 -3
- package/js/SegmentedControlTab.js +2 -2
- package/js/SegmentsSeparators.js +1 -1
- package/js/__tests__/SegmentedControl.test.js +99 -0
- package/js/types.js +8 -0
- package/package.json +28 -9
package/README.md
CHANGED
|
@@ -241,6 +241,15 @@ An object container
|
|
|
241
241
|
|
|
242
242
|
Extends [ViewStyles](https://reactnative.dev/docs/view-style-props)
|
|
243
243
|
|
|
244
|
+
### `sliderStyle`
|
|
245
|
+
|
|
246
|
+
(Android and Web only) Styles the slider component (Animated.View)
|
|
247
|
+
| Type | Required | Platform |
|
|
248
|
+
| ------ | -------- | -------- |
|
|
249
|
+
| object | No | Android, Web |
|
|
250
|
+
|
|
251
|
+
Extends [ViewStyles](https://reactnative.dev/docs/view-style-props)
|
|
252
|
+
|
|
244
253
|
## Tips and Tricks
|
|
245
254
|
|
|
246
255
|
### How can I increase the height of the tab ?
|
package/index.d.ts
CHANGED
|
@@ -111,6 +111,11 @@ export interface SegmentedControlProps extends ViewProps {
|
|
|
111
111
|
* Touchable style properties for Segmented Control Tab
|
|
112
112
|
*/
|
|
113
113
|
tabStyle?: ViewStyle;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Style properties for the Animated.View component
|
|
117
|
+
*/
|
|
118
|
+
sliderStyle?: ViewStyle;
|
|
114
119
|
}
|
|
115
120
|
|
|
116
121
|
/**
|
|
@@ -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 (
|
package/js/SegmentedControl.js
CHANGED
|
@@ -38,14 +38,19 @@ const SegmentedControl = ({
|
|
|
38
38
|
appearance,
|
|
39
39
|
accessibilityHintSeperator = 'out of',
|
|
40
40
|
testIDS,
|
|
41
|
+
sliderStyle,
|
|
41
42
|
}: SegmentedControlProps): React.Node => {
|
|
42
43
|
const colorSchemeHook = useColorScheme();
|
|
43
44
|
const colorScheme = appearance || colorSchemeHook;
|
|
44
45
|
const [segmentWidth, setSegmentWidth] = React.useState(0);
|
|
45
46
|
const animation = React.useRef(new Animated.Value(0)).current;
|
|
46
|
-
const ref = React.useRef();
|
|
47
|
+
const ref = React.useRef<?React.ElementRef<typeof View>>(null);
|
|
47
48
|
|
|
48
49
|
const handleChange = (index: number) => {
|
|
50
|
+
if (index === selectedIndex) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
49
54
|
// mocks iOS's nativeEvent
|
|
50
55
|
const event: any = {
|
|
51
56
|
nativeEvent: {
|
|
@@ -74,7 +79,7 @@ const SegmentedControl = ({
|
|
|
74
79
|
Animated.timing(animation, {
|
|
75
80
|
toValue: isRTL * (selectedIndex || 0),
|
|
76
81
|
duration: 300,
|
|
77
|
-
easing: Easing.out(Easing.quad),
|
|
82
|
+
easing: Easing.out(Easing.quad.bind(Easing)),
|
|
78
83
|
useNativeDriver: true,
|
|
79
84
|
}).start();
|
|
80
85
|
}
|
|
@@ -82,7 +87,9 @@ const SegmentedControl = ({
|
|
|
82
87
|
|
|
83
88
|
React.useEffect(() => {
|
|
84
89
|
if (ref.current) {
|
|
85
|
-
ref.current.measure((_x, _y, width) =>
|
|
90
|
+
ref.current.measure((_x: number, _y: number, width: number) =>
|
|
91
|
+
updateSegmentWidth(width),
|
|
92
|
+
);
|
|
86
93
|
}
|
|
87
94
|
}, [values, updateSegmentWidth]);
|
|
88
95
|
|
|
@@ -118,6 +125,7 @@ const SegmentedControl = ({
|
|
|
118
125
|
backgroundColor:
|
|
119
126
|
tintColor || (colorScheme === 'dark' ? '#636366' : 'white'),
|
|
120
127
|
},
|
|
128
|
+
sliderStyle,
|
|
121
129
|
]}
|
|
122
130
|
/>
|
|
123
131
|
) : null}
|
|
@@ -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 = ({
|
package/js/SegmentsSeparators.js
CHANGED
|
@@ -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
|
@@ -112,4 +112,12 @@ export type SegmentedControlProps = $ReadOnly<{|
|
|
|
112
112
|
* array testID to each segment button
|
|
113
113
|
*/
|
|
114
114
|
testIDS: $ReadOnlyArray<string>,
|
|
115
|
+
/**
|
|
116
|
+
* Style properties for the slider component (Animated.View)
|
|
117
|
+
*/
|
|
118
|
+
sliderStyle?: ViewStyle,
|
|
119
|
+
/**
|
|
120
|
+
* Accessibility hint separator text
|
|
121
|
+
*/
|
|
122
|
+
accessibilityHintSeperator?: string,
|
|
115
123
|
|}>;
|
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.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
|
-
"
|
|
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
|
-
"@
|
|
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": "^
|
|
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.
|
|
44
|
-
"husky": "^
|
|
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
|
-
"
|
|
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": [
|