Skip to content

Commit bb61233

Browse files
authored
feat: optionally override TextInput inside AutoCompleteInput completely using props (#3198)
* feat: optionally override TextInput inside AutoCompleteInput completely using props * fix: usage of text input
1 parent 8a69a74 commit bb61233

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

package/src/components/AutoCompleteInput/AutoCompleteInput.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import React, { useCallback, useEffect, useMemo, useState } from 'react';
22
import {
33
I18nManager,
4-
NativeSyntheticEvent,
4+
TextInput as RNTextInput,
55
StyleSheet,
6-
TextInput,
7-
TextInputContentSizeChangeEventData,
6+
TextInputContentSizeChangeEvent,
87
TextInputProps,
9-
TextInputSelectionChangeEventData,
8+
TextInputSelectionChangeEvent,
109
} from 'react-native';
1110

1211
import { MessageComposerConfig, TextComposerState } from 'stream-chat';
@@ -37,6 +36,11 @@ type AutoCompleteInputPropsWithContext = TextInputProps &
3736
* that would happen if we put this in the MessageInputContext
3837
*/
3938
cooldownActive?: boolean;
39+
TextInputComponent?: React.ComponentType<
40+
TextInputProps & {
41+
ref: React.Ref<RNTextInput> | undefined;
42+
}
43+
>;
4044
};
4145

4246
type AutoCompleteInputProps = Partial<AutoCompleteInputPropsWithContext>;
@@ -53,7 +57,14 @@ const configStateSelector = (state: MessageComposerConfig) => ({
5357
const MAX_NUMBER_OF_LINES = 5;
5458

5559
const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext) => {
56-
const { channel, cooldownActive = false, setInputBoxRef, t, ...rest } = props;
60+
const {
61+
channel,
62+
cooldownActive = false,
63+
setInputBoxRef,
64+
t,
65+
TextInputComponent = RNTextInput,
66+
...rest
67+
} = props;
5768
const [localText, setLocalText] = useState('');
5869
const [textHeight, setTextHeight] = useState(0);
5970
const messageComposer = useMessageComposer();
@@ -74,7 +85,7 @@ const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext)
7485
}, [text]);
7586

7687
const handleSelectionChange = useCallback(
77-
(e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
88+
(e: TextInputSelectionChangeEvent) => {
7889
const { selection } = e.nativeEvent;
7990
textComposer.setSelection(selection);
8091
},
@@ -108,16 +119,14 @@ const AutoCompleteInputWithContext = (props: AutoCompleteInputPropsWithContext)
108119
}, [command, cooldownActive, t]);
109120

110121
const handleContentSizeChange = useCallback(
111-
({
112-
nativeEvent: { contentSize },
113-
}: NativeSyntheticEvent<TextInputContentSizeChangeEventData>) => {
122+
({ nativeEvent: { contentSize } }: TextInputContentSizeChangeEvent) => {
114123
setTextHeight(contentSize.height);
115124
},
116125
[],
117126
);
118127

119128
return (
120-
<TextInput
129+
<TextInputComponent
121130
autoFocus={!!command}
122131
editable={enabled}
123132
maxLength={maxMessageLength}

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useCallback, useEffect, useMemo, useState } from 'react';
2-
import { Modal, SafeAreaView, StyleSheet, View } from 'react-native';
2+
import { Modal, SafeAreaView, StyleSheet, TextInput, TextInputProps, View } from 'react-native';
33

44
import {
55
Gesture,
@@ -151,7 +151,14 @@ type MessageInputPropsWithContext = Pick<
151151
> &
152152
Pick<MessagesContextValue, 'Reply'> &
153153
Pick<TranslationContextValue, 't'> &
154-
Pick<MessageComposerAPIContextValue, 'clearEditingState'> & { editing: boolean };
154+
Pick<MessageComposerAPIContextValue, 'clearEditingState'> & {
155+
editing: boolean;
156+
TextInputComponent?: React.ComponentType<
157+
TextInputProps & {
158+
ref: React.Ref<TextInput> | undefined;
159+
}
160+
>;
161+
};
155162

156163
const textComposerStateSelector = (state: TextComposerState) => ({
157164
command: state.command,
@@ -207,6 +214,7 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
207214
ShowThreadMessageInChannelButton,
208215
StartAudioRecordingButton,
209216
StopMessageStreamingButton,
217+
TextInputComponent,
210218
watchers,
211219
} = props;
212220

@@ -504,6 +512,7 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
504512
<View style={[styles.autoCompleteInputContainer, autoCompleteInputContainer]}>
505513
<AutoCompleteInput
506514
cooldownActive={!!cooldownRemainingSeconds}
515+
TextInputComponent={TextInputComponent}
507516
{...additionalTextInputProps}
508517
/>
509518
</View>

0 commit comments

Comments
 (0)