Skip to content

Instantly share code, notes, and snippets.

@valokafor
Created December 11, 2023 17:43
Show Gist options
  • Save valokafor/b5f8acf1169827e7c88cc813940a384e to your computer and use it in GitHub Desktop.
Save valokafor/b5f8acf1169827e7c88cc813940a384e to your computer and use it in GitHub Desktop.
CustomOutlinedText Field
//How to use
CustomOutlinedText(
textValue = uiState.email,
testTag = "username_field",
hintTextId = R.string.hint_enter_email,
labelTextId = R.string.hint_email,
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Email,
imeAction = ImeAction.Next
),
onValueChanged = viewModel::onEmailChange
)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CustomOutlinedText(
modifier: Modifier = Modifier.fillMaxWidth(),
enabled: Boolean = true,
testTag: String = "",
textValue: String,
hintTextId: Int ? = null,
labelTextId: Int,
maxLines: Int = Int.MAX_VALUE,
keyboardOptions: KeyboardOptions,
keyboardActions: KeyboardActions = KeyboardActions.Default,
visualTransformation: VisualTransformation = VisualTransformation.None,
leadingIcon: @Composable (() -> Unit)? = null,
trailingIcon: @Composable (() -> Unit)? = null,
onValueChanged: (String) -> Unit
) {
OutlinedTextField(
value = textValue,
onValueChange = onValueChanged,
shape = RoundedCornerShape(4.dp),
maxLines = maxLines,
colors = TextFieldDefaults.outlinedTextFieldColors(
focusedBorderColor = MaterialTheme.colorScheme.primary,
unfocusedBorderColor = MaterialTheme.colorScheme.outline
),
modifier = modifier.testTag(testTag),
enabled = enabled,
textStyle = TextStyle(color = MaterialTheme.colorScheme.onSurface),
label = {
Text(
text = stringResource(id =labelTextId),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.primary
)
},
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
visualTransformation = visualTransformation,
placeholder = {
hintTextId?.let {
Text(
text = stringResource(id = it),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurface
)
}
},
trailingIcon = trailingIcon,
leadingIcon = leadingIcon)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment