Fix `currentlyFocusedField` by Removing `this` usage in TextInputState (#19834)

Summary:
I broke `currentlyFocusedField` when adding it back in ce3b7b8204 because `this` no longer refers to the proper object because it is assigned here ce3b7b8204 (diff-b48972356bc8dca4a00747d002fc3dd5R330). This code was pretty prone to breaking so I simply removed the `this` usage and rely on a top level variable instead. Also moved everything to named functions.
Pull Request resolved: https://github.com/facebook/react-native/pull/19834

Differential Revision: D8943088

Pulled By: hramos

fbshipit-source-id: 24d1470f6117138a5978fb7e467147847a9f3658
This commit is contained in:
Janic Duplessis 2018-07-20 16:28:04 -07:00 committed by Facebook Github Bot
parent ebf5aeab28
commit b4b594cec1
1 changed files with 61 additions and 60 deletions

View File

@ -18,73 +18,74 @@
const Platform = require('Platform'); const Platform = require('Platform');
const UIManager = require('UIManager'); const UIManager = require('UIManager');
let currentlyFocusedID: ?number = null;
const inputs = new Set(); const inputs = new Set();
const TextInputState = { /**
/** * Returns the ID of the currently focused text field, if one exists
* Internal state * If no text field is focused it returns null
*/ */
_currentlyFocusedID: (null: ?number), function currentlyFocusedField(): ?number {
return currentlyFocusedID;
}
/** /**
* Returns the ID of the currently focused text field, if one exists * @param {number} TextInputID id of the text field to focus
* If no text field is focused it returns null * Focuses the specified text field
*/ * noop if the text field was already focused
currentlyFocusedField: function(): ?number { */
return this._currentlyFocusedID; function focusTextInput(textFieldID: ?number) {
}, if (currentlyFocusedID !== textFieldID && textFieldID !== null) {
currentlyFocusedID = textFieldID;
/** if (Platform.OS === 'ios') {
* @param {number} TextInputID id of the text field to focus UIManager.focus(textFieldID);
* Focuses the specified text field } else if (Platform.OS === 'android') {
* noop if the text field was already focused UIManager.dispatchViewManagerCommand(
*/ textFieldID,
focusTextInput: function(textFieldID: ?number) { UIManager.AndroidTextInput.Commands.focusTextInput,
if (this._currentlyFocusedID !== textFieldID && textFieldID !== null) { null,
this._currentlyFocusedID = textFieldID; );
if (Platform.OS === 'ios') {
UIManager.focus(textFieldID);
} else if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
textFieldID,
UIManager.AndroidTextInput.Commands.focusTextInput,
null,
);
}
} }
}, }
}
/** /**
* @param {number} textFieldID id of the text field to unfocus * @param {number} textFieldID id of the text field to unfocus
* Unfocuses the specified text field * Unfocuses the specified text field
* noop if it wasn't focused * noop if it wasn't focused
*/ */
blurTextInput: function(textFieldID: ?number) { function blurTextInput(textFieldID: ?number) {
if (this._currentlyFocusedID === textFieldID && textFieldID !== null) { if (currentlyFocusedID === textFieldID && textFieldID !== null) {
this._currentlyFocusedID = null; currentlyFocusedID = null;
if (Platform.OS === 'ios') { if (Platform.OS === 'ios') {
UIManager.blur(textFieldID); UIManager.blur(textFieldID);
} else if (Platform.OS === 'android') { } else if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand( UIManager.dispatchViewManagerCommand(
textFieldID, textFieldID,
UIManager.AndroidTextInput.Commands.blurTextInput, UIManager.AndroidTextInput.Commands.blurTextInput,
null, null,
); );
}
} }
}, }
}
registerInput: function(textFieldID: number) { function registerInput(textFieldID: number) {
inputs.add(textFieldID); inputs.add(textFieldID);
}, }
unregisterInput: function(textFieldID: number) { function unregisterInput(textFieldID: number) {
inputs.delete(textFieldID); inputs.delete(textFieldID);
}, }
isTextInput: function(textFieldID: number) { function isTextInput(textFieldID: number) {
return inputs.has(textFieldID); return inputs.has(textFieldID);
}, }
module.exports = {
currentlyFocusedField,
focusTextInput,
blurTextInput,
registerInput,
unregisterInput,
isTextInput,
}; };
module.exports = TextInputState;