Skip to content

Commit 679bdb2

Browse files
Changes before error encountered
Co-authored-by: kesha-antonov <11584712+kesha-antonov@users.noreply.github.com>
1 parent 4213121 commit 679bdb2

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/GiftedChat/index.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
8989
minComposerHeight = MIN_COMPOSER_HEIGHT,
9090
maxComposerHeight = MAX_COMPOSER_HEIGHT,
9191
isKeyboardInternallyHandled = true,
92+
disableKeyboardController = false,
9293
} = props
9394

9495
const actionSheetRef = useRef<ActionSheetProviderRef>(null)
@@ -112,7 +113,16 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
112113
const [text, setText] = useState<string | undefined>(() => props.text || '')
113114
const [isTypingDisabled, setIsTypingDisabled] = useState<boolean>(false)
114115

115-
const keyboard = useReanimatedKeyboardAnimation()
116+
// Always call the hook, but conditionally use its data
117+
const keyboardControllerData = useReanimatedKeyboardAnimation()
118+
119+
// Create a mock keyboard object when disabled
120+
const keyboard = useMemo(() => {
121+
if (disableKeyboardController)
122+
return { height: { value: 0 } }
123+
return keyboardControllerData
124+
}, [disableKeyboardController, keyboardControllerData])
125+
116126
const trackingKeyboardMovement = useSharedValue(false)
117127
const debounceEnableTypingTimeoutId = useRef<ReturnType<typeof setTimeout>>(undefined)
118128
const keyboardOffsetBottom = useSharedValue(0)
@@ -380,9 +390,14 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
380390
setText(props.text)
381391
}, [props.text])
382392

393+
// Only set up keyboard animation when keyboard controller is enabled
383394
useAnimatedReaction(
384-
() => -keyboard.height.value,
395+
() => disableKeyboardController ? 0 : -keyboard.height.value,
385396
(value, prevValue) => {
397+
// Skip keyboard handling when disabled
398+
if (disableKeyboardController)
399+
return
400+
386401
if (prevValue !== null && value !== prevValue) {
387402
const isKeyboardMovingUp = value > prevValue
388403
if (isKeyboardMovingUp !== trackingKeyboardMovement.value) {
@@ -420,6 +435,7 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
420435
disableTyping,
421436
debounceEnableTyping,
422437
bottomOffset,
438+
disableKeyboardController,
423439
]
424440
)
425441

@@ -433,7 +449,7 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
433449
>
434450
{isInitialized
435451
? (
436-
<Animated.View style={[stylesCommon.fill, isKeyboardInternallyHandled && contentStyleAnim]}>
452+
<Animated.View style={[stylesCommon.fill, (isKeyboardInternallyHandled && !disableKeyboardController) && contentStyleAnim]}>
437453
{renderMessages}
438454
{inputToolbarFragment}
439455
</Animated.View>
@@ -448,6 +464,10 @@ function GiftedChat<TMessage extends IMessage = IMessage> (
448464
}
449465

450466
function GiftedChatWrapper<TMessage extends IMessage = IMessage> (props: GiftedChatProps<TMessage>) {
467+
// Don't use KeyboardProvider when keyboard controller is disabled
468+
if (props.disableKeyboardController)
469+
return <GiftedChat<TMessage> {...props} />
470+
451471
return (
452472
<KeyboardProvider>
453473
<GiftedChat<TMessage> {...props} />

src/GiftedChat/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export interface GiftedChatProps<TMessage extends IMessage> extends Partial<Mess
7575
isLoadingEarlier?: boolean
7676
/* Determine whether to handle keyboard awareness inside the plugin. If you have your own keyboard handling outside the plugin set this to false; default is `true` */
7777
isKeyboardInternallyHandled?: boolean
78+
/* Completely disable react-native-keyboard-controller. Useful when using react-native-navigation or other conflicting keyboard libraries; default is `false` */
79+
disableKeyboardController?: boolean
7880
/* Whether to render an avatar for the current user; default is false, only show avatars for other users */
7981
showUserAvatar?: boolean
8082
/* When false, avatars will only be displayed when a consecutive message is from the same user on the same day; default is false */

src/__tests__/GiftedChat.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,28 @@ it('should render <GiftedChat/> and compare with snapshot', () => {
4040

4141
expect(tree.toJSON()).toMatchSnapshot()
4242
})
43+
44+
it('should render <GiftedChat/> with disableKeyboardController=true', () => {
45+
let tree
46+
47+
renderer.act(() => {
48+
(useReanimatedKeyboardAnimation as jest.Mock).mockReturnValue({
49+
height: {
50+
value: 0,
51+
},
52+
})
53+
54+
tree = renderer.create(
55+
<GiftedChat
56+
messages={messages}
57+
onSend={() => {}}
58+
user={{
59+
_id: 1,
60+
}}
61+
disableKeyboardController={true}
62+
/>
63+
)
64+
})
65+
66+
expect(tree.toJSON()).toMatchSnapshot()
67+
})

0 commit comments

Comments
 (0)