1
- import React , { Component } from 'react' ;
1
+ import React , { Component , useRef } from 'react' ;
2
2
import { View , FlatList } from 'react-native' ;
3
3
import { withOnyx } from 'react-native-onyx' ;
4
4
import PropTypes from 'prop-types' ;
@@ -22,6 +22,7 @@ import CategoryShortcutBar from '../CategoryShortcutBar';
22
22
import TextInput from '../../TextInput' ;
23
23
import isEnterWhileComposition from '../../../libs/KeyboardShortcut/isEnterWhileComposition' ;
24
24
import canFocusInputOnScreenFocus from '../../../libs/canFocusInputOnScreenFocus' ;
25
+ import { TextInput as RNTextInput } from 'react-native' ;
25
26
26
27
const propTypes = {
27
28
/** Function to add the selected emoji to the main compose text input */
@@ -53,7 +54,7 @@ const EmojiPickerMenu = (props) => {
53
54
const { forwardedRef, frequentlyUsedEmojis, preferredSkinTone, onEmojiSelected, preferredLocale, isSmallScreenWidth, windowWidth, windowHeight, translate} = props ;
54
55
55
56
// Ref for the emoji search input
56
- this . searchInput = undefined ;
57
+ const searchInputRef = useRef < RNTextInput > ( null ) ; // TODO: is RNTextInput correct?
57
58
58
59
// Ref for emoji FlatList
59
60
this . emojiList = undefined ;
@@ -100,7 +101,7 @@ const EmojiPickerMenu = (props) => {
100
101
// <constructor ref={el => this.textInput = el} /> this will not
101
102
// return a ref to the component, but rather the HTML element by default
102
103
if ( this . shouldFocusInputOnScreenFocus && forwardedRef && _ . isFunction ( forwardedRef ) ) {
103
- forwardedRef ( this . searchInput ) ;
104
+ forwardedRef ( searchInputRef . current ) ;
104
105
}
105
106
this . setupEventHandlers ( ) ;
106
107
this . setFirstNonHeaderIndex ( this . emojis ) ;
@@ -200,15 +201,15 @@ const EmojiPickerMenu = (props) => {
200
201
// Enable keyboard movement if tab or enter is pressed or if shift is pressed while the input
201
202
// is not focused, so that the navigation and tab cycling can be done using the keyboard without
202
203
// interfering with the input behaviour.
203
- if ( keyBoardEvent . key === 'Tab' || keyBoardEvent . key === 'Enter' || ( keyBoardEvent . key === 'Shift' && this . searchInput && ! this . searchInput . isFocused ( ) ) ) {
204
+ if ( keyBoardEvent . key === 'Tab' || keyBoardEvent . key === 'Enter' || ( keyBoardEvent . key === 'Shift' && ! searchInputRef . current ? .isFocused ( ) ) ) {
204
205
this . setState ( { isUsingKeyboardMovement : true } ) ;
205
206
return ;
206
207
}
207
208
208
209
// We allow typing in the search box if any key is pressed apart from Arrow keys.
209
- if ( this . searchInput && ! this . searchInput . isFocused ( ) ) {
210
+ if ( ! searchInputRef . current ? .isFocused ( ) ) {
210
211
this . setState ( { selectTextOnFocus : false } ) ;
211
- this . searchInput . focus ( ) ;
212
+ searchInputRef . current . focus ( ) ;
212
213
213
214
// Re-enable selection on the searchInput
214
215
this . setState ( { selectTextOnFocus : true } ) ;
@@ -270,12 +271,12 @@ const EmojiPickerMenu = (props) => {
270
271
* Focuses the search Input and has the text selected
271
272
*/
272
273
function focusInputWithTextSelect ( ) {
273
- if ( ! this . searchInput ) {
274
+ if ( ! searchInputRef . current ) {
274
275
return ;
275
276
}
276
277
277
278
this . setState ( { selectTextOnFocus : true } ) ;
278
- this . searchInput . focus ( ) ;
279
+ searchInputRef . current . focus ( ) ;
279
280
}
280
281
281
282
/**
@@ -288,17 +289,17 @@ const EmojiPickerMenu = (props) => {
288
289
}
289
290
290
291
// Arrow Down and Arrow Right enable arrow navigation when search is focused
291
- if ( this . searchInput && this . searchInput . isFocused ( ) ) {
292
+ if ( searchInputRef . current ? .isFocused ( ) ) {
292
293
if ( arrowKey !== 'ArrowDown' && arrowKey !== 'ArrowRight' ) {
293
294
return ;
294
295
}
295
296
296
- if ( arrowKey === 'ArrowRight' && ! ( this . searchInput . value . length === this . state . selection . start && this . state . selection . start === this . state . selection . end ) ) {
297
+ if ( arrowKey === 'ArrowRight' && ! ( searchInputRef . current . value . length === this . state . selection . start && this . state . selection . start === this . state . selection . end ) ) {
297
298
return ;
298
299
}
299
300
300
301
// Blur the input, change the highlight type to keyboard, and disable pointer events
301
- this . searchInput . blur ( ) ;
302
+ searchInputRef . current . blur ( ) ;
302
303
this . setState ( { isUsingKeyboardMovement : true , arePointerEventsDisabled : true } ) ;
303
304
304
305
// We only want to hightlight the Emoji if none was highlighted already
@@ -505,7 +506,7 @@ const EmojiPickerMenu = (props) => {
505
506
accessibilityRole = { CONST . ACCESSIBILITY_ROLE . TEXT }
506
507
onChangeText = { this . filterEmojis }
507
508
defaultValue = ""
508
- ref = { ( el ) => ( this . searchInput = el ) }
509
+ ref = { searchInputRef }
509
510
autoFocus = { this . shouldFocusInputOnScreenFocus }
510
511
selectTextOnFocus = { this . state . selectTextOnFocus }
511
512
onSelectionChange = { this . onSelectionChange }
0 commit comments