@@ -10,7 +10,7 @@ import PropTypes from "prop-types";
1010
1111const backAsset = require ( "./back.png" ) ;
1212
13- class PinKeyboard extends Component {
13+ class VirtualKeyboard extends Component {
1414 /**
1515 * [ Built-in React method. ]
1616 *
@@ -23,6 +23,7 @@ class PinKeyboard extends Component {
2323 super ( props ) ;
2424
2525 this . state = {
26+ text : "" ,
2627 disabled : false ,
2728 message : null
2829 } ;
@@ -46,6 +47,17 @@ class PinKeyboard extends Component {
4647 this . props . onRef ( undefined ) ;
4748 }
4849
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+
4961 /**
5062 * [ Built-in React method. ]
5163 *
@@ -69,22 +81,22 @@ class PinKeyboard extends Component {
6981
7082 return (
7183 < 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+ ) ;
88100 }
89101
90102 /**
@@ -109,9 +121,9 @@ class PinKeyboard extends Component {
109121 if ( message ) {
110122 return (
111123 < View style = { [ messageDefaultStyle , messageStyle ] } >
112- < Text style = { [ messageTextDefaultStyle , messageTextStyle ] } > { message } </ Text >
113- </ View >
114- ) ;
124+ < Text style = { [ messageTextDefaultStyle , messageTextStyle ] } > { message } </ Text >
125+ </ View >
126+ ) ;
115127 }
116128
117129 return null ;
@@ -137,17 +149,26 @@ class PinKeyboard extends Component {
137149 } = styles ;
138150 /** Props */
139151 const {
140- keyDown,
141152 keyboardFunc,
142153 keyboardDisabledStyle,
143154 vibration,
155+ onKeyDown,
156+ onChange,
144157 // Style Props
145158 keyStyle,
146159 keyTextStyle,
147160 keyImageStyle
148161 } = this . props ;
149162 /** State */
150163 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+ }
151172
152173 // Custom functions for the keyboard key
153174 const keyboardFuncSet = keyboardFunc
@@ -156,47 +177,68 @@ class PinKeyboard extends Component {
156177 [ null , null , null ] ,
157178 [ null , null , null ] ,
158179 [ null , null , null ] ,
159- [ ( ) => this . props . keyDown ( "custom" ) , null , ( ) => this . props . keyDown ( "back" ) ]
180+ [ ( ) => keyDown ( "custom" ) , null , ( ) => keyDown ( "back" ) ]
160181 ] ;
161182
162183 // Decide if the element passed as the key is text
163184 const keyJsx = keyboardFuncSet [ row ] [ column ] ? (
164185 < Image style = { [ keyImageDefaultStyle , keyImageStyle ] } source = { entity } />
165- ) : (
166- < Text style = { [ keyTextDefaultStyle , keyTextStyle ] } > { entity } </ Text >
167- ) ;
186+ ) : (
187+ < Text style = { [ keyTextDefaultStyle , keyTextStyle ] } > { entity } </ Text >
188+ ) ;
168189
169190 // We want to block keyboard interactions if it has been disabled.
170191 if ( ! disabled ) {
171192 return (
172193 < 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 ) ;
177198
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+ ) ;
185206 } else {
186207 return (
187208 < 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+ }
200242 }
201243 }
202244
@@ -243,9 +285,11 @@ class PinKeyboard extends Component {
243285 }
244286}
245287
246- PinKeyboard . propTypes = {
288+ VirtualKeyboard . propTypes = {
247289 onRef : PropTypes . any . isRequired ,
248- keyDown : PropTypes . func . isRequired ,
290+ onKeyDown : PropTypes . func ,
291+ onChange : PropTypes . func ,
292+ onCustomKey : PropTypes . func ,
249293 keyboard : PropTypes . array ,
250294 keyboardFunc : PropTypes . array ,
251295 keyboardCustomKeyImage : PropTypes . number ,
@@ -261,7 +305,7 @@ PinKeyboard.propTypes = {
261305 messageTextStyle : PropTypes . object
262306} ;
263307
264- PinKeyboard . defaultProps = {
308+ VirtualKeyboard . defaultProps = {
265309 // Keyboard functions. By default the text (number) in the
266310 // keyboard array will be passed via the keyDown callback.
267311 // Use this array to set custom functions for certain keys.
@@ -339,4 +383,4 @@ const styles = StyleSheet.create({
339383 }
340384} ) ;
341385
342- export default PinKeyboard ;
386+ export default VirtualKeyboard ;
0 commit comments