From 80f83da63d87c8014666a1a489f9e0ca11f37eb3 Mon Sep 17 00:00:00 2001 From: Chen Cen <79880926+ccen-stripe@users.noreply.github.com> Date: Wed, 24 May 2023 09:00:50 -0700 Subject: [PATCH] [stripe-ui-core] Fix phone number length (#6771) --- CHANGELOG.md | 2 ++ .../uicore/elements/PhoneNumberElementUI.kt | 10 ++++++- .../uicore/elements/PhoneNumberFormatter.kt | 4 +-- .../elements/PhoneNumberFormatterTest.kt | 27 +++++++++++++++---- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81672e18b57..2f8710c12b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # CHANGELOG ## XX.XX.XX - 2023-XX-XX +### All SDKs +* [FIXED][6771](https://github.com/stripe/stripe-android/pull/6771) Fixed the length of phone number field. ## 20.25.3 - 2023-05-23 diff --git a/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/PhoneNumberElementUI.kt b/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/PhoneNumberElementUI.kt index 7e94e110b7c..00b347baacb 100644 --- a/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/PhoneNumberElementUI.kt +++ b/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/PhoneNumberElementUI.kt @@ -28,6 +28,7 @@ import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.focus.onFocusEvent import androidx.compose.ui.platform.LocalFocusManager +import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardType @@ -39,6 +40,9 @@ import kotlinx.coroutines.job import kotlinx.coroutines.launch import com.stripe.android.core.R as CoreR +@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) +const val PHONE_NUMBER_TEXT_FIELD_TAG = "PhoneNumberTextField" + @Preview @Composable private fun PhoneNumberCollectionPreview() { @@ -114,7 +118,8 @@ fun PhoneNumberElementUI( controller.onFocusChange(it.isFocused) } hasFocus = it.isFocused - }, + } + .testTag(PHONE_NUMBER_TEXT_FIELD_TAG), enabled = enabled, label = { FormLabel( @@ -163,3 +168,6 @@ fun PhoneNumberElementUI( } } } + +@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) +const val PHONE_NUMBER_FIELD_TAG = "phone_number" diff --git a/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/PhoneNumberFormatter.kt b/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/PhoneNumberFormatter.kt index 5693d4c31f3..f26da8e1a90 100644 --- a/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/PhoneNumberFormatter.kt +++ b/stripe-ui-core/src/main/java/com/stripe/android/uicore/elements/PhoneNumberFormatter.kt @@ -55,9 +55,7 @@ internal sealed class PhoneNumberFormatter { override val placeholder = metadata.pattern.replace('#', '5') override val countryCode = metadata.regionCode - // Maximum number of digits for the subscriber number for this region. - private val maxSubscriberDigits = E164_MAX_DIGITS - - (prefix.length - 1) // prefix minus the '+' + private val maxSubscriberDigits = metadata.pattern.count { it == '#' } override fun userInputFilter(input: String) = input.filter { VALID_INPUT_RANGE.contains(it) }.run { diff --git a/stripe-ui-core/src/test/java/com/stripe/android/uicore/elements/PhoneNumberFormatterTest.kt b/stripe-ui-core/src/test/java/com/stripe/android/uicore/elements/PhoneNumberFormatterTest.kt index 72708364d1f..fdca6b79877 100644 --- a/stripe-ui-core/src/test/java/com/stripe/android/uicore/elements/PhoneNumberFormatterTest.kt +++ b/stripe-ui-core/src/test/java/com/stripe/android/uicore/elements/PhoneNumberFormatterTest.kt @@ -8,7 +8,7 @@ internal class PhoneNumberFormatterTest { @Test fun `Phone number is correctly formatted for US locale`() { - val formatter = PhoneNumberFormatter.forCountry("US") + val formatter = PhoneNumberFormatter.forCountry("US") // "(###) ###-####" assertThat(formatter.format("123")).isEqualTo("(123") assertThat(formatter.format("1234")).isEqualTo("(123) 4") @@ -17,21 +17,21 @@ internal class PhoneNumberFormatterTest { assertThat(formatter.format("1234567890")).isEqualTo("(123) 456-7890") // prefix has 1 digit so full number must be at most 14 digits assertThat(formatter.format("12345asdfg678901234567890")) - .isEqualTo("(123) 456-7890 1234") + .isEqualTo("(123) 456-7890") } @Test fun `Phone number is correctly formatted for FI locale`() { - val formatter = PhoneNumberFormatter.forCountry("FI") + val formatter = PhoneNumberFormatter.forCountry("FI") // "## ### ## ##" assertThat(formatter.format("123")).isEqualTo("12 3") assertThat(formatter.format("1234")).isEqualTo("12 34") assertThat(formatter.format("123456")).isEqualTo("12 345 6") assertThat(formatter.format("1234567")).isEqualTo("12 345 67") - assertThat(formatter.format("1234567890")).isEqualTo("12 345 67 89 0") + assertThat(formatter.format("1234567890")).isEqualTo("12 345 67 89") // prefix has 3 digits so full number must be at most 12 digits assertThat(formatter.format("12345asdfg678901234567890")) - .isEqualTo("12 345 67 89 012") + .isEqualTo("12 345 67 89") } @Test @@ -44,6 +44,23 @@ internal class PhoneNumberFormatterTest { .isEqualTo("+123456789012345") } + @Test + fun `WithRegion correctly formats with pattern`() { + val pattern = "(###)-###+#####!#" + val formatter = PhoneNumberFormatter.WithRegion( + PhoneNumberFormatter.Metadata( + "prefix", + "regionCode", + pattern + ) + ) + + assertThat(formatter.format("123")).isEqualTo("(123") + assertThat(formatter.format("1234567")).isEqualTo("(123)-456+7") + assertThat(formatter.format("123456789012")).isEqualTo("(123)-456+78901!2") + assertThat(formatter.format("123456789012456")).isEqualTo("(123)-456+78901!2") + } + private fun PhoneNumberFormatter.format(input: String) = visualTransformation.filter(AnnotatedString(userInputFilter(input))).text.text }