Skip to content

Commit

Permalink
x-model with fill modifier takes input value on null, empty string or…
Browse files Browse the repository at this point in the history
… undefined (#3941)
  • Loading branch information
rgbc-alex authored Jan 21, 2024
1 parent 88be3ec commit 35f9ac1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/alpinejs/src/directives/x-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ directive('model', (el, { modifiers, expression }, { effect, cleanup }) => {
})

if (modifiers.includes('fill'))
if ([null, ''].includes(getValue())
if ([undefined, null, ''].includes(getValue())
|| (el.type === 'checkbox' && Array.isArray(getValue()))) {
el.dispatchEvent(new Event(event, {}));
}
Expand Down
7 changes: 5 additions & 2 deletions tests/cypress/integration/directives/x-model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ test('x-model updates value when the form is reset',
}
)

test('x-model with fill modifier takes input value on null or empty string',
test('x-model with fill modifier takes input value on null, empty string or undefined',
html`
<div x-data="{ a: 123, b: 0, c: '', d: null }">
<div x-data="{ a: 123, b: 0, c: '', d: null, e: {} }">
<input x-model.fill="a" value="123456" />
<span id="a" x-text="a"></span>
<input x-model.fill="b" value="123456" />
Expand All @@ -221,13 +221,16 @@ test('x-model with fill modifier takes input value on null or empty string',
<span id="c" x-text="c"></span>
<input x-model.fill="d" value="123456" />
<span id="d" x-text="d"></span>
<input x-model.fill="e.a" value="123456" />
<span id="e" x-text="e.a"></span>
</div>
`,
({ get }) => {
get('#a').should(haveText('123'))
get('#b').should(haveText('0'))
get('#c').should(haveText('123456'))
get('#d').should(haveText('123456'))
get('#e').should(haveText('123456'))
}
)

Expand Down

0 comments on commit 35f9ac1

Please sign in to comment.