Handle special binding case for 'checked' and 'selected' #3535
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I ran into an interesting issue with
x-bind
and thechecked
property today. Here's the Twitter thread summarizing the issue, and here's a codesandbox demo showing it in action.Basically, the
value
,checked
andselected
properties are special in that they're not kept in sync with their corresponding attributes. That means that the following is perfectly legit javascript:This will result in a checkbox that is not checked, but has a checked attribute set (which, as far as I can tell, is visually checked but not "technically" checked).
Anyway, 90% of the time this won't come up because both
x-bind:value
andx-model
have special bind logic for checkbox values that set the property rather than the attribute:alpine/packages/alpinejs/src/utils/bind.js
Lines 54 to 58 in 6142a83
There are some cases, though, where you want to handle both sides of the binding on your own (in our case, the checkboxes are driven from Algolia instant search state, so changing the checkbox triggers an event that then updates the state). This PR makes that case possible.