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 fill modifier for radio buttons #4101

Merged
merged 4 commits into from
Mar 21, 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
22 changes: 18 additions & 4 deletions packages/alpinejs/src/directives/x-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,27 @@ function getInputValue(el, modifiers, event, currentValue) {
return option.value || option.text
})
} else {
let newValue

if (el.type === 'radio') {
if (event.target.checked) {
newValue = event.target.value
} else {
newValue = currentValue
}
} else {
newValue = event.target.value
}

if (modifiers.includes('number')) {
return safeParseNumber(event.target.value)
return safeParseNumber(newValue)
} else if (modifiers.includes('boolean')) {
return safeParseBoolean(event.target.value)
return safeParseBoolean(newValue)
} else if (modifiers.includes('trim')) {
return newValue.trim()
} else {
return newValue
}

return modifiers.includes('trim') ? event.target.value.trim() : event.target.value
}
})
}
Expand Down
31 changes: 30 additions & 1 deletion tests/cypress/integration/directives/x-model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ test('x-model with fill modifier takes input value on null, empty string or unde
}
)

test('x-model with fill modifier works with select/radio elements',
test('x-model with fill modifier works with select elements',
html`
<div x-data="{ a: null, b: null, c: null, d: null }">
<select x-model.fill="a">
Expand All @@ -253,6 +253,35 @@ test('x-model with fill modifier works with select/radio elements',
}
);

test('x-model with fill modifier works with radio elements',
html`
<div x-data="{ a: null, b: null, c: '101112', d: null }">
<input x-model.fill="a" type="radio" value="123" />
<input x-model.fill="a" type="radio" value="456" checked />
<input x-model.fill="a" type="radio" value="789" />
<input x-model.fill="a" type="radio" value="101112" />
<input x-model.fill="a" type="radio" value="131415" />

<input x-model.fill="b" name="b" type="radio" value="123" />
<input x-model.fill="b" name="b" type="radio" value="456" />
<input x-model.fill="b" name="b" type="radio" value="789" checked />
<input x-model.fill="b" name="b" type="radio" value="101112" />
<input x-model.fill="b" name="b" type="radio" value="131415" />

<input x-model.fill="c" type="radio" value="123" />
<input x-model.fill="c" type="radio" value="456" />
<input x-model.fill="c" type="radio" value="789" />
<input x-model.fill="c" type="radio" value="101112" />
<input x-model.fill="c" type="radio" value="131415" />
</div>
`,
({ get }) => {
get('[x-data]').should(haveData('a', '456'));
get('[x-data]').should(haveData('b', '789'));
get('[x-data]').should(haveData('c', '101112'));
}
);

test('x-model with fill modifier respects number modifier',
html`
<div x-data="{ a: null, b: null, c: null, d: null }">
Expand Down
Loading