Fix x-mask attribute changes on x-model inputs#4852
Open
joshhanley wants to merge 2 commits into
Open
Conversation
x-mask attribute changes with x-modelx-mask attribute changes on x-model inputs
3 tasks
Collaborator
|
Traced this through |
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.
The Scenario
When Livewire morphs an input that uses both
wire:modelandx-mask, changing the server-renderedx-maskattribute can leave the input using a previous mask.In this example, the input starts with a five-digit French postal code mask, changes to a four-digit Belgian mask, then changes back to the five-digit French mask. The
x-maskattribute updates, but the input remains limited by the older four-digit mask.The Problem
The mask directive wraps
el._x_forceModelUpdateso programmatic model updates are formatted before they are applied to the input.When an existing
x-maskattribute changes, Alpine treats that as directive cleanup followed by directive initialisation. The cleanup aborts the directive's event listeners, but it does not restoreel._x_forceModelUpdate.That means every reinitialised mask wraps the updater currently on the element. Previous masks stay in the update chain, so an older four-digit mask can still trim a value after the element has changed back to a five-digit mask.
The Solution
The solution is to let the mask directive unregister the model updater wrapper it installs.
The directive now keeps a reference to its wrapper and restores the previous updater during cleanup, but only when the element is still using that exact wrapper. This avoids removing a newer wrapper that may have been installed after another directive initialisation.
Fixes livewire/livewire#10377