diff --git a/src/ReactTelephoneInput.js b/src/ReactTelephoneInput.js index 64ec4184..ec0c7371 100644 --- a/src/ReactTelephoneInput.js +++ b/src/ReactTelephoneInput.js @@ -232,7 +232,7 @@ export class ReactTelephoneInput extends Component { newSelectedCountry = this.guessSelectedCountry(inputNumber.substring(0, 6)) freezeSelection = false } - formattedNumber = formatNumber(inputNumber, newSelectedCountry.format, this.props.autoFormat) + formattedNumber = formatNumber(inputNumber, newSelectedCountry.format, this.props.autoFormat, this.props.numberLength) } let caretPosition = event.target.selectionStart @@ -290,7 +290,8 @@ export class ReactTelephoneInput extends Component { const formattedNumber = formatNumber( newNumber, nextSelectedCountry.format, - this.props.autoFormat + this.props.autoFormat, + this.props.numberLength ) this.setState( @@ -345,7 +346,8 @@ export class ReactTelephoneInput extends Component { const formattedNumber = formatNumber( inputNumber.replace(/\D/g, ''), selectedCountryGuess ? selectedCountryGuess.format : null, - this.props.autoFormat + this.props.autoFormat, + this.props.numberLength ) return { @@ -623,6 +625,7 @@ ReactTelephoneInput.propTypes = { value: PropTypes.string, initialValue: PropTypes.string, autoFormat: PropTypes.bool, + numberLength: PropTypes.number, defaultCountry: PropTypes.string, isValid: PropTypes.func, onlyCountries: PropTypes.arrayOf(PropTypes.object), diff --git a/src/format_number.js b/src/format_number.js index 91da5b04..0a8d23a0 100644 --- a/src/format_number.js +++ b/src/format_number.js @@ -2,7 +2,7 @@ import R from 'cramda' const { first, tail } = R -function formatNumber(text, pattern, autoFormat) { +function formatNumber(text, pattern, autoFormat, length) { if (!text || text.length === 0) { return '+' } @@ -13,6 +13,14 @@ function formatNumber(text, pattern, autoFormat) { return `+${text}` } + if (length === null) { + length = text.length; + } + + if (text.length > length) { + text = text.substring(0, length-1); + } + const formattedObject = pattern.split('').reduce( (acc, character) => { if (acc.remainingText.length === 0) {