Skip to content

Commit

Permalink
Merge pull request #39 from Breens-Mbaka/fix/keyboard-not-showing
Browse files Browse the repository at this point in the history
Fix/keyboard not showing
  • Loading branch information
Breens-Mbaka authored Aug 19, 2023
2 parents f5786d8 + 45ebbfb commit 26157d5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MainActivity : ComponentActivity() {
Column(
Modifier
.fillMaxSize()
.padding(16.dp)
.padding(16.dp),
) {
val sports = mutableListOf(
Sport("Basketball", "🏀"),
Expand All @@ -39,7 +39,7 @@ class MainActivity : ComponentActivity() {
Sport("MMA", "🤼‍♂️"),
Sport("Motorsport", "🏁"),
Sport("Snooker", "🎱"),
Sport("Tennis", "🎾")
Sport("Tennis", "🎾"),
)
LazyColumn {
items(50) {
Expand All @@ -50,15 +50,15 @@ class MainActivity : ComponentActivity() {
Toast.makeText(
applicationContext,
item.name,
Toast.LENGTH_SHORT
Toast.LENGTH_SHORT,
).show()
},
dropdownItem = { test ->
DropDownItem(test = test)
},
defaultItem = {
Log.e("DEFAULT_ITEM", it.name)
}
},
)
Spacer(modifier = Modifier.height(12.dp))
}
Expand All @@ -74,7 +74,7 @@ fun DropDownItem(test: Sport) {
Row(
modifier = Modifier
.padding(8.dp)
.wrapContentSize()
.wrapContentSize(),
) {
Text(text = test.emoji)
Spacer(modifier = Modifier.width(12.dp))
Expand All @@ -84,7 +84,7 @@ fun DropDownItem(test: Sport) {

data class Sport(
val name: String,
val emoji: String
val emoji: String,
) {
override fun toString(): String {
return "$emoji $name"
Expand Down
2 changes: 1 addition & 1 deletion searchable-dropdown/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ afterEvaluate {

groupId = 'com.github.Breens-Mbaka'
artifactId = 'searchable-dropdown'
version = '0.1.0'
version = '0.3.0'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2023 d.light Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kanyidev.searchable_dropdown

import androidx.compose.foundation.interaction.MutableInteractionSource
Expand Down Expand Up @@ -32,13 +47,14 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp


/**
* 🚀 A Jetpack Compose Android Library to create a dropdown menu that is searchable.
* @param modifier a modifier for this SearchableExpandedDropDownMenu and its children
Expand All @@ -59,7 +75,7 @@ import androidx.compose.ui.unit.dp
* @param defaultItem Returns the item selected by default from the dropdown list
*/

@OptIn(ExperimentalMaterial3Api::class)
@OptIn(ExperimentalMaterial3Api::class, ExperimentalComposeUiApi::class)
@Composable
fun <T> SearchableExpandedDropDownMenu(
modifier: Modifier = Modifier,
Expand All @@ -76,13 +92,13 @@ fun <T> SearchableExpandedDropDownMenu(
isError: Boolean = false,
showDefaultSelectedItem: Boolean = false,
defaultItemIndex: Int = 0,
defaultItem: (T) -> Unit
defaultItem: (T) -> Unit,
) {
var selectedOptionText by rememberSaveable { mutableStateOf("") }
var searchedOption by rememberSaveable { mutableStateOf("") }
var expanded by remember { mutableStateOf(false) }
var filteredItems = mutableListOf<T>()

val keyboardController = LocalSoftwareKeyboardController.current
val itemHeights = remember { mutableStateMapOf<Int, Int>() }
val baseHeight = 530.dp
val density = LocalDensity.current
Expand All @@ -91,7 +107,7 @@ fun <T> SearchableExpandedDropDownMenu(
selectedOptionText = selectedOptionText.ifEmpty { listOfItems[defaultItemIndex].toString() }

defaultItem(
listOfItems[defaultItemIndex]
listOfItems[defaultItemIndex],
)
}

Expand All @@ -116,7 +132,7 @@ fun <T> SearchableExpandedDropDownMenu(

Column(
modifier = modifier,
horizontalAlignment = Alignment.CenterHorizontally
horizontalAlignment = Alignment.CenterHorizontally,
) {
OutlinedTextField(
modifier = modifier,
Expand All @@ -131,40 +147,45 @@ fun <T> SearchableExpandedDropDownMenu(
checked = expanded,
onCheckedChange = {
expanded = it
}
},
) {
if (expanded) Icon(
imageVector = openedIcon,
contentDescription = null
) else Icon(
imageVector = closedIcon,
contentDescription = null
)
if (expanded) {
Icon(
imageVector = openedIcon,
contentDescription = null,
)
} else {
Icon(
imageVector = closedIcon,
contentDescription = null,
)
}
}
},
shape = RoundedCornerShape(parentTextFieldCornerRadius),
isError = isError,
interactionSource = remember { MutableInteractionSource() }
.also { interactionSource ->
LaunchedEffect(interactionSource) {
keyboardController?.show()
interactionSource.interactions.collect {
if (it is PressInteraction.Release) {
expanded = !expanded
}
}
}
}
},
)
if (expanded) {
DropdownMenu(
modifier = Modifier
.fillMaxWidth(0.75f)
.requiredSizeIn(maxHeight = maxHeight),
expanded = expanded,
onDismissRequest = { expanded = false }
onDismissRequest = { expanded = false },
) {
Column(
verticalArrangement = Arrangement.spacedBy(10.dp)
verticalArrangement = Arrangement.spacedBy(10.dp),
) {
OutlinedTextField(
modifier = modifier
Expand All @@ -176,7 +197,7 @@ fun <T> SearchableExpandedDropDownMenu(
filteredItems = listOfItems.filter {
it.toString().contains(
searchedOption,
ignoreCase = true
ignoreCase = true,
)
}.toMutableList()
},
Expand All @@ -185,7 +206,7 @@ fun <T> SearchableExpandedDropDownMenu(
},
placeholder = {
Text(text = "Search")
}
},
)

val items = if (filteredItems.isEmpty()) {
Expand All @@ -197,6 +218,7 @@ fun <T> SearchableExpandedDropDownMenu(
items.forEach { selectedItem ->
DropdownMenuItem(
onClick = {
keyboardController?.hide()
selectedOptionText = selectedItem.toString()
onDropDownItemSelected(selectedItem)
searchedOption = ""
Expand All @@ -205,7 +227,7 @@ fun <T> SearchableExpandedDropDownMenu(
text = {
dropdownItem(selectedItem)
},
colors = MenuDefaults.itemColors()
colors = MenuDefaults.itemColors(),
)
}
}
Expand Down

0 comments on commit 26157d5

Please sign in to comment.