-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathSegmentedControl.ios.js
More file actions
123 lines (111 loc) · 3.38 KB
/
SegmentedControl.ios.js
File metadata and controls
123 lines (111 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
'use strict';
import * as React from 'react';
import type {Event, SegmentedControlProps} from './types';
import {Image, StyleSheet, processColor} from 'react-native';
import RNCSegmentedControlNativeComponent from './RNCSegmentedControlNativeComponent';
type Props = $ReadOnly<{|
...SegmentedControlProps,
forwardedRef: ?React.Ref<typeof RNCSegmentedControlNativeComponent>,
|}>;
/**
* Use `SegmentedControlIOS` to render a UISegmentedControl iOS.
*
* #### Programmatically changing selected index
*
* The selected index can be changed on the fly by assigning the
* selectedIndex prop to a state variable, then changing that variable.
* Note that the state variable would need to be updated as the user
* selects a value and changes the index, as shown in the example below.
*
* ````
* <SegmentedControlIOS
* values={['One', 'Two']}
* selectedIndex={this.state.selectedIndex}
* onChange={(event) => {
* this.setState({selectedIndex: event.nativeEvent.selectedSegmentIndex});
* }}
* />
* ````
*/
class SegmentedControlIOS extends React.Component<Props> {
static defaultProps: {|
values: $ReadOnlyArray<string | number>,
enabled: boolean,
|} = {
values: [],
enabled: true,
};
_onChange = (event: Event) => {
this.props.onChange && this.props.onChange(event);
this.props.onValueChange &&
this.props.onValueChange(event.nativeEvent.value);
};
render(): React.Node {
const {forwardedRef, fontStyle, activeFontStyle, values, ...props} =
this.props;
return (
<RNCSegmentedControlNativeComponent
fontStyle={
fontStyle
? {
...fontStyle,
color: fontStyle.color
? // $FlowFixMe color is converted to number
processColor(fontStyle.color)
: undefined,
}
: undefined
}
activeFontStyle={
activeFontStyle
? {
...activeFontStyle,
color: activeFontStyle.color
? // $FlowFixMe color is converted to number
processColor(activeFontStyle.color)
: undefined,
}
: undefined
}
values={values.map((val) => {
if (typeof val === 'string') {
return val;
}
// SF Symbol objects have a systemImage key — pass through as-is
if (typeof val === 'object' && val !== null && val.systemImage) {
return val;
}
// Image sources (require() numbers or objects) need resolution
return Image.resolveAssetSource(val);
})}
{...props}
ref={forwardedRef}
style={[styles.segmentedControl, this.props.style]}
onChange={this._onChange}
/>
);
}
}
const styles = StyleSheet.create({
segmentedControl: {
height: 32,
},
});
// $FlowFixMe
const SegmentedControlIOSWithRef = React.forwardRef(
(
props: SegmentedControlProps,
forwardedRef: ?React.Ref<typeof RNCSegmentedControlNativeComponent>,
) => {
return <SegmentedControlIOS {...props} forwardedRef={forwardedRef} />;
},
);
export default SegmentedControlIOSWithRef;