|
18 | 18 | const Platform = require('Platform');
|
19 | 19 | const UIManager = require('UIManager');
|
20 | 20 |
|
| 21 | +let currentlyFocusedID: ?number = null; |
21 | 22 | const inputs = new Set();
|
22 | 23 |
|
23 |
| -const TextInputState = { |
24 |
| - /** |
25 |
| - * Internal state |
26 |
| - */ |
27 |
| - _currentlyFocusedID: (null: ?number), |
28 |
| - |
29 |
| - /** |
30 |
| - * Returns the ID of the currently focused text field, if one exists |
31 |
| - * If no text field is focused it returns null |
32 |
| - */ |
33 |
| - currentlyFocusedField: function(): ?number { |
34 |
| - return this._currentlyFocusedID; |
35 |
| - }, |
| 24 | +/** |
| 25 | + * Returns the ID of the currently focused text field, if one exists |
| 26 | + * If no text field is focused it returns null |
| 27 | + */ |
| 28 | +function currentlyFocusedField(): ?number { |
| 29 | + return currentlyFocusedID; |
| 30 | +} |
36 | 31 |
|
37 |
| - /** |
38 |
| - * @param {number} TextInputID id of the text field to focus |
39 |
| - * Focuses the specified text field |
40 |
| - * noop if the text field was already focused |
41 |
| - */ |
42 |
| - focusTextInput: function(textFieldID: ?number) { |
43 |
| - if (this._currentlyFocusedID !== textFieldID && textFieldID !== null) { |
44 |
| - this._currentlyFocusedID = textFieldID; |
45 |
| - if (Platform.OS === 'ios') { |
46 |
| - UIManager.focus(textFieldID); |
47 |
| - } else if (Platform.OS === 'android') { |
48 |
| - UIManager.dispatchViewManagerCommand( |
49 |
| - textFieldID, |
50 |
| - UIManager.AndroidTextInput.Commands.focusTextInput, |
51 |
| - null, |
52 |
| - ); |
53 |
| - } |
| 32 | +/** |
| 33 | + * @param {number} TextInputID id of the text field to focus |
| 34 | + * Focuses the specified text field |
| 35 | + * noop if the text field was already focused |
| 36 | + */ |
| 37 | +function focusTextInput(textFieldID: ?number) { |
| 38 | + if (currentlyFocusedID !== textFieldID && textFieldID !== null) { |
| 39 | + currentlyFocusedID = textFieldID; |
| 40 | + if (Platform.OS === 'ios') { |
| 41 | + UIManager.focus(textFieldID); |
| 42 | + } else if (Platform.OS === 'android') { |
| 43 | + UIManager.dispatchViewManagerCommand( |
| 44 | + textFieldID, |
| 45 | + UIManager.AndroidTextInput.Commands.focusTextInput, |
| 46 | + null, |
| 47 | + ); |
54 | 48 | }
|
55 |
| - }, |
| 49 | + } |
| 50 | +} |
56 | 51 |
|
57 |
| - /** |
58 |
| - * @param {number} textFieldID id of the text field to unfocus |
59 |
| - * Unfocuses the specified text field |
60 |
| - * noop if it wasn't focused |
61 |
| - */ |
62 |
| - blurTextInput: function(textFieldID: ?number) { |
63 |
| - if (this._currentlyFocusedID === textFieldID && textFieldID !== null) { |
64 |
| - this._currentlyFocusedID = null; |
65 |
| - if (Platform.OS === 'ios') { |
66 |
| - UIManager.blur(textFieldID); |
67 |
| - } else if (Platform.OS === 'android') { |
68 |
| - UIManager.dispatchViewManagerCommand( |
69 |
| - textFieldID, |
70 |
| - UIManager.AndroidTextInput.Commands.blurTextInput, |
71 |
| - null, |
72 |
| - ); |
73 |
| - } |
| 52 | +/** |
| 53 | + * @param {number} textFieldID id of the text field to unfocus |
| 54 | + * Unfocuses the specified text field |
| 55 | + * noop if it wasn't focused |
| 56 | + */ |
| 57 | +function blurTextInput(textFieldID: ?number) { |
| 58 | + if (currentlyFocusedID === textFieldID && textFieldID !== null) { |
| 59 | + currentlyFocusedID = null; |
| 60 | + if (Platform.OS === 'ios') { |
| 61 | + UIManager.blur(textFieldID); |
| 62 | + } else if (Platform.OS === 'android') { |
| 63 | + UIManager.dispatchViewManagerCommand( |
| 64 | + textFieldID, |
| 65 | + UIManager.AndroidTextInput.Commands.blurTextInput, |
| 66 | + null, |
| 67 | + ); |
74 | 68 | }
|
75 |
| - }, |
| 69 | + } |
| 70 | +} |
76 | 71 |
|
77 |
| - registerInput: function(textFieldID: number) { |
78 |
| - inputs.add(textFieldID); |
79 |
| - }, |
| 72 | +function registerInput(textFieldID: number) { |
| 73 | + inputs.add(textFieldID); |
| 74 | +} |
80 | 75 |
|
81 |
| - unregisterInput: function(textFieldID: number) { |
82 |
| - inputs.delete(textFieldID); |
83 |
| - }, |
| 76 | +function unregisterInput(textFieldID: number) { |
| 77 | + inputs.delete(textFieldID); |
| 78 | +} |
84 | 79 |
|
85 |
| - isTextInput: function(textFieldID: number) { |
86 |
| - return inputs.has(textFieldID); |
87 |
| - }, |
88 |
| -}; |
| 80 | +function isTextInput(textFieldID: number) { |
| 81 | + return inputs.has(textFieldID); |
| 82 | +} |
89 | 83 |
|
90 |
| -module.exports = TextInputState; |
| 84 | +module.exports = { |
| 85 | + currentlyFocusedField, |
| 86 | + focusTextInput, |
| 87 | + blurTextInput, |
| 88 | + registerInput, |
| 89 | + unregisterInput, |
| 90 | + isTextInput, |
| 91 | +}; |
0 commit comments