Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8f7323
commit 06251fc
Showing
5 changed files
with
157 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
payments-ui-core/src/main/java/com/stripe/android/ui/core/elements/PostalCodeConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.stripe.android.ui.core.elements | ||
|
||
import androidx.annotation.StringRes | ||
import androidx.compose.ui.text.input.KeyboardCapitalization | ||
import androidx.compose.ui.text.input.KeyboardType | ||
import androidx.compose.ui.text.input.VisualTransformation | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
|
||
class PostalCodeConfig( | ||
@StringRes override val label: Int, | ||
override val capitalization: KeyboardCapitalization = KeyboardCapitalization.Words, | ||
override val keyboard: KeyboardType = KeyboardType.Text, | ||
override val trailingIcon: MutableStateFlow<TextFieldIcon?> = MutableStateFlow(null), | ||
country: String | ||
) : TextFieldConfig { | ||
override val debugLabel: String = "postal_code_text" | ||
override val visualTransformation: VisualTransformation? = null | ||
override val loading: MutableStateFlow<Boolean> = MutableStateFlow(false) | ||
private val format = CountryPostalFormat.forCountry(country) | ||
|
||
override fun determineState(input: String): TextFieldState = object : TextFieldState { | ||
override fun shouldShowError(hasFocus: Boolean) = false | ||
|
||
override fun isValid(): Boolean { | ||
return input.length in format.minimumLength..format.maximumLength && | ||
input.matches(format.regexPattern) | ||
} | ||
|
||
override fun getError(): FieldError? = null | ||
|
||
override fun isFull(): Boolean = input.length >= format.minimumLength | ||
|
||
override fun isBlank(): Boolean = input.isBlank() | ||
} | ||
|
||
override fun filter(userTyped: String): String = | ||
if ( | ||
setOf(KeyboardType.Number, KeyboardType.NumberPassword).contains(keyboard) | ||
) { | ||
userTyped.filter { it.isDigit() } | ||
} else { | ||
userTyped | ||
}.replace(Regex("\\s*"), "") | ||
|
||
override fun convertToRaw(displayName: String) = displayName | ||
|
||
override fun convertFromRaw(rawValue: String) = rawValue | ||
|
||
sealed class CountryPostalFormat( | ||
val minimumLength: Int, | ||
val maximumLength: Int, | ||
val regexPattern: Regex | ||
) { | ||
object CA : CountryPostalFormat( | ||
minimumLength = 6, | ||
maximumLength = 6, | ||
regexPattern = Regex("([a-zA-Z]\\d)+") | ||
) | ||
object US : CountryPostalFormat( | ||
minimumLength = 5, | ||
maximumLength = 9, | ||
regexPattern = Regex("\\d+") | ||
) | ||
object Other : CountryPostalFormat( | ||
minimumLength = 4, | ||
maximumLength = 12, | ||
regexPattern = Regex(".+") | ||
) | ||
|
||
companion object { | ||
fun forCountry(country: String): CountryPostalFormat { | ||
return when (country) { | ||
"US" -> US | ||
"CA" -> CA | ||
else -> Other | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
payments-ui-core/src/test/java/com/stripe/android/ui/core/elements/PostalCodeConfigTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.stripe.android.ui.core.elements | ||
|
||
import androidx.compose.ui.text.input.KeyboardType | ||
import com.google.common.truth.Truth | ||
import com.stripe.android.core.R | ||
import org.junit.Test | ||
|
||
class PostalCodeConfigTest { | ||
|
||
@Test | ||
fun `verify US postal codes`() { | ||
with(createConfigForCountry("US", KeyboardType.Number)) { | ||
Truth.assertThat(determineStateForInput("").isValid()).isFalse() | ||
Truth.assertThat(determineStateForInput("").isFull()).isFalse() | ||
Truth.assertThat(determineStateForInput("12345").isValid()).isTrue() | ||
Truth.assertThat(determineStateForInput("12345").isFull()).isTrue() | ||
Truth.assertThat(determineStateForInput("1234567890").isValid()).isFalse() | ||
Truth.assertThat(determineStateForInput("1234567890").isFull()).isFalse() | ||
Truth.assertThat(determineStateForInput("abcde").isValid()).isFalse() | ||
Truth.assertThat(determineStateForInput("abcde").isFull()).isFalse() | ||
} | ||
} | ||
|
||
@Test | ||
fun `verify CA postal codes`() { | ||
with(createConfigForCountry("CA")) { | ||
Truth.assertThat(determineStateForInput("").isValid()).isFalse() | ||
Truth.assertThat(determineStateForInput("").isFull()).isFalse() | ||
Truth.assertThat(determineStateForInput("AAA AAA").isValid()).isFalse() | ||
Truth.assertThat(determineStateForInput("AAAAAA").isValid()).isFalse() | ||
Truth.assertThat(determineStateForInput("A0A 0A0").isValid()).isTrue() | ||
Truth.assertThat(determineStateForInput("A0A 0A0").isFull()).isTrue() | ||
Truth.assertThat(determineStateForInput("A0A0A0").isValid()).isTrue() | ||
Truth.assertThat(determineStateForInput("A0A0A0").isFull()).isTrue() | ||
Truth.assertThat(determineStateForInput("A0A0A0A0").isValid()).isFalse() | ||
Truth.assertThat(determineStateForInput("A0A0A0A0").isFull()).isTrue() | ||
} | ||
} | ||
|
||
private fun createConfigForCountry( | ||
country: String, | ||
keyboardType: KeyboardType = KeyboardType.Text | ||
): PostalCodeConfig { | ||
return PostalCodeConfig( | ||
label = R.string.address_label_postal_code, | ||
country = country, | ||
keyboard = keyboardType | ||
) | ||
} | ||
|
||
private fun PostalCodeConfig.determineStateForInput(input: String): TextFieldState { | ||
return determineState(filter(input)) | ||
} | ||
} |