Skip to content

Commit 01aeca4

Browse files
author
Johan-dutoit
committed
feat(ref): expose textinput ref
1 parent 3fa9065 commit 01aeca4

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ interface QuickReplies {
321321

322322
## Props
323323

324+
- **`messageContainerRef`** _(FlatList ref)_ - Ref to the flatlist
325+
- **`textInputRef`** _(TextInput ref)_ - Ref to the text input
324326
- **`messages`** _(Array)_ - Messages to display
325327
- **`isTyping`** _(Bool)_ - Typing Indicator state; default `false`. If you use`renderFooter` it will override this.
326328
- **`text`** _(String)_ - Input text; default is `undefined`, but if specified, it will override GiftedChat's internal state (e.g. for redux; [see notes below](#notes-for-redux))
@@ -408,10 +410,6 @@ interface QuickReplies {
408410
* **`shouldUpdateMessage`** _(Function)_ - Lets the message component know when to update outside of normal cases.
409411
* **`infiniteScroll`** _(Bool)_ - infinite scroll up when reach the top of messages container, automatically call onLoadEarlier function if exist (not yet supported for the web). You need to add `loadEarlier` prop too.
410412

411-
## Imperative methods
412-
413-
- `focusTextInput()` - Open the keyboard and focus the text input box
414-
415413
## Notes for [Redux](https://github.com/reactjs/redux)
416414

417415
The `messages` prop should work out-of-the-box with Redux. In most cases, this is all you need.

src/GiftedChat.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ dayjs.extend(localizedFormat)
5555
export interface GiftedChatProps<TMessage extends IMessage = IMessage> {
5656
/* Message container ref */
5757
messageContainerRef?: React.RefObject<FlatList<IMessage>>
58+
/* text input ref */
59+
textInputRef?: React.RefObject<TextInput>
5860
/* Messages to display */
5961
messages?: TMessage[]
6062
/* Typing Indicator state */
@@ -259,6 +261,7 @@ function GiftedChat<TMessage extends IMessage = IMessage>(
259261
minComposerHeight = MIN_COMPOSER_HEIGHT,
260262
maxComposerHeight = MAX_COMPOSER_HEIGHT,
261263
messageContainerRef = createRef<FlatList<IMessage>>(),
264+
textInputRef = createRef<TextInput>(),
262265
} = props
263266

264267
const isMountedRef = useRef(false)
@@ -269,7 +272,6 @@ function GiftedChat<TMessage extends IMessage = IMessage>(
269272
const actionSheetRef = useRef<ActionSheetProviderRef>(null)
270273

271274
let _isTextInputWasFocused: boolean = false
272-
let textInputRef = useRef<TextInput>()
273275

274276
const [state, setState] = useState<GiftedChatState>({
275277
isInitialized: false, // initialization will calculate maxHeight before rendering the chat
@@ -643,7 +645,7 @@ function GiftedChat<TMessage extends IMessage = IMessage>(
643645
onTextChanged: _onInputTextChanged,
644646
textInputProps: {
645647
...textInputProps,
646-
ref: (textInput: any) => (textInputRef = textInput),
648+
ref: textInputRef,
647649
maxLength: state.typingDisabled ? 0 : maxInputLength,
648650
},
649651
}

0 commit comments

Comments
 (0)