-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
common/src/main/kotlin/dev/msfjarvis/claw/common/ui/PasswordField.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,66 @@ | ||
/* | ||
* Copyright © 2023 Harsh Shandilya. | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
*/ | ||
package dev.msfjarvis.claw.common.ui | ||
|
||
import androidx.compose.foundation.clickable | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Visibility | ||
import androidx.compose.material.icons.filled.VisibilityOff | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.vector.rememberVectorPainter | ||
import androidx.compose.ui.text.input.PasswordVisualTransformation | ||
import androidx.compose.ui.text.input.VisualTransformation | ||
import dev.msfjarvis.claw.common.theme.LobstersTheme | ||
import dev.msfjarvis.claw.common.ui.preview.ThemePreviews | ||
|
||
@Composable | ||
fun PasswordField( | ||
value: String, | ||
label: String, | ||
onValueChange: (String) -> Unit, | ||
modifier: Modifier = Modifier, | ||
) { | ||
var isPasswordVisible by remember { mutableStateOf(false) } | ||
TextField( | ||
value = value, | ||
onValueChange = onValueChange, | ||
label = { Text(label) }, | ||
visualTransformation = | ||
if (isPasswordVisible) VisualTransformation.None else PasswordVisualTransformation(), | ||
trailingIcon = { | ||
Icon( | ||
painter = | ||
if (isPasswordVisible) rememberVectorPainter(Icons.Filled.VisibilityOff) | ||
else rememberVectorPainter(Icons.Filled.Visibility), | ||
contentDescription = null, | ||
modifier = Modifier.clickable { isPasswordVisible = !isPasswordVisible } | ||
) | ||
}, | ||
modifier = modifier, | ||
) | ||
} | ||
|
||
@ThemePreviews | ||
@Composable | ||
private fun PasswordFieldPreview() { | ||
LobstersTheme { | ||
var value by remember { mutableStateOf("") } | ||
PasswordField( | ||
value = value, | ||
label = "Password", | ||
onValueChange = { value = it }, | ||
) | ||
} | ||
} |