Handling Enter presses

This commit is contained in:
dankito 2024-09-01 18:53:02 +02:00
parent 362675cb12
commit e888437efa
2 changed files with 9 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package net.codinux.banking.ui.forms
import androidx.compose.foundation.ScrollState import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.material.CircularProgressIndicator import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Divider import androidx.compose.material.Divider
@ -30,6 +31,7 @@ fun <T> AutocompleteTextField(
label: @Composable () -> Unit = { Text("Search") }, label: @Composable () -> Unit = { Text("Search") },
showDividersBetweenItems: Boolean = true, showDividersBetweenItems: Boolean = true,
getItemTitle: ((T) -> String)? = null, getItemTitle: ((T) -> String)? = null,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
fetchSuggestions: suspend (query: String) -> List<T> = { emptyList() }, fetchSuggestions: suspend (query: String) -> List<T> = { emptyList() },
suggestionContent: @Composable (T) -> Unit suggestionContent: @Composable (T) -> Unit
) { ) {
@ -71,6 +73,7 @@ fun <T> AutocompleteTextField(
.onGloballyPositioned { .onGloballyPositioned {
textFieldSize = it.size.toSize() textFieldSize = it.size.toSize()
}, },
keyboardOptions = keyboardOptions,
trailingIcon = { trailingIcon = {
if (isLoading) { if (isLoading) {
CircularProgressIndicator( CircularProgressIndicator(

View File

@ -8,13 +8,16 @@ import androidx.compose.material.MaterialTheme
import androidx.compose.material.TextFieldDefaults import androidx.compose.material.TextFieldDefaults
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.input.key.* import androidx.compose.ui.input.key.*
import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.VisualTransformation import androidx.compose.ui.text.input.VisualTransformation
import net.codinux.banking.ui.config.Colors import net.codinux.banking.ui.config.Colors
@OptIn(ExperimentalComposeUiApi::class)
@Composable @Composable
fun OutlinedTextField( fun OutlinedTextField(
value: String, value: String,
@ -47,7 +50,7 @@ fun OutlinedTextField(
androidx.compose.material.OutlinedTextField( androidx.compose.material.OutlinedTextField(
value = value, value = value,
onValueChange = onValueChange, 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)) { if (onEnterPressed != null && event.type == KeyEventType.KeyUp && (event.key == Key.Enter || event.key == Key.NumPadEnter)) {
onEnterPressed() onEnterPressed()
true true
@ -64,8 +67,8 @@ fun OutlinedTextField(
trailingIcon = trailingIcon, trailingIcon = trailingIcon,
isError = isError, isError = isError,
visualTransformation = visualTransformation, visualTransformation = visualTransformation,
keyboardOptions = keyboardOptions, keyboardOptions = if (onEnterPressed != null) keyboardOptions.copy(imeAction = ImeAction.Done) else keyboardOptions,
keyboardActions = keyboardActions, 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, singleLine = singleLine,
maxLines = maxLines, maxLines = maxLines,
minLines = minLines, minLines = minLines,