Skip to content

Commit ca78497

Browse files
elicwhitefacebook-github-bot
authored andcommitted
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
1 parent c70a093 commit ca78497

File tree

1 file changed

+52
-52
lines changed

1 file changed

+52
-52
lines changed

Libraries/Components/TextInput/TextInput.js

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -812,16 +812,6 @@ const TextInput = createReactClass({
812812
*/
813813
mixins: [NativeMethodsMixin],
814814

815-
/**
816-
* Returns `true` if the input is currently focused; `false` otherwise.
817-
*/
818-
isFocused: function(): boolean {
819-
return (
820-
TextInputState.currentlyFocusedField() ===
821-
ReactNative.findNodeHandle(this._inputRef)
822-
);
823-
},
824-
825815
_inputRef: (undefined: any),
826816
_focusSubscription: (undefined: ?Function),
827817
_lastNativeText: (undefined: ?string),
@@ -841,6 +831,40 @@ const TextInput = createReactClass({
841831
}
842832
},
843833

834+
componentDidUpdate: function() {
835+
// This is necessary in case native updates the text and JS decides
836+
// that the update should be ignored and we should stick with the value
837+
// that we have in JS.
838+
const nativeProps = {};
839+
840+
if (
841+
this._lastNativeText !== this.props.value &&
842+
typeof this.props.value === 'string'
843+
) {
844+
nativeProps.text = this.props.value;
845+
}
846+
847+
// Selection is also a controlled prop, if the native value doesn't match
848+
// JS, update to the JS value.
849+
const {selection} = this.props;
850+
if (
851+
this._lastNativeSelection &&
852+
selection &&
853+
(this._lastNativeSelection.start !== selection.start ||
854+
this._lastNativeSelection.end !== selection.end)
855+
) {
856+
nativeProps.selection = this.props.selection;
857+
}
858+
859+
if (
860+
Object.keys(nativeProps).length > 0 &&
861+
this._inputRef &&
862+
this._inputRef.setNativeProps
863+
) {
864+
this._inputRef.setNativeProps(nativeProps);
865+
}
866+
},
867+
844868
componentWillUnmount: function() {
845869
this._focusSubscription && this._focusSubscription.remove();
846870
if (this.isFocused()) {
@@ -862,6 +886,20 @@ const TextInput = createReactClass({
862886
this.setNativeProps({text: ''});
863887
},
864888

889+
/**
890+
* Returns `true` if the input is currently focused; `false` otherwise.
891+
*/
892+
isFocused: function(): boolean {
893+
return (
894+
TextInputState.currentlyFocusedField() ===
895+
ReactNative.findNodeHandle(this._inputRef)
896+
);
897+
},
898+
899+
getNativeRef: function(): ?React.ElementRef<HostComponent<mixed>> {
900+
return this._inputRef;
901+
},
902+
865903
render: function() {
866904
let textInput = null;
867905
let additionalTouchableProps: {|
@@ -974,17 +1012,6 @@ const TextInput = createReactClass({
9741012
this._inputRef = ref;
9751013
},
9761014

977-
getNativeRef: function(): ?React.ElementRef<HostComponent<mixed>> {
978-
return this._inputRef;
979-
},
980-
981-
_onFocus: function(event: FocusEvent) {
982-
TextInputState.focusField(ReactNative.findNodeHandle(this._inputRef));
983-
if (this.props.onFocus) {
984-
this.props.onFocus(event);
985-
}
986-
},
987-
9881015
_onPress: function(event: PressEvent) {
9891016
if (this.props.editable || this.props.editable === undefined) {
9901017
this.focus();
@@ -1030,37 +1057,10 @@ const TextInput = createReactClass({
10301057
}
10311058
},
10321059

1033-
componentDidUpdate: function() {
1034-
// This is necessary in case native updates the text and JS decides
1035-
// that the update should be ignored and we should stick with the value
1036-
// that we have in JS.
1037-
const nativeProps = {};
1038-
1039-
if (
1040-
this._lastNativeText !== this.props.value &&
1041-
typeof this.props.value === 'string'
1042-
) {
1043-
nativeProps.text = this.props.value;
1044-
}
1045-
1046-
// Selection is also a controlled prop, if the native value doesn't match
1047-
// JS, update to the JS value.
1048-
const {selection} = this.props;
1049-
if (
1050-
this._lastNativeSelection &&
1051-
selection &&
1052-
(this._lastNativeSelection.start !== selection.start ||
1053-
this._lastNativeSelection.end !== selection.end)
1054-
) {
1055-
nativeProps.selection = this.props.selection;
1056-
}
1057-
1058-
if (
1059-
Object.keys(nativeProps).length > 0 &&
1060-
this._inputRef &&
1061-
this._inputRef.setNativeProps
1062-
) {
1063-
this._inputRef.setNativeProps(nativeProps);
1060+
_onFocus: function(event: FocusEvent) {
1061+
TextInputState.focusField(ReactNative.findNodeHandle(this._inputRef));
1062+
if (this.props.onFocus) {
1063+
this.props.onFocus(event);
10641064
}
10651065
},
10661066

0 commit comments

Comments
 (0)