Skip to content

Commit ab0dd77

Browse files
author
breensmbaka
committed
Return callback when search-text-field in dropdown menu is clicked.
1 parent 4f4ef80 commit ab0dd77

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

searchable-dropdown/src/main/java/com/kanyidev/searchable_dropdown/SearchableExpandedDropDownMenu.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.kanyidev.searchable_dropdown
1717

18-
import androidx.compose.foundation.clickable
1918
import androidx.compose.foundation.interaction.MutableInteractionSource
2019
import androidx.compose.foundation.interaction.PressInteraction
2120
import androidx.compose.foundation.layout.Arrangement
@@ -52,7 +51,6 @@ import androidx.compose.ui.platform.LocalDensity
5251
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
5352
import androidx.compose.ui.unit.Dp
5453
import androidx.compose.ui.unit.dp
55-
import kotlinx.coroutines.InternalCoroutinesApi
5654
import kotlinx.coroutines.flow.collect
5755

5856
/**
@@ -76,7 +74,7 @@ import kotlinx.coroutines.flow.collect
7674
* @param onSearchTextFieldClicked use this if you are having problems with the keyboard showing, use this to show keyboard on your side
7775
*/
7876

79-
@OptIn(ExperimentalComposeUiApi::class, InternalCoroutinesApi::class)
77+
@OptIn(ExperimentalComposeUiApi::class)
8078
@Composable
8179
fun <T> SearchableExpandedDropDownMenu(
8280
modifier: Modifier = Modifier,
@@ -192,10 +190,7 @@ fun <T> SearchableExpandedDropDownMenu(
192190
OutlinedTextField(
193191
modifier = modifier
194192
.fillMaxWidth()
195-
.padding(16.dp)
196-
.clickable {
197-
onSearchTextFieldClicked()
198-
},
193+
.padding(16.dp),
199194
value = searchedOption,
200195
onValueChange = { selectedSport ->
201196
searchedOption = selectedSport
@@ -212,6 +207,17 @@ fun <T> SearchableExpandedDropDownMenu(
212207
placeholder = {
213208
Text(text = "Search")
214209
},
210+
interactionSource = remember { MutableInteractionSource() }
211+
.also { interactionSource ->
212+
LaunchedEffect(interactionSource) {
213+
keyboardController?.show()
214+
interactionSource.interactions.collect {
215+
if (it is PressInteraction.Release) {
216+
onSearchTextFieldClicked()
217+
}
218+
}
219+
}
220+
},
215221
)
216222

217223
val items = if (filteredItems.isEmpty()) {

0 commit comments

Comments
 (0)