1010
1111'use strict' ;
1212
13- const DeprecatedViewPropTypes = require ( 'DeprecatedViewPropTypes' ) ;
14- const NativeMethodsMixin = require ( 'NativeMethodsMixin' ) ;
15- const PropTypes = require ( 'prop-types' ) ;
1613const React = require ( 'React' ) ;
17- const ReactNative = require ( 'ReactNative' ) ;
1814const StyleSheet = require ( 'StyleSheet' ) ;
1915
20- const createReactClass = require ( 'create-react-class' ) ;
2116const requireNativeComponent = require ( 'requireNativeComponent' ) ;
2217
18+ import type { SyntheticEvent } from 'CoreEventTypes' ;
2319import type { ViewProps } from 'ViewPropTypes' ;
20+ import type { NativeComponent } from 'ReactNative' ;
2421
25- const RCTSegmentedControl = requireNativeComponent ( 'RCTSegmentedControl' ) ;
22+ type Event = SyntheticEvent <
23+ $ReadOnly < { |
24+ value : number ,
25+ selectedSegmentIndex : number ,
26+ | } > ,
27+ > ;
2628
27- type DefaultProps = {
28- values : $ReadOnlyArray < string > ,
29- enabled : boolean ,
30- } ;
31-
32- type Props = $ReadOnly < { |
29+ type SegmentedControlIOSProps = $ReadOnly < { |
3330 ...ViewProps ,
34- values ?: ?$ReadOnlyArray < string > ,
31+ /**
32+ * The labels for the control's segment buttons, in order.
33+ */
34+ values ?: $ReadOnlyArray < string > ,
35+ /**
36+ * The index in `props.values` of the segment to be (pre)selected.
37+ */
3538 selectedIndex ?: ?number ,
36- onValueChange ?: ?Function ,
37- onChange ?: ?Function ,
38- enabled ?: ?boolean ,
39+ /**
40+ * Callback that is called when the user taps a segment;
41+ * passes the segment's value as an argument
42+ */
43+ onValueChange ?: ?( value : number ) => mixed ,
44+ /**
45+ * Callback that is called when the user taps a segment;
46+ * passes the event as an argument
47+ */
48+ onChange ?: ?( event : Event ) => mixed ,
49+ /**
50+ * If false the user won't be able to interact with the control.
51+ * Default value is true.
52+ */
53+ enabled ?: boolean ,
54+ /**
55+ * Accent color of the control.
56+ */
3957 tintColor ?: ?string ,
58+ /**
59+ * If true, then selecting a segment won't persist visually.
60+ * The `onValueChange` callback will still work as expected.
61+ */
4062 momentary ?: ?boolean ,
4163| } > ;
4264
43- const SEGMENTED_CONTROL_REFERENCE = 'segmentedcontrol' ;
65+ type Props = $ReadOnly < { |
66+ ...SegmentedControlIOSProps ,
67+ forwardedRef : ?React . Ref < typeof RCTSegmentedControl > ,
68+ | } > ;
4469
45- type Event = Object ;
70+ type NativeSegmentedControlIOS = Class <
71+ NativeComponent < SegmentedControlIOSProps > ,
72+ > ;
4673
4774/**
4875 * Use `SegmentedControlIOS` to render a UISegmentedControl iOS.
@@ -64,83 +91,50 @@ type Event = Object;
6491 * />
6592 * ````
6693 */
67- const SegmentedControlIOS = createReactClass ( {
68- displayName : 'SegmentedControlIOS' ,
69- mixins : [ NativeMethodsMixin ] ,
70-
71- propTypes : {
72- ...DeprecatedViewPropTypes ,
73- /**
74- * The labels for the control's segment buttons, in order.
75- */
76- values : PropTypes . arrayOf ( PropTypes . string ) ,
77-
78- /**
79- * The index in `props.values` of the segment to be (pre)selected.
80- */
81- selectedIndex : PropTypes . number ,
82-
83- /**
84- * Callback that is called when the user taps a segment;
85- * passes the segment's value as an argument
86- */
87- onValueChange : PropTypes . func ,
88-
89- /**
90- * Callback that is called when the user taps a segment;
91- * passes the event as an argument
92- */
93- onChange : PropTypes . func ,
94-
95- /**
96- * If false the user won't be able to interact with the control.
97- * Default value is true.
98- */
99- enabled : PropTypes . bool ,
100-
101- /**
102- * Accent color of the control.
103- */
104- tintColor : PropTypes . string ,
105-
106- /**
107- * If true, then selecting a segment won't persist visually.
108- * The `onValueChange` callback will still work as expected.
109- */
110- momentary : PropTypes . bool ,
111- } ,
11294
113- getDefaultProps : function ( ) : DefaultProps {
114- return {
115- values : [ ] ,
116- enabled : true ,
117- } ;
118- } ,
95+ const RCTSegmentedControl = ( ( requireNativeComponent (
96+ 'RCTSegmentedControl' ,
97+ ) : any ) : NativeSegmentedControlIOS ) ;
11998
120- _onChange : function ( event : Event ) {
99+ class SegmentedControlIOS extends React . Component < Props > {
100+ static defaultProps = {
101+ values : [ ] ,
102+ enabled : true ,
103+ } ;
104+
105+ _onChange = ( event : Event ) => {
121106 this . props . onChange && this . props . onChange ( event ) ;
122107 this . props . onValueChange &&
123108 this . props . onValueChange ( event . nativeEvent . value ) ;
124- } ,
109+ } ;
125110
126- render : function ( ) {
111+ render ( ) {
112+ const { forwardedRef, ...props } = this . props ;
127113 return (
128114 < RCTSegmentedControl
129- { ...this . props }
130- ref = { SEGMENTED_CONTROL_REFERENCE }
115+ { ...props }
116+ ref = { forwardedRef }
131117 style = { [ styles . segmentedControl , this . props . style ] }
132118 onChange = { this . _onChange }
133119 />
134120 ) ;
135- } ,
136- } ) ;
121+ }
122+ }
137123
138124const styles = StyleSheet . create ( {
139125 segmentedControl : {
140126 height : 28 ,
141127 } ,
142128} ) ;
143129
144- module . exports = ( ( SegmentedControlIOS : any ) : Class <
145- ReactNative . NativeComponent < Props > ,
146- > ) ;
130+ // $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet.
131+ const SegmentedControlIOSWithRef = React . forwardRef (
132+ (
133+ props : SegmentedControlIOSProps ,
134+ forwardedRef : ?React . Ref < typeof RCTSegmentedControl > ,
135+ ) => {
136+ return < SegmentedControlIOS { ...props } forwardedRef = { forwardedRef} / > ;
137+ } ,
138+ ) ;
139+
140+ module . exports = ( SegmentedControlIOSWithRef : NativeSegmentedControlIOS ) ;
0 commit comments