Skip to content

Commit 52eb14c

Browse files
committed
progress #3: searchInputRef -> searchInputRef
Signed-off-by: Ashutosh Khanduala <[email protected]>
1 parent 5716e80 commit 52eb14c

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/components/EmojiPicker/EmojiPickerMenu/EmojiPickerMenu copy.tsx

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {Component} from 'react';
1+
import React, {Component, useRef} from 'react';
22
import {View, FlatList} from 'react-native';
33
import {withOnyx} from 'react-native-onyx';
44
import PropTypes from 'prop-types';
@@ -22,6 +22,7 @@ import CategoryShortcutBar from '../CategoryShortcutBar';
2222
import TextInput from '../../TextInput';
2323
import isEnterWhileComposition from '../../../libs/KeyboardShortcut/isEnterWhileComposition';
2424
import canFocusInputOnScreenFocus from '../../../libs/canFocusInputOnScreenFocus';
25+
import {TextInput as RNTextInput} from 'react-native';
2526

2627
const propTypes = {
2728
/** Function to add the selected emoji to the main compose text input */
@@ -53,7 +54,7 @@ const EmojiPickerMenu = (props) => {
5354
const {forwardedRef, frequentlyUsedEmojis, preferredSkinTone, onEmojiSelected, preferredLocale, isSmallScreenWidth, windowWidth, windowHeight, translate} = props;
5455

5556
// Ref for the emoji search input
56-
this.searchInput = undefined;
57+
const searchInputRef = useRef<RNTextInput>(null); // TODO: is RNTextInput correct?
5758

5859
// Ref for emoji FlatList
5960
this.emojiList = undefined;
@@ -100,7 +101,7 @@ const EmojiPickerMenu = (props) => {
100101
// <constructor ref={el => this.textInput = el} /> this will not
101102
// return a ref to the component, but rather the HTML element by default
102103
if (this.shouldFocusInputOnScreenFocus && forwardedRef && _.isFunction(forwardedRef)) {
103-
forwardedRef(this.searchInput);
104+
forwardedRef(searchInputRef.current);
104105
}
105106
this.setupEventHandlers();
106107
this.setFirstNonHeaderIndex(this.emojis);
@@ -200,15 +201,15 @@ const EmojiPickerMenu = (props) => {
200201
// Enable keyboard movement if tab or enter is pressed or if shift is pressed while the input
201202
// is not focused, so that the navigation and tab cycling can be done using the keyboard without
202203
// 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())) {
204205
this.setState({isUsingKeyboardMovement: true});
205206
return;
206207
}
207208

208209
// 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()) {
210211
this.setState({selectTextOnFocus: false});
211-
this.searchInput.focus();
212+
searchInputRef.current.focus();
212213

213214
// Re-enable selection on the searchInput
214215
this.setState({selectTextOnFocus: true});
@@ -270,12 +271,12 @@ const EmojiPickerMenu = (props) => {
270271
* Focuses the search Input and has the text selected
271272
*/
272273
function focusInputWithTextSelect() {
273-
if (!this.searchInput) {
274+
if (!searchInputRef.current) {
274275
return;
275276
}
276277

277278
this.setState({selectTextOnFocus: true});
278-
this.searchInput.focus();
279+
searchInputRef.current.focus();
279280
}
280281

281282
/**
@@ -288,17 +289,17 @@ const EmojiPickerMenu = (props) => {
288289
}
289290

290291
// Arrow Down and Arrow Right enable arrow navigation when search is focused
291-
if (this.searchInput && this.searchInput.isFocused()) {
292+
if (searchInputRef.current?.isFocused()) {
292293
if (arrowKey !== 'ArrowDown' && arrowKey !== 'ArrowRight') {
293294
return;
294295
}
295296

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)) {
297298
return;
298299
}
299300

300301
// Blur the input, change the highlight type to keyboard, and disable pointer events
301-
this.searchInput.blur();
302+
searchInputRef.current.blur();
302303
this.setState({isUsingKeyboardMovement: true, arePointerEventsDisabled: true});
303304

304305
// We only want to hightlight the Emoji if none was highlighted already
@@ -505,7 +506,7 @@ const EmojiPickerMenu = (props) => {
505506
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT}
506507
onChangeText={this.filterEmojis}
507508
defaultValue=""
508-
ref={(el) => (this.searchInput = el)}
509+
ref={searchInputRef}
509510
autoFocus={this.shouldFocusInputOnScreenFocus}
510511
selectTextOnFocus={this.state.selectTextOnFocus}
511512
onSelectionChange={this.onSelectionChange}

0 commit comments

Comments
 (0)