Skip to content

Commit

Permalink
Become compatible with Flow's TypesFirst
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TheSavior authored and facebook-github-bot committed Nov 15, 2019
1 parent 8f60141 commit 99dc4e2
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions Libraries/Components/TextInput/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -791,16 +803,16 @@ const emptyFunctionThatReturnsTrue = () => true;
* or control this param programmatically with native code.
*
*/
class TextInput extends React.Component<Props> {
static defaultProps = {
class TextInput extends React.Component<Props, State> {
static defaultProps: DefaultProps = {
allowFontScaling: true,
rejectResponderTermination: true,
underlineColorAndroid: 'transparent',
};

static propTypes = DeprecatedTextInputPropTypes;

static State = {
static State: State = {
currentlyFocusedField: TextInputState.currentlyFocusedField,
focusTextInput: TextInputState.focusTextInput,
blurTextInput: TextInputState.blurTextInput,
Expand Down Expand Up @@ -876,52 +888,54 @@ class TextInput extends React.Component<Props> {
/**
* 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<HostComponent<mixed>> => {
getNativeRef: () => ?React.ElementRef<HostComponent<mixed>> = () => {
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<HostComponent<mixed>>,
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<
Expand Down

0 comments on commit 99dc4e2

Please sign in to comment.