forked from beefe/react-native-keyboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
209 lines (173 loc) · 5.27 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* Keyboard
*/
'use strict';
import React, { Component, PropTypes } from 'react';
import {
View, Text, Image, TouchableHighlight
} from 'react-native';
import styles, {
keyStyle, BG_COLOR
} from './styles';
const numberKeys = [
[
{ mainText: '1', otherText: '' },
{ mainText: '2', otherText: 'ABC' },
{ mainText: '3', otherText: 'DEF' }
],
[
{ mainText: '4', otherText: 'GHI' },
{ mainText: '5', otherText: 'JKL' },
{ mainText: '6', otherText: 'MNO' }
],
[
{ mainText: '7', otherText: 'PQRS' },
{ mainText: '8', otherText: 'TUV' },
{ mainText: '9', otherText: 'WXYZ' }
]
];
class Keyboard extends Component {
constructor(props) {
super(props);
}
_clearAll() {
this.props.onClear();
}
_onPress(key) {
if (key === '') {
return;
// delete key
} else if (key === 'del') {
this.props.onDelete();
// number key
} else {
this.props.onKeyPress(key);
}
}
_renderOtherText(key) {
if (this.props.disableOtherText !== true) {
return (<Text style={keyStyle.otherText}>{key.otherText}</Text>);
}
return null;
}
_disableBorder() {
if (this.props.disableBorder !== true) {
return keyStyle.bd;
}
return keyStyle.border;
}
_disableClearButtonBackground() {
if (this.props.disableClearButtonBackground !== true) {
return keyStyle.bg_d2d5dc;
}
return keyStyle.bgLess;
}
_clearBtnUnderlayColor() {
if (this.props.disableClearButtonBackground !== true) {
return '#ffffff';
}
return '#d2d5dc';
}
_renderKey(key, index) {
return (
<TouchableHighlight
key={index}
underlayColor={BG_COLOR}
style={keyStyle.wrapper}
onPress={this._onPress.bind(this, key.mainText)}
>
<View style={[keyStyle.bd, this._disableBorder()]}>
<Text style={keyStyle.mainText}>{key.mainText}</Text>
{this._renderOtherText(key)}
</View>
</TouchableHighlight>
);
}
_renderNumberKeys() {
return numberKeys.map((group, groupIndex) => {
return (
<View key={groupIndex} style={styles.row}>
{group.map(this._renderKey.bind(this))}
</View>
);
});
}
_isDecimalPad() {
return this.props.keyboardType === 'decimal-pad';
}
_renderDotKey() {
if (this.props.disableDot !== true) {
let dotNode = null,
dotText = '';
if (this._isDecimalPad()) {
dotText = '.';
dotNode = <Text style={[keyStyle.mainText, keyStyle.dot]}>.</Text>;
}
return (
<TouchableHighlight
underlayColor="#ffffff"
style={[keyStyle.wrapper, keyStyle.bg_d2d5dc]}
onPress={this._onPress.bind(this, dotText)}
>
<View style={[keyStyle.bd, this._disableBorder()]}>{dotNode}</View>
</TouchableHighlight>
);
}
return (
<TouchableHighlight
style={keyStyle.wrapper}
>
<View />
</TouchableHighlight>
);
}
render() {
const props = this.props;
return (
<View style={styles.wrapper}>
<View style={styles.main}>
{this._renderNumberKeys()}
<View style={styles.row}>
{this._renderDotKey()}
<TouchableHighlight
underlayColor={BG_COLOR}
style={keyStyle.wrapper}
onPress={this._onPress.bind(this, '0')}
>
<View style={[keyStyle.bd, this._disableBorder()]}>
<Text style={keyStyle.mainText}>0</Text>
</View>
</TouchableHighlight>
<TouchableHighlight
underlayColor={this._clearBtnUnderlayColor()}
style={[keyStyle.wrapper, this._disableClearButtonBackground()]}
onPress={this._onPress.bind(this, 'del')}
onLongPress={this._clearAll.bind(this)}
>
<View style={[keyStyle.bd, this._disableBorder()]}>
<Image source={require('./images/icon_delete.png')} />
</View>
</TouchableHighlight>
</View>
</View>
</View>
);
}
}
Keyboard.propTypes = {
// 是否显示小数点符号
keyboardType: PropTypes.oneOf(['number-pad', 'decimal-pad']),
// 点击键盘按键
onKeyPress: PropTypes.func,
// 点击删除按钮
onDelete: PropTypes.func,
// 长按删除按钮
onClear: PropTypes.func
};
Keyboard.defaultProps = {
keyboardType: 'number-pad',
onKeyPress: () => {},
onDelete: () => {},
onClear: () => {}
};
export default Keyboard;