Skip to content

Commit

Permalink
Fix currentlyFocusedField by Removing this usage in TextInputState (
Browse files Browse the repository at this point in the history
#19834)

Summary:
I broke `currentlyFocusedField` when adding it back in ce3b7b8 because `this` no longer refers to the proper object because it is assigned here ce3b7b8#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: #19834

Differential Revision: D8943088

Pulled By: hramos

fbshipit-source-id: 24d1470f6117138a5978fb7e467147847a9f3658
  • Loading branch information
janicduplessis authored and facebook-github-bot committed Jul 20, 2018
1 parent ebf5aea commit b4b594c
Showing 1 changed file with 61 additions and 60 deletions.
121 changes: 61 additions & 60 deletions Libraries/Components/TextInput/TextInputState.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,74 @@
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;
},
/**
* Returns the ID of the currently focused text field, if one exists
* If no text field is focused it returns null
*/
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;
if (Platform.OS === 'ios') {
UIManager.focus(textFieldID);
} else if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
textFieldID,
UIManager.AndroidTextInput.Commands.focusTextInput,
null,
);
}
/**
* @param {number} TextInputID id of the text field to focus
* Focuses the specified text field
* noop if the text field was already focused
*/
function focusTextInput(textFieldID: ?number) {
if (currentlyFocusedID !== textFieldID && textFieldID !== null) {
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
* Unfocuses the specified text field
* noop if it wasn't focused
*/
blurTextInput: function(textFieldID: ?number) {
if (this._currentlyFocusedID === textFieldID && textFieldID !== null) {
this._currentlyFocusedID = null;
if (Platform.OS === 'ios') {
UIManager.blur(textFieldID);
} else if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
textFieldID,
UIManager.AndroidTextInput.Commands.blurTextInput,
null,
);
}
/**
* @param {number} textFieldID id of the text field to unfocus
* Unfocuses the specified text field
* noop if it wasn't focused
*/
function blurTextInput(textFieldID: ?number) {
if (currentlyFocusedID === textFieldID && textFieldID !== null) {
currentlyFocusedID = null;
if (Platform.OS === 'ios') {
UIManager.blur(textFieldID);
} else if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
textFieldID,
UIManager.AndroidTextInput.Commands.blurTextInput,
null,
);
}
},
}
}

registerInput: function(textFieldID: number) {
inputs.add(textFieldID);
},
function registerInput(textFieldID: number) {
inputs.add(textFieldID);
}

unregisterInput: function(textFieldID: number) {
inputs.delete(textFieldID);
},
function unregisterInput(textFieldID: number) {
inputs.delete(textFieldID);
}

isTextInput: function(textFieldID: number) {
return inputs.has(textFieldID);
},
};
function isTextInput(textFieldID: number) {
return inputs.has(textFieldID);
}

module.exports = TextInputState;
module.exports = {
currentlyFocusedField,
focusTextInput,
blurTextInput,
registerInput,
unregisterInput,
isTextInput,
};

0 comments on commit b4b594c

Please sign in to comment.