forked from DickyT/react-native-textinput-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ios.js
More file actions
151 lines (139 loc) · 5.09 KB
/
index.ios.js
File metadata and controls
151 lines (139 loc) · 5.09 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
const React = require('react');
const {findNodeHandle, TextInput, NativeAppEventEmitter, NativeModules: {
KeyboardToolbar
}, processColor} = require('react-native');
class RCTKeyboardToolbarHelper {
static sharedInstance = new RCTKeyboardToolbarHelper();
constructor() {
this.counter = 0;
this.callbackList = {};
}
static getUid() {
return RCTKeyboardToolbarHelper.sharedInstance.counter++;
}
static setCallback(key, value) {
RCTKeyboardToolbarHelper.sharedInstance.callbackList[key] = value;
}
static getCallback(key) {
return RCTKeyboardToolbarHelper.sharedInstance.callbackList[key];
}
static clearCallback(key) {
delete RCTKeyboardToolbarHelper.sharedInstance.callbackList[key];
}
}
NativeAppEventEmitter.addListener('TUKeyboardToolbarDidTouchOnCancel', (currentUid) => {
let eventHandler = RCTKeyboardToolbarHelper.getCallback(currentUid).onCancel;
if (eventHandler) {
eventHandler();
}
});
NativeAppEventEmitter.addListener('TUKeyboardToolbarDidTouchOnDone', (currentUid) => {
let eventHandler = RCTKeyboardToolbarHelper.getCallback(currentUid).onDone;
if (eventHandler) {
eventHandler();
}
});
NativeAppEventEmitter.addListener('TUKeyboardPickerViewDidSelected', (data) => {
console.log(`TUKeyboardPickerViewDidSelected => data => ${data['selectedIndex']}`);
var currentUid = data['currentUid'];
var selectedIndex = data['selectedIndex'];
let eventHandler = RCTKeyboardToolbarHelper.getCallback(currentUid).onPickerSelect;
if (eventHandler) {
eventHandler(selectedIndex);
}
});
NativeAppEventEmitter.addListener('TUKeyboardDatePickerViewDidSelected', (data) => {
console.log(`TUKeyboardDatePickerViewDidSelected => data => ${data['selectedDate']}`);
var currentUid = data['currentUid'];
var selectedDate = data['selectedDate'];
let eventHandler = RCTKeyboardToolbarHelper.getCallback(currentUid).onDateSelect;
if (eventHandler) {
eventHandler(selectedDate);
}
});
class RCTKeyboardToolbarManager {
static configure(node, options, callbacks) {
var reactNode = findNodeHandle(node);
options.uid = RCTKeyboardToolbarHelper.getUid();
KeyboardToolbar.configure(reactNode, options, (error, currentUid) => {
node.uid = currentUid;
if (!error) {
RCTKeyboardToolbarHelper.setCallback(currentUid, {
onCancel: callbacks.onCancel,
onDone: callbacks.onDone,
onPickerSelect: callbacks.onPickerSelect,
onDateSelect: callbacks.onDateSelect
});
}
});
}
static dismissKeyboard(node) {
var nodeHandle = findNodeHandle(node);
KeyboardToolbar.dismissKeyboard(nodeHandle);
}
static moveCursorToLast(node) {
var nodeHandle = findNodeHandle(node);
KeyboardToolbar.moveCursorToLast(nodeHandle);
}
static setSelectedTextRange(node, NSRange) {
var nodeHandle = findNodeHandle(node);
KeyboardToolbar.setSelectedTextRange(nodeHandle, NSRange);
}
}
class RCTKeyboardToolbarTextInput extends React.Component {
componentDidMount() {
var pickerViewData = [];
if (this.props.pickerViewData) {
this.props.pickerViewData.map((eachData) => {
pickerViewData.push(eachData.label);
});
}
var callbacks = {
onCancel: () => {
// onCancel
if (this.props.onCancel) {
this.props.onCancel(RCTKeyboardToolbarManager.dismissKeyboard.bind(this, this.refs.input));
}
},
onDone: () => {
// onDone
if (this.props.onDone) {
this.props.onDone(RCTKeyboardToolbarManager.dismissKeyboard.bind(this, this.refs.input));
}
},
onPickerSelect: this.props.onPickerSelect,
onDateSelect: this.props.onDateSelect
};
RCTKeyboardToolbarManager.configure(this.refs.input, {
barStyle: this.props.barStyle,
leftButtonText: this.props.leftButtonText,
rightButtonText: this.props.rightButtonText,
pickerViewData: pickerViewData,
datePickerOptions: this.props.datePickerOptions,
tintColor: processColor(this.props.tintColor)
}, callbacks);
}
componentWillUnmount() {
RCTKeyboardToolbarHelper.clearCallback(this.refs.input.uid);
}
dismissKeyboard() {
RCTKeyboardToolbarManager.dismissKeyboard(this.refs.input);
}
moveCursorToLast() {
RCTKeyboardToolbarManager.moveCursorToLast(this.refs.input);
}
setSelection(start, length) {
RCTKeyboardToolbarManager.setSelectedTextRange(this.refs.input, {
start: start,
length: length
});
}
focus() {
this.refs.MygKD.focus();
}
render() {
return (<TextInput {...this.props} ref="input"/>);
}
}
module.exports = RCTKeyboardToolbarTextInput;