4
4
* This source code is licensed under the MIT license found in the
5
5
* LICENSE file in the root directory of this source tree.
6
6
*
7
- * @flow
8
7
* @format
8
+ * @flow
9
9
*/
10
10
11
11
'use strict' ;
12
12
13
13
const Platform = require ( 'Platform' ) ;
14
14
const React = require ( 'React' ) ;
15
- const ReactNative = require ( 'ReactNative' ) ;
16
15
const ReactNativeStyleAttributes = require ( 'ReactNativeStyleAttributes' ) ;
17
- const ReactNativeViewAttributes = require ( 'ReactNativeViewAttributes' ) ;
18
16
const TextAncestor = require ( 'TextAncestor' ) ;
19
17
const ViewPropTypes = require ( 'ViewPropTypes' ) ;
20
18
21
19
const invariant = require ( 'fbjs/lib/invariant' ) ;
22
20
const requireNativeComponent = require ( 'requireNativeComponent' ) ;
23
21
22
+ import type { NativeComponent } from 'ReactNative' ;
24
23
import type { ViewProps } from 'ViewPropTypes' ;
25
24
26
25
export type Props = ViewProps ;
@@ -32,50 +31,25 @@ export type Props = ViewProps;
32
31
*
33
32
* @see http://facebook.github.io/react-native/docs/view.html
34
33
*/
35
- class View extends ReactNative . NativeComponent < Props > {
36
- static propTypes = ViewPropTypes ;
37
-
38
- viewConfig = {
39
- uiViewClassName : 'RCTView' ,
40
- validAttributes : ReactNativeViewAttributes . RCTView ,
41
- } ;
42
-
43
- /**
44
- * WARNING: This method will not be used in production mode as in that mode we
45
- * replace wrapper component View with generated native wrapper RCTView. Avoid
46
- * adding functionality this component that you'd want to be available in both
47
- * dev and prod modes.
48
- */
49
- render ( ) {
50
- return (
51
- < TextAncestor . Consumer >
52
- { hasTextAncestor => {
53
- // TODO: Change iOS to behave the same as Android.
54
- invariant (
55
- ! hasTextAncestor || Platform . OS !== 'android' ,
56
- 'Nesting of <View> within <Text> is not supported on Android.' ,
57
- ) ;
58
- return < RCTView { ...this . props } /> ;
59
- } }
60
- </ TextAncestor . Consumer >
61
- ) ;
62
- }
63
- }
64
-
65
- const RCTView = requireNativeComponent ( 'RCTView' , View , {
66
- nativeOnly : {
67
- nativeBackgroundAndroid : true ,
68
- nativeForegroundAndroid : true ,
34
+ const RCTView = requireNativeComponent (
35
+ 'RCTView' ,
36
+ {
37
+ propTypes : ViewPropTypes ,
38
+ } ,
39
+ {
40
+ nativeOnly : {
41
+ nativeBackgroundAndroid : true ,
42
+ nativeForegroundAndroid : true ,
43
+ } ,
69
44
} ,
70
- } ) ;
45
+ ) ;
71
46
72
47
if ( __DEV__ ) {
73
48
const UIManager = require ( 'UIManager' ) ;
74
49
const viewConfig =
75
50
( UIManager . viewConfigs && UIManager . viewConfigs . RCTView ) || { } ;
76
51
for ( const prop in viewConfig . nativeProps ) {
77
- const viewAny : any = View ; // Appease flow
78
- if ( ! viewAny . propTypes [ prop ] && ! ReactNativeStyleAttributes [ prop ] ) {
52
+ if ( ! ViewPropTypes [ prop ] && ! ReactNativeStyleAttributes [ prop ] ) {
79
53
throw new Error (
80
54
'View is missing propType for native prop `' + prop + '`' ,
81
55
) ;
@@ -85,8 +59,20 @@ if (__DEV__) {
85
59
86
60
let ViewToExport = RCTView ;
87
61
if ( __DEV__ ) {
88
- ViewToExport = View ;
62
+ // $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet.
63
+ ViewToExport = React . forwardRef ( ( props , ref ) => (
64
+ < TextAncestor . Consumer >
65
+ { hasTextAncestor => {
66
+ // TODO: Change iOS to behave the same as Android.
67
+ invariant (
68
+ ! hasTextAncestor || Platform . OS !== 'android' ,
69
+ 'Nesting of <View> within <Text> is not supported on Android.' ,
70
+ ) ;
71
+ return < RCTView { ...props } ref = { ref } /> ;
72
+ } }
73
+ </ TextAncestor . Consumer >
74
+ ) ) ;
75
+ ViewToExport . displayName = 'View' ;
89
76
}
90
77
91
- // No one should depend on the DEV-mode createClass View wrapper.
92
- module . exports = ( ( ViewToExport : any ) : typeof View ) ;
78
+ module . exports = ( ( ViewToExport : any ) : Class < NativeComponent < ViewProps , any >> ) ;
0 commit comments