forked from TreeNewbie/react-native-custom-keyboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (83 loc) · 2.41 KB
/
index.js
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
87
88
89
90
91
92
93
import React, { Component } from 'react';
import {
NativeModules,
TextInput,
findNodeHandle,
AppRegistry
} from 'react-native';
import PropTypes from 'prop-types'
const { CustomKeyboard} = NativeModules;
const {
install, uninstall, getSelectionRange,
insertText, backSpace, doDelete,
moveLeft, moveRight,
switchSystemKeyboard,
} = CustomKeyboard;
export {
install, uninstall, getSelectionRange,
insertText, backSpace, doDelete,
moveLeft, moveRight,
switchSystemKeyboard,
};
const keyboardTypeRegistry = {};
const defaultKeyboardHeight = 216
export function register(type, keyboardInfo) {
keyboardTypeRegistry[type] = keyboardInfo;
}
const getKeyboardHeightByType = (type) => {
const height = keyboardTypeRegistry[type].height
return height || defaultKeyboardHeight
}
class CustomKeyboardContainer extends Component {
render() {
const {tag, type} = this.props;
const factory = keyboardTypeRegistry[type].factory;
const inputFilter = keyboardTypeRegistry[type].inputFilter
if (!factory) {
console.warn(`Custom keyboard type ${type} not registered.`);
return null;
}
const Comp = factory();
return <Comp tag={tag} inputFilter={inputFilter} />;
}
}
AppRegistry.registerComponent("CustomKeyboard", ()=>CustomKeyboardContainer);
export class CustomTextInput extends Component {
static propTypes = {
...TextInput.propTypes,
customKeyboardType: PropTypes.string,
};
componentDidMount() {
setTimeout(()=>{
if(!this.input) {
return
}
install(
findNodeHandle(this.input),
this.props.customKeyboardType,
this.props.maxLength === undefined ? 1024 : this.props.maxLength,
getKeyboardHeightByType(this.props.customKeyboardType)
);
}, 100)
}
componentWillReceiveProps(newProps) {
if (this.props.customKeyboardType && newProps.customKeyboardType && newProps.customKeyboardType !== this.props.customKeyboardType) {
if(!this.input) {
return
}
install(
findNodeHandle(this.input),
newProps.customKeyboardType,
newProps.maxLength === undefined ? 1024 : this.props.maxLength,
getKeyboardHeightByType(newProps.customKeyboardType)
);
}
}
onRef = ref => {
this.input = ref;
};
render() {
const { customKeyboardType, ...others } = this.props;
return <TextInput {...others} keyboardType={'numeric'} ref={this.onRef}/>;
}
}