Skip to content

Commit

Permalink
UI: TextField Fix the cursor
Browse files Browse the repository at this point in the history
Cursor always displayed at the start of Placeholder
Cursor dislayed as soon as the field is focused.
  • Loading branch information
ksharma-xyz committed Sep 29, 2024
1 parent 0e1ba38 commit 145708a
Showing 1 changed file with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package xyz.ksharma.krail.design.system.components

import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -16,6 +20,8 @@ import androidx.compose.foundation.text.selection.LocalTextSelectionColors
import androidx.compose.foundation.text.selection.TextSelectionColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.SolidColor
Expand Down Expand Up @@ -43,7 +49,11 @@ fun TextField(
readOnly: Boolean = false,
imeAction: ImeAction = ImeAction.Default,
) {
val interactionSource = remember { MutableInteractionSource() }
val isFocused by interactionSource.collectIsFocusedAsState()

val textFieldState = rememberTextFieldState()

val textSelectionColors = TextSelectionColors(
handleColor = KrailTheme.colors.tertiary,
backgroundColor = KrailTheme.colors.tertiary.copy(alpha = 0.4f)
Expand Down Expand Up @@ -71,6 +81,7 @@ fun TextField(
),
lineLimits = TextFieldLineLimits.SingleLine,
readOnly = readOnly,
interactionSource = interactionSource,
cursorBrush = SolidColor(KrailTheme.colors.onSecondaryContainer),
decorator = { innerTextField ->
Row(
Expand All @@ -80,20 +91,32 @@ fun TextField(
color = KrailTheme.colors.secondaryContainer
)
.padding(horizontal = 12.dp, vertical = 4.dp),
horizontalArrangement = Arrangement.Start,
verticalAlignment = Alignment.CenterVertically,
) {
if (textFieldState.text.isEmpty()) {
if (textFieldState.text.isEmpty() && isFocused) {
/* Using a Box ensures that cursor and placeholder are displayed on top of
each other, so the cursor is always displayed at the start if the TextField.
*/
Box {
innerTextField() // To display cursor
Text(
text = placeholder.orEmpty(),
style = LocalTextStyle.current,
color = LocalTextColor.current,
maxLines = 1,
)
}
} else if (textFieldState.text.isEmpty()) {
Text(
text = placeholder.orEmpty(),
style = LocalTextStyle.current,
color = LocalTextColor.current,
maxLines = 1,
)
} else {

// add leading icon here
// add leading icon here - todo
innerTextField()

// add trailing icon here / Clear - todo
}
}
Expand All @@ -113,9 +136,18 @@ private fun DvTextFieldEnabledPreview() {
.fillMaxSize()
.background(color = KrailTheme.colors.secondary)
.padding(16.dp),
contentAlignment = Alignment.Center,
contentAlignment = Alignment.TopCenter,
) {
TextField(placeholder = "Station")
Column {
TextField(placeholder = "Station")
androidx.compose.material3.TextField(
modifier = Modifier.padding(top = 24.dp),
value = "",
onValueChange = {},
placeholder = {
Text(text = "aa")
})
}
}
}
}
Expand Down

0 comments on commit 145708a

Please sign in to comment.