TextFieldData
Represents the state of a text input field with optional validation error.
This data class is designed to work within the UiState wrapper pattern, providing a clean way to manage text field state and validation errors. It's commonly used in screen data classes to represent form inputs.
Illustrative screen data (not an existing class)
data class LoginScreenData(
val email: TextFieldData = TextFieldData(""),
val password: TextFieldData = TextFieldData("")
)Content copied to clipboard
Illustrative ViewModel excerpt (imports and repository omitted)
@HiltViewModel
class LoginViewModel @Inject constructor(
private val authRepository: AuthRepository
) : ViewModel() {
private val _uiState = MutableStateFlow(UiState(LoginScreenData()))
val uiState = _uiState.asStateFlow()
fun updateEmail(email: String) {
_uiState.updateState {
copy(email = TextFieldData(email, validateEmail(email)))
}
}
private fun validateEmail(email: String): String? {
return when {
email.isBlank() -> "Email cannot be empty"
!email.isEmailValid() -> "Invalid email format"
else -> null
}
}
}Content copied to clipboard
Illustrative composable excerpt (imports omitted)
@Composable
fun LoginScreen(
screenData: LoginScreenData,
onEmailChange: (String) -> Unit
) {
JetpackTextField(
value = screenData.email.value,
onValueChange = onEmailChange,
label = { Text("Email") },
errorMessage = screenData.email.errorMessage,
)
}Content copied to clipboard
UiText is available for localized UI text, but this class currently stores String? errors. Changing that type requires updating the form state and rendering calls together; the form guide explains the current validation and localization boundaries.