Disable enter-to-submit for invalid phone numbers in regV5.

This commit is contained in:
Greyson Parrelli
2026-07-07 17:55:10 +00:00
parent cf394aeee8
commit e687718ec3
2 changed files with 67 additions and 1 deletions
@@ -256,6 +256,7 @@ private fun OnePaneLayout(
hasValidCountry = state.countryName.isNotEmpty(),
countryCode = state.countryCode,
formattedNumber = state.formattedNumber,
canSubmit = !state.showSpinner && state.isNumberPossible,
onCountryCodeChanged = { onEvent(PhoneNumberEntryScreenEvents.CountryCodeChanged(it)) },
onPhoneNumberChanged = { onEvent(PhoneNumberEntryScreenEvents.NationalNumberChanged(it)) },
onPhoneNumberSubmitted = { onEvent(PhoneNumberEntryScreenEvents.NextClicked) },
@@ -324,6 +325,7 @@ private fun TwoPaneLayout(
hasValidCountry = state.countryName.isNotEmpty(),
countryCode = state.countryCode,
formattedNumber = state.formattedNumber,
canSubmit = !state.showSpinner && state.isNumberPossible,
onCountryCodeChanged = { onEvent(PhoneNumberEntryScreenEvents.CountryCodeChanged(it)) },
onPhoneNumberChanged = { onEvent(PhoneNumberEntryScreenEvents.NationalNumberChanged(it)) },
onPhoneNumberSubmitted = { onEvent(PhoneNumberEntryScreenEvents.NextClicked) },
@@ -499,6 +501,7 @@ private fun PhoneNumberInputFields(
hasValidCountry: Boolean,
countryCode: String,
formattedNumber: String,
canSubmit: Boolean,
onCountryCodeChanged: (String) -> Unit,
onPhoneNumberChanged: (String) -> Unit,
onPhoneNumberSubmitted: () -> Unit,
@@ -590,7 +593,11 @@ private fun PhoneNumberInputFields(
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(
onDone = { onPhoneNumberSubmitted() }
onDone = {
if (canSubmit) {
onPhoneNumberSubmitted()
}
}
),
singleLine = true,
textStyle = MaterialTheme.typography.bodyLarge.copy(
@@ -11,6 +11,7 @@ import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performImeAction
import androidx.test.core.app.ApplicationProvider
import org.junit.Rule
import org.junit.Test
@@ -99,6 +100,64 @@ class PhoneNumberScreenTest {
}
}
@Test
fun `pressing done does not emit NextClicked when number is not valid`() {
// Given
var emittedEvent: PhoneNumberEntryScreenEvents? = null
composeTestRule.setContent {
SignalTheme {
PhoneNumberScreen(
state = PhoneNumberEntryState(
countryCode = "1",
nationalNumber = "555",
formattedNumber = "555"
),
onEvent = { event ->
emittedEvent = event
}
)
}
}
// When - press the IME done action on the phone number field
composeTestRule.onNodeWithTag(TestTags.PHONE_NUMBER_PHONE_FIELD).performImeAction()
// Then
assert(emittedEvent !is PhoneNumberEntryScreenEvents.NextClicked) {
"Expected no NextClicked event for an invalid number but got $emittedEvent"
}
}
@Test
fun `pressing done emits NextClicked when number is valid`() {
// Given
var emittedEvent: PhoneNumberEntryScreenEvents? = null
composeTestRule.setContent {
SignalTheme {
PhoneNumberScreen(
state = PhoneNumberEntryState(
countryCode = "1",
nationalNumber = "5551234567",
formattedNumber = "(555) 123-4567"
),
onEvent = { event ->
emittedEvent = event
}
)
}
}
// When - press the IME done action on the phone number field
composeTestRule.onNodeWithTag(TestTags.PHONE_NUMBER_PHONE_FIELD).performImeAction()
// Then
assert(emittedEvent is PhoneNumberEntryScreenEvents.NextClicked) {
"Expected NextClicked event for a valid number but got $emittedEvent"
}
}
@Test
fun `clicking country picker emits CountryPicker event`() {
// Given