10
10
11
11
'use strict' ;
12
12
13
- const ColorPropType = require ( 'ColorPropType' ) ;
14
- const NativeMethodsMixin = require ( 'NativeMethodsMixin' ) ;
15
13
const Platform = require ( 'Platform' ) ;
16
14
const ProgressBarAndroid = require ( 'ProgressBarAndroid' ) ;
17
- const PropTypes = require ( 'prop-types' ) ;
18
15
const React = require ( 'React' ) ;
19
16
const StyleSheet = require ( 'StyleSheet' ) ;
20
17
const View = require ( 'View' ) ;
21
- const ViewPropTypes = require ( 'ViewPropTypes' ) ;
22
18
23
- const createReactClass = require ( 'create-react-class' ) ;
24
19
const requireNativeComponent = require ( 'requireNativeComponent' ) ;
25
20
21
+ import type { NativeComponent } from 'ReactNative' ;
26
22
import type { ViewProps } from 'ViewPropTypes' ;
27
23
28
24
let RCTActivityIndicator ;
@@ -31,113 +27,103 @@ const GRAY = '#999999';
31
27
32
28
type IndicatorSize = number | 'small' | 'large' ;
33
29
30
+ type IOSProps = $ReadOnly < { |
31
+ /**
32
+ * Whether the indicator should hide when not animating (true by default).
33
+ *
34
+ * See http://facebook.github.io/react-native/docs/activityindicator.html#hideswhenstopped
35
+ */
36
+ hidesWhenStopped ?: ?boolean ,
37
+ | } > ;
34
38
type Props = $ReadOnly < { |
35
39
...ViewProps ,
40
+ ...IOSProps ,
36
41
42
+ /**
43
+ * Whether to show the indicator (true, the default) or hide it (false).
44
+ *
45
+ * See http://facebook.github.io/react-native/docs/activityindicator.html#animating
46
+ */
37
47
animating ?: ?boolean ,
48
+
49
+ /**
50
+ * The foreground color of the spinner (default is gray).
51
+ *
52
+ * See http://facebook.github.io/react-native/docs/activityindicator.html#color
53
+ */
38
54
color ?: ?string ,
39
- hidesWhenStopped ?: ?boolean ,
55
+
56
+ /**
57
+ * Size of the indicator (default is 'small').
58
+ * Passing a number to the size prop is only supported on Android.
59
+ *
60
+ * See http://facebook.github.io/react-native/docs/activityindicator.html#size
61
+ */
40
62
size ?: ?IndicatorSize ,
41
63
| } > ;
42
64
43
- type DefaultProps = {
44
- animating : boolean ,
45
- color : ?string ,
46
- hidesWhenStopped : boolean ,
47
- size : IndicatorSize ,
48
- } ;
49
-
50
65
/**
51
66
* Displays a circular loading indicator.
52
67
*
53
68
* See http://facebook.github.io/react-native/docs/activityindicator.html
54
69
*/
55
- const ActivityIndicator = ( ( createReactClass ( {
56
- displayName : 'ActivityIndicator' ,
57
- mixins : [ NativeMethodsMixin ] ,
58
-
59
- propTypes : {
60
- ...ViewPropTypes ,
61
- /**
62
- * Whether to show the indicator (true, the default) or hide it (false).
63
- *
64
- * See http://facebook.github.io/react-native/docs/activityindicator.html#animating
65
- */
66
- animating : PropTypes . bool ,
67
- /**
68
- * The foreground color of the spinner (default is gray).
69
- *
70
- * See http://facebook.github.io/react-native/docs/activityindicator.html#color
71
- */
72
- color : ColorPropType ,
73
- /**
74
- * Size of the indicator (default is 'small').
75
- * Passing a number to the size prop is only supported on Android.
76
- *
77
- * See http://facebook.github.io/react-native/docs/activityindicator.html#size
78
- */
79
- size : PropTypes . oneOfType ( [
80
- PropTypes . oneOf ( [ 'small' , 'large' ] ) ,
81
- PropTypes . number ,
82
- ] ) ,
83
- /**
84
- * Whether the indicator should hide when not animating (true by default).
85
- *
86
- * @platform ios
87
- *
88
- * See http://facebook.github.io/react-native/docs/activityindicator.html#hideswhenstopped
89
- */
90
- hidesWhenStopped : PropTypes . bool ,
91
- } ,
70
+ const ActivityIndicator = (
71
+ props : $ReadOnly < { |
72
+ ...Props ,
73
+ forwardedRef ?: ?React . Ref < 'RCTActivityIndicatorView' > ,
74
+ | } > ,
75
+ ) => {
76
+ const { onLayout, style, forwardedRef, ...restProps } = props ;
77
+ let sizeStyle ;
78
+
79
+ switch ( props . size ) {
80
+ case 'small' :
81
+ sizeStyle = styles . sizeSmall ;
82
+ break ;
83
+ case 'large' :
84
+ sizeStyle = styles . sizeLarge ;
85
+ break ;
86
+ default :
87
+ sizeStyle = { height : props . size , width : props . size } ;
88
+ break ;
89
+ }
90
+
91
+ const nativeProps = {
92
+ ...restProps ,
93
+ ref : forwardedRef ,
94
+ style : sizeStyle ,
95
+ styleAttr : 'Normal' ,
96
+ indeterminate : true ,
97
+ } ;
98
+
99
+ return (
100
+ < View onLayout = { onLayout } style = { [ styles . container , style ] } >
101
+ { Platform . OS === 'ios' ? (
102
+ < RCTActivityIndicator { ...nativeProps } />
103
+ ) : (
104
+ < ProgressBarAndroid { ...nativeProps } />
105
+ ) }
106
+ </ View >
107
+ ) ;
108
+ } ;
92
109
93
- getDefaultProps ( ) : DefaultProps {
94
- return {
95
- animating : true ,
96
- color : Platform . OS === 'ios' ? GRAY : null ,
97
- hidesWhenStopped : true ,
98
- size : 'small' ,
99
- } ;
100
- } ,
110
+ // $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet.
111
+ const ActivityIndicatorWithRef = React . forwardRef ( ( props : Props , ref ) => {
112
+ return < ActivityIndicator { ...props } forwardedRef = { ref } /> ;
113
+ } ) ;
101
114
102
- render ( ) {
103
- const { onLayout, style, ...props } = this . props ;
104
- let sizeStyle ;
105
-
106
- switch ( props . size ) {
107
- case 'small' :
108
- sizeStyle = styles . sizeSmall ;
109
- break ;
110
- case 'large' :
111
- sizeStyle = styles . sizeLarge ;
112
- break ;
113
- default :
114
- sizeStyle = { height : props . size , width : props . size } ;
115
- break ;
116
- }
117
-
118
- const nativeProps = {
119
- ...props ,
120
- style : sizeStyle ,
121
- styleAttr : 'Normal' ,
122
- indeterminate : true ,
123
- } ;
124
-
125
- return (
126
- < View onLayout = { onLayout } style = { [ styles . container , style ] } >
127
- { Platform . OS === 'ios' ? (
128
- < RCTActivityIndicator { ...nativeProps } />
129
- ) : (
130
- < ProgressBarAndroid { ...nativeProps } />
131
- ) }
132
- </ View >
133
- ) ;
134
- } ,
135
- } ) : any ) : React . ComponentType < Props > ) ;
115
+ ActivityIndicatorWithRef . defaultProps = {
116
+ animating : true ,
117
+ color : Platform . OS === 'ios' ? GRAY : null ,
118
+ hidesWhenStopped : true ,
119
+ size : 'small' ,
120
+ } ;
121
+ ActivityIndicatorWithRef . displayName = 'ActivityIndicator' ;
136
122
137
123
if ( Platform . OS === 'ios' ) {
138
124
RCTActivityIndicator = requireNativeComponent (
139
125
'RCTActivityIndicatorView' ,
140
- ActivityIndicator ,
126
+ null ,
141
127
{ nativeOnly : { activityIndicatorViewStyle : true } } ,
142
128
) ;
143
129
}
@@ -157,4 +143,4 @@ const styles = StyleSheet.create({
157
143
} ,
158
144
} ) ;
159
145
160
- module . exports = ActivityIndicator ;
146
+ module . exports = ( ActivityIndicatorWithRef : Class < NativeComponent < Props >> ) ;
0 commit comments