2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
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
|
2018-05-11 02:06:46 +00:00
|
|
|
*
|
|
|
|
* @format
|
|
|
|
* @flow
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-03-03 23:04:46 +00:00
|
|
|
const Platform = require('Platform');
|
|
|
|
const UIManager = require('UIManager');
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2018-07-20 23:28:04 +00:00
|
|
|
let currentlyFocusedID: ?number = null;
|
2018-04-16 20:03:50 +00:00
|
|
|
const inputs = new Set();
|
|
|
|
|
2018-07-20 23:28:04 +00:00
|
|
|
/**
|
|
|
|
* Returns the ID of the currently focused text field, if one exists
|
|
|
|
* If no text field is focused it returns null
|
|
|
|
*/
|
|
|
|
function currentlyFocusedField(): ?number {
|
|
|
|
return currentlyFocusedID;
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2018-07-20 23:28:04 +00:00
|
|
|
/**
|
|
|
|
* @param {number} TextInputID id of the text field to focus
|
|
|
|
* Focuses the specified text field
|
|
|
|
* noop if the text field was already focused
|
|
|
|
*/
|
|
|
|
function focusTextInput(textFieldID: ?number) {
|
|
|
|
if (currentlyFocusedID !== textFieldID && textFieldID !== null) {
|
|
|
|
currentlyFocusedID = textFieldID;
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
UIManager.focus(textFieldID);
|
|
|
|
} else if (Platform.OS === 'android') {
|
|
|
|
UIManager.dispatchViewManagerCommand(
|
|
|
|
textFieldID,
|
|
|
|
UIManager.AndroidTextInput.Commands.focusTextInput,
|
|
|
|
null,
|
|
|
|
);
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
2018-07-20 23:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2018-07-20 23:28:04 +00:00
|
|
|
/**
|
|
|
|
* @param {number} textFieldID id of the text field to unfocus
|
|
|
|
* Unfocuses the specified text field
|
|
|
|
* noop if it wasn't focused
|
|
|
|
*/
|
|
|
|
function blurTextInput(textFieldID: ?number) {
|
|
|
|
if (currentlyFocusedID === textFieldID && textFieldID !== null) {
|
|
|
|
currentlyFocusedID = null;
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
UIManager.blur(textFieldID);
|
|
|
|
} else if (Platform.OS === 'android') {
|
|
|
|
UIManager.dispatchViewManagerCommand(
|
|
|
|
textFieldID,
|
|
|
|
UIManager.AndroidTextInput.Commands.blurTextInput,
|
|
|
|
null,
|
|
|
|
);
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
2018-07-20 23:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-16 20:03:50 +00:00
|
|
|
|
2018-07-20 23:28:04 +00:00
|
|
|
function registerInput(textFieldID: number) {
|
|
|
|
inputs.add(textFieldID);
|
|
|
|
}
|
2018-04-16 20:03:50 +00:00
|
|
|
|
2018-07-20 23:28:04 +00:00
|
|
|
function unregisterInput(textFieldID: number) {
|
|
|
|
inputs.delete(textFieldID);
|
|
|
|
}
|
2018-04-16 20:03:50 +00:00
|
|
|
|
2018-07-20 23:28:04 +00:00
|
|
|
function isTextInput(textFieldID: number) {
|
|
|
|
return inputs.has(textFieldID);
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2018-07-20 23:28:04 +00:00
|
|
|
module.exports = {
|
|
|
|
currentlyFocusedField,
|
|
|
|
focusTextInput,
|
|
|
|
blurTextInput,
|
|
|
|
registerInput,
|
|
|
|
unregisterInput,
|
|
|
|
isTextInput,
|
|
|
|
};
|