Skip to content

Commit af00f57

Browse files
excedfacebook-github-bot
authored andcommitted
Flow strict TextInput (facebook#22250)
Summary: Related to facebook#22100 Enhance TextInput with callback event types. This is a first draft and I will need more help on this one. Flow checks are successful now but I am not sure types are accurate though. Moreover I find my separation approach kind of dirty for callback event types. - All flow tests succeed. [GENERAL] [ENHANCEMENT] [TextInput.js] - Flow types [GENERAL] [ENHANCEMENT] [TextInputExample.android.js] - Fixing Flow types [GENERAL] [ENHANCEMENT] [TextInputExample.ios.js] - Fixing Flow types [GENERAL] [ENHANCEMENT] [XHRExampleFetch.js] - Fixing Flow types Pull Request resolved: facebook#22250 Reviewed By: TheSavior Differential Revision: D13104820 Pulled By: RSNara fbshipit-source-id: 3fbb98d0ec2b62be676f71ae1053933d9c78485e
1 parent f9a11bb commit af00f57

File tree

4 files changed

+92
-30
lines changed

4 files changed

+92
-30
lines changed

Libraries/Components/TextInput/TextInput.js

+83-22
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const warning = require('fbjs/lib/warning');
3434
import type {TextStyleProp, ViewStyleProp} from 'StyleSheet';
3535
import type {ColorValue} from 'StyleSheetTypes';
3636
import type {ViewProps} from 'ViewPropTypes';
37+
import type {SyntheticEvent} from 'CoreEventTypes';
38+
import type {PressEvent} from 'CoreEventTypes';
3739

3840
let AndroidTextInput;
3941
let RCTMultilineTextInputView;
@@ -55,11 +57,70 @@ const onlyMultiline = {
5557
children: true,
5658
};
5759

58-
type Event = Object;
59-
type Selection = {
60+
type Range = $ReadOnly<{|
61+
start: number,
62+
end: number,
63+
|}>;
64+
type Selection = $ReadOnly<{|
6065
start: number,
6166
end?: number,
62-
};
67+
|}>;
68+
type ContentSize = $ReadOnly<{|
69+
width: number,
70+
height: number,
71+
|}>;
72+
type ContentOffset = $ReadOnly<{|
73+
x: number,
74+
y: number,
75+
|}>;
76+
type ChangeEvent = SyntheticEvent<
77+
$ReadOnly<{|
78+
target: number,
79+
eventCount: number,
80+
text: string,
81+
|}>,
82+
>;
83+
type TextInputEvent = SyntheticEvent<
84+
$ReadOnly<{|
85+
previousText: string,
86+
range: Range,
87+
target: number,
88+
text: string,
89+
|}>,
90+
>;
91+
type ContentSizeChangeEvent = SyntheticEvent<
92+
$ReadOnly<{|
93+
target: number,
94+
contentSize: ContentSize,
95+
|}>,
96+
>;
97+
type ScrollEvent = SyntheticEvent<
98+
$ReadOnly<{|
99+
target: number,
100+
contentOffset: ContentOffset,
101+
|}>,
102+
>;
103+
type TargetEvent = SyntheticEvent<
104+
$ReadOnly<{|
105+
target: number,
106+
|}>,
107+
>;
108+
type SelectionChangeEvent = SyntheticEvent<
109+
$ReadOnly<{|
110+
selection: Selection,
111+
|}>,
112+
>;
113+
type KeyPressEvent = SyntheticEvent<
114+
$ReadOnly<{|
115+
key: string,
116+
|}>,
117+
>;
118+
type EditingEvent = SyntheticEvent<
119+
$ReadOnly<{|
120+
text: string,
121+
target: number,
122+
|}>,
123+
>;
63124

64125
const DataDetectorTypes = [
65126
'phoneNumber',
@@ -184,17 +245,17 @@ type Props = $ReadOnly<{|
184245
returnKeyType?: ?ReturnKeyType,
185246
maxLength?: ?number,
186247
multiline?: ?boolean,
187-
onBlur?: ?Function,
188-
onFocus?: ?Function,
189-
onChange?: ?Function,
190-
onChangeText?: ?Function,
191-
onContentSizeChange?: ?Function,
192-
onTextInput?: ?Function,
193-
onEndEditing?: ?Function,
194-
onSelectionChange?: ?Function,
195-
onSubmitEditing?: ?Function,
196-
onKeyPress?: ?Function,
197-
onScroll?: ?Function,
248+
onBlur?: ?(e: TargetEvent) => void,
249+
onFocus?: ?(e: TargetEvent) => void,
250+
onChange?: ?(e: ChangeEvent) => void,
251+
onChangeText?: ?(text: string) => void,
252+
onContentSizeChange?: ?(e: ContentSizeChangeEvent) => void,
253+
onTextInput?: ?(e: TextInputEvent) => void,
254+
onEndEditing?: ?(e: EditingEvent) => void,
255+
onSelectionChange?: ?(e: SelectionChangeEvent) => void,
256+
onSubmitEditing?: ?(e: EditingEvent) => void,
257+
onKeyPress?: ?(e: KeyPressEvent) => void,
258+
onScroll?: ?(e: ScrollEvent) => void,
198259
placeholder?: ?Stringish,
199260
placeholderTextColor?: ?ColorValue,
200261
secureTextEntry?: ?boolean,
@@ -792,7 +853,7 @@ const TextInput = createReactClass({
792853
'oneTimeCode',
793854
]),
794855
},
795-
getDefaultProps(): Object {
856+
getDefaultProps() {
796857
return {
797858
allowFontScaling: true,
798859
underlineColorAndroid: 'transparent',
@@ -1108,7 +1169,7 @@ const TextInput = createReactClass({
11081169
);
11091170
},
11101171

1111-
_onFocus: function(event: Event) {
1172+
_onFocus: function(event: TargetEvent) {
11121173
if (this.props.onFocus) {
11131174
this.props.onFocus(event);
11141175
}
@@ -1118,13 +1179,13 @@ const TextInput = createReactClass({
11181179
}
11191180
},
11201181

1121-
_onPress: function(event: Event) {
1182+
_onPress: function(event: PressEvent) {
11221183
if (this.props.editable || this.props.editable === undefined) {
11231184
this.focus();
11241185
}
11251186
},
11261187

1127-
_onChange: function(event: Event) {
1188+
_onChange: function(event: ChangeEvent) {
11281189
// Make sure to fire the mostRecentEventCount first so it is already set on
11291190
// native when the text value is set.
11301191
if (this._inputRef && this._inputRef.setNativeProps) {
@@ -1147,7 +1208,7 @@ const TextInput = createReactClass({
11471208
this.forceUpdate();
11481209
},
11491210

1150-
_onSelectionChange: function(event: Event) {
1211+
_onSelectionChange: function(event: SelectionChangeEvent) {
11511212
this.props.onSelectionChange && this.props.onSelectionChange(event);
11521213

11531214
if (!this._inputRef) {
@@ -1201,7 +1262,7 @@ const TextInput = createReactClass({
12011262
}
12021263
},
12031264

1204-
_onBlur: function(event: Event) {
1265+
_onBlur: function(event: TargetEvent) {
12051266
if (this.props.onBlur) {
12061267
this.props.onBlur(event);
12071268
}
@@ -1211,11 +1272,11 @@ const TextInput = createReactClass({
12111272
}
12121273
},
12131274

1214-
_onTextInput: function(event: Event) {
1275+
_onTextInput: function(event: TextInputEvent) {
12151276
this.props.onTextInput && this.props.onTextInput(event);
12161277
},
12171278

1218-
_onScroll: function(event: Event) {
1279+
_onScroll: function(event: ScrollEvent) {
12191280
this.props.onScroll && this.props.onScroll(event);
12201281
},
12211282
});

RNTester/js/TextInputExample.android.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class TextEventsExample extends React.Component<{}, $FlowFixMeState> {
4848
}
4949
onContentSizeChange={event =>
5050
this.updateText(
51-
'onContentSizeChange size: ' + event.nativeEvent.contentSize,
51+
'onContentSizeChange size: ' +
52+
JSON.stringify(event.nativeEvent.contentSize),
5253
)
5354
}
5455
onEndEditing={event =>
@@ -253,10 +254,10 @@ class ToggleDefaultPaddingExample extends React.Component<
253254
}
254255

255256
type SelectionExampleState = {
256-
selection: {
257+
selection: $ReadOnly<{|
257258
start: number,
258-
end: number,
259-
},
259+
end?: number,
260+
|}>,
260261
value: string,
261262
};
262263

RNTester/js/TextInputExample.ios.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class TextEventsExample extends React.Component<{}, $FlowFixMeState> {
7171
'onSelectionChange range: ' +
7272
event.nativeEvent.selection.start +
7373
',' +
74-
event.nativeEvent.selection.end,
74+
(event.nativeEvent.selection.end || ''),
7575
)
7676
}
7777
onKeyPress={event => {
@@ -348,10 +348,10 @@ class BlurOnSubmitExample extends React.Component<{}> {
348348
}
349349

350350
type SelectionExampleState = {
351-
selection: {|
351+
selection: $ReadOnly<{|
352352
start: number,
353353
end?: number,
354-
|},
354+
|}>,
355355
value: string,
356356
};
357357

RNTester/js/XHRExampleFetch.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class XHRExampleFetch extends React.Component<any, any> {
2727
this.responseHeaders = null;
2828
}
2929

30-
submit(uri: String) {
30+
submit(uri: string) {
3131
fetch(uri)
3232
.then(response => {
3333
this.responseURL = response.url;

0 commit comments

Comments
 (0)