How to commit in TextField without losing focus?

Viewed 59

So I want to have an interaction in my app similar to the text entry in WhatsApp. I.e. a text field remains in focus, and when I press the return button, the text in the field gets committed, the text field gets cleared, and the keyboard remains up so I can enter another line without focusing the text field again.

Right now this is my implementation:

TextField(
    "Enter text here",
    text: $text,
    onCommit: {
        if text != "" {
            self.submitText(text)
            self.text = ""
        }
    }
)

So if I run this code, the TextField loses focus every time I press the "return" key on the keyboard. Is there any way to prevent this?

I have tried also setting the focus to true inside the onCommit block, and this sort of works, but the text field briefly loses focus before gaining it again, which is distracting.

0 Answers
Related