You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.7 KiB

/**
* Copyright (c) Nicolas Gallagher.
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import UIManager from '../../exports/UIManager';
/**
* This class is responsible for coordinating the "focused"
* state for TextInputs. All calls relating to the keyboard
* should be funneled through here
*/
const TextInputState = {
/**
* Internal state
*/
_currentlyFocusedNode: (null: ?Object),
/**
* Returns the ID of the currently focused text field, if one exists
* If no text field is focused it returns null
*/
currentlyFocusedField(): ?Object {
if (document.activeElement !== this._currentlyFocusedNode) {
this._currentlyFocusedNode = null;
}
return this._currentlyFocusedNode;
},
/**
* @param {Object} TextInputID id of the text field to focus
* Focuses the specified text field
* noop if the text field was already focused
*/
focusTextInput(textFieldNode: ?Object) {
if (textFieldNode !== null) {
this._currentlyFocusedNode = textFieldNode;
if (document.activeElement !== textFieldNode) {
UIManager.focus(textFieldNode);
}
}
},
/**
* @param {Object} textFieldNode id of the text field to focus
* Unfocuses the specified text field
* noop if it wasn't focused
*/
blurTextInput(textFieldNode: ?Object) {
if (textFieldNode !== null) {
this._currentlyFocusedNode = null;
if (document.activeElement === textFieldNode) {
UIManager.blur(textFieldNode);
}
}
}
};
export default TextInputState;