Skip to content

Commit

Permalink
fix: fixed the multiline issue on BottomSheetTextInput #411
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhom committed Aug 6, 2021
1 parent d421f09 commit e21d676
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/components/bottomSheetTextInput/BottomSheetTextInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { memo, useCallback, forwardRef } from 'react';
import { TextInput } from 'react-native-gesture-handler';
import { runOnUI } from 'react-native-reanimated';
import { useBottomSheetInternal } from '../../hooks';
import type { BottomSheetTextInputProps } from './types';

Expand All @@ -15,9 +14,7 @@ const BottomSheetTextInputComponent = forwardRef<
//#region callbacks
const handleOnFocus = useCallback(
args => {
runOnUI(() => {
shouldHandleKeyboardEvents.value = true;
})();
shouldHandleKeyboardEvents.value = true;
if (onFocus) {
onFocus(args);
}
Expand All @@ -26,9 +23,7 @@ const BottomSheetTextInputComponent = forwardRef<
);
const handleOnBlur = useCallback(
args => {
runOnUI(() => {
shouldHandleKeyboardEvents.value = false;
})();
shouldHandleKeyboardEvents.value = false;
if (onBlur) {
onBlur(args);
}
Expand Down
24 changes: 24 additions & 0 deletions src/hooks/useKeyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'react-native';
import {
runOnUI,
useAnimatedReaction,
useSharedValue,
useWorkletCallback,
} from 'react-native-reanimated';
Expand Down Expand Up @@ -36,12 +37,19 @@ export const useKeyboard = () => {
const keyboardAnimationEasing =
useSharedValue<KeyboardEventEasing>('keyboard');
const keyboardAnimationDuration = useSharedValue(500);
const temporaryCachedKeyboardEvent = useSharedValue<any>([]);
//#endregion

//#region worklets
const handleKeyboardEvent = useWorkletCallback(
(state, height, duration, easing) => {
if (state === KEYBOARD_STATE.SHOWN && !shouldHandleKeyboardEvents.value) {
/**
* if the keyboard event was fired before the `onFocus` on TextInput,
* then we cache the input, and wait till the `shouldHandleKeyboardEvents`
* to be updated then fire this function again.
*/
temporaryCachedKeyboardEvent.value = [state, height, duration, easing];
return;
}
keyboardHeight.value =
Expand All @@ -53,6 +61,7 @@ export const useKeyboard = () => {
keyboardAnimationDuration.value = duration;
keyboardAnimationEasing.value = easing;
keyboardState.value = state;
temporaryCachedKeyboardEvent.value = [];
},
[]
);
Expand Down Expand Up @@ -99,6 +108,21 @@ export const useKeyboard = () => {
);
};
}, [handleKeyboardEvent]);

/**
* This reaction is needed to handle the issue with multiline text input.
*
* @link https://github.com/gorhom/react-native-bottom-sheet/issues/411
*/
useAnimatedReaction(
() => shouldHandleKeyboardEvents.value,
result => {
const params = temporaryCachedKeyboardEvent.value;
if (result && params.length > 0) {
handleKeyboardEvent(params[0], params[1], params[2], params[3]);
}
}
);
//#endregion

return {
Expand Down

0 comments on commit e21d676

Please sign in to comment.