Skip to content
New issue

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

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

Already on GitHub? # to your account

Toggle value not updated when it's reset from a binding #287

Closed
MaxDesiatov opened this issue Oct 6, 2020 · 5 comments · Fixed by #309
Closed

Toggle value not updated when it's reset from a binding #287

MaxDesiatov opened this issue Oct 6, 2020 · 5 comments · Fixed by #309
Labels
bug Something isn't working

Comments

@MaxDesiatov
Copy link
Collaborator

Previously reported by @rbartolome. Here's a reproducible test case:

class TestData: ObservableObject
{
    @Published var stringData: String = ""
    @Published var booleanData: Bool = false
    
    func reset()
    {
        self.stringData = ""
        self.booleanData = false
    }
}

struct DemoToggle: App
{
    @ObservedObject var testdata = TestData()
    var body: some Scene
    {
        WindowGroup("DemoTracker")
        {
            VStack
            {
                Toggle("Toogle", isOn: self.$testdata.booleanData)
                TextField("Placeholder", text: self.$testdata.stringData)
                Button(action:
                {
                    self.testdata.reset()
                },
                label:
                {
                    Text("Reset")
                })
            }
        }
    }
}

DemoToggle.main()
@MaxDesiatov MaxDesiatov added the bug Something isn't working label Oct 6, 2020
@RayZhao1998
Copy link

It seems that checked.toggle() can't update the Toggle value too.

Full code:

public struct ToggleDemo: View {
  @State var checked = false

  public var body: some View {
    VStack {
      Toggle("Toogle", isOn: $checked)
      Text(checked ? "Checked" : "Not Checked")
      Button(action:
      {
        self.checked.toggle()
      },
      label:
      {
          Text("Reset")
      })
    }
  }
}

@RayZhao1998
Copy link

Found the reason. The checkbox in HTML is so weird. Whether it's checked is not determined by the checked property. Even though I uncheck the checkbox, the property checked="checked" is still there.
截屏2020-11-27 上午1 02 50

@MaxDesiatov
Copy link
Collaborator Author

Thanks for the investigation! Now I remember something similar was mentioned during review when @j-f1 was working on #159.

@RayZhao1998
Copy link

Usually we uncheck the checkbox in JS like getElementsByTagName('input')[0].checked = false.

I find a modifier: _domRef. Can we get the input DOM and set its checked?

MaxDesiatov added a commit that referenced this issue Nov 28, 2020
Resolve #287.

The `checked` attribute is a peculiar one, as any value on it keeps the checkbox checked. Attribute updates in `DOMRenderer` don't handle removals of attributes, but this seems to be the only case where this is relevant. I've added special handling for this attribute and checkbox inputs, and also had to declare `HTMLAttribute.checked` to set `isUpdatedAsProperty: true` on it for it to fully work.
@MaxDesiatov
Copy link
Collaborator Author

MaxDesiatov commented Nov 28, 2020

@RayZhao1998 a fix for this was included as a part of the latest 0.5.3 release. _domRef wasn't needed after all 🙂

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

2 participants