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-model checkbox weirdness #3351

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/alpinejs/src/directives/x-model.js
Expand Up @@ -5,8 +5,9 @@ import { nextTick } from '../nextTick'
import bind from '../utils/bind'
import on from '../utils/on'
import { warn } from '../utils/warn'
import { skipDuringClone } from '../clone'

directive('model', (el, { modifiers, expression }, { effect, cleanup }) => {
directive('model', skipDuringClone((el, { modifiers, expression }, { effect, cleanup }) => {
let scopeTarget = el

if (modifiers.includes('parent')) {
Expand Down Expand Up @@ -119,7 +120,7 @@ directive('model', (el, { modifiers, expression }, { effect, cleanup }) => {

el._x_forceModelUpdate(value)
})
})
}))

function getInputValue(el, modifiers, event, currentValue) {
return mutateDom(() => {
Expand Down
31 changes: 31 additions & 0 deletions tests/cypress/integration/clone.spec.js
Expand Up @@ -97,3 +97,34 @@ test('wont register listeners on clone',
get('#copy span').should(haveText('1'))
}
)

test('wont register extra listeners on x-model on clone',
html`
<script>
document.addEventListener('alpine:initialized', () => {
window.original = document.getElementById('original')
window.copy = document.getElementById('copy')
})
</script>

<button x-data @click="Alpine.clone(original, copy)">click</button>

<div x-data="{ checks: [] }" id="original">
<input type="checkbox" x-model="checks" value="1">
<span x-text="checks"></span>
</div>

<div x-data="{ checks: [] }" id="copy">
<input type="checkbox" x-model="checks" value="1">
<span x-text="checks"></span>
</div>
`,
({ get }) => {
get('#original span').should(haveText(''))
get('#copy span').should(haveText(''))
get('button').click()
get('#copy span').should(haveText(''))
get('#copy input').click()
get('#copy span').should(haveText('1'))
}
)