@@ -10,7 +10,7 @@ import PropTypes from "prop-types";
10
10
11
11
const backAsset = require ( "./back.png" ) ;
12
12
13
- class PinKeyboard extends Component {
13
+ class VirtualKeyboard extends Component {
14
14
/**
15
15
* [ Built-in React method. ]
16
16
*
@@ -23,6 +23,7 @@ class PinKeyboard extends Component {
23
23
super ( props ) ;
24
24
25
25
this . state = {
26
+ text : "" ,
26
27
disabled : false ,
27
28
message : null
28
29
} ;
@@ -46,6 +47,17 @@ class PinKeyboard extends Component {
46
47
this . props . onRef ( undefined ) ;
47
48
}
48
49
50
+ /**
51
+ * [ Built-in React method. ]
52
+ *
53
+ * Executed when the components props are updated.
54
+ */
55
+ componentDidUpdate ( prevProps , prevState ) {
56
+ if ( prevState . text !== this . state . text ) {
57
+ if ( this . props . onChange ) this . props . onChange ( this . state . text ) ;
58
+ }
59
+ }
60
+
49
61
/**
50
62
* [ Built-in React method. ]
51
63
*
@@ -69,22 +81,22 @@ class PinKeyboard extends Component {
69
81
70
82
return (
71
83
< View style = { containerStyle } >
72
- { this . renderMessage ( ) }
73
- < View style = { [ keyboardDefaultStyle , keyboardStyle ] } >
74
- { // Maps each array of numbers in the keyboardValues array
75
- defaultKeyboard . map ( ( row , r ) => {
76
- return (
77
- < View key = { r } style = { keyboardRowStyle } >
78
- { // Maps each number in row and creates key for that number
79
- row . map ( ( element , k ) => {
80
- return this . renderKey ( element , r , k ) ;
81
- } ) }
82
- </ View >
83
- ) ;
84
- } ) }
85
- </ View >
86
- </ View >
87
- ) ;
84
+ { this . renderMessage ( ) }
85
+ < View style = { [ keyboardDefaultStyle , keyboardStyle ] } >
86
+ { // Maps each array of numbers in the keyboardValues array
87
+ defaultKeyboard . map ( ( row , r ) => {
88
+ return (
89
+ < View key = { r } style = { keyboardRowStyle } >
90
+ { // Maps each number in row and creates key for that number
91
+ row . map ( ( element , k ) => {
92
+ return this . renderKey ( element , r , k ) ;
93
+ } ) }
94
+ </ View >
95
+ ) ;
96
+ } ) }
97
+ </ View >
98
+ </ View >
99
+ ) ;
88
100
}
89
101
90
102
/**
@@ -109,9 +121,9 @@ class PinKeyboard extends Component {
109
121
if ( message ) {
110
122
return (
111
123
< View style = { [ messageDefaultStyle , messageStyle ] } >
112
- < Text style = { [ messageTextDefaultStyle , messageTextStyle ] } > { message } </ Text >
113
- </ View >
114
- ) ;
124
+ < Text style = { [ messageTextDefaultStyle , messageTextStyle ] } > { message } </ Text >
125
+ </ View >
126
+ ) ;
115
127
}
116
128
117
129
return null ;
@@ -137,17 +149,26 @@ class PinKeyboard extends Component {
137
149
} = styles ;
138
150
/** Props */
139
151
const {
140
- keyDown,
141
152
keyboardFunc,
142
153
keyboardDisabledStyle,
143
154
vibration,
155
+ onKeyDown,
156
+ onChange,
144
157
// Style Props
145
158
keyStyle,
146
159
keyTextStyle,
147
160
keyImageStyle
148
161
} = this . props ;
149
162
/** State */
150
163
const { disabled } = this . state ;
164
+ /** Variables */
165
+ const keyDown = ( value ) => {
166
+ this . setState ( {
167
+ text : this . resolveKeyDownVirtualKeyboard ( this . state . text , value ) ,
168
+ } ) ;
169
+
170
+ if ( onKeyDown ) onKeyDown ( value ) ;
171
+ }
151
172
152
173
// Custom functions for the keyboard key
153
174
const keyboardFuncSet = keyboardFunc
@@ -156,47 +177,68 @@ class PinKeyboard extends Component {
156
177
[ null , null , null ] ,
157
178
[ null , null , null ] ,
158
179
[ null , null , null ] ,
159
- [ ( ) => this . props . keyDown ( "custom" ) , null , ( ) => this . props . keyDown ( "back" ) ]
180
+ [ ( ) => keyDown ( "custom" ) , null , ( ) => keyDown ( "back" ) ]
160
181
] ;
161
182
162
183
// Decide if the element passed as the key is text
163
184
const keyJsx = keyboardFuncSet [ row ] [ column ] ? (
164
185
< Image style = { [ keyImageDefaultStyle , keyImageStyle ] } source = { entity } />
165
- ) : (
166
- < Text style = { [ keyTextDefaultStyle , keyTextStyle ] } > { entity } </ Text >
167
- ) ;
186
+ ) : (
187
+ < Text style = { [ keyTextDefaultStyle , keyTextStyle ] } > { entity } </ Text >
188
+ ) ;
168
189
169
190
// We want to block keyboard interactions if it has been disabled.
170
191
if ( ! disabled ) {
171
192
return (
172
193
< Ripple
173
- rippleColor = { "#000" }
174
- key = { column }
175
- onPressIn = { ( ) => {
176
- if ( vibration ) Vibration . vibrate ( 50 ) ;
194
+ rippleColor = { "#000" }
195
+ key = { column }
196
+ onPressIn = { ( ) => {
197
+ if ( vibration ) Vibration . vibrate ( 50 ) ;
177
198
178
- keyboardFuncSet [ row ] [ column ] ? keyboardFuncSet [ row ] [ column ] ( ) : keyDown ( entity )
179
- } }
180
- style = { [ keyContainerStyle , keyDefaultStyle , keyStyle ] }
181
- >
182
- { keyJsx }
183
- </ Ripple >
184
- ) ;
199
+ keyboardFuncSet [ row ] [ column ] ? keyboardFuncSet [ row ] [ column ] ( ) : keyDown ( entity )
200
+ } }
201
+ style = { [ keyContainerStyle , keyDefaultStyle , keyStyle ] }
202
+ >
203
+ { keyJsx }
204
+ </ Ripple >
205
+ ) ;
185
206
} else {
186
207
return (
187
208
< View
188
- key = { column }
189
- style = { [
190
- keyContainerStyle ,
191
- keyDefaultStyle ,
192
- keyStyle ,
193
- keyboardDisabledDefaultStyle ,
194
- keyboardDisabledStyle
195
- ] }
196
- >
197
- { keyJsx }
198
- </ View >
199
- ) ;
209
+ key = { column }
210
+ style = { [
211
+ keyContainerStyle ,
212
+ keyDefaultStyle ,
213
+ keyStyle ,
214
+ keyboardDisabledDefaultStyle ,
215
+ keyboardDisabledStyle
216
+ ] }
217
+ >
218
+ { keyJsx }
219
+ </ View >
220
+ ) ;
221
+ }
222
+ }
223
+ /**
224
+ * Resolves a key press on virtual keyboard
225
+ *
226
+ * @param string
227
+ * @param char
228
+ */
229
+ resolveKeyDownVirtualKeyboard ( string = "" , char ) {
230
+ const newString = string ;
231
+
232
+ switch ( char ) {
233
+ case "back" : {
234
+ return newString . substring ( 0 , newString . length - 1 ) ;
235
+ }
236
+ case "custom" :
237
+ if ( this . props . onCustomKey ) this . props . onCustomKey ( string ) ;
238
+ return string ;
239
+ default : {
240
+ return newString . concat ( char ) ;
241
+ }
200
242
}
201
243
}
202
244
@@ -243,9 +285,11 @@ class PinKeyboard extends Component {
243
285
}
244
286
}
245
287
246
- PinKeyboard . propTypes = {
288
+ VirtualKeyboard . propTypes = {
247
289
onRef : PropTypes . any . isRequired ,
248
- keyDown : PropTypes . func . isRequired ,
290
+ onKeyDown : PropTypes . func ,
291
+ onChange : PropTypes . func ,
292
+ onCustomKey : PropTypes . func ,
249
293
keyboard : PropTypes . array ,
250
294
keyboardFunc : PropTypes . array ,
251
295
keyboardCustomKeyImage : PropTypes . number ,
@@ -261,7 +305,7 @@ PinKeyboard.propTypes = {
261
305
messageTextStyle : PropTypes . object
262
306
} ;
263
307
264
- PinKeyboard . defaultProps = {
308
+ VirtualKeyboard . defaultProps = {
265
309
// Keyboard functions. By default the text (number) in the
266
310
// keyboard array will be passed via the keyDown callback.
267
311
// Use this array to set custom functions for certain keys.
@@ -339,4 +383,4 @@ const styles = StyleSheet.create({
339
383
}
340
384
} ) ;
341
385
342
- export default PinKeyboard ;
386
+ export default VirtualKeyboard ;
0 commit comments