2015-01-29 17:10:49 -08:00
|
|
|
/**
|
2015-03-23 15:07:33 -07:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-01-29 17:10:49 -08:00
|
|
|
*
|
|
|
|
* @providesModule TextInputState
|
2015-03-25 15:36:50 -07:00
|
|
|
* @flow
|
2015-01-29 17:10:49 -08:00
|
|
|
*
|
|
|
|
* This class is responsible for coordinating the "focused"
|
|
|
|
* state for TextInputs. All calls relating to the keyboard
|
|
|
|
* should be funneled through here
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-03-18 15:57:49 -07:00
|
|
|
var RCTUIManager = require('NativeModules').UIManager;
|
2015-01-29 17:10:49 -08:00
|
|
|
|
|
|
|
var TextInputState = {
|
|
|
|
/**
|
|
|
|
* Internal state
|
|
|
|
*/
|
2015-05-12 18:55:13 -07:00
|
|
|
_currentlyFocusedID: (null: ?number),
|
2015-01-29 17:10:49 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ID of the currently focused text field, if one exists
|
|
|
|
* If no text field is focused it returns null
|
|
|
|
*/
|
2015-05-12 18:55:13 -07:00
|
|
|
currentlyFocusedField: function(): ?number {
|
2015-01-29 17:10:49 -08:00
|
|
|
return this._currentlyFocusedID;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-05-12 18:55:13 -07:00
|
|
|
* @param {number} TextInputID id of the text field to focus
|
2015-01-29 17:10:49 -08:00
|
|
|
* Focuses the specified text field
|
|
|
|
* noop if the text field was already focused
|
|
|
|
*/
|
2015-05-12 18:55:13 -07:00
|
|
|
focusTextInput: function(textFieldID: ?number) {
|
2015-03-26 06:32:01 -07:00
|
|
|
if (this._currentlyFocusedID !== textFieldID && textFieldID !== null) {
|
2015-01-29 17:10:49 -08:00
|
|
|
this._currentlyFocusedID = textFieldID;
|
2015-03-17 13:42:44 -07:00
|
|
|
RCTUIManager.focus(textFieldID);
|
2015-01-29 17:10:49 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-05-12 18:55:13 -07:00
|
|
|
* @param {number} textFieldID id of the text field to focus
|
2015-01-29 17:10:49 -08:00
|
|
|
* Unfocuses the specified text field
|
|
|
|
* noop if it wasn't focused
|
|
|
|
*/
|
2015-05-12 18:55:13 -07:00
|
|
|
blurTextInput: function(textFieldID: ?number) {
|
2015-03-26 06:32:01 -07:00
|
|
|
if (this._currentlyFocusedID === textFieldID && textFieldID !== null) {
|
2015-01-29 17:10:49 -08:00
|
|
|
this._currentlyFocusedID = null;
|
2015-03-17 13:42:44 -07:00
|
|
|
RCTUIManager.blur(textFieldID);
|
2015-01-29 17:10:49 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = TextInputState;
|