Skip to content

Commit e708010

Browse files
yungstersfacebook-github-bot
authored andcommitted
RN: Switch Text to React.forwardRef
Reviewed By: sahrens Differential Revision: D7902262 fbshipit-source-id: 218f95cde6d77f21d9362a2f2bd47c5f83d5ee15
1 parent 06c05e7 commit e708010

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

Libraries/Components/Touchable/__tests__/__snapshots__/TouchableHighlight-test.js.snap

-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ exports[`TouchableHighlight renders correctly 1`] = `
2222
tvParallaxProperties={undefined}
2323
>
2424
<Text
25-
accessible={true}
26-
allowFontScaling={true}
27-
ellipsizeMode="tail"
2825
style={null}
2926
>
3027
Touchable

Libraries/Text/Text.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
'use strict';
1212

1313
const React = require('React');
14-
const ReactNative = require('ReactNative');
1514
const ReactNativeViewAttributes = require('ReactNativeViewAttributes');
1615
const TextAncestor = require('TextAncestor');
1716
const TextPropTypes = require('TextPropTypes');
@@ -23,6 +22,7 @@ const nullthrows = require('fbjs/lib/nullthrows');
2322
const processColor = require('processColor');
2423

2524
import type {PressEvent} from 'CoreEventTypes';
25+
import type {NativeComponent} from 'ReactNative';
2626
import type {PressRetentionOffset, TextProps} from 'TextProps';
2727

2828
type ResponseHandlers = $ReadOnly<{|
@@ -34,7 +34,10 @@ type ResponseHandlers = $ReadOnly<{|
3434
onResponderTerminationRequest: () => boolean,
3535
|}>;
3636

37-
type Props = TextProps;
37+
type Props = $ReadOnly<{
38+
...TextProps,
39+
forwardedRef: ?React.Ref<NativeComponent<TextProps, any>>,
40+
}>;
3841

3942
type State = {|
4043
touchable: {|
@@ -70,9 +73,7 @@ const viewConfig = {
7073
*
7174
* See https://facebook.github.io/react-native/docs/text.html
7275
*/
73-
class Text extends ReactNative.NativeComponent<Props, State> {
74-
static propTypes = TextPropTypes;
75-
76+
class TouchableText extends React.Component<Props, State> {
7677
static defaultProps = {
7778
accessible: true,
7879
allowFontScaling: true,
@@ -138,10 +139,10 @@ class Text extends ReactNative.NativeComponent<Props, State> {
138139
<TextAncestor.Consumer>
139140
{hasTextAncestor =>
140141
hasTextAncestor ? (
141-
<RCTVirtualText {...props} />
142+
<RCTVirtualText {...props} ref={props.forwardedRef} />
142143
) : (
143144
<TextAncestor.Provider value={true}>
144-
<RCTText {...props} />
145+
<RCTText {...props} ref={props.forwardedRef} />
145146
</TextAncestor.Provider>
146147
)
147148
}
@@ -260,4 +261,13 @@ const RCTVirtualText =
260261
uiViewClassName: 'RCTVirtualText',
261262
}));
262263

263-
module.exports = Text;
264+
// $FlowFixMe - TODO T29156721 `React.forwardRef` is not defined in Flow, yet.
265+
const Text = React.forwardRef((props, ref) => (
266+
<TouchableText {...props} forwardedRef={ref} />
267+
));
268+
Text.displayName = 'Text';
269+
270+
// TODO: Deprecate this.
271+
Text.propTypes = TextPropTypes;
272+
273+
module.exports = ((Text: any): NativeComponent<TextProps, any>);

jest/mockComponent.js

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ module.exports = (moduleName, instanceMethods) => {
2828
}
2929
};
3030

31+
if (RealComponent.propTypes != null) {
32+
Component.propTypes = RealComponent.propTypes;
33+
}
34+
3135
if (instanceMethods != null) {
3236
Object.assign(Component.prototype, instanceMethods);
3337
}

jest/setup.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jest.setMock('ErrorUtils', require('ErrorUtils'));
3535
jest
3636
.mock('InitializeCore', () => {})
3737
.mock('Image', () => mockComponent('Image'))
38-
.mock('Text', () => mockComponent('Text'))
38+
.mock('Text', () => mockComponent('Text', MockNativeMethods))
3939
.mock('TextInput', () => mockComponent('TextInput'))
4040
.mock('Modal', () => mockComponent('Modal'))
4141
.mock('View', () => mockComponent('View', MockNativeMethods))

0 commit comments

Comments
 (0)