From 99dc4e204e07e1ffb4ab263ceab460c70ad5ae70 Mon Sep 17 00:00:00 2001 From: Eli White Date: Fri, 15 Nov 2019 13:58:48 -0800 Subject: [PATCH] Become compatible with Flow's TypesFirst Summary: When the TextInput class is exported directly Flow complains about some definitions because they don't properly define the export type. This change adds those types but still doesn't export the TextInput directly as there are more things that still need to get fixed. Changelog: [Internal] Reviewed By: JoshuaGross Differential Revision: D18444096 fbshipit-source-id: 18c88bbf1de5504f350681a71ea21d7e41876e49 --- Libraries/Components/TextInput/TextInput.js | 42 ++++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/Libraries/Components/TextInput/TextInput.js b/Libraries/Components/TextInput/TextInput.js index f5e71414cde..aba912d03a9 100644 --- a/Libraries/Components/TextInput/TextInput.js +++ b/Libraries/Components/TextInput/TextInput.js @@ -678,6 +678,18 @@ export type Props = $ReadOnly<{| contextMenuHidden?: ?boolean, |}>; +type DefaultProps = $ReadOnly<{| + allowFontScaling: boolean, + rejectResponderTermination: boolean, + underlineColorAndroid: 'transparent', +|}>; + +type State = {| + currentlyFocusedField: typeof TextInputState.currentlyFocusedField, + focusTextInput: typeof TextInputState.focusTextInput, + blurTextInput: typeof TextInputState.blurTextInput, +|}; + const emptyFunctionThatReturnsTrue = () => true; /** @@ -791,8 +803,8 @@ const emptyFunctionThatReturnsTrue = () => true; * or control this param programmatically with native code. * */ -class TextInput extends React.Component { - static defaultProps = { +class TextInput extends React.Component { + static defaultProps: DefaultProps = { allowFontScaling: true, rejectResponderTermination: true, underlineColorAndroid: 'transparent', @@ -800,7 +812,7 @@ class TextInput extends React.Component { static propTypes = DeprecatedTextInputPropTypes; - static State = { + static State: State = { currentlyFocusedField: TextInputState.currentlyFocusedField, focusTextInput: TextInputState.focusTextInput, blurTextInput: TextInputState.blurTextInput, @@ -876,52 +888,54 @@ class TextInput extends React.Component { /** * Removes all text from the `TextInput`. */ - clear = () => { + clear: () => void = () => { this.setNativeProps({text: ''}); }; /** * Returns `true` if the input is currently focused; `false` otherwise. */ - isFocused = (): boolean => { + isFocused: () => boolean = () => { return ( TextInputState.currentlyFocusedField() === ReactNative.findNodeHandle(this._inputRef) ); }; - getNativeRef = (): ?React.ElementRef> => { + getNativeRef: () => ?React.ElementRef> = () => { return this._inputRef; }; // From NativeMethodsMixin // We need these instead of using forwardRef because we also have the other // methods we expose - blur = () => { + blur: () => void = () => { this._inputRef && this._inputRef.blur(); }; - focus = () => { + focus: () => void = () => { this._inputRef && this._inputRef.focus(); }; - measure = (callback: MeasureOnSuccessCallback) => { + measure: (callback: MeasureOnSuccessCallback) => void = callback => { this._inputRef && this._inputRef.measure(callback); }; - measureInWindow = (callback: MeasureInWindowOnSuccessCallback) => { + measureInWindow: ( + callback: MeasureInWindowOnSuccessCallback, + ) => void = callback => { this._inputRef && this._inputRef.measureInWindow(callback); }; - measureLayout = ( + measureLayout: ( relativeToNativeNode: number | React.ElementRef>, onSuccess: MeasureLayoutOnSuccessCallback, onFail?: () => void, - ) => { + ) => void = (relativeToNativeNode, onSuccess, onFail) => { this._inputRef && this._inputRef.measureLayout(relativeToNativeNode, onSuccess, onFail); }; - setNativeProps = (nativeProps: Object) => { + setNativeProps: (nativeProps: Object) => void = nativeProps => { this._inputRef && this._inputRef.setNativeProps(nativeProps); }; - render() { + render(): React.Node { let textInput = null; let additionalTouchableProps: {| rejectResponderTermination?: $PropertyType<