Skip to content

Commit

Permalink
Merge pull request #2573 from danilvalov/feature/add-support-for-disa…
Browse files Browse the repository at this point in the history
…bling-focus-on-textinput-when-opening-keyboard

Add `focusOnInputWhenKeyboardOpens` prop to disable focus on `<TextInput />` when opening the keyboard
  • Loading branch information
kesha-antonov authored Jan 9, 2025
2 parents d00b0c1 + 9dd7dd3 commit 59842b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ interface QuickReplies {
- **`renderAccessory`** _(Function)_ - Custom second line of actions below the message composer
- **`onPressActionButton`** _(Function)_ - Callback when the Action button is pressed (if set, the default `actionSheet` will not be used)
- **`bottomOffset`** _(Integer)_ - Distance of the chat from the bottom of the screen (e.g. useful if you display a tab bar)
- **`focusOnInputWhenOpeningKeyboard`** _(Bool)_ - Focus on <TextInput> automatically when opening the keyboard; default `true`
- **`minInputToolbarHeight`** _(Integer)_ - Minimum height of the input toolbar; default is `44`
- **`listViewProps`** _(Object)_ - Extra props to be passed to the messages [`<ListView>`](https://facebook.github.io/react-native/docs/listview.html); some props can't be overridden, see the code in `MessageContainer.render()` for details
- **`textInputProps`** _(Object)_ - Extra props to be passed to the [`<TextInput>`](https://facebook.github.io/react-native/docs/textinput.html)
Expand Down
13 changes: 9 additions & 4 deletions src/GiftedChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export interface GiftedChatProps<TMessage extends IMessage = IMessage> {
lightboxProps?: LightboxProps
/* Distance of the chat from the bottom of the screen (e.g. useful if you display a tab bar); default is 0 */
bottomOffset?: number
/* Focus on <TextInput> automatically when opening the keyboard; default is true */
focusOnInputWhenOpeningKeyboard?: boolean
/* Minimum height of the input toolbar; default is 44 */
minInputToolbarHeight?: number
/* Extra props to be passed to the messages <ListView>; some props can't be overridden, see the code in MessageContainer.render() for details */
Expand Down Expand Up @@ -253,6 +255,7 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
renderChatFooter = null,
renderInputToolbar = null,
bottomOffset = 0,
focusOnInputWhenOpeningKeyboard = true,
keyboardShouldPersistTaps = Platform.select({
ios: 'never',
android: 'always',
Expand Down Expand Up @@ -577,10 +580,11 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
}
)

if (isKeyboardMovingUp)
runOnJS(handleTextInputFocusWhenKeyboardShow)()
else
runOnJS(handleTextInputFocusWhenKeyboardHide)()
if (focusOnInputWhenOpeningKeyboard)
if (isKeyboardMovingUp)
runOnJS(handleTextInputFocusWhenKeyboardShow)()
else
runOnJS(handleTextInputFocusWhenKeyboardHide)()

if (value === 0) {
runOnJS(enableTyping)()
Expand All @@ -594,6 +598,7 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
[
keyboard,
trackingKeyboardMovement,
focusOnInputWhenOpeningKeyboard,
insets,
handleTextInputFocusWhenKeyboardHide,
handleTextInputFocusWhenKeyboardShow,
Expand Down

0 comments on commit 59842b2

Please sign in to comment.