if minTextLengthForSearch == 0, showing dropdown when TextField gets focus

This commit is contained in:
dankito 2024-09-04 21:48:39 +02:00
parent 88ef0a703e
commit 2e1e06de23
1 changed files with 31 additions and 24 deletions

View File

@ -8,13 +8,12 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusProperties
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.*
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.PopupProperties
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.codinux.banking.ui.config.Colors
@ -30,13 +29,14 @@ fun <T> AutocompleteTextFieldNew(
getItemTitle: ((T) -> String)? = null,
dropdownMaxHeight: Dp = 400.dp,
leadingIcon: @Composable (() -> Unit)? = null,
fetchSuggestions: (query: String) -> Collection<T> = { emptyList() },
fetchSuggestions: suspend (query: String) -> Collection<T> = { emptyList() },
suggestionContent: @Composable (T) -> Unit
) {
var searchQuery by remember { mutableStateOf("") }
var expanded by remember { mutableStateOf(false) }
var isLoading by remember { mutableStateOf(false) }
var suggestions by remember { mutableStateOf<Collection<T>>(emptyList()) }
var suggestionsFetchJob: Job? = null
val textFieldFocus = remember { FocusRequester() }
val keyboardController = LocalSoftwareKeyboardController.current
@ -45,6 +45,27 @@ fun <T> AutocompleteTextFieldNew(
val coroutineScope = rememberCoroutineScope()
fun fetchSuggestionsAsync() {
suggestionsFetchJob?.cancel()
suggestionsFetchJob = coroutineScope.launch {
isLoading = true
suggestions = fetchSuggestions(searchQuery)
isLoading = false
if (expanded == false && suggestions.isNotEmpty()) {
expanded = true
}
// Delay to ensure menu is fully displayed before requesting focus
delay(100)
textFieldFocus.requestFocus() // Request focus back to TextField
keyboardController?.show()
}
}
ExposedDropdownMenuBox(expanded, { isExpanded -> expanded = isExpanded }, Modifier.fillMaxWidth()) {
OutlinedTextField(
value = searchQuery,
@ -54,31 +75,17 @@ fun <T> AutocompleteTextFieldNew(
onEnteredTextChanged?.invoke(query)
if (query.length >= minTextLengthForSearch) {
isLoading = true
coroutineScope.launch {
suggestions = fetchSuggestions(query)
isLoading = false
if (expanded == false) {
expanded = true
}
// Delay to ensure menu is fully displayed before requesting focus
delay(100)
textFieldFocus.requestFocus() // Request focus back to TextField
keyboardController?.show() // Show keyboard
delay(1000)
textFieldFocus.requestFocus()
keyboardController?.show() // Show keyboard
}
fetchSuggestionsAsync()
} else {
suggestions = emptyList()
expanded = false
}
},
modifier = Modifier.fillMaxWidth().focusRequester(textFieldFocus),
modifier = Modifier.fillMaxWidth().focusRequester(textFieldFocus).onFocusChanged { focusState ->
if (focusState.isFocused && minTextLengthForSearch == 0) {
fetchSuggestionsAsync()
}
},
interactionSource = interactionSource,
label = { Text(label) },
maxLines = 1,