kenmueller / TextView

The missing TextView in SwiftUI
MIT License
244 stars 32 forks source link

BUG - When using TextView in Form #26

Open JonasDeichelmann opened 4 years ago

JonasDeichelmann commented 4 years ago

Hi,

I want to use the TextView in a Form, but it always jumps to the textview when the user is typing something into other Form-TextFields. That mades the TextView unusable.

Cheers, Jonas

DenTelezhkin commented 4 years ago

For anyone interested - I encountered this as well, and the only fix I found was explicitly setting isEditing as state variable on my View, so that it would remember value, but not reset every time:

// This state property is actually never used by this view, and serves as a storage of editing state for text view.
@State private var isEditing: Bool = true

var body: some View {
    VStack {
        TextView(text: $model.text, isEditing: $isEditing)

        TextField("Placeholder", text: $model.textForTextfield)
    }
}

This seems like a small design flaw, since TextView should remember it's state, but what happens view hierarchy changes for any reason(editing textfield is one of them), TextView may be recreated by SwiftUI, and if you passed .constant(true) as isEditing property, view will be recreated with focus. With my @State workaround, isEditing is stored outside of TextView, and therefore the issue does not happen.

JonasDeichelmann commented 4 years ago

@DenTelezhkin Thank you for your solution! As we still recognize SwiftUI is in the beginning.

kenmueller commented 4 years ago

Hey, sorry for not making this clear. I should have mentioned in the README that it is not possible to set a constant Binding variable for isEditing, and it must be set on the view so that it isn't recreated. So I wouldn't call it a workaround, but instead a necessary thing to do.