Thread
#compose
    manueldidonna

    manueldidonna

    2 years ago
    I'm having some trouble on limiting the number of chars of a TextFieldValue.
    var selection by state { TextRange(0, 0) }
    var text by state { repository.getText() }
    TextField(
        value = TextFieldValue(text, selection),
        onValueChange = { 
            selection = it.selection
            // there is a a limit on the number of chars. 
            // The passed text is automatically truncated
            text = repository.updateText(it.text) 
        }
    )
    When I exceed the number of chars, I have some problems with the keyboard that continues to record the inputs, so I have to press the delete button more times than needed.
    l

    Leland Richardson [G]

    2 years ago
    can you file a bug on this? This use case should work properly. Thanks
    v

    Val Salamakha

    2 years ago
    If you would like to limit your text in TextField according to a selected range, you need to do it before TextField. For example using the extract from TextField as following:  var value by state { TextFieldValue( “123456789”, selection = TextRange(0,5) )} val fullModel = state { EditorValue() } if (fullModel.value.text != value.text || fullModel.value.selection != value.selection) {   val newSelection = TextRange(     value.selection.start.coerceIn(0, value.text.length),     value.selection.end.coerceIn(0, value.text.length)   )   fullModel.value = EditorValue(     text = value.text,     selection = newSelection   ) }val text = fullModel.value.getSelectedText() value = TextFieldValue(text)   TextField(     value = value,     onValueChange = {     },     modifier = Modifier.preferredSize(width = 200.dp, height = 56.dp)   )