Skip to content

Commit 06ce643

Browse files
yungstersfacebook-github-bot
authored andcommitted
Text: Cleanup Native Component Configuration
Summary: Cleans up the native component configuration for `RCTText` and `RCTVirtualText`. This //does// lead to a breaking change because `Text.viewConfig` will no longer exist. However, I think this is acceptable because `viewConfig` has already long stopped being an exported prop on other core components (e.g. `View`). Changelog: [General][Removed] - `Text.viewConfig` is no longer exported. Reviewed By: shergin Differential Revision: D23708205 fbshipit-source-id: 1ad0b0772735834d9162a65d9434a9bbbd142416
1 parent 9da4d87 commit 06ce643

File tree

2 files changed

+81
-62
lines changed

2 files changed

+81
-62
lines changed

Libraries/Text/Text.js

+13-62
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010

1111
'use strict';
1212

13+
import {NativeText, NativeVirtualText} from './TextNativeComponent';
14+
1315
const DeprecatedTextPropTypes = require('../DeprecatedPropTypes/DeprecatedTextPropTypes');
1416
const React = require('react');
15-
const ReactNativeViewAttributes = require('../Components/View/ReactNativeViewAttributes');
1617
const TextAncestor = require('./TextAncestor');
1718
const Touchable = require('../Components/Touchable/Touchable');
18-
const UIManager = require('../ReactNative/UIManager');
1919

20-
const createReactNativeComponentClass = require('../Renderer/shims/createReactNativeComponentClass');
2120
const nullthrows = require('nullthrows');
2221
const processColor = require('../StyleSheet/processColor');
2322

@@ -27,7 +26,7 @@ import type {PressRetentionOffset, TextProps} from './TextProps';
2726

