added error inputfield
This commit is contained in:
		
							parent
							
								
									7934154af9
								
							
						
					
					
						commit
						0b65c44da1
					
				
					 1 changed files with 93 additions and 4 deletions
				
			
		|  | @ -1,11 +1,11 @@ | ||||||
| package be.ugent.sel.studeez.common.composable | package be.ugent.sel.studeez.common.composable | ||||||
| 
 | 
 | ||||||
| import androidx.annotation.StringRes | import androidx.annotation.StringRes | ||||||
|  | import androidx.compose.foundation.layout.Column | ||||||
|  | import androidx.compose.foundation.layout.padding | ||||||
|  | import androidx.compose.foundation.text.KeyboardActions | ||||||
| import androidx.compose.foundation.text.KeyboardOptions | import androidx.compose.foundation.text.KeyboardOptions | ||||||
| import androidx.compose.material.Icon | import androidx.compose.material.* | ||||||
| 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.Icons | ||||||
| import androidx.compose.material.icons.filled.Email | import androidx.compose.material.icons.filled.Email | ||||||
| import androidx.compose.material.icons.filled.Lock | import androidx.compose.material.icons.filled.Lock | ||||||
|  | @ -14,10 +14,15 @@ import androidx.compose.runtime.* | ||||||
| import androidx.compose.ui.Modifier | import androidx.compose.ui.Modifier | ||||||
| import androidx.compose.ui.res.painterResource | import androidx.compose.ui.res.painterResource | ||||||
| import androidx.compose.ui.res.stringResource | import androidx.compose.ui.res.stringResource | ||||||
|  | import androidx.compose.ui.text.input.ImeAction | ||||||
| import androidx.compose.ui.text.input.KeyboardType | import androidx.compose.ui.text.input.KeyboardType | ||||||
| import androidx.compose.ui.text.input.PasswordVisualTransformation | import androidx.compose.ui.text.input.PasswordVisualTransformation | ||||||
| import androidx.compose.ui.text.input.VisualTransformation | import androidx.compose.ui.text.input.VisualTransformation | ||||||
|  | import androidx.compose.ui.tooling.preview.Preview | ||||||
|  | import androidx.compose.ui.unit.dp | ||||||
| import be.ugent.sel.studeez.common.ext.fieldModifier | import be.ugent.sel.studeez.common.ext.fieldModifier | ||||||
|  | import be.ugent.sel.studeez.resources | ||||||
|  | import kotlin.math.sin | ||||||
| import be.ugent.sel.studeez.R.drawable as AppIcon | import be.ugent.sel.studeez.R.drawable as AppIcon | ||||||
| import be.ugent.sel.studeez.R.string as AppText | import be.ugent.sel.studeez.R.string as AppText | ||||||
| 
 | 
 | ||||||
|  | @ -85,6 +90,90 @@ fun EmailField( | ||||||
|     ) |     ) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @Composable | ||||||
|  | fun LabeledNumberInputField( | ||||||
|  |     value: Int, | ||||||
|  |     onNewValue: (Int) -> Unit, | ||||||
|  |     @StringRes label: Int, | ||||||
|  |     singleLine: Boolean = false | ||||||
|  | ) { | ||||||
|  |     var number by remember { mutableStateOf(value) } | ||||||
|  |     OutlinedTextField( | ||||||
|  |         value = number.toString(), | ||||||
|  |         singleLine = singleLine, | ||||||
|  |         label = { Text(resources().getString(label)) }, | ||||||
|  |         keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), | ||||||
|  |         onValueChange = {typedInt -> | ||||||
|  |             val isNumber = typedInt.matches(Regex("[1-9]+\\d*]")) | ||||||
|  |             if (isNumber) { | ||||||
|  |                 number = typedInt.toInt() | ||||||
|  |                 onNewValue(typedInt.toInt()) | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     ) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | @Composable | ||||||
|  | fun LabeledErrorTextField( | ||||||
|  |     modifier: Modifier = Modifier, | ||||||
|  |     initialValue: String, | ||||||
|  |     @StringRes label: Int, | ||||||
|  |     singleLine: Boolean = false, | ||||||
|  |     errorText: String, | ||||||
|  |     keyboardType: KeyboardType, | ||||||
|  |     predicate: (String) -> Boolean, | ||||||
|  |     onNewCorrectValue: (String) -> Unit | ||||||
|  | ) { | ||||||
|  |     var value by remember { | ||||||
|  |         mutableStateOf(initialValue) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     var isValid by remember { | ||||||
|  |         mutableStateOf(predicate(value)) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     Column { | ||||||
|  |         OutlinedTextField( | ||||||
|  |             modifier = modifier.fieldModifier(), | ||||||
|  |             value = value, | ||||||
|  |             onValueChange = { newText -> | ||||||
|  |                 value = newText | ||||||
|  |                 isValid = predicate(value) | ||||||
|  |                 if (isValid) { | ||||||
|  |                     onNewCorrectValue(newText) | ||||||
|  |                 } | ||||||
|  |             }, | ||||||
|  |             singleLine = singleLine, | ||||||
|  |             label = { Text(text = resources().getString(label)) }, | ||||||
|  |             isError = !isValid, | ||||||
|  |             keyboardOptions = KeyboardOptions( | ||||||
|  |                 keyboardType = keyboardType, | ||||||
|  |                 imeAction = ImeAction.Done | ||||||
|  |             ) | ||||||
|  |         ) | ||||||
|  | 
 | ||||||
|  |         if (!isValid) { | ||||||
|  |             Text( | ||||||
|  |                 modifier = Modifier.padding(start = 16.dp), | ||||||
|  |                 text = errorText, | ||||||
|  |                 color = MaterialTheme.colors.error | ||||||
|  |             ) | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fun isNumber(string: String): Boolean { | ||||||
|  |     return string.matches(Regex("[1-9]+\\d*")) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |  @Preview(showBackground = true) | ||||||
|  |  @Composable | ||||||
|  |  fun IntInputPreview() { | ||||||
|  |      LabeledNumberInputField(value = 1, onNewValue = {}, label = AppText.email) | ||||||
|  |  } | ||||||
|  | 
 | ||||||
| @Composable | @Composable | ||||||
| fun PasswordField( | fun PasswordField( | ||||||
|     value: String, |     value: String, | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Lukas Barragan Torres
						Lukas Barragan Torres