Fix `currentlyFocusedField` by Removing `this` usage in TextInputState (#19834)
Summary: I broke `currentlyFocusedField` when adding it back ince3b7b8204
because `this` no longer refers to the proper object because it is assigned herece3b7b8204 (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:
parent
ebf5aeab28
commit
b4b594cec1
|
@ -18,30 +18,25 @@
|
|||
const Platform = require('Platform');
|
||||
const UIManager = require('UIManager');
|
||||
|
||||
let currentlyFocusedID: ?number = null;
|
||||
const inputs = new Set();
|
||||
|
||||
const TextInputState = {
|
||||
/**
|
||||
* Internal state
|
||||
*/
|
||||
_currentlyFocusedID: (null: ?number),
|
||||
|
||||
/**
|
||||
/**
|
||||
* Returns the ID of the currently focused text field, if one exists
|
||||
* If no text field is focused it returns null
|
||||
*/
|
||||
currentlyFocusedField: function(): ?number {
|
||||
return this._currentlyFocusedID;
|
||||
},
|
||||
function currentlyFocusedField(): ?number {
|
||||
return currentlyFocusedID;
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* @param {number} TextInputID id of the text field to focus
|
||||
* Focuses the specified text field
|
||||
* noop if the text field was already focused
|
||||
*/
|
||||
focusTextInput: function(textFieldID: ?number) {
|
||||
if (this._currentlyFocusedID !== textFieldID && textFieldID !== null) {
|
||||
this._currentlyFocusedID = textFieldID;
|
||||
function focusTextInput(textFieldID: ?number) {
|
||||
if (currentlyFocusedID !== textFieldID && textFieldID !== null) {
|
||||
currentlyFocusedID = textFieldID;
|
||||
if (Platform.OS === 'ios') {
|
||||
UIManager.focus(textFieldID);
|
||||
} else if (Platform.OS === 'android') {
|
||||
|
@ -52,16 +47,16 @@ const TextInputState = {
|
|||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* @param {number} textFieldID id of the text field to unfocus
|
||||
* Unfocuses the specified text field
|
||||
* noop if it wasn't focused
|
||||
*/
|
||||
blurTextInput: function(textFieldID: ?number) {
|
||||
if (this._currentlyFocusedID === textFieldID && textFieldID !== null) {
|
||||
this._currentlyFocusedID = null;
|
||||
function blurTextInput(textFieldID: ?number) {
|
||||
if (currentlyFocusedID === textFieldID && textFieldID !== null) {
|
||||
currentlyFocusedID = null;
|
||||
if (Platform.OS === 'ios') {
|
||||
UIManager.blur(textFieldID);
|
||||
} else if (Platform.OS === 'android') {
|
||||
|
@ -72,19 +67,25 @@ const TextInputState = {
|
|||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
registerInput: function(textFieldID: number) {
|
||||
function registerInput(textFieldID: number) {
|
||||
inputs.add(textFieldID);
|
||||
},
|
||||
}
|
||||
|
||||
unregisterInput: function(textFieldID: number) {
|
||||
function unregisterInput(textFieldID: number) {
|
||||
inputs.delete(textFieldID);
|
||||
},
|
||||
}
|
||||
|
||||
isTextInput: function(textFieldID: number) {
|
||||
function isTextInput(textFieldID: number) {
|
||||
return inputs.has(textFieldID);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = TextInputState;
|
||||
module.exports = {
|
||||
currentlyFocusedField,
|
||||
focusTextInput,
|
||||
blurTextInput,
|
||||
registerInput,
|
||||
unregisterInput,
|
||||
isTextInput,
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue