Added list of reusable textfield components
This commit is contained in:
		
							parent
							
								
									2b6bcbd2b8
								
							
						
					
					
						commit
						9bb48a63fa
					
				
					 1 changed files with 94 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -0,0 +1,94 @@
 | 
			
		|||
package be.ugent.sel.studeez.common.composable
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.StringRes
 | 
			
		||||
import androidx.compose.foundation.text.KeyboardOptions
 | 
			
		||||
import androidx.compose.material.Icon
 | 
			
		||||
import androidx.compose.material.IconButton
 | 
			
		||||
import androidx.compose.material.OutlinedTextField
 | 
			
		||||
import androidx.compose.material.Text
 | 
			
		||||
import androidx.compose.material.icons.Icons
 | 
			
		||||
import androidx.compose.material.icons.filled.Email
 | 
			
		||||
import androidx.compose.material.icons.filled.Lock
 | 
			
		||||
import androidx.compose.runtime.*
 | 
			
		||||
import be.ugent.sel.studeez.R.string as AppText
 | 
			
		||||
import be.ugent.sel.studeez.R.drawable as AppIcon
 | 
			
		||||
import androidx.compose.ui.Modifier
 | 
			
		||||
import androidx.compose.ui.res.painterResource
 | 
			
		||||
import androidx.compose.ui.res.stringResource
 | 
			
		||||
import androidx.compose.ui.text.input.KeyboardType
 | 
			
		||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
 | 
			
		||||
import androidx.compose.ui.text.input.VisualTransformation
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun BasicField(
 | 
			
		||||
    @StringRes text: Int,
 | 
			
		||||
    value: String,
 | 
			
		||||
    onNewValue: (String) -> Unit,
 | 
			
		||||
    modifier: Modifier = Modifier
 | 
			
		||||
) {
 | 
			
		||||
    OutlinedTextField(
 | 
			
		||||
        singleLine = true,
 | 
			
		||||
        modifier = modifier,
 | 
			
		||||
        value = value,
 | 
			
		||||
        onValueChange = { onNewValue(it) },
 | 
			
		||||
        placeholder = { Text(stringResource(text)) }
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun EmailField(value: String, onNewValue: (String) -> Unit, modifier: Modifier = Modifier) {
 | 
			
		||||
    OutlinedTextField(
 | 
			
		||||
        singleLine = true,
 | 
			
		||||
        modifier = modifier,
 | 
			
		||||
        value = value,
 | 
			
		||||
        onValueChange = { onNewValue(it) },
 | 
			
		||||
        placeholder = { Text(stringResource(AppText.email)) },
 | 
			
		||||
        leadingIcon = { Icon(imageVector = Icons.Default.Email, contentDescription = "Email") }
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun PasswordField(value: String, onNewValue: (String) -> Unit, modifier: Modifier = Modifier) {
 | 
			
		||||
    PasswordField(value, AppText.password, onNewValue, modifier)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
fun RepeatPasswordField(
 | 
			
		||||
    value: String,
 | 
			
		||||
    onNewValue: (String) -> Unit,
 | 
			
		||||
    modifier: Modifier = Modifier
 | 
			
		||||
) {
 | 
			
		||||
    PasswordField(value, AppText.repeat_password, onNewValue, modifier)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Composable
 | 
			
		||||
private fun PasswordField(
 | 
			
		||||
    value: String,
 | 
			
		||||
    @StringRes placeholder: Int,
 | 
			
		||||
    onNewValue: (String) -> Unit,
 | 
			
		||||
    modifier: Modifier = Modifier
 | 
			
		||||
) {
 | 
			
		||||
    var isVisible by remember { mutableStateOf(false) }
 | 
			
		||||
 | 
			
		||||
    val icon =
 | 
			
		||||
        if (isVisible) painterResource(AppIcon.ic_visibility_on)
 | 
			
		||||
        else painterResource(AppIcon.ic_visibility_off)
 | 
			
		||||
 | 
			
		||||
    val visualTransformation =
 | 
			
		||||
        if (isVisible) VisualTransformation.None else PasswordVisualTransformation()
 | 
			
		||||
 | 
			
		||||
    OutlinedTextField(
 | 
			
		||||
        modifier = modifier,
 | 
			
		||||
        value = value,
 | 
			
		||||
        onValueChange = { onNewValue(it) },
 | 
			
		||||
        placeholder = { Text(text = stringResource(placeholder)) },
 | 
			
		||||
        leadingIcon = { Icon(imageVector = Icons.Default.Lock, contentDescription = "Lock") },
 | 
			
		||||
        trailingIcon = {
 | 
			
		||||
            IconButton(onClick = { isVisible = !isVisible }) {
 | 
			
		||||
                Icon(painter = icon, contentDescription = "Visibility")
 | 
			
		||||
            }
 | 
			
		||||
        },
 | 
			
		||||
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
 | 
			
		||||
        visualTransformation = visualTransformation
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
		Reference in a new issue