Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onChangeCode callback and defaultCode props added. Readme updated. #33

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ Prop | Type | Default | Description
`inactiveColor` | string | `rgba(255, 255, 255, 0.2)` | color of cells when inactive
`ignoreCase` | boolean | `false` | ignore case when checking code
`autoFocus` | boolean | `true` | auto focus on code input
`defaultCode` | string | | initial value for code inputs. Set `autoFocus` to `false` when using `defaultCode`
`codeInputStyle` | style object | | custom style for code input
`containerStyle` | style object | | custom style for code input container
`onChangeCode` | function | | callback function called when any of the input value is changed. Always returns last `(code)` in callback.
`onFulfill` | function | | callback function called when fulfilling code. If `compareWithCode` is null -> return `(code)` in callback, else return `(isValid, code)`. **Required**

## functions
Expand Down
38 changes: 32 additions & 6 deletions components/ConfirmationCodeInput.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import { View, TextInput, StyleSheet, Dimensions } from 'react-native';
import { View, TextInput, StyleSheet, Dimensions, ViewPropTypes } from 'react-native';
import _ from 'lodash';

// if ViewPropTypes is not defined fall back to View.propType (to support RN < 0.44)
const viewPropTypes = ViewPropTypes || View.propTypes;

export default class ConfirmationCodeInput extends Component {
static propTypes = {
codeLength: PropTypes.number,
Expand All @@ -17,8 +20,10 @@ export default class ConfirmationCodeInput extends Component {
ignoreCase: PropTypes.bool,
autoFocus: PropTypes.bool,
codeInputStyle: TextInput.propTypes.style,
containerStyle: View.propTypes.style,
containerStyle: viewPropTypes.style,
onFulfill: PropTypes.func,
onChangeCode: PropTypes.func,
defaultCode: PropTypes.string,
};

static defaultProps = {
Expand All @@ -32,7 +37,8 @@ export default class ConfirmationCodeInput extends Component {
inactiveColor: 'rgba(255, 255, 255, 0.2)',
space: 8,
compareWithCode: '',
ignoreCase: false
ignoreCase: false,
defaultCode: '',
};

constructor(props) {
Expand All @@ -47,14 +53,23 @@ export default class ConfirmationCodeInput extends Component {
}

componentDidMount() {
const { compareWithCode, codeLength, inputPosition } = this.props;
const { compareWithCode, codeLength, inputPosition, defaultCode } = 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');
}

if (defaultCode && defaultCode.length !== codeLength) {
console.error("Invalid props: defaultCode length is not equal to codeLength");
}
else {
this.setState({
codeArr: _.split(defaultCode, '')
});
}
}

clear() {
Expand All @@ -74,6 +89,8 @@ export default class ConfirmationCodeInput extends Component {
}

_onFocus(index) {
const { onChangeCode } = this.props;

let newCodeArr = _.clone(this.state.codeArr);
const currentEmptyIndex = _.findIndex(newCodeArr, c => !c);
if (currentEmptyIndex !== -1 && currentEmptyIndex < index) {
Expand All @@ -84,6 +101,10 @@ export default class ConfirmationCodeInput extends Component {
newCodeArr[i] = '';
}
}

if (onChangeCode) {
onChangeCode(newCodeArr.join(''));
}

this.setState({
codeArr: newCodeArr,
Expand Down Expand Up @@ -199,7 +220,7 @@ export default class ConfirmationCodeInput extends Component {
}

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

Expand All @@ -214,6 +235,11 @@ export default class ConfirmationCodeInput extends Component {
onFulfill(code);
}
this._blur(this.state.currentIndex);

if (onChangeCode) {
onChangeCode(code);
}

} else {
this._setFocus(this.state.currentIndex + 1);
}
Expand Down Expand Up @@ -290,4 +316,4 @@ const styles = StyleSheet.create({
textAlign: 'center',
padding: 0
}
});
});
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ declare module "react-native-confirmation-code-input" {
activeColor?: string;
inactiveColor?: string;
ignoreCase?: boolean;
codeInputStyle?: any,
codeInputStyle?: any;
containerStyle?: any;
onFulfill: Function;
onChangeCode?: Function;
defaultCode?: string;
}

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