Skip to content

Commit 4fa18c4

Browse files
author
leonardocarvalho
committed
Add support to subscribe to keyboard events
This commit is not strictly necessary, once lib users can make their own subscribe to `Keyboard` events. On the other hand, this helps everything related to keyboard to be on the same place. Also, this commit makes the lib compliant with its documentation, which state that `onKeyboardWillShow` is a possible prop for the component.
1 parent b3cc038 commit 4fa18c4

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

lib/KeyboardAwareHOC.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,34 @@ import ReactNative, {
99
TextInput
1010
} from 'react-native'
1111
import { isIphoneX } from 'react-native-iphone-x-helper'
12-
1312
import type { KeyboardAwareInterface } from './KeyboardAwareInterface'
1413

1514
const _KAM_DEFAULT_TAB_BAR_HEIGHT: number = isIphoneX() ? 83 : 49
1615
const _KAM_KEYBOARD_OPENING_TIME: number = 250
1716
const _KAM_EXTRA_HEIGHT: number = 75
1817

18+
const supportedKeyboardEvents = [
19+
'keyboardWillShow',
20+
'keyboardDidShow',
21+
'keyboardWillHide',
22+
'keyboardDidHide',
23+
'keyboardWillChangeFrame',
24+
'keyboardDidChangeFrame',
25+
]
26+
const keyboardEventToCallbackName = (eventName: string) => (
27+
'on' + eventName[0].toUpperCase() + eventName.substring(1)
28+
)
29+
const keyboardEventPropTypes = supportedKeyboardEvents.reduce(
30+
(acc: Object, eventName: string) => (
31+
{ ...acc, [keyboardEventToCallbackName(eventName)]: PropTypes.func }
32+
), {}
33+
)
34+
const keyboardAwareHOCTypeEvents = supportedKeyboardEvents.reduce(
35+
(acc: Object, eventName: string) => (
36+
{ ...acc, [keyboardEventToCallbackName(eventName)]: Function }
37+
), {}
38+
)
39+
1940
export type KeyboardAwareHOCProps = {
2041
viewIsInsideTabBar?: boolean,
2142
resetScrollToCoords?: {
@@ -30,7 +51,8 @@ export type KeyboardAwareHOCProps = {
3051
onScroll?: Function,
3152
contentContainerStyle?: any,
3253
enableOnAndroid?: boolean,
33-
innerRef?: Function
54+
innerRef?: Function,
55+
...keyboardAwareHOCTypeEvents
3456
}
3557
export type KeyboardAwareHOCState = {
3658
keyboardSpace: number
@@ -65,7 +87,8 @@ function listenToKeyboardEvents(ScrollableComponent: React$Component) {
6587
onScroll: PropTypes.func,
6688
contentContainerStyle: PropTypes.any,
6789
enableOnAndroid: PropTypes.bool,
68-
innerRef: PropTypes.func
90+
innerRef: PropTypes.func,
91+
...keyboardEventPropTypes
6992
}
7093

7194
static defaultProps = {
@@ -81,6 +104,7 @@ function listenToKeyboardEvents(ScrollableComponent: React$Component) {
81104
super(props)
82105
this.keyboardWillShowEvent = undefined
83106
this.keyboardWillHideEvent = undefined
107+
this.callbacks = {}
84108
this.position = { x: 0, y: 0 }
85109
this.defaultResetScrollToCoords = null
86110
const keyboardSpace: number = props.viewIsInsideTabBar
@@ -111,6 +135,13 @@ function listenToKeyboardEvents(ScrollableComponent: React$Component) {
111135
this._resetKeyboardSpace
112136
)
113137
}
138+
139+
supportedKeyboardEvents.forEach((eventName: string) => {
140+
const callbackName = keyboardEventToCallbackName(eventName);
141+
if (this.props[callbackName]) {
142+
this.callbacks[eventName] = Keyboard.addListener(eventName, this.props[callbackName])
143+
}
144+
})
114145
}
115146

116147
componentWillReceiveProps(nextProps: KeyboardAwareHOCProps) {
@@ -126,6 +157,7 @@ function listenToKeyboardEvents(ScrollableComponent: React$Component) {
126157
this.mountedComponent = false
127158
this.keyboardWillShowEvent && this.keyboardWillShowEvent.remove()
128159
this.keyboardWillHideEvent && this.keyboardWillHideEvent.remove()
160+
Object.values(this.callbacks).forEach((callback: Object) => callback.remove())
129161
}
130162

131163
getScrollResponder = () => {

0 commit comments

Comments
 (0)