Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Dismiss the keyboard when clearing search field
Browse files Browse the repository at this point in the history
At the moment there is no way to dismiss the keyboard
on iOS once opened from Search.
  • Loading branch information
chrisbanes committed Jul 7, 2023
1 parent 261a4b0 commit e7725ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

package app.tivi.common.compose.ui

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
Expand All @@ -17,12 +14,14 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.input.TextFieldValue
import app.tivi.common.ui.resources.MR
import dev.icerock.moko.resources.compose.stringResource

@OptIn(ExperimentalMaterial3Api::class)
@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
@Composable
fun SearchTextField(
value: TextFieldValue,
Expand All @@ -31,9 +30,10 @@ fun SearchTextField(
modifier: Modifier = Modifier,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions(),
showClearButton: Boolean = value.text.isNotEmpty(),
onCleared: (() -> Unit) = { onValueChange(TextFieldValue()) },
) {
val keyboardController = LocalSoftwareKeyboardController.current

TextField(
value = value,
onValueChange = onValueChange,
Expand All @@ -44,17 +44,18 @@ fun SearchTextField(
)
},
trailingIcon = {
AnimatedVisibility(
visible = showClearButton,
enter = fadeIn(),
exit = fadeOut(),
IconButton(
onClick = {
onCleared()
// This is mostly for iOS, otherwise there is no way to dismiss the iOS
// keyboard once opened.
keyboardController?.hide()
},
) {
IconButton(onClick = onCleared) {
Icon(
imageVector = Icons.Default.Clear,
contentDescription = stringResource(MR.strings.cd_clear_text),
)
}
Icon(
imageVector = Icons.Default.Clear,
contentDescription = stringResource(MR.strings.cd_clear_text),
)
}
},
placeholder = { Text(text = hint) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ private fun LibraryGrid(
},
hint = stringResource(MR.strings.filter_shows, lazyPagingItems.itemCount),
modifier = Modifier.fillMaxWidth(),
showClearButton = true,
onCleared = {
filter = TextFieldValue()
onFilterChanged("")
Expand Down

0 comments on commit e7725ce

Please sign in to comment.