Skip to content

Commit

Permalink
Merge pull request #232 from philippbosch/checkbox-values
Browse files Browse the repository at this point in the history
Allow checkboxes to have distinct values
  • Loading branch information
calebporzio authored Mar 7, 2020
2 parents 98d26e1 + 0a07fe9 commit 3fab0cc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
11 changes: 9 additions & 2 deletions dist/alpine-ie11.js
Original file line number Diff line number Diff line change
Expand Up @@ -4943,9 +4943,9 @@

return new Function(['dataContext'].concat(_toConsumableArray(Object.keys(additionalHelperVariables))), "with(dataContext) { ".concat(expression, " }")).apply(void 0, [dataContext].concat(_toConsumableArray(Object.values(additionalHelperVariables))));
}
var xAttrRE = /^x-(on|bind|data|text|html|model|if|for|show|cloak|transition|ref)\b/;
function isXAttr(attr) {
var name = replaceAtAndColonWithStandardSyntax(attr.name);
var xAttrRE = /x-(on|bind|data|text|html|model|if|for|show|cloak|transition|ref)/;
return xAttrRE.test(name);
}
function getXAttrs(el, type) {
Expand All @@ -4957,7 +4957,7 @@
_newArrowCheck(this, _this2);

var name = replaceAtAndColonWithStandardSyntax(attr.name);
var typeMatch = name.match(/x-(on|bind|data|text|html|model|if|for|show|cloak|transition|ref)/);
var typeMatch = name.match(xAttrRE);
var valueMatch = name.match(/:([a-zA-Z\-:]+)/);
var modifiers = name.match(/\.[^.\]]+(?=[^\]]*$)/g) || [];
return {
Expand Down Expand Up @@ -5522,6 +5522,13 @@
el.checked = valueFound;
} else {
el.checked = !!value;
} // If we are explicitly binding a string to the :value, set the string,
// If the value is a boolean, leave it alone, it will be set to "on"
// automatically.


if (typeof value === 'string') {
el.value = value;
}
} else if (el.tagName === 'SELECT') {
updateSelect(el, value);
Expand Down
7 changes: 7 additions & 0 deletions dist/alpine.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,13 @@
el.checked = valueFound;
} else {
el.checked = !!value;
} // If we are explicitly binding a string to the :value, set the string,
// If the value is a boolean, leave it alone, it will be set to "on"
// automatically.


if (typeof value === 'string') {
el.value = value;
}
} else if (el.tagName === 'SELECT') {
updateSelect(el, value);
Expand Down
6 changes: 6 additions & 0 deletions src/directives/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export function handleAttributeBindingDirective(component, el, attrName, express
} else {
el.checked = !! value
}
// If we are explicitly binding a string to the :value, set the string,
// If the value is a boolean, leave it alone, it will be set to "on"
// automatically.
if (typeof value === 'string') {
el.value = value
}
} else if (el.tagName === 'SELECT') {
updateSelect(el, value)
} else {
Expand Down
16 changes: 16 additions & 0 deletions test/bind.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,19 @@ test('binding supports short syntax', async () => {

expect(document.querySelector('span').classList.contains('bar')).toBeTruthy()
})

test('checkbox values are set correctly', async () => {
document.body.innerHTML = `
<div x-data="{ stringValue: 'foo', trueValue: true, falseValue: false }">
<input type="checkbox" name="stringCheckbox" :value="stringValue" />
<input type="checkbox" name="trueCheckbox" :value="trueValue" />
<input type="checkbox" name="falseCheckbox" :value="falseValue" />
</div>
`

Alpine.start()

expect(document.querySelector('input[name="trueCheckbox"]').value).toEqual('on')
expect(document.querySelector('input[name="falseCheckbox"]').value).toEqual('on')
expect(document.querySelector('input[name="stringCheckbox"]').value).toEqual('foo')
});

0 comments on commit 3fab0cc

Please sign in to comment.