changed labeled error field to take some states
This commit is contained in:
		
							parent
							
								
									74d2920562
								
							
						
					
					
						commit
						d6496cb8ad
					
				
					 1 changed files with 8 additions and 11 deletions
				
			
		|  | @ -3,7 +3,6 @@ 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.Column | ||||||
| import androidx.compose.foundation.layout.padding | 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.* | import androidx.compose.material.* | ||||||
| import androidx.compose.material.icons.Icons | import androidx.compose.material.icons.Icons | ||||||
|  | @ -22,7 +21,6 @@ import androidx.compose.ui.tooling.preview.Preview | ||||||
| import androidx.compose.ui.unit.dp | 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 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 | ||||||
| 
 | 
 | ||||||
|  | @ -119,7 +117,9 @@ fun LabeledErrorTextField( | ||||||
|     initialValue: String, |     initialValue: String, | ||||||
|     @StringRes label: Int, |     @StringRes label: Int, | ||||||
|     singleLine: Boolean = false, |     singleLine: Boolean = false, | ||||||
|     errorText: Int, |     isValid: MutableState<Boolean>, | ||||||
|  |     isFirst: MutableState<Boolean> = remember { mutableStateOf(false) }, | ||||||
|  |     @StringRes errorText: Int, | ||||||
|     keyboardType: KeyboardType, |     keyboardType: KeyboardType, | ||||||
|     predicate: (String) -> Boolean, |     predicate: (String) -> Boolean, | ||||||
|     onNewCorrectValue: (String) -> Unit |     onNewCorrectValue: (String) -> Unit | ||||||
|  | @ -128,31 +128,28 @@ fun LabeledErrorTextField( | ||||||
|         mutableStateOf(initialValue) |         mutableStateOf(initialValue) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     var isValid by remember { |  | ||||||
|         mutableStateOf(predicate(value)) |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     Column { |     Column { | ||||||
|         OutlinedTextField( |         OutlinedTextField( | ||||||
|             modifier = modifier.fieldModifier(), |             modifier = modifier.fieldModifier(), | ||||||
|             value = value, |             value = value, | ||||||
|             onValueChange = { newText -> |             onValueChange = { newText -> | ||||||
|  |                 isFirst.value = false | ||||||
|                 value = newText |                 value = newText | ||||||
|                 isValid = predicate(value) |                 isValid.value = predicate(value) | ||||||
|                 if (isValid) { |                 if (isValid.value) { | ||||||
|                     onNewCorrectValue(newText) |                     onNewCorrectValue(newText) | ||||||
|                 } |                 } | ||||||
|             }, |             }, | ||||||
|             singleLine = singleLine, |             singleLine = singleLine, | ||||||
|             label = { Text(text = stringResource(id = label)) }, |             label = { Text(text = stringResource(id = label)) }, | ||||||
|             isError = !isValid, |             isError = !isValid.value && !isFirst.value, | ||||||
|             keyboardOptions = KeyboardOptions( |             keyboardOptions = KeyboardOptions( | ||||||
|                 keyboardType = keyboardType, |                 keyboardType = keyboardType, | ||||||
|                 imeAction = ImeAction.Done |                 imeAction = ImeAction.Done | ||||||
|             ) |             ) | ||||||
|         ) |         ) | ||||||
| 
 | 
 | ||||||
|         if (!isValid) { |         if (!isValid.value && !isFirst.value) { | ||||||
|             Text( |             Text( | ||||||
|                 modifier = Modifier.padding(start = 16.dp), |                 modifier = Modifier.padding(start = 16.dp), | ||||||
|                 text = stringResource(id = errorText), |                 text = stringResource(id = errorText), | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Lukas Barragan Torres
						Lukas Barragan Torres