-
Notifications
You must be signed in to change notification settings - Fork 751
Expand file tree
/
Copy pathCustomKeyboardViewBase.tsx
More file actions
86 lines (71 loc) · 2.55 KB
/
CustomKeyboardViewBase.tsx
File metadata and controls
86 lines (71 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React, {Component} from 'react';
import {EventSubscription} from 'react-native';
import KeyboardRegistry from '../KeyboardRegistry';
export type CustomKeyboardViewBaseProps = {
inputRef?: any;
initialProps?: any;
keyboardHeight?: number;
onKeyboardDismiss?: () => void;
shouldFocus?: boolean;
component?: string;
onItemSelected?: (component?: string, args?: any) => void;
onRequestShowKeyboard?: (keyboardId: string) => void;
children?: React.ReactNode;
};
export default class CustomKeyboardViewBase<T extends CustomKeyboardViewBaseProps> extends Component<T> {
static defaultProps = {
initialProps: {}
};
registeredRequestShowKeyboard = false;
keyboardExpandedToggle: any;
keyboardEventListeners: EventSubscription[] = [];
constructor(props: T) {
super(props);
const {component, onItemSelected} = props;
if (component) {
this.addOnItemSelectListener(onItemSelected, component);
}
this.keyboardExpandedToggle = {};
}
shouldComponentUpdate(nextProps: T) {
return nextProps.component !== this.props.component;
}
componentWillUnmount() {
const {component} = this.props;
KeyboardRegistry.removeListeners('onRequestShowKeyboard');
if (this.keyboardEventListeners) {
this.keyboardEventListeners.forEach((eventListener: EventSubscription) => eventListener.remove());
}
if (component) {
KeyboardRegistry.removeListeners(`${component}.onItemSelected`);
}
}
addOnItemSelectListener(onItemSelected: CustomKeyboardViewBaseProps['onItemSelected'],
component: CustomKeyboardViewBaseProps['component']) {
if (onItemSelected) {
KeyboardRegistry.addListener(`${component}.onItemSelected`, (args: any) => {
onItemSelected(component, args);
});
}
}
componentDidUpdate(prevProps: T) {
const {onRequestShowKeyboard} = this.props;
if (onRequestShowKeyboard && !this.registeredRequestShowKeyboard) {
this.registeredRequestShowKeyboard = true;
KeyboardRegistry.addListener('onRequestShowKeyboard', (args: any) => {
onRequestShowKeyboard(args.keyboardId);
});
}
this.registerListener(prevProps, this.props);
}
registerListener(props: T, nextProps: T) {
const {component, onItemSelected} = nextProps;
if (component && props.component !== component) {
if (props.component) {
KeyboardRegistry.removeListeners(`${props.component}.onItemSelected`);
}
KeyboardRegistry.removeListeners(`${component}.onItemSelected`);
this.addOnItemSelectListener(onItemSelected, component);
}
}
}