2827
type ResponseHandlers = $ReadOnly<{|
2928
onStartShouldSetResponder: () => boolean,
30-
onResponderGrant: (event: PressEvent, dispatchID: string) => void,
29+
onResponderGrant: (event: PressEvent) => void,
3130
onResponderMove: (event: PressEvent) => void,
3231
onResponderRelease: (event: PressEvent) => void,
3332
onResponderTerminate: (event: PressEvent) => void,
@@ -36,7 +35,7 @@ type ResponseHandlers = $ReadOnly<{|
3635

3736
type Props = $ReadOnly<{|
3837
...TextProps,
39-
forwardedRef: ?React.Ref<'RCTText' | 'RCTVirtualText'>,
38+
forwardedRef: ?React.Ref<typeof NativeText | typeof NativeVirtualText>,
4039
|}>;
4140

4241
type State = {|
@@ -51,36 +50,6 @@ type State = {|
5150

5251
const PRESS_RECT_OFFSET = {top: 20, left: 20, right: 20, bottom: 30};
5352

54-
const viewConfig = {
55-
validAttributes: {
56-
...ReactNativeViewAttributes.UIView,
57-
isHighlighted: true,
58-
numberOfLines: true,
59-
ellipsizeMode: true,
60-
allowFontScaling: true,
61-
maxFontSizeMultiplier: true,
62-
disabled: true,
63-
selectable: true,
64-
selectionColor: true,
65-
adjustsFontSizeToFit: true,
66-
minimumFontScale: true,
67-
textBreakStrategy: true,
68-
onTextLayout: true,
69-
onInlineViewLayout: true,
70-
dataDetectorType: true,
71-
android_hyphenationFrequency: true,
72-
},
73-
directEventTypes: {
74-
topTextLayout: {
75-
registrationName: 'onTextLayout',
76-
},
77-
topInlineViewLayout: {
78-
registrationName: 'onInlineViewLayout',
79-
},
80-
},
81-
uiViewClassName: 'RCTText',
82-
};
83-
8453
/**
8554
* A React component for displaying text.
8655
*
@@ -122,21 +91,19 @@ class TouchableText extends React.Component<Props, State> {
12291
: null;
12392
}
12493

125-
static viewConfig = viewConfig;
126-
12794
render(): React.Node {
128-
let props = this.props;
129-
if (isTouchable(props)) {
95+
let {forwardedRef, selectionColor, ...props} = this.props;
96+
if (isTouchable(this.props)) {
13097
props = {
13198
...props,
13299
...this.state.responseHandlers,
133100
isHighlighted: this.state.isHighlighted,
134101
};
135102
}
136-
if (props.selectionColor != null) {
103+
if (selectionColor != null) {
137104
props = {
138105
...props,
139-
selectionColor: processColor(props.selectionColor),
106+
selectionColor: processColor(selectionColor),
140107
};
141108
}
142109
if (__DEV__) {
@@ -151,16 +118,17 @@ class TouchableText extends React.Component<Props, State> {
151118
<TextAncestor.Consumer>
152119
{hasTextAncestor =>
153120
hasTextAncestor ? (
154-
<RCTVirtualText
121+
// $FlowFixMe[prop-missing] For the `onClick` workaround.
122+
<NativeVirtualText
155123
{...props}
156124
// This is used on Android to call a nested Text component's press handler from the context menu.
157125
// TODO T75145059 Clean this up once Text is migrated off of Touchable
158126
onClick={props.onPress}
159-
ref={props.forwardedRef}
127+
ref={forwardedRef}
160128
/>
161129
) : (
162130
<TextAncestor.Provider value={true}>
163-
<RCTText {...props} ref={props.forwardedRef} />
131+
<NativeText {...props} ref={forwardedRef} />
164132
</TextAncestor.Provider>
165133
)
166134
}
@@ -263,26 +231,9 @@ const isTouchable = (props: Props): boolean =>
263231
props.onLongPress != null ||
264232
props.onStartShouldSetResponder != null;
265233

266-
const RCTText = createReactNativeComponentClass(
267-
viewConfig.uiViewClassName,
268-
() => viewConfig,
269-
);
270-
271-
const RCTVirtualText =
272-
UIManager.getViewManagerConfig('RCTVirtualText') == null
273-
? RCTText
274-
: createReactNativeComponentClass('RCTVirtualText', () => ({
275-
validAttributes: {
276-
...ReactNativeViewAttributes.UIView,
277-
isHighlighted: true,
278-
maxFontSizeMultiplier: true,
279-
},
280-
uiViewClassName: 'RCTVirtualText',
281-
}));
282-
283234
const Text = (
284235
props: TextProps,
285-
forwardedRef: ?React.Ref<'RCTText' | 'RCTVirtualText'>,
236+
forwardedRef: ?React.Ref<typeof NativeText | typeof NativeVirtualText>,
286237
) => {
287238
return <TouchableText {...props} forwardedRef={forwardedRef} />;
288239
};

Libraries/Text/TextNativeComponent.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
import ReactNativeViewAttributes from '../Components/View/ReactNativeViewAttributes';
14+
import UIManager from '../ReactNative/UIManager';
15+
import {type HostComponent} from '../Renderer/shims/ReactNativeTypes';
16+
import createReactNativeComponentClass from '../Renderer/shims/createReactNativeComponentClass';
17+
import {type ProcessedColorValue} from '../StyleSheet/processColor';
18+
import {type TextProps} from './TextProps';
19+
20+
type NativeTextProps = $ReadOnly<{
21+
...TextProps,
22+
isHighlighted?: ?boolean,
23+
selectionColor?: ?ProcessedColorValue,
24+
}>;
25+
26+
export const NativeText: HostComponent<NativeTextProps> = (createReactNativeComponentClass(
27+
'RCTText',
28+
() => ({
29+
validAttributes: {
30+
...ReactNativeViewAttributes.UIView,
31+
isHighlighted: true,
32+
numberOfLines: true,
33+
ellipsizeMode: true,
34+
allowFontScaling: true,
35+
maxFontSizeMultiplier: true,
36+
disabled: true,
37+
selectable: true,
38+
selectionColor: true,
39+
adjustsFontSizeToFit: true,
40+
minimumFontScale: true,
41+
textBreakStrategy: true,
42+
onTextLayout: true,
43+
onInlineViewLayout: true,
44+
dataDetectorType: true,
45+
},
46+
directEventTypes: {
47+
topTextLayout: {
48+
registrationName: 'onTextLayout',
49+
},
50+
topInlineViewLayout: {
51+
registrationName: 'onInlineViewLayout',
52+
},
53+
},
54+
uiViewClassName: 'RCTText',
55+
}),
56+
): any);
57+
58+
export const NativeVirtualText: HostComponent<NativeTextProps> =
59+
UIManager.getViewManagerConfig('RCTVirtualText') == null
60+
? NativeText
61+
: (createReactNativeComponentClass('RCTVirtualText', () => ({
62+
validAttributes: {
63+
...ReactNativeViewAttributes.UIView,
64+
isHighlighted: true,
65+
maxFontSizeMultiplier: true,
66+
},
67+
uiViewClassName: 'RCTVirtualText',
68+
})): any);

0 commit comments

Comments
 (0)