Phoenix_live_view: Checkboxes check state can't be overwritten by server reliably

Created on 6 Feb 2020  路  13Comments  路  Source: phoenixframework/phoenix_live_view

Environment

  • Elixir version (elixir -v):
    Erlang/OTP 22 [erts-10.6.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Elixir 1.10.0 (compiled with Erlang/OTP 22)

  • Phoenix version (mix deps):
    phoenix branch 1.4
  • Phoenix LiveView version (mix deps):
    0e91c2576255124c912c7cdc5f94d2fae871d5ad (master HEAD)
  • Operating system:
    OS X

Actual behavior

See: https://github.com/benwilson512/bug_example/blob/master/lib/demo_web/bad.ex

On page load there are two checkboxes, one checked, one unchecked. Clicking the submit button inverts the checked state of each. If you click on a check box, that checkbox is no longer affected by clicking the submit button, but the other is. Whichever is clicked last is unable to pick up changes.

It feels like a focus issue based on that description, but adding a text field or some other input to capture focus doesn't change anything. Perhaps related to https://github.com/phoenixframework/phoenix_live_view/issues/611 ?

Expected behavior

I should be able to override the checked state.

Most helpful comment

I think LV should set the property based on what we receive from the server, effectively making the attribute coupled to the property on updates. @snewcomer can you think of a reason not to do this? We'd simply set the property onElUpdated so easy for us to handle on the LV side without modify morphdom behavior. Thoughts?

All 13 comments

@benwilson512 I think I might have an idea what is wrong. Looking at another minor fix as well so hopefully can have it fixed. In the meantime, I linked an issue that is related...

@benwilson512 can you please check if the merged PR address this and if not, please let us know?

Hi Jose, I do not see this issue fixed, nor what I think is a related issue where selects spaz out in firefox each time the page updates.

Checkbox issue: https://github.com/benwilson512/bug_example/blob/master/lib/demo_web/bad.ex
Select in firefox issue: https://github.com/benwilson512/bug_example/blob/select-variant/lib/demo_web/bad.ex

Looking at the checkbox issue in firefox is actually very interesting, because when you click "invert" after touching one of the check boxes, the touched check box border flashes, and its value (as in chrome) doesn't change. This is true even if you switch focus to a text box on the page. I spent some time saturday trying to trace this stuff down but I'm pretty out of my depths on this sadly.

I'm around today if you want help reproducing.

Note: the merged Pr is only part of the issue. Will try and get to the remaining today!

Noted, thanks for the update!

@benwilson512 This is an interesting situation! Initial thoughts include - Are we straying a bit from how a native checkbox in forms work? Should we be explicitly updating the checked attribute on "click"?

Checkboxes get initial state from their "attribute" and then throughout the lifecycle of the form, have there "property" (idl property) toggled (which is another form of state). That is...the checked "attribute" (which is different than "property") is only the initial state and not indicative if the checkbox is currently checked.

So the boundary of what an LV example should do is at most "reset" the default state of the checkbox with the server response on "submit". However, updating the checked attribute on "change" seems to be circumventing (unnecessarily?) the built in state management of the checkbox "attribute" and "property".

So if I remove the "change" handler, everything seems to work. But perhaps you have a different situation you are trying to solve? Do you have further thoughts?

I think LV should set the property based on what we receive from the server, effectively making the attribute coupled to the property on updates. @snewcomer can you think of a reason not to do this? We'd simply set the property onElUpdated so easy for us to handle on the LV side without modify morphdom behavior. Thoughts?

@chrismccord This would technically be a breaking change? Those with <input type="checkbox" /> would have their possible checked state overridden?

If that is a route you definitely want to go, I'm ok with it! Small surface area so I can keep exploring other options in the meantime and if a better fix comes up, we can integrate that.

The programming model of LV demands that the user maintain form input state on the server based on form data it receives on the client so I wouldn't consider it a breaking change. If the untracked state was held on the client, it worked by accident before.

I can confirm that #627 fixed our issue, thank you!

Was this page helpful?
0 / 5 - 0 ratings