Skip to content

Commit

Permalink
Merge pull request #32 from hasithaWalpola/master
Browse files Browse the repository at this point in the history
Add an boolean property to clear the OTP Input fields
  • Loading branch information
fpena authored Dec 30, 2019
2 parents 7cc7b68 + 0025335 commit afbee61
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class OTPInputView extends Component {
code: PropTypes.string,
secureTextEntry: PropTypes.bool,
keyboardType: PropTypes.string,
clearInputs: PropTypes.bool
}

static defaultProps = {
Expand All @@ -24,7 +25,8 @@ export default class OTPInputView extends Component {
onCodeFilled: null,
autoFocusOnLoad: true,
secureTextEntry: false,
keyboardType: "number-pad"
keyboardType: "number-pad",
clearInputs: false
}

fields = []
Expand Down Expand Up @@ -174,10 +176,19 @@ export default class OTPInputView extends Component {
})
}


clearAllFields = () => {
const { clearInputs, code } = this.props;
if (clearInputs && code === "") {
this.setState({ digits: code })
}
}

renderOneInputField = (_, index) => {
const { codeInputFieldStyle, codeInputHighlightStyle, secureTextEntry, keyboardType } = this.props
const { defaultTextFieldStyle } = styles
const { selectedIndex, digits } = this.state
const { clearInputs } = this.props
return (
<View pointerEvents="none" key={index + "view"}>
<TextInput
Expand All @@ -188,7 +199,7 @@ export default class OTPInputView extends Component {
this.handleChangeText(index, text)
}}
onKeyPress={({ nativeEvent: { key } }) => { this.handleKeyPressTextInput(index, key) }}
value={digits[index]}
value={ !clearInputs ? digits[index]: "" }
keyboardType={keyboardType}
textContentType={isAutoFillSupported ? "oneTimeCode" : "none"}
key={index}
Expand All @@ -206,7 +217,7 @@ export default class OTPInputView extends Component {
}

render() {
const { pinCount, style } = this.props
const { pinCount, style, clearInputs } = this.props
const digits = this.getDigits()
return (
<View
Expand All @@ -215,8 +226,13 @@ export default class OTPInputView extends Component {
<TouchableWithoutFeedback
style={{ width: '100%', height: '100%' }}
onPress={() => {
let filledPinCount = digits.filter((digit) => { return (digit !== null && digit !== undefined) }).length
this.focusField(Math.min(filledPinCount, pinCount - 1))
if (!clearInputs) {
let filledPinCount = digits.filter((digit) => { return (digit !== null && digit !== undefined) }).length
this.focusField(Math.min(filledPinCount, pinCount - 1))
} else {
this.clearAllFields();
this.focusField(0)
}
}}
>
<View
Expand Down

2 comments on commit afbee61

@hasithaWalpola
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fpena is this PR added to the new version?

@fpena
Copy link
Contributor Author

@fpena fpena commented on afbee61 Dec 30, 2019 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.