Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cursor goes to start when BasicTextField gets empty when using TextAlign.Center #4743

Closed
mahozad opened this issue May 1, 2024 · 1 comment
Labels
bug Something isn't working duplicate This issue or pull request already exists submitted

Comments

@mahozad
Copy link
Contributor

mahozad commented May 1, 2024

Describe the bug
When removing all the text in a basic text field, the cursor (blinking vertical line) goes to the start of text field instead of staying at center.

Screenshot (59)

Affected platforms

  • Desktop (Windows, Linux, macOS)

Versions

  • Kotlin version*: 1.9.23
  • Compose Multiplatform version*: 1.6.2 (and previous versions)
  • OS version(s)* (required for Desktop and iOS issues): Windows 11
  • OS architecture (x86 or arm64): x86
  • JDK (for desktop issues): 21

To Reproduce

fun main() = application {
    Window(onCloseRequest = ::exitApplication) {
        MyCustomTextField()
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
private fun MyCustomTextField() {
    var text by remember { mutableStateOf("1.2.3.4") }
    val interactionSource = remember(::MutableInteractionSource)
    Box(contentAlignment = Alignment.CenterEnd, modifier = Modifier.width(500.dp)) {
        BasicTextField(
            value = text,
            onValueChange = { text = it },
            cursorBrush = SolidColor(MaterialTheme.colors.onSurface),
            textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center), // <<<<<<<<<<<< -------------
            interactionSource = interactionSource,
            modifier = Modifier
                .fillMaxWidth()
                .height(36.dp)
                .background(Color.LightGray)
        ) {
            CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
                TextFieldDefaults.TextFieldDecorationBox(
                    value = text,
                    innerTextField = it,
                    singleLine = true,
                    enabled = true,
                    placeholder = {
                        Text(
                            text = "hint",
                            style = LocalTextStyle.current.copy(textAlign = TextAlign.Center),
                            fontSize = 9.sp,
                            modifier = Modifier.fillMaxWidth()
                        )
                    },
                    visualTransformation = VisualTransformation.None,
                    interactionSource = interactionSource,
                    contentPadding = TextFieldDefaults.textFieldWithoutLabelPadding(top = 0.dp, bottom = 0.dp)
                )
            }
        }
    }
}

Expected behavior
The cursor should stay at center or there should be an option to customize its behavior.

@mahozad mahozad added bug Something isn't working submitted labels May 1, 2024
@igordmn
Copy link
Collaborator

igordmn commented May 3, 2024

Thanks! It was reported before: #4611

@igordmn igordmn closed this as not planned Won't fix, can't repro, duplicate, stale May 3, 2024
@igordmn igordmn added the duplicate This issue or pull request already exists label May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists submitted
Projects
None yet
Development

No branches or pull requests

2 participants