Skip to content

Commit b4b594c

Browse files
janicduplessisfacebook-github-bot
authored andcommitted
Fix currentlyFocusedField by Removing this usage in TextInputState (#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
1 parent ebf5aea commit b4b594c

File tree

1 file changed

+61
-60
lines changed

1 file changed

+61
-60
lines changed

Libraries/Components/TextInput/TextInputState.js

+61-60
Original file line numberDiff line numberDiff line change
@@ -18,73 +18,74 @@
1818
const Platform = require('Platform');
1919
const UIManager = require('UIManager');
2020

21+
let currentlyFocusedID: ?number = null;
2122
const inputs = new Set();
2223

23-
const TextInputState = {
24-
/**
25-
* Internal state
26-
*/
27-
_currentlyFocusedID: (null: ?number),
28-
29-
/**
30-
* Returns the ID of the currently focused text field, if one exists
31-
* If no text field is focused it returns null
32-
*/
33-
currentlyFocusedField: function(): ?number {
34-
return this._currentlyFocusedID;
35-
},
24+
/**
25+
* Returns the ID of the currently focused text field, if one exists
26+
* If no text field is focused it returns null
27+
*/
28+
function currentlyFocusedField(): ?number {
29+
return currentlyFocusedID;
30+
}
3631

37-
/**
38-
* @param {number} TextInputID id of the text field to focus
39-
* Focuses the specified text field
40-
* noop if the text field was already focused
41-
*/
42-
focusTextInput: function(textFieldID: ?number) {
43-
if (this._currentlyFocusedID !== textFieldID && textFieldID !== null) {
44-
this._currentlyFocusedID = textFieldID;
45-
if (Platform.OS === 'ios') {
46-
UIManager.focus(textFieldID);
47-
} else if (Platform.OS === 'android') {
48-
UIManager.dispatchViewManagerCommand(
49-
textFieldID,
50-
UIManager.AndroidTextInput.Commands.focusTextInput,
51-
null,
52-
);
53-
}
32+
/**
33+
* @param {number} TextInputID id of the text field to focus
34+
* Focuses the specified text field
35+
* noop if the text field was already focused
36+
*/
37+
function focusTextInput(textFieldID: ?number) {
38+
if (currentlyFocusedID !== textFieldID && textFieldID !== null) {
39+
currentlyFocusedID = textFieldID;
40+
if (Platform.OS === 'ios') {
41+
UIManager.focus(textFieldID);
42+
} else if (Platform.OS === 'android') {
43+
UIManager.dispatchViewManagerCommand(
44+
textFieldID,
45+
UIManager.AndroidTextInput.Commands.focusTextInput,
46+
null,
47+
);
5448
}
55-
},
49+
}
50+
}
5651

57-
/**
58-
* @param {number} textFieldID id of the text field to unfocus
59-
* Unfocuses the specified text field
60-
* noop if it wasn't focused
61-
*/
62-
blurTextInput: function(textFieldID: ?number) {
63-
if (this._currentlyFocusedID === textFieldID && textFieldID !== null) {
64-
this._currentlyFocusedID = null;
65-
if (Platform.OS === 'ios') {
66-
UIManager.blur(textFieldID);
67-
} else if (Platform.OS === 'android') {
68-
UIManager.dispatchViewManagerCommand(
69-
textFieldID,
70-
UIManager.AndroidTextInput.Commands.blurTextInput,
71-
null,
72-
);
73-
}
52+
/**
53+
* @param {number} textFieldID id of the text field to unfocus
54+
* Unfocuses the specified text field
55+
* noop if it wasn't focused
56+
*/
57+
function blurTextInput(textFieldID: ?number) {
58+
if (currentlyFocusedID === textFieldID && textFieldID !== null) {
59+
currentlyFocusedID = null;
60+
if (Platform.OS === 'ios') {
61+
UIManager.blur(textFieldID);
62+
} else if (Platform.OS === 'android') {
63+
UIManager.dispatchViewManagerCommand(
64+
textFieldID,
65+
UIManager.AndroidTextInput.Commands.blurTextInput,
66+
null,
67+
);
7468
}
75-
},
69+
}
70+
}
7671

77-
registerInput: function(textFieldID: number) {
78-
inputs.add(textFieldID);
79-
},
72+
function registerInput(textFieldID: number) {
73+
inputs.add(textFieldID);
74+
}
8075

81-
unregisterInput: function(textFieldID: number) {
82-
inputs.delete(textFieldID);
83-
},
76+
function unregisterInput(textFieldID: number) {
77+
inputs.delete(textFieldID);
78+
}
8479

85-
isTextInput: function(textFieldID: number) {
86-
return inputs.has(textFieldID);
87-
},
88-
};
80+
function isTextInput(textFieldID: number) {
81+
return inputs.has(textFieldID);
82+
}
8983

90-
module.exports = TextInputState;
84+
module.exports = {
85+
currentlyFocusedField,
86+
focusTextInput,
87+
blurTextInput,
88+
registerInput,
89+
unregisterInput,
90+
isTextInput,
91+
};

0 commit comments

Comments
 (0)