From 9969a980b9f6476a089c178d8351a504d912f7d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=CC=A7ag=CC=86das=CC=A7=20Kurultay=20Kalkan?= Date: Mon, 19 Nov 2018 05:39:46 +0300 Subject: [PATCH] Add focus feature and ReturnKey Done Option --- lib/index.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index e3e464e4..f9d47a67 100644 --- a/lib/index.js +++ b/lib/index.js @@ -51,13 +51,15 @@ export default class PhoneInput extends Component { this.setState({ value }); this.updateFlagAndFormatNumber(value); } + + nextProps.focus && this.focus(); } onChangePhoneNumber(number) { const actionAfterSetState = this.props.onChangePhoneNumber ? () => { - this.props.onChangePhoneNumber(number); - } + this.props.onChangePhoneNumber(number); + } : null; this.updateFlagAndFormatNumber(number, actionAfterSetState); } @@ -137,6 +139,12 @@ export default class PhoneInput extends Component { ); } + format(text) { + return this.props.autoFormat + ? PhoneNumber.format(text, this.state.iso2) + : text; + } + updateFlagAndFormatNumber(number, actionAfterSetState = null) { const { allowZeroAfterCountryCode, initialCountry } = this.props; let iso2 = initialCountry; @@ -148,8 +156,10 @@ export default class PhoneInput extends Component { ? phoneNumber : this.possiblyEliminateZeroAfterCountryCode(phoneNumber); iso2 = PhoneNumber.getCountryCodeOfNumber(phoneNumber); + } - this.setState({ iso2, formattedNumber: phoneNumber }, actionAfterSetState); + + this.setState({ iso2, formattedNumber: this.format(phoneNumber) }, actionAfterSetState); } possiblyEliminateZeroAfterCountryCode(number) { @@ -163,8 +173,10 @@ export default class PhoneInput extends Component { this.inputPhone.focus(); } + render() { const { iso2, formattedNumber, disabled } = this.state; + const { focus, returnKey } = this.props; const TextComponent = this.props.textComponent || TextInput; return ( @@ -189,6 +201,8 @@ export default class PhoneInput extends Component { onChangeText={text => { this.onChangePhoneNumber(text); }} + autoFocus={focus} + returnKeyType={returnKey ? 'done' : null} keyboardType="phone-pad" underlineColorAndroid="rgba(0,0,0,0)" value={formattedNumber}