Skip to content

Instantly share code, notes, and snippets.

@FannyDemey
Last active September 14, 2022 17:58
Show Gist options
  • Save FannyDemey/ae9087e17aa8e021cc1dd94e4d379def to your computer and use it in GitHub Desktop.
Save FannyDemey/ae9087e17aa8e021cc1dd94e4d379def to your computer and use it in GitHub Desktop.
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun Password(
password: MutableState<String>,
modifier: Modifier = Modifier
) {
val keyboardController = LocalSoftwareKeyboardController.current
var isValid by remember {
mutableStateOf(false)
}
Column {
OutlinedTextField(
modifier = modifier.fillMaxWidth(),
label = {
Text("Mot de passe")
},
trailingIcon = {
IconButton(onClick = {
isValid = !isValid
}) {
Icon(
if (isValid) {
Icons.Default.Visibility
} else {
Icons.Default.VisibilityOff
},
contentDescription = if (isValid) {
"Hide password"
} else {
"Show password"
}
)
}
},
value = password.value,
onValueChange = {
password.value = it
isValid = checkPasswordValidity(it)
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Password,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(onNext = {
keyboardController?.hide()
if (!isValid) {
// TODO announce for accessibility password is invalid something like :
// announceForAccessibility("Password is invalid. Min 6 characters are needed.")
}
}),
visualTransformation = if (isValid) {
VisualTransformation.None
} else {
PasswordVisualTransformation()
}
)
if(!isValid){
Text("Password is invalid. Min 6 characters are needed.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment