From 2e1914080f68c08cb8bfc8b1db8c255744744062 Mon Sep 17 00:00:00 2001 From: Nicolas Gallagher Date: Tue, 15 Mar 2016 13:38:28 -0700 Subject: [PATCH] [add] TextInputState --- src/components/TextInput/TextInputState.js | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/components/TextInput/TextInputState.js diff --git a/src/components/TextInput/TextInputState.js b/src/components/TextInput/TextInputState.js new file mode 100644 index 00000000..0807a5f8 --- /dev/null +++ b/src/components/TextInput/TextInputState.js @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2016-present, Nicolas Gallagher. + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * @flow + */ + +import UIManager from '../../apis/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 { + 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 (this._currentlyFocusedNode !== textFieldNode && textFieldNode !== null) { + this._currentlyFocusedNode = 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 (this._currentlyFocusedNode === textFieldNode && textFieldNode !== null) { + this._currentlyFocusedNode = null + UIManager.blur(textFieldNode) + } + } +} + +module.exports = TextInputState