14
14
15
15
'use strict' ;
16
16
17
+ const React = require ( 'react' ) ;
17
18
const Platform = require ( '../../Utilities/Platform' ) ;
18
19
const UIManager = require ( '../../ReactNative/UIManager' ) ;
20
+ const { findNodeHandle} = require ( '../../Renderer/shims/ReactNative' ) ;
19
21
20
- let currentlyFocusedID : ?number = null ;
22
+ import type { HostComponent } from '../../Renderer/shims/ReactNativeTypes' ;
23
+ type ComponentRef = React . ElementRef < HostComponent < mixed >> ;
24
+
25
+ let currentlyFocusedInputRef : ?ComponentRef = null ;
21
26
const inputs = new Set ( ) ;
22
27
28
+ function currentlyFocusedInput ( ) : ?ComponentRef {
29
+ return currentlyFocusedInputRef ;
30
+ }
31
+
23
32
/**
24
33
* Returns the ID of the currently focused text field, if one exists
25
34
* If no text field is focused it returns null
26
35
*/
27
36
function currentlyFocusedField ( ) : ?number {
28
- return currentlyFocusedID ;
37
+ if ( __DEV__ ) {
38
+ console . error (
39
+ 'currentlyFocusedField is deprecated and will be removed in a future release. Use currentlyFocusedInput' ,
40
+ ) ;
41
+ }
42
+
43
+ return findNodeHandle ( currentlyFocusedInputRef ) ;
44
+ }
45
+
46
+ function focusInput ( textField : ?ComponentRef ) : void {
47
+ if ( currentlyFocusedInputRef !== textField && textField != null ) {
48
+ currentlyFocusedInputRef = textField ;
49
+ }
50
+ }
51
+
52
+ function blurInput ( textField : ?ComponentRef ) : void {
53
+ if ( currentlyFocusedInputRef === textField && textField != null ) {
54
+ currentlyFocusedInputRef = null ;
55
+ }
29
56
}
30
57
31
58
function focusField ( textFieldID : ?number ) : void {
32
- if ( currentlyFocusedID !== textFieldID && textFieldID != null ) {
33
- currentlyFocusedID = textFieldID ;
59
+ if ( __DEV__ ) {
60
+ console . error ( 'focusField no longer works. Use focusInput' ) ;
34
61
}
62
+
63
+ return ;
35
64
}
36
65
37
66
function blurField ( textFieldID : ?number ) {
38
- if ( currentlyFocusedID === textFieldID && textFieldID != null ) {
39
- currentlyFocusedID = null ;
67
+ if ( __DEV__ ) {
68
+ console . error ( 'blurField no longer works. Use blurInput' ) ;
40
69
}
70
+
71
+ return ;
41
72
}
42
73
43
74
/**
44
75
* @param {number } TextInputID id of the text field to focus
45
76
* Focuses the specified text field
46
77
* noop if the text field was already focused
47
78
*/
48
- function focusTextInput ( textFieldID : ?number ) {
49
- if ( currentlyFocusedID !== textFieldID && textFieldID != null ) {
50
- focusField ( textFieldID ) ;
79
+ function focusTextInput ( textField : ?ComponentRef ) {
80
+ if ( typeof textField === 'number' ) {
81
+ if ( __DEV__ ) {
82
+ console . error (
83
+ 'focusTextInput must be called with a host component. Passing a react tag is deprecated.' ,
84
+ ) ;
85
+ }
86
+
87
+ return ;
88
+ }
89
+
90
+ if ( currentlyFocusedInputRef !== textField && textField != null ) {
91
+ const textFieldID = findNodeHandle ( textField ) ;
92
+ focusInput ( textField ) ;
51
93
if ( Platform . OS === 'ios' ) {
52
94
UIManager . focus ( textFieldID ) ;
53
95
} else if ( Platform . OS === 'android' ) {
@@ -66,9 +108,20 @@ function focusTextInput(textFieldID: ?number) {
66
108
* Unfocuses the specified text field
67
109
* noop if it wasn't focused
68
110
*/
69
- function blurTextInput ( textFieldID : ?number ) {
70
- if ( currentlyFocusedID === textFieldID && textFieldID != null ) {
71
- blurField ( textFieldID ) ;
111
+ function blurTextInput ( textField : ?ComponentRef ) {
112
+ if ( typeof textField === 'number' ) {
113
+ if ( __DEV__ ) {
114
+ console . error (
115
+ 'focusTextInput must be called with a host component. Passing a react tag is deprecated.' ,
116
+ ) ;
117
+ }
118
+
119
+ return ;
120
+ }
121
+
122
+ if ( currentlyFocusedInputRef === textField && textField != null ) {
123
+ const textFieldID = findNodeHandle ( textField ) ;
124
+ blurInput ( textField ) ;
72
125
if ( Platform . OS === 'ios' ) {
73
126
UIManager . blur ( textFieldID ) ;
74
127
} else if ( Platform . OS === 'android' ) {
@@ -82,19 +135,52 @@ function blurTextInput(textFieldID: ?number) {
82
135
}
83
136
}
84
137
85
- function registerInput ( textFieldID : number ) {
86
- inputs . add ( textFieldID ) ;
138
+ function registerInput ( textField : ComponentRef ) {
139
+ if ( typeof textField === 'number' ) {
140
+ if ( __DEV__ ) {
141
+ console . error (
142
+ 'registerInput must be called with a host component. Passing a react tag is deprecated.' ,
143
+ ) ;
144
+ }
145
+
146
+ return ;
147
+ }
148
+
149
+ inputs . add ( textField ) ;
87
150
}
88
151
89
- function unregisterInput ( textFieldID : number ) {
90
- inputs . delete ( textFieldID ) ;
152
+ function unregisterInput ( textField : ComponentRef ) {
153
+ if ( typeof textField === 'number' ) {
154
+ if ( __DEV__ ) {
155
+ console . error (
156
+ 'unregisterInput must be called with a host component. Passing a react tag is deprecated.' ,
157
+ ) ;
158
+ }
159
+
160
+ return ;
161
+ }
162
+ inputs . delete ( textField ) ;
91
163
}
92
164
93
- function isTextInput ( textFieldID : number ) : boolean {
94
- return inputs . has ( textFieldID ) ;
165
+ function isTextInput ( textField : ComponentRef ) : boolean {
166
+ if ( typeof textField === 'number' ) {
167
+ if ( __DEV__ ) {
168
+ console . error (
169
+ 'isTextInput must be called with a host component. Passing a react tag is deprecated.' ,
170
+ ) ;
171
+ }
172
+
173
+ return false ;
174
+ }
175
+
176
+ return inputs . has ( textField ) ;
95
177
}
96
178
97
179
module . exports = {
180
+ currentlyFocusedInput,
181
+ focusInput,
182
+ blurInput,
183
+
98
184
currentlyFocusedField,
99
185
focusField,
100
186
blurField,
0 commit comments