From ca78497f738bb4df5aaa82b925d24afbefe099bd Mon Sep 17 00:00:00 2001 From: Eli White Date: Fri, 15 Nov 2019 13:58:48 -0800 Subject: [PATCH] Reorder methods Summary: Reordering the methds in TextInput to be a bit more consistent. Changelog: [Internal] Reviewed By: JoshuaGross Differential Revision: D18435732 fbshipit-source-id: 05a1d9d2c70a4b5fa00a3dc6be0520a216a24106 --- Libraries/Components/TextInput/TextInput.js | 104 ++++++++++---------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/Libraries/Components/TextInput/TextInput.js b/Libraries/Components/TextInput/TextInput.js index 0e51d2a22a3..2ae08f40127 100644 --- a/Libraries/Components/TextInput/TextInput.js +++ b/Libraries/Components/TextInput/TextInput.js @@ -812,16 +812,6 @@ const TextInput = createReactClass({ */ mixins: [NativeMethodsMixin], - /** - * Returns `true` if the input is currently focused; `false` otherwise. - */ - isFocused: function(): boolean { - return ( - TextInputState.currentlyFocusedField() === - ReactNative.findNodeHandle(this._inputRef) - ); - }, - _inputRef: (undefined: any), _focusSubscription: (undefined: ?Function), _lastNativeText: (undefined: ?string), @@ -841,6 +831,40 @@ const TextInput = createReactClass({ } }, + componentDidUpdate: function() { + // This is necessary in case native updates the text and JS decides + // that the update should be ignored and we should stick with the value + // that we have in JS. + const nativeProps = {}; + + if ( + this._lastNativeText !== this.props.value && + typeof this.props.value === 'string' + ) { + nativeProps.text = this.props.value; + } + + // Selection is also a controlled prop, if the native value doesn't match + // JS, update to the JS value. + const {selection} = this.props; + if ( + this._lastNativeSelection && + selection && + (this._lastNativeSelection.start !== selection.start || + this._lastNativeSelection.end !== selection.end) + ) { + nativeProps.selection = this.props.selection; + } + + if ( + Object.keys(nativeProps).length > 0 && + this._inputRef && + this._inputRef.setNativeProps + ) { + this._inputRef.setNativeProps(nativeProps); + } + }, + componentWillUnmount: function() { this._focusSubscription && this._focusSubscription.remove(); if (this.isFocused()) { @@ -862,6 +886,20 @@ const TextInput = createReactClass({ this.setNativeProps({text: ''}); }, + /** + * Returns `true` if the input is currently focused; `false` otherwise. + */ + isFocused: function(): boolean { + return ( + TextInputState.currentlyFocusedField() === + ReactNative.findNodeHandle(this._inputRef) + ); + }, + + getNativeRef: function(): ?React.ElementRef> { + return this._inputRef; + }, + render: function() { let textInput = null; let additionalTouchableProps: {| @@ -974,17 +1012,6 @@ const TextInput = createReactClass({ this._inputRef = ref; }, - getNativeRef: function(): ?React.ElementRef> { - return this._inputRef; - }, - - _onFocus: function(event: FocusEvent) { - TextInputState.focusField(ReactNative.findNodeHandle(this._inputRef)); - if (this.props.onFocus) { - this.props.onFocus(event); - } - }, - _onPress: function(event: PressEvent) { if (this.props.editable || this.props.editable === undefined) { this.focus(); @@ -1030,37 +1057,10 @@ const TextInput = createReactClass({ } }, - componentDidUpdate: function() { - // This is necessary in case native updates the text and JS decides - // that the update should be ignored and we should stick with the value - // that we have in JS. - const nativeProps = {}; - - if ( - this._lastNativeText !== this.props.value && - typeof this.props.value === 'string' - ) { - nativeProps.text = this.props.value; - } - - // Selection is also a controlled prop, if the native value doesn't match - // JS, update to the JS value. - const {selection} = this.props; - if ( - this._lastNativeSelection && - selection && - (this._lastNativeSelection.start !== selection.start || - this._lastNativeSelection.end !== selection.end) - ) { - nativeProps.selection = this.props.selection; - } - - if ( - Object.keys(nativeProps).length > 0 && - this._inputRef && - this._inputRef.setNativeProps - ) { - this._inputRef.setNativeProps(nativeProps); + _onFocus: function(event: FocusEvent) { + TextInputState.focusField(ReactNative.findNodeHandle(this._inputRef)); + if (this.props.onFocus) { + this.props.onFocus(event); } },