Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ render() {
inputPosition='left'
onFulfill={(code) => this._onFulfill(code)}
/>

<CodeInput
ref="codeInputRef2"
secureTextEntry
Expand All @@ -59,7 +59,7 @@ render() {
containerStyle={{ marginTop: 30 }}
codeInputStyle={{ borderWidth: 1.5 }}
/>

<CodeInput
ref="codeInputRef2"
keyboardType="numeric"
Expand Down Expand Up @@ -92,14 +92,15 @@ Prop | Type | Default | Description
`autoFocus` | boolean | `true` | auto focus on code input
`codeInputStyle` | style object | | custom style for code input
`containerStyle` | style object | | custom style for code input container
`getCurrentCode` | function | | callback function called when change focus and return current code.
`onFulfill` | function | | callback function called when fulfilling code. If `compareWithCode` is null -> return `(code)` in callback, else return `(isValid, code)`. **Required**

## functions
clear input:
```javascript
this.refs.refName.clear();
...
<CodeInput
<CodeInput
...
ref="refName"
/>
Expand All @@ -115,5 +116,5 @@ react-native run-ios / react-native run-android
## License

react-native-confirmation-code-input is released under the MIT license. See [LICENSE](LICENSE) for details.
Any question or support will welcome.

Any question or support will welcome.
59 changes: 32 additions & 27 deletions components/ConfirmationCodeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ export default class ConfirmationCodeInput extends Component {
codeInputStyle: TextInput.propTypes.style,
containerStyle: viewPropTypes.style,
onFulfill: PropTypes.func,
getCurrentCode: PropTypes.func,
};

static defaultProps = {
codeLength: 5,
inputPosition: 'center',
Expand All @@ -37,48 +38,50 @@ export default class ConfirmationCodeInput extends Component {
compareWithCode: '',
ignoreCase: false
};

constructor(props) {
super(props);

this.state = {
codeArr: new Array(this.props.codeLength).fill(''),
currentIndex: 0
};

this.codeInputRefs = [];
}

componentDidMount() {
const { compareWithCode, codeLength, inputPosition } = this.props;
if (compareWithCode && compareWithCode.length !== codeLength) {
console.error("Invalid props: compareWith length is not equal to codeLength");
}

if (_.indexOf(['center', 'left', 'right', 'full-width'], inputPosition) === -1) {
console.error('Invalid input position. Must be in: center, left, right, full');
}
}

clear() {
this.setState({
codeArr: new Array(this.props.codeLength).fill(''),
currentIndex: 0
});
this._setFocus(0);
}

_setFocus(index) {
this.codeInputRefs[index].focus();
}

_blur(index) {
this.codeInputRefs[index].blur();
}

_onFocus(index) {
let newCodeArr = _.clone(this.state.codeArr);
const currentEmptyIndex = _.findIndex(newCodeArr, c => !c);
const { getCurrentCode } = this.props;

if (currentEmptyIndex !== -1 && currentEmptyIndex < index) {
return this._setFocus(currentEmptyIndex);
}
Expand All @@ -87,20 +90,22 @@ export default class ConfirmationCodeInput extends Component {
newCodeArr[i] = '';
}
}


getCurrentCode(newCodeArr.join(''));

this.setState({
codeArr: newCodeArr,
currentIndex: index
})
}

_isMatchingCode(code, compareWithCode, ignoreCase = false) {
if (ignoreCase) {
return code.toLowerCase() == compareWithCode.toLowerCase();
}
return code == compareWithCode;
}

_getContainerStyle(size, position) {
switch (position) {
case 'left':
Expand All @@ -125,7 +130,7 @@ export default class ConfirmationCodeInput extends Component {
}
}
}

_getInputSpaceStyle(space) {
const { inputPosition } = this.props;
switch (inputPosition) {
Expand All @@ -149,14 +154,14 @@ export default class ConfirmationCodeInput extends Component {
};
}
}

_getClassStyle(className, active) {
const { cellBorderWidth, activeColor, inactiveColor, space } = this.props;
let classStyle = {
...this._getInputSpaceStyle(space),
color: activeColor
};

switch (className) {
case 'clear':
return _.merge(classStyle, { borderWidth: 0 });
Expand Down Expand Up @@ -192,23 +197,23 @@ export default class ConfirmationCodeInput extends Component {
return className;
}
}

_onKeyPress(e) {
if (e.nativeEvent.key === 'Backspace') {
const { currentIndex } = this.state;
const nextIndex = currentIndex > 0 ? currentIndex - 1 : 0;
this._setFocus(nextIndex);
}
}

_onInputCode(character, index) {
const { codeLength, onFulfill, compareWithCode, ignoreCase } = this.props;
let newCodeArr = _.clone(this.state.codeArr);
newCodeArr[index] = character;

if (index == codeLength - 1) {
const code = newCodeArr.join('');

if (compareWithCode) {
const isMatching = this._isMatchingCode(code, compareWithCode, ignoreCase);
onFulfill(isMatching, code);
Expand All @@ -220,15 +225,15 @@ export default class ConfirmationCodeInput extends Component {
} else {
this._setFocus(this.state.currentIndex + 1);
}

this.setState(prevState => {
return {
codeArr: newCodeArr,
currentIndex: prevState.currentIndex + 1
};
});
}

render() {
const {
codeLength,
Expand All @@ -240,12 +245,12 @@ export default class ConfirmationCodeInput extends Component {
size,
activeColor
} = this.props;

const initialCodeInputStyle = {
width: size,
height: size
};

let codeInputs = [];
for (let i = 0; i < codeLength; i++) {
const id = i;
Expand All @@ -254,8 +259,8 @@ export default class ConfirmationCodeInput extends Component {
key={id}
ref={ref => (this.codeInputRefs[id] = ref)}
style={[
styles.codeInput,
initialCodeInputStyle,
styles.codeInput,
initialCodeInputStyle,
this._getClassStyle(className, this.state.currentIndex == id),
codeInputStyle
]}
Expand All @@ -273,7 +278,7 @@ export default class ConfirmationCodeInput extends Component {
/>
)
}

return (
<View style={[styles.container, this._getContainerStyle(size, inputPosition), containerStyle]}>
{codeInputs}
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ declare module "react-native-confirmation-code-input" {
codeInputStyle?: any,
containerStyle?: any;
onFulfill: Function;
getCurrentCode?: Function;
}

export default class CodeInput extends React.Component<CodeInputProps, any> { }
Expand Down