2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00: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-30 01:10:49 +00:00
|
|
|
*
|
|
|
|
* @providesModule TextInputState
|
2015-03-25 22:36:50 +00:00
|
|
|
* @flow
|
2015-01-30 01:10:49 +00: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-07-31 17:49:29 +00:00
|
|
|
var Platform = require('Platform');
|
2015-11-27 13:39:00 +00:00
|
|
|
var UIManager = require('UIManager');
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
var TextInputState = {
|
|
|
|
/**
|
|
|
|
* Internal state
|
|
|
|
*/
|
2015-05-13 01:55:13 +00:00
|
|
|
_currentlyFocusedID: (null: ?number),
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ID of the currently focused text field, if one exists
|
|
|
|
* If no text field is focused it returns null
|
|
|
|
*/
|
2015-05-13 01:55:13 +00:00
|
|
|
currentlyFocusedField: function(): ?number {
|
2015-01-30 01:10:49 +00:00
|
|
|
return this._currentlyFocusedID;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-05-13 01:55:13 +00:00
|
|
|
* @param {number} TextInputID id of the text field to focus
|
2015-01-30 01:10:49 +00:00
|
|
|
* Focuses the specified text field
|
|
|
|
* noop if the text field was already focused
|
|
|
|
*/
|
2015-05-13 01:55:13 +00:00
|
|
|
focusTextInput: function(textFieldID: ?number) {
|
2015-03-26 13:32:01 +00:00
|
|
|
if (this._currentlyFocusedID !== textFieldID && textFieldID !== null) {
|
2015-01-30 01:10:49 +00:00
|
|
|
this._currentlyFocusedID = textFieldID;
|
2015-07-31 17:49:29 +00:00
|
|
|
if (Platform.OS === 'ios') {
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.focus(textFieldID);
|
2015-07-31 17:49:29 +00:00
|
|
|
} else if (Platform.OS === 'android') {
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.dispatchViewManagerCommand(
|
2015-07-31 17:49:29 +00:00
|
|
|
textFieldID,
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.AndroidTextInput.Commands.focusTextInput,
|
2015-07-31 17:49:29 +00:00
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-06-21 02:21:27 +00:00
|
|
|
* @param {number} textFieldID id of the text field to unfocus
|
2015-01-30 01:10:49 +00:00
|
|
|
* Unfocuses the specified text field
|
|
|
|
* noop if it wasn't focused
|
|
|
|
*/
|
2015-05-13 01:55:13 +00:00
|
|
|
blurTextInput: function(textFieldID: ?number) {
|
2015-03-26 13:32:01 +00:00
|
|
|
if (this._currentlyFocusedID === textFieldID && textFieldID !== null) {
|
2015-01-30 01:10:49 +00:00
|
|
|
this._currentlyFocusedID = null;
|
2015-07-31 17:49:29 +00:00
|
|
|
if (Platform.OS === 'ios') {
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.blur(textFieldID);
|
2015-07-31 17:49:29 +00:00
|
|
|
} else if (Platform.OS === 'android') {
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.dispatchViewManagerCommand(
|
2015-07-31 17:49:29 +00:00
|
|
|
textFieldID,
|
2015-11-27 13:39:00 +00:00
|
|
|
UIManager.AndroidTextInput.Commands.blurTextInput,
|
2015-07-31 17:49:29 +00:00
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = TextInputState;
|