Skip to content
This repository has been archived by the owner on May 7, 2022. It is now read-only.

Commit

Permalink
Fixes after PR
Browse files Browse the repository at this point in the history
  • Loading branch information
demchenkoalex committed Nov 1, 2020
1 parent 3006eb5 commit 06e323d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flyerhq/react-native-keyboard-accessory-view",
"version": "2.0.0",
"version": "2.1.0",
"description": "Keyboard accessory (sticky) view for your React Native app. Supports interactive dismiss on iOS.",
"homepage": "https://github.com/flyerhq/react-native-keyboard-accessory-view#readme",
"main": "lib/index.js",
Expand Down
16 changes: 0 additions & 16 deletions src/hooks/__tests__/useKeyboardDimensions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,4 @@ describe('useKeyboardDimensions', () => {
expect(result.current.keyboardHeight).toStrictEqual(0)
unmount()
})

it('dont uses listeners on Android', () => {
expect.assertions(4)
const { result, unmount } = renderHook(() => useKeyboardDimensions(false))
act(() => {
emitter.emit('keyboardDidShow', keyboardOpenEvent)
})
expect(result.current.keyboardEndPositionY).toStrictEqual(896)
expect(result.current.keyboardHeight).toStrictEqual(0)
act(() => {
emitter.emit('keyboardDidHide', keyboardHideEvent)
})
expect(result.current.keyboardEndPositionY).toStrictEqual(896)
expect(result.current.keyboardHeight).toStrictEqual(0)
unmount()
})
})
38 changes: 18 additions & 20 deletions src/hooks/useKeyboardDimensions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,29 @@ export const useKeyboardDimensions = (useListenersOnAndroid?: boolean) => {
})

React.useEffect(() => {
const handleDimensionsChange = ({ window }: { window: ScaledSize }) => {
setState((cur) => ({
const handleDimensionsChange = ({ window }: { window: ScaledSize }) =>
setState((current) => ({
...current,
keyboardEndPositionY: window.height,
keyboardHeight: cur.keyboardHeight,
}))
}

const resetKeyboardDimensions = () =>
setState({
keyboardEndPositionY: height,
keyboardHeight: 0,
})

const updateKeyboardDimensions = (event: KeyboardEvent) => {
setState((cur) => {
const { screenY } = event.endCoordinates
const newKeyboardHeight = height - screenY
if (newKeyboardHeight === cur.keyboardHeight) {
return cur
const updateKeyboardDimensions = (event: KeyboardEvent) =>
setState((current) => {
const { screenY: keyboardEndPositionY } = event.endCoordinates
const keyboardHeight = height - keyboardEndPositionY

if (keyboardHeight === current.keyboardHeight) {
return current
}

const { duration, easing } = event

if (duration && easing) {
// We have to pass the duration equal to minimal
// accepted duration defined here: RCTLayoutAnimation.m
Expand All @@ -64,23 +65,20 @@ export const useKeyboardDimensions = (useListenersOnAndroid?: boolean) => {
}

return {
keyboardEndPositionY: screenY,
keyboardHeight: newKeyboardHeight,
keyboardEndPositionY,
keyboardHeight,
}
})
}

Dimensions.addEventListener('change', handleDimensionsChange)

const listeners: EmitterSubscription[] = []

if (Platform.OS === 'android') {
if (useListenersOnAndroid) {
listeners.push(
Keyboard.addListener('keyboardDidHide', resetKeyboardDimensions),
Keyboard.addListener('keyboardDidShow', updateKeyboardDimensions)
)
}
if (Platform.OS === 'android' && useListenersOnAndroid) {
listeners.push(
Keyboard.addListener('keyboardDidHide', resetKeyboardDimensions),
Keyboard.addListener('keyboardDidShow', updateKeyboardDimensions)
)
} else {
listeners.push(
Keyboard.addListener(
Expand Down

0 comments on commit 06e323d

Please sign in to comment.