Last active
November 5, 2023 16:13
-
-
Save saurabharora90/e3aba869495c4349166a7dd304c40e86 to your computer and use it in GitHub Desktop.
second approach with state object as key
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
@Composable | |
private fun PostHashtags( | |
hashtags: ImmutableList<String>, | |
onAddHashTag: (String) -> Unit, | |
modifier: Modifier | |
) { | |
- var inputHashTag by remember(hashtags) { mutableStateOf("") } | |
+ val inputHashTag = remember(hashtags) { mutableStateOf("") } | |
- val isValidHashtag: Boolean by remember { | |
+ val isValidHashtag: Boolean by remember(inputHashTag) { | |
derivedStateOf { | |
- !inputHashTag.contains(" ") | |
+ !inputHashTag.value.contains(" ") | |
} | |
} | |
Column(modifier = modifier) { | |
OutlinedTextField( | |
- value = inputHashTag, | |
+ value = inputHashTag.value, | |
- onValueChange = { inputHashTag = it }, | |
+ onValueChange = { inputHashTag.value = it }, | |
leadingIcon = { Text(text = "#", fontWeight = FontWeight.Bold) }, | |
placeholder = { Text(text = "Type a hashtag here") }, | |
trailingIcon = { | |
AnimatedVisibility( | |
- visible = inputHashTag.isNotEmpty() && isValidHashtag, | |
+ visible = inputHashTag.value.isNotEmpty() && isValidHashtag, | |
enter = fadeIn(), | |
exit = fadeOut() | |
) { | |
- IconButton(onClick = { onAddHashTag(inputHashTag) }) { | |
+ IconButton(onClick = { onAddHashTag(inputHashTag.value) }) { | |
Icon(imageVector = Icons.Default.AddCircle, | |
contentDescription = "Add hashtag") | |
} | |
} | |
}, | |
isError = !isValidHashtag, | |
) | |
AnimatedVisibility(visible = !isValidHashtag) { | |
Text( | |
text = "Hashtag cannot have a space", | |
style = MaterialTheme.typography.labelSmall, | |
modifier = Modifier.padding(vertical = 2.dp) | |
) | |
} | |
ReusableVerticalLazyList(content = hashtags) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment