Repository navigation
Conversation
Author
How to reproduceOn the current <div x-data="{ selected: 'Orange' }">
<select x-model="selected">
<optgroup label="Colors">
<template x-for="color in ['Red', 'Orange', 'Yellow']">
<option :value="color" x-text="color"></option>
</template>
</optgroup>
</select>
<span x-text="selected"></span>
</div>When the component initialises, the span reports The expected behaviour is for the select to display |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR fixes x-model synchronization for select options that are rendered dynamically inside an optgroup. Previously, the select-specific MutationObserver watched only direct children of the select element. When x-for inserted option elements beneath an optgroup, the mutation target was the optgroup, so x-model did not reapply the model value and the browser-selected option could disagree with Alpine state.
This can be reproduced with a select whose model is initialized to Orange and whose Red, Orange, and Yellow options are rendered by an x-for template inside an optgroup. On main, the model continues to report Orange while the select displays Red. The initial x-model effect runs before x-for creates the options, and the later nested mutation does not trigger a resynchronization. The same problem occurs when a matching option is inserted later while the model value remains unchanged.
This PR fixes the problem by enabling subtree observation on the existing select child-list observer. Additions and removals beneath optgroup elements now trigger the same x-model synchronization already used for options rendered directly beneath a select.
This PR also updates the x-model documentation to explain that dynamically added and removed options remain synchronized when nested inside an optgroup. The example now demonstrates grouped options rendered with x-for and an initial model value that selects a non-first option.
This PR adds Cypress coverage for both affected paths: selecting an initial model value after x-for renders options inside an optgroup, and resynchronizing when x-for later inserts an option matching an unchanged model value. The focused x-model suite passes with 42 tests and no failures using
npx cypress run --spec ./tests/cypress/integration/directives/x-model.spec.js --browser electron.