Handling Enter presses
This commit is contained in:
parent
362675cb12
commit
e888437efa
|
@ -3,6 +3,7 @@ package net.codinux.banking.ui.forms
|
|||
import androidx.compose.foundation.ScrollState
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.CircularProgressIndicator
|
||||
import androidx.compose.material.Divider
|
||||
|
@ -30,6 +31,7 @@ fun <T> AutocompleteTextField(
|
|||
label: @Composable () -> Unit = { Text("Search") },
|
||||
showDividersBetweenItems: Boolean = true,
|
||||
getItemTitle: ((T) -> String)? = null,
|
||||
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
|
||||
fetchSuggestions: suspend (query: String) -> List<T> = { emptyList() },
|
||||
suggestionContent: @Composable (T) -> Unit
|
||||
) {
|
||||
|
@ -71,6 +73,7 @@ fun <T> AutocompleteTextField(
|
|||
.onGloballyPositioned {
|
||||
textFieldSize = it.size.toSize()
|
||||
},
|
||||
keyboardOptions = keyboardOptions,
|
||||
trailingIcon = {
|
||||
if (isLoading) {
|
||||
CircularProgressIndicator(
|
||||
|
|
|
@ -8,13 +8,16 @@ import androidx.compose.material.MaterialTheme
|
|||
import androidx.compose.material.TextFieldDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.ExperimentalComposeUiApi
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Shape
|
||||
import androidx.compose.ui.input.key.*
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.VisualTransformation
|
||||
import net.codinux.banking.ui.config.Colors
|
||||
|
||||
@OptIn(ExperimentalComposeUiApi::class)
|
||||
@Composable
|
||||
fun OutlinedTextField(
|
||||
value: String,
|
||||
|
@ -47,7 +50,7 @@ fun OutlinedTextField(
|
|||
androidx.compose.material.OutlinedTextField(
|
||||
value = value,
|
||||
onValueChange = onValueChange,
|
||||
modifier = modifier.onKeyEvent { event ->
|
||||
modifier = modifier.onKeyEvent { event -> // onKeyEvent only handle input on hardware keyboards
|
||||
if (onEnterPressed != null && event.type == KeyEventType.KeyUp && (event.key == Key.Enter || event.key == Key.NumPadEnter)) {
|
||||
onEnterPressed()
|
||||
true
|
||||
|
@ -64,8 +67,8 @@ fun OutlinedTextField(
|
|||
trailingIcon = trailingIcon,
|
||||
isError = isError,
|
||||
visualTransformation = visualTransformation,
|
||||
keyboardOptions = keyboardOptions,
|
||||
keyboardActions = keyboardActions,
|
||||
keyboardOptions = if (onEnterPressed != null) keyboardOptions.copy(imeAction = ImeAction.Done) else keyboardOptions,
|
||||
keyboardActions = if (onEnterPressed != null) KeyboardActions(onDone = { onEnterPressed.invoke() }) else keyboardActions, // onKeyEvent { } only handles input on hardware keyboards, therefore we have also have to overwrite onDone for IME / soft keyboards
|
||||
singleLine = singleLine,
|
||||
maxLines = maxLines,
|
||||
minLines = minLines,
|
||||
|
|
Loading…
Reference in New Issue