Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions components/ConfirmationCodeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const getInputSpaceStyle = (space, inputPosition) => {
return positions[inputPosition] || positions.default
}

const getBorderStyle = (cellBorderWidth, borderType) =>{
const getBorderStyle = (cellBorderWidth, borderType) => {
const types = {
clear: {borderWidth: 0},
square: {borderWidth: cellBorderWidth},
Expand Down Expand Up @@ -129,12 +129,20 @@ export default class ConfirmationCodeInput extends Component {
}
}

_onInputCode = index => character => {
_onInputCode = index => _.debounce(character => {
if (character.length === this.props.codeLength) {
this._onPasteCode(index, character)
} else if (character.length === 1) {
this._onTypeCode(index, character)
}
}, 100)

_onTypeCode = (index, character) => {
const {codeLength, onFulfill} = this.props
let newCodeArr = _.clone(this.state.codeArr)
newCodeArr[index] = character

if (index == codeLength - 1) {
if (index === codeLength - 1) {
const code = newCodeArr.join('')
onFulfill(code)
this._blur(this.state.currentIndex)
Expand All @@ -150,6 +158,20 @@ export default class ConfirmationCodeInput extends Component {
})
}

_onPasteCode = (index, code) => {
const {codeLength, onFulfill} = this.props;
const newCodeArr = code.split('');
const newCurrentIndex = codeLength - 1;

this._blur(this.state.currentIndex)

this.setState(() => ({
codeArr: newCodeArr,
currentIndex: newCurrentIndex,
}))
onFulfill(code)
}

focus = () => this._setFocus(this.state.currentIndex)

blur = () => this._blur(this.state.currentIndex)
Expand Down Expand Up @@ -180,7 +202,7 @@ export default class ConfirmationCodeInput extends Component {
value={this.state.codeArr[id] ? this.state.codeArr[id].toString() : ''}
onChangeText={this._onInputCode(id)}
onKeyPress={this._onKeyPress}
maxLength={1}
maxLength={codeLength}
/>
))

Expand All @@ -201,4 +223,3 @@ const styles = StyleSheet.create({
padding: 0,
},
})