Skip to content

Commit 0c154ec

Browse files
committed
fix: add useCallback
1 parent 1e421f4 commit 0c154ec

File tree

2 files changed

+21
-30
lines changed

2 files changed

+21
-30
lines changed

package/src/components/AutoCompleteInput/AutoCompleteInput.tsx

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useEffect, useMemo, useState } from 'react';
1+
import React, { useCallback, useMemo, useState } from 'react';
22
import {
33
I18nManager,
44
NativeSyntheticEvent,
@@ -23,17 +23,6 @@ import {
2323

2424
import { useStateStore } from '../../hooks/useStateStore';
2525

26-
const styles = StyleSheet.create({
27-
inputBox: {
28-
flex: 1,
29-
fontSize: 16,
30-
includeFontPadding: false, // for android vertical text centering
31-
padding: 0, // removal of default text input padding on android
32-
paddingTop: 0, // removal of iOS top padding for weird centering
33-
textAlignVertical: 'center', // for android vertical text centering
34-
},
35-
});
36-
3726
type AutoCompleteInputPropsWithContext = Pick<
3827
MessageInputContextValue,
3928
| 'additionalTextInputProps'
@@ -56,6 +45,7 @@ type AutoCompleteInputPropsWithContext = Pick<
5645
export type AutoCompleteInputProps = Partial<AutoCompleteInputPropsWithContext>;
5746

5847
const messageComposerStateSelector = (state: TextComposerState) => ({
48+
selection: state.selection,
5949
text: state.text,
6050
});
6151

@@ -72,18 +62,13 @@ const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext)
7262
setInputBoxRef,
7363
t,
7464
} = props;
75-
const [localText, setLocalText] = useState('');
7665
const [selection, setSelection] = useState({ end: 0, start: 0 });
7766
const [textHeight, setTextHeight] = useState(0);
7867
const messageComposer = useMessageComposer();
7968
const { textComposer } = messageComposer;
8069

8170
const { text } = useStateStore(textComposer.state, messageComposerStateSelector);
8271

83-
useEffect(() => {
84-
setLocalText(text);
85-
}, [text]);
86-
8772
const handleSelectionChange = useCallback(
8873
(e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
8974
const { selection } = e.nativeEvent;
@@ -106,7 +91,6 @@ const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext)
10691
additionalTextInputProps.onChangeText(newText);
10792
return;
10893
}
109-
setLocalText(newText);
11094
const isGiphy = giphyEnabled && newText && newText.startsWith('/giphy ');
11195

11296
if (isGiphy) {
@@ -121,15 +105,7 @@ const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext)
121105
text: isGiphy ? newText.slice(7) : newText,
122106
});
123107
},
124-
[
125-
additionalTextInputProps,
126-
giphyEnabled,
127-
onChangeText,
128-
textComposer,
129-
selection.end,
130-
selection.start,
131-
setGiphyActive,
132-
],
108+
[additionalTextInputProps, giphyEnabled, onChangeText, textComposer, selection, setGiphyActive],
133109
);
134110

135111
const {
@@ -177,7 +153,7 @@ const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext)
177153
inputBox,
178154
]}
179155
testID='auto-complete-text-input'
180-
value={localText}
156+
value={text}
181157
{...additionalTextInputProps}
182158
/>
183159
);
@@ -244,4 +220,15 @@ export const AutoCompleteInput = (props: AutoCompleteInputProps) => {
244220
);
245221
};
246222

223+
const styles = StyleSheet.create({
224+
inputBox: {
225+
flex: 1,
226+
fontSize: 16,
227+
includeFontPadding: false, // for android vertical text centering
228+
padding: 0, // removal of default text input padding on android
229+
paddingTop: 0, // removal of iOS top padding for weird centering
230+
textAlignVertical: 'center', // for android vertical text centering
231+
},
232+
});
233+
247234
AutoCompleteInput.displayName = 'AutoCompleteInput{messageInput{inputBox}}';

package/src/components/AutoCompleteInput/AutoCompleteSuggestionItem.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useCallback } from 'react';
22
import { Pressable, StyleSheet, Text, View } from 'react-native';
33

44
import { CommandSuggestion, TextComposerSuggestion, UserSuggestion } from 'stream-chat';
@@ -120,9 +120,13 @@ export const AutoCompleteSuggestionItem = ({
120120
},
121121
} = useTheme();
122122

123+
const handlePress = useCallback(async () => {
124+
await textComposer.handleSelect(itemProps);
125+
}, [itemProps, textComposer]);
126+
123127
return (
124128
<Pressable
125-
onPress={() => textComposer.handleSelect(itemProps)}
129+
onPress={handlePress}
126130
style={({ pressed }) => [{ opacity: pressed ? 0.2 : 1 }, itemStyle]}
127131
>
128132
{renderSuggestionItem(itemProps, triggerType)}

0 commit comments

Comments
 (0)