Skip to content

Commit

Permalink
feat(common): add PasswordField
Browse files Browse the repository at this point in the history
  • Loading branch information
msfjarvis committed Nov 17, 2023
1 parent fb87882 commit 89fce20
Showing 1 changed file with 66 additions and 0 deletions.
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 },
)
}
}

0 comments on commit 89fce20

Please sign in to comment.