Skip to content

Instantly share code, notes, and snippets.

@saurabharora90
Last active November 5, 2023 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurabharora90/d38eba197fc8cad51b4ad715e5456d67 to your computer and use it in GitHub Desktop.
Save saurabharora90/d38eba197fc8cad51b4ad715e5456d67 to your computer and use it in GitHub Desktop.
Add is valid hashtag check
@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