Skip to content

Commit ab92c00

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Convert Text to ES6 Class
Summary: Utilizing ES6 Classes instead of createReactClass lets us actually enforce the way Text is used via Flow. Reviewed By: fkgozali Differential Revision: D7227755 fbshipit-source-id: 8e8285f9ebb3783a0dc4837c37c163178910ff9f
1 parent 0459e4f commit ab92c00

File tree

1 file changed

+59
-48
lines changed

1 file changed

+59
-48
lines changed

Libraries/Text/Text.js

+59-48
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,35 @@
1010
*/
1111
'use strict';
1212

13-
const NativeMethodsMixin = require('NativeMethodsMixin');
1413
const React = require('React');
14+
const ReactNative = require('ReactNative');
1515
const ReactNativeViewAttributes = require('ReactNativeViewAttributes');
1616
const TextPropTypes = require('TextPropTypes');
1717
const Touchable = require('Touchable');
1818
const UIManager = require('UIManager');
1919

20-
const createReactClass = require('create-react-class');
2120
const createReactNativeComponentClass = require('createReactNativeComponentClass');
2221
const mergeFast = require('mergeFast');
2322
const processColor = require('processColor');
2423
const {ViewContextTypes} = require('ViewContext');
2524

25+
import type {PressEvent} from 'CoreEventTypes';
26+
import type {TextProps} from 'TextProps';
27+
import type {ViewChildContext} from 'ViewContext';
28+
29+
type State = {
30+
isHighlighted: boolean,
31+
};
32+
33+
type RectOffset = {
34+
top: number,
35+
left: number,
36+
right: number,
37+
bottom: number,
38+
};
39+
40+
const PRESS_RECT_OFFSET = {top: 20, left: 20, right: 20, bottom: 30};
41+
2642
const viewConfig = {
2743
validAttributes: mergeFast(ReactNativeViewAttributes.UIView, {
2844
isHighlighted: true,
@@ -39,54 +55,54 @@ const viewConfig = {
3955
uiViewClassName: 'RCTText',
4056
};
4157

42-
import type {ViewChildContext} from 'ViewContext';
43-
4458
/**
4559
* A React component for displaying text.
4660
*
4761
* See https://facebook.github.io/react-native/docs/text.html
4862
*/
63+
class Text extends ReactNative.NativeComponent<TextProps, State> {
64+
static propTypes = TextPropTypes;
65+
static childContextTypes = ViewContextTypes;
66+
static contextTypes = ViewContextTypes;
67+
68+
static defaultProps = {
69+
accessible: true,
70+
allowFontScaling: true,
71+
ellipsizeMode: 'tail',
72+
};
73+
74+
state = mergeFast(Touchable.Mixin.touchableGetInitialState(), {
75+
isHighlighted: false,
76+
});
77+
78+
viewConfig = viewConfig;
4979

50-
const Text = createReactClass({
51-
displayName: 'Text',
52-
propTypes: TextPropTypes,
53-
getDefaultProps(): Object {
54-
return {
55-
accessible: true,
56-
allowFontScaling: true,
57-
ellipsizeMode: 'tail',
58-
};
59-
},
60-
getInitialState: function(): Object {
61-
return mergeFast(Touchable.Mixin.touchableGetInitialState(), {
62-
isHighlighted: false,
63-
});
64-
},
65-
mixins: [NativeMethodsMixin],
66-
viewConfig: viewConfig,
6780
getChildContext(): ViewChildContext {
6881
return {
6982
isInAParentText: true,
7083
};
71-
},
72-
childContextTypes: ViewContextTypes,
73-
contextTypes: ViewContextTypes,
74-
/**
75-
* Only assigned if touch is needed.
76-
*/
77-
_handlers: (null: ?Object),
84+
}
85+
86+
_handlers: ?Object;
87+
7888
_hasPressHandler(): boolean {
7989
return !!this.props.onPress || !!this.props.onLongPress;
80-
},
90+
}
8191
/**
8292
* These are assigned lazily the first time the responder is set to make plain
8393
* text nodes as cheap as possible.
8494
*/
85-
touchableHandleActivePressIn: (null: ?Function),
86-
touchableHandleActivePressOut: (null: ?Function),
87-
touchableHandlePress: (null: ?Function),
88-
touchableHandleLongPress: (null: ?Function),
89-
touchableGetPressRectOffset: (null: ?Function),
95+
touchableHandleActivePressIn: ?Function;
96+
touchableHandleActivePressOut: ?Function;
97+
touchableHandlePress: ?Function;
98+
touchableHandleLongPress: ?Function;
99+
touchableHandleResponderGrant: ?Function;
100+
touchableHandleResponderMove: ?Function;
101+
touchableHandleResponderRelease: ?Function;
102+
touchableHandleResponderTerminate: ?Function;
103+
touchableHandleResponderTerminationRequest: ?Function;
104+
touchableGetPressRectOffset: ?Function;
105+
90106
render(): React.Element<any> {
91107
let newProps = this.props;
92108
if (this.props.onStartShouldSetResponder || this._hasPressHandler()) {
@@ -95,7 +111,6 @@ const Text = createReactClass({
95111
onStartShouldSetResponder: (): boolean => {
96112
const shouldSetFromProps =
97113
this.props.onStartShouldSetResponder &&
98-
// $FlowFixMe(>=0.41.0)
99114
this.props.onStartShouldSetResponder();
100115
const setResponder = shouldSetFromProps || this._hasPressHandler();
101116
if (setResponder && !this.touchableHandleActivePressIn) {
@@ -130,11 +145,11 @@ const Text = createReactClass({
130145
});
131146
};
132147

133-
this.touchableHandlePress = (e: SyntheticEvent<>) => {
148+
this.touchableHandlePress = (e: PressEvent) => {
134149
this.props.onPress && this.props.onPress(e);
135150
};
136151

137-
this.touchableHandleLongPress = (e: SyntheticEvent<>) => {
152+
this.touchableHandleLongPress = (e: PressEvent) => {
138153
this.props.onLongPress && this.props.onLongPress(e);
139154
};
140155

@@ -145,28 +160,33 @@ const Text = createReactClass({
145160
return setResponder;
146161
},
147162
onResponderGrant: function(e: SyntheticEvent<>, dispatchID: string) {
163+
// $FlowFixMe TouchableMixin handlers couldn't actually be null
148164
this.touchableHandleResponderGrant(e, dispatchID);
149165
this.props.onResponderGrant &&
150166
this.props.onResponderGrant.apply(this, arguments);
151167
}.bind(this),
152168
onResponderMove: function(e: SyntheticEvent<>) {
169+
// $FlowFixMe TouchableMixin handlers couldn't actually be null
153170
this.touchableHandleResponderMove(e);
154171
this.props.onResponderMove &&
155172
this.props.onResponderMove.apply(this, arguments);
156173
}.bind(this),
157174
onResponderRelease: function(e: SyntheticEvent<>) {
175+
// $FlowFixMe TouchableMixin handlers couldn't actually be null
158176
this.touchableHandleResponderRelease(e);
159177
this.props.onResponderRelease &&
160178
this.props.onResponderRelease.apply(this, arguments);
161179
}.bind(this),
162180
onResponderTerminate: function(e: SyntheticEvent<>) {
181+
// $FlowFixMe TouchableMixin handlers couldn't actually be null
163182
this.touchableHandleResponderTerminate(e);
164183
this.props.onResponderTerminate &&
165184
this.props.onResponderTerminate.apply(this, arguments);
166185
}.bind(this),
167186
onResponderTerminationRequest: function(): boolean {
168187
// Allow touchable or props.onResponderTerminationRequest to deny
169188
// the request
189+
// $FlowFixMe TouchableMixin handlers couldn't actually be null
170190
var allowTermination = this.touchableHandleResponderTerminationRequest();
171191
if (allowTermination && this.props.onResponderTerminationRequest) {
172192
allowTermination = this.props.onResponderTerminationRequest.apply(
@@ -201,17 +221,8 @@ const Text = createReactClass({
201221
} else {
202222
return <RCTText {...newProps} />;
203223
}
204-
},
205-
});
206-
207-
type RectOffset = {
208-
top: number,
209-
left: number,
210-
right: number,
211-
bottom: number,
212-
};
213-
214-
var PRESS_RECT_OFFSET = {top: 20, left: 20, right: 20, bottom: 30};
224+
}
225+
}
215226

216227
var RCTText = createReactNativeComponentClass(
217228
viewConfig.uiViewClassName,

0 commit comments

Comments
 (0)