Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/alpinejs/src/directives/x-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ directive('model', (el, { modifiers, expression }, { effect, cleanup }) => {
// submit handler runs. Register a pending update on the form
// so it can be flushed before submit handlers execute.
if (el.form) {
let form = el.form
let syncCallback = () => syncValue({ target: el })

if (!el.form._x_pendingModelUpdates) el.form._x_pendingModelUpdates = []
el.form._x_pendingModelUpdates.push(syncCallback)
if (!form._x_pendingModelUpdates) form._x_pendingModelUpdates = []
form._x_pendingModelUpdates.push(syncCallback)

cleanup(() => el.form._x_pendingModelUpdates.splice(el.form._x_pendingModelUpdates.indexOf(syncCallback), 1))
cleanup(() => form._x_pendingModelUpdates.splice(form._x_pendingModelUpdates.indexOf(syncCallback), 1))
}
}

Expand Down
71 changes: 71 additions & 0 deletions tests/cypress/integration/directives/x-model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,3 +660,74 @@ test('x-model.blur syncs value before form submit handler runs',
}
)

test('x-model.blur cleanup does not crash when element is removed from DOM',
html`
<div x-data="{ show: true, value: '' }">
<form>
<template x-if="show">
<input x-model.blur="value" type="text">
</template>
</form>
<button @click="show = false">hide</button>
<span x-text="value"></span>
</div>
`,
({ get }) => {
get('input').type('hello')
get('input').blur()
get('span').should(haveText('hello'))
// Remove the input from the DOM via x-if — this triggers cleanup
get('button').click()
get('input').should('not.exist')
// Alpine should still be functional after cleanup
get('span').should(haveText('hello'))
}
)

test('x-model.blur cleanup does not crash when element without form is removed',
html`
<div x-data="{ show: true, value: '' }">
<template x-if="show">
<input x-model.blur="value" type="text">
</template>
<button @click="show = false">hide</button>
<span x-text="value"></span>
</div>
`,
({ get }) => {
get('input').type('world')
get('input').blur()
get('span').should(haveText('world'))
get('button').click()
get('input').should('not.exist')
get('span').should(haveText('world'))
}
)

test('x-model.blur form submit still works after sibling input is removed',
html`
<div x-data="{ showExtra: true, name: '', extra: '', submitted: '' }">
<form @submit.prevent="submitted = name + extra">
<input id="name" x-model.blur="name" type="text">
<template x-if="showExtra">
<input id="extra" x-model.blur="extra" type="text">
</template>
<button id="remove" type="button" @click="showExtra = false">remove</button>
<button id="submit" type="submit">submit</button>
</form>
<span x-text="submitted"></span>
</div>
`,
({ get }) => {
get('#name').type('Alice')
get('#extra').type('!')
get('#extra').blur()
// Remove the extra input — cleanup must not corrupt the form
get('#remove').click()
get('#extra').should('not.exist')
// Submit should still work with the remaining input
get('form').then(([form]) => form.requestSubmit())
get('span').should(haveText('Alice!'))
}
)

Loading