Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix x-mask triggering update requests on load #4481

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/mask/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ export default function (Alpine) {
}

// Override x-model's initial value...
if (el._x_model) el._x_model.set(el.value)
if (el._x_model) {
// If the x-model value is the same, don't override it as that will trigger updates...
if (el._x_model.get() === el.value) return

// If the x-model value is `null` and the input value is an empty
// string, don't override it as that will trigger updates...
if (el._x_model.get() === null && el.value === '') return

el._x_model.set(el.value)
}
})

const controller = new AbortController()
Expand Down
15 changes: 15 additions & 0 deletions tests/cypress/integration/plugins/mask.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ test('x-mask with x-model with initial value',
},
)

test('x-mask with x-model if initial value is null it should remain null',
[html`
<div x-data="{ value: null }">
<input x-mask="(999) 999-9999" x-model="value" id="1">
<input id="2" x-model="value">
<span id="3" x-text="value === null ? 'NULL' : value"></span>
</div>
`],
({ get }) => {
get('#1').should(haveValue(''))
get('#2').should(haveValue(''))
get('#3').contains('NULL')
},
)

test('x-mask with a falsy input',
[html`<input x-data x-mask="">`],
({ get }) => {
Expand Down
Loading