Skip to content

Commit e21d676

Browse files
committed
fix: fixed the multiline issue on BottomSheetTextInput #411
1 parent d421f09 commit e21d676

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

src/components/bottomSheetTextInput/BottomSheetTextInput.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { memo, useCallback, forwardRef } from 'react';
22
import { TextInput } from 'react-native-gesture-handler';
3-
import { runOnUI } from 'react-native-reanimated';
43
import { useBottomSheetInternal } from '../../hooks';
54
import type { BottomSheetTextInputProps } from './types';
65

@@ -15,9 +14,7 @@ const BottomSheetTextInputComponent = forwardRef<
1514
//#region callbacks
1615
const handleOnFocus = useCallback(
1716
args => {
18-
runOnUI(() => {
19-
shouldHandleKeyboardEvents.value = true;
20-
})();
17+
shouldHandleKeyboardEvents.value = true;
2118
if (onFocus) {
2219
onFocus(args);
2320
}
@@ -26,9 +23,7 @@ const BottomSheetTextInputComponent = forwardRef<
2623
);
2724
const handleOnBlur = useCallback(
2825
args => {
29-
runOnUI(() => {
30-
shouldHandleKeyboardEvents.value = false;
31-
})();
26+
shouldHandleKeyboardEvents.value = false;
3227
if (onBlur) {
3328
onBlur(args);
3429
}

src/hooks/useKeyboard.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from 'react-native';
99
import {
1010
runOnUI,
11+
useAnimatedReaction,
1112
useSharedValue,
1213
useWorkletCallback,
1314
} from 'react-native-reanimated';
@@ -36,12 +37,19 @@ export const useKeyboard = () => {
3637
const keyboardAnimationEasing =
3738
useSharedValue<KeyboardEventEasing>('keyboard');
3839
const keyboardAnimationDuration = useSharedValue(500);
40+
const temporaryCachedKeyboardEvent = useSharedValue<any>([]);
3941
//#endregion
4042

4143
//#region worklets
4244
const handleKeyboardEvent = useWorkletCallback(
4345
(state, height, duration, easing) => {
4446
if (state === KEYBOARD_STATE.SHOWN && !shouldHandleKeyboardEvents.value) {
47+
/**
48+
* if the keyboard event was fired before the `onFocus` on TextInput,
49+
* then we cache the input, and wait till the `shouldHandleKeyboardEvents`
50+
* to be updated then fire this function again.
51+
*/
52+
temporaryCachedKeyboardEvent.value = [state, height, duration, easing];
4553
return;
4654
}
4755
keyboardHeight.value =
@@ -53,6 +61,7 @@ export const useKeyboard = () => {
5361
keyboardAnimationDuration.value = duration;
5462
keyboardAnimationEasing.value = easing;
5563
keyboardState.value = state;
64+
temporaryCachedKeyboardEvent.value = [];
5665
},
5766
[]
5867
);
@@ -99,6 +108,21 @@ export const useKeyboard = () => {
99108
);
100109
};
101110
}, [handleKeyboardEvent]);
111+
112+
/**
113+
* This reaction is needed to handle the issue with multiline text input.
114+
*
115+
* @link https://github.com/gorhom/react-native-bottom-sheet/issues/411
116+
*/
117+
useAnimatedReaction(
118+
() => shouldHandleKeyboardEvents.value,
119+
result => {
120+
const params = temporaryCachedKeyboardEvent.value;
121+
if (result && params.length > 0) {
122+
handleKeyboardEvent(params[0], params[1], params[2], params[3]);
123+
}
124+
}
125+
);
102126
//#endregion
103127

104128
return {

0 commit comments

Comments
 (0)