if minTextLengthForSearch == 0, showing dropdown when TextField gets focus
This commit is contained in:
parent
88ef0a703e
commit
2e1e06de23
|
@ -8,13 +8,12 @@ import androidx.compose.material.icons.Icons
|
||||||
import androidx.compose.material.icons.filled.Close
|
import androidx.compose.material.icons.filled.Close
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.focus.FocusRequester
|
import androidx.compose.ui.focus.*
|
||||||
import androidx.compose.ui.focus.focusProperties
|
|
||||||
import androidx.compose.ui.focus.focusRequester
|
|
||||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||||
import androidx.compose.ui.unit.Dp
|
import androidx.compose.ui.unit.Dp
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.window.PopupProperties
|
import androidx.compose.ui.window.PopupProperties
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import net.codinux.banking.ui.config.Colors
|
import net.codinux.banking.ui.config.Colors
|
||||||
|
@ -30,13 +29,14 @@ fun <T> AutocompleteTextFieldNew(
|
||||||
getItemTitle: ((T) -> String)? = null,
|
getItemTitle: ((T) -> String)? = null,
|
||||||
dropdownMaxHeight: Dp = 400.dp,
|
dropdownMaxHeight: Dp = 400.dp,
|
||||||
leadingIcon: @Composable (() -> Unit)? = null,
|
leadingIcon: @Composable (() -> Unit)? = null,
|
||||||
fetchSuggestions: (query: String) -> Collection<T> = { emptyList() },
|
fetchSuggestions: suspend (query: String) -> Collection<T> = { emptyList() },
|
||||||
suggestionContent: @Composable (T) -> Unit
|
suggestionContent: @Composable (T) -> Unit
|
||||||
) {
|
) {
|
||||||
var searchQuery by remember { mutableStateOf("") }
|
var searchQuery by remember { mutableStateOf("") }
|
||||||
var expanded by remember { mutableStateOf(false) }
|
var expanded by remember { mutableStateOf(false) }
|
||||||
var isLoading by remember { mutableStateOf(false) }
|
var isLoading by remember { mutableStateOf(false) }
|
||||||
var suggestions by remember { mutableStateOf<Collection<T>>(emptyList()) }
|
var suggestions by remember { mutableStateOf<Collection<T>>(emptyList()) }
|
||||||
|
var suggestionsFetchJob: Job? = null
|
||||||
|
|
||||||
val textFieldFocus = remember { FocusRequester() }
|
val textFieldFocus = remember { FocusRequester() }
|
||||||
val keyboardController = LocalSoftwareKeyboardController.current
|
val keyboardController = LocalSoftwareKeyboardController.current
|
||||||
|
@ -45,6 +45,27 @@ fun <T> AutocompleteTextFieldNew(
|
||||||
val coroutineScope = rememberCoroutineScope()
|
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()) {
|
ExposedDropdownMenuBox(expanded, { isExpanded -> expanded = isExpanded }, Modifier.fillMaxWidth()) {
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
value = searchQuery,
|
value = searchQuery,
|
||||||
|
@ -54,31 +75,17 @@ fun <T> AutocompleteTextFieldNew(
|
||||||
onEnteredTextChanged?.invoke(query)
|
onEnteredTextChanged?.invoke(query)
|
||||||
|
|
||||||
if (query.length >= minTextLengthForSearch) {
|
if (query.length >= minTextLengthForSearch) {
|
||||||
isLoading = true
|
fetchSuggestionsAsync()
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
suggestions = emptyList()
|
suggestions = emptyList()
|
||||||
expanded = false
|
expanded = false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
modifier = Modifier.fillMaxWidth().focusRequester(textFieldFocus),
|
modifier = Modifier.fillMaxWidth().focusRequester(textFieldFocus).onFocusChanged { focusState ->
|
||||||
|
if (focusState.isFocused && minTextLengthForSearch == 0) {
|
||||||
|
fetchSuggestionsAsync()
|
||||||
|
}
|
||||||
|
},
|
||||||
interactionSource = interactionSource,
|
interactionSource = interactionSource,
|
||||||
label = { Text(label) },
|
label = { Text(label) },
|
||||||
maxLines = 1,
|
maxLines = 1,
|
||||||
|
|
Loading…
Reference in New Issue