Last active
November 5, 2023 15:31
-
-
Save saurabharora90/d38eba197fc8cad51b4ad715e5456d67 to your computer and use it in GitHub Desktop.
Add is valid hashtag check
This file contains hidden or 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 isValidHashtag: Boolean by remember { | |
+ derivedStateOf { | |
+ !inputHashTag.contains(" ") | |
+ } | |
+ } | |
Column(modifier = modifier) { | |
OutlinedTextField( | |
value = inputHashTag, | |
onValueChange = { inputHashTag = it }, | |
leadingIcon = { Text(text = "#", fontWeight = FontWeight.Bold) }, | |
placeholder = { Text(text = "Type a hashtag here") }, | |
trailingIcon = { | |
+ AnimatedVisibility( | |
+ visible = inputHashTag.isNotEmpty() && isValidHashtag, | |
+ enter = fadeIn(), | |
+ exit = fadeOut() | |
+ ) { | |
IconButton(onClick = { onAddHashTag(inputHashTag) }) { | |
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