-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinpicker.js
143 lines (121 loc) · 3.82 KB
/
pinpicker.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
// Copyright (c) 2014-Present All rights reserved.
// The Authors at Excubito Pvt Ltd.
'use strict';
import React, {Component} from 'react';
import { connect } from 'react-redux'
import {
Text,
StyleSheet,
TextInput,
View,
} from 'react-native';
import RegisterAPI from '../../lib/libregisterapi'
import FilledButton from '../filledbutton'
import CommonStyles from '../commonstyles'
import LoginPINVerify from './loginpinverify'
import Icon from 'react-native-vector-icons/FontAwesome'
import { parse, format, asYouType } from 'libphonenumber-js'
/* maps callingCode -> CCA2 */
import CallingCodeToCCA2 from 'libphonenumber-js/metadata.min'
import CountryPicker from './countrypicker'
/* maps CCA2 -> CountryLocalDetails */
import Countries from './data'
var styles = StyleSheet.create({
containerRow: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center'
},
});
class PinPicker extends React.Component {
constructor (props) {
super(props)
let digits = []
let releaseFocus = []
for (let i = 0; i < this.props.numDigits; i++) {
digits[i] = ""
releaseFocus[i] = 0
}
this.state = {
digits:digits,
releaseFocus:releaseFocus,
setFocusIdx:0
}
}
updateFocus() {
if (this.state.setFocusIdx !== undefined)
this.refs[Math.min(this.state.setFocusIdx, this.props.numDigits - 1)].focus()
}
pinChanged() {
let pin=""
for (let i = 0; i < this.props.numDigits; i++) {
pin += this.state.digits[i]
}
pin = pin.replace(/\D/g,'');
this.props.onPinChanged(pin)
this.updateFocus()
}
renderPinInput() {
let pinBoxes = []
for (let i = 0; i < this.props.numDigits; i ++) {
let onChangeText = (text) => {
let setFocusIdx = i
if (text.length) {
setFocusIdx = i + 1
}
var digits = this.state.digits.slice()
digits[i] = text
this.setState({digits:digits, setFocusIdx:setFocusIdx},
this.pinChanged.bind(this))
}
let onKeyPress = () => {
/* detect a backspace and focus on the prior element */
if (i == 0) {
return
}
let focusIdx = i
if (this.state.digits[i] === undefined ||
this.state.digits[i].length == 0) {
/* detect backspace pressed twice in a row with contents being empty */
var releaseFocus = this.state.releaseFocus.slice()
releaseFocus[i] = releaseFocus[i] + 1
if (releaseFocus[i] > 1) {
releaseFocus[i] = 0
focusIdx = i - 1
}
this.setState({releaseFocus:releaseFocus, setFocusIdx:focusIdx},
this.updateFocus.bind(this))
}
}
pinBoxes.push(
<TextInput key={i}
autoFocus={i == 0}
style={{fontSize:20, height:100, width:20}}
value={this.state.digits[i]}
ref={i}
onChangeText={onChangeText.bind(this)}
maxLength={1}
placeholder={"#"}
onKeyPress={onKeyPress.bind(this)}
keyboardType="numeric"
/>)
}
return pinBoxes
}
render() {
return (
<View style={[styles.containerRow, {marginHorizontal: 0}]}>
{this.renderPinInput()}
</View>
);
}
}
PinPicker.PropTypes = {
onChangePin: React.PropTypes.func.isRequired,
numDigits: React.PropTypes.number,
}
PinPicker.defaultProps = {
numDigits: 6
}
const mapStateToProps = (state) => ({})
export default connect(mapStateToProps)(PinPicker)