TextInputState.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow strict-local
  9. */
  10. // This class is responsible for coordinating the "focused" state for
  11. // TextInputs. All calls relating to the keyboard should be funneled
  12. // through here.
  13. 'use strict';
  14. const React = require('react');
  15. const Platform = require('../../Utilities/Platform');
  16. const {findNodeHandle} = require('../../Renderer/shims/ReactNative');
  17. import {Commands as AndroidTextInputCommands} from '../../Components/TextInput/AndroidTextInputNativeComponent';
  18. import {Commands as iOSTextInputCommands} from '../../Components/TextInput/RCTSingelineTextInputNativeComponent';
  19. import type {HostComponent} from '../../Renderer/shims/ReactNativeTypes';
  20. type ComponentRef = React.ElementRef<HostComponent<mixed>>;
  21. let currentlyFocusedInputRef: ?ComponentRef = null;
  22. const inputs = new Set();
  23. function currentlyFocusedInput(): ?ComponentRef {
  24. return currentlyFocusedInputRef;
  25. }
  26. /**
  27. * Returns the ID of the currently focused text field, if one exists
  28. * If no text field is focused it returns null
  29. */
  30. function currentlyFocusedField(): ?number {
  31. if (__DEV__) {
  32. console.error(
  33. 'currentlyFocusedField is deprecated and will be removed in a future release. Use currentlyFocusedInput',
  34. );
  35. }
  36. return findNodeHandle(currentlyFocusedInputRef);
  37. }
  38. function focusInput(textField: ?ComponentRef): void {
  39. if (currentlyFocusedInputRef !== textField && textField != null) {
  40. currentlyFocusedInputRef = textField;
  41. }
  42. }
  43. function blurInput(textField: ?ComponentRef): void {
  44. if (currentlyFocusedInputRef === textField && textField != null) {
  45. currentlyFocusedInputRef = null;
  46. }
  47. }
  48. function focusField(textFieldID: ?number): void {
  49. if (__DEV__) {
  50. console.error('focusField no longer works. Use focusInput');
  51. }
  52. return;
  53. }
  54. function blurField(textFieldID: ?number) {
  55. if (__DEV__) {
  56. console.error('blurField no longer works. Use blurInput');
  57. }
  58. return;
  59. }
  60. /**
  61. * @param {number} TextInputID id of the text field to focus
  62. * Focuses the specified text field
  63. * noop if the text field was already focused
  64. */
  65. function focusTextInput(textField: ?ComponentRef) {
  66. if (typeof textField === 'number') {
  67. if (__DEV__) {
  68. console.error(
  69. 'focusTextInput must be called with a host component. Passing a react tag is deprecated.',
  70. );
  71. }
  72. return;
  73. }
  74. if (currentlyFocusedInputRef !== textField && textField != null) {
  75. focusInput(textField);
  76. if (Platform.OS === 'ios') {
  77. // This isn't necessarily a single line text input
  78. // But commands don't actually care as long as the thing being passed in
  79. // actually has a command with that name. So this should work with single
  80. // and multiline text inputs. Ideally we'll merge them into one component
  81. // in the future.
  82. iOSTextInputCommands.focus(textField);
  83. } else if (Platform.OS === 'android') {
  84. AndroidTextInputCommands.focus(textField);
  85. }
  86. }
  87. }
  88. /**
  89. * @param {number} textFieldID id of the text field to unfocus
  90. * Unfocuses the specified text field
  91. * noop if it wasn't focused
  92. */
  93. function blurTextInput(textField: ?ComponentRef) {
  94. if (typeof textField === 'number') {
  95. if (__DEV__) {
  96. console.error(
  97. 'focusTextInput must be called with a host component. Passing a react tag is deprecated.',
  98. );
  99. }
  100. return;
  101. }
  102. if (currentlyFocusedInputRef === textField && textField != null) {
  103. blurInput(textField);
  104. if (Platform.OS === 'ios') {
  105. // This isn't necessarily a single line text input
  106. // But commands don't actually care as long as the thing being passed in
  107. // actually has a command with that name. So this should work with single
  108. // and multiline text inputs. Ideally we'll merge them into one component
  109. // in the future.
  110. iOSTextInputCommands.blur(textField);
  111. } else if (Platform.OS === 'android') {
  112. AndroidTextInputCommands.blur(textField);
  113. }
  114. }
  115. }
  116. function registerInput(textField: ComponentRef) {
  117. if (typeof textField === 'number') {
  118. if (__DEV__) {
  119. console.error(
  120. 'registerInput must be called with a host component. Passing a react tag is deprecated.',
  121. );
  122. }
  123. return;
  124. }
  125. inputs.add(textField);
  126. }
  127. function unregisterInput(textField: ComponentRef) {
  128. if (typeof textField === 'number') {
  129. if (__DEV__) {
  130. console.error(
  131. 'unregisterInput must be called with a host component. Passing a react tag is deprecated.',
  132. );
  133. }
  134. return;
  135. }
  136. inputs.delete(textField);
  137. }
  138. function isTextInput(textField: ComponentRef): boolean {
  139. if (typeof textField === 'number') {
  140. if (__DEV__) {
  141. console.error(
  142. 'isTextInput must be called with a host component. Passing a react tag is deprecated.',
  143. );
  144. }
  145. return false;
  146. }
  147. return inputs.has(textField);
  148. }
  149. module.exports = {
  150. currentlyFocusedInput,
  151. focusInput,
  152. blurInput,
  153. currentlyFocusedField,
  154. focusField,
  155. blurField,
  156. focusTextInput,
  157. blurTextInput,
  158. registerInput,
  159. unregisterInput,
  160. isTextInput,
  161. };