https://kotlinlang.org logo
Title
f

Filip Stanis

02/07/2022, 1:58 PM
Is there something akin to
.replace
that works on
TextFieldValue
(takes cursor / selection into account)? I'm trying to have forbidden characters (like @) in my
TextField
so I'm just doing
.replace("@", "")
inside
onValueChange
but this means the cursor will stay on the position of the removed character (i.e. behaves like "insert" key is on). I feel this is simple to solve, but just wanted to check if there's some builtin method I'm missing.
o

Oleksandr Balan

02/07/2022, 2:20 PM
I guess you want to check the
VisualTransformation
and
OffsetMapping
https://developer.android.com/reference/kotlin/androidx/compose/ui/text/input/VisualTransformation
f

Filip Stanis

02/07/2022, 3:08 PM
This wouldn't change the value though - just what the use sees. I want to ensure the invalid character(s) never reach my DB / backend.
o

Oleksandr Balan

02/07/2022, 3:12 PM
Then I would pre-process the user’s input before it is persisted or send.
f

Filip Stanis

02/07/2022, 3:58 PM
That's sort of what I'm doing - but then either I display the processed input (which is the issue I'm having, cursor position might end up wrong) or I show something different to the user
s

Siyamed

02/08/2022, 7:04 PM
cursor position is TextFieldValue.selection = TextRange(myPosition)