Skip to content

Commit

Permalink
fix: single checkbox binding true/false
Browse files Browse the repository at this point in the history
  • Loading branch information
remy committed Nov 19, 2020
1 parent fa17a03 commit b233590
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
14 changes: 14 additions & 0 deletions cypress/fixtures/checkbox.html
@@ -0,0 +1,14 @@
<input type="checkbox" id="one">
<input type="checkbox" id="two" value="a">
<script src="./bind.js"></script>
<script>
data = new Bind(
{
one: false,
two: true,
},
{
one: '#one',
two: '#two',
});
</script>
27 changes: 27 additions & 0 deletions cypress/integration/checkbox.browser.js
@@ -0,0 +1,27 @@
describe('checkboxes', function () {
beforeEach(function () {
cy.visit('/checkbox.html').then((win) => {
cy.wrap({ data: win.data });
});
});

it('should have checked boxes', () => {
cy.get('input:checked').should((t) => {
expect(t).have.length(1);
});
});

it.only('should check the box when data changes', () => {
let data;
cy.window()
.its('data')
.then((d) => {
data = d;
data.one = true;
})
.get('input:checked')
.should((t) => {
expect(t).have.length(2);
});
});
});
11 changes: 7 additions & 4 deletions lib/bind.js
Expand Up @@ -146,7 +146,8 @@ var Bind = (function Bind(global) {
var path = [].slice.call(_path);
var callback;
var transform = function (v) {
return safe(v);
if (typeof v === 'string') return safe(v);
return v;
};
var parse = pass;

Expand Down Expand Up @@ -234,8 +235,8 @@ var Bind = (function Bind(global) {
// special case for multi-select items
var result = transform(value, target);
if (element.type === 'checkbox') {
if (value instanceof Array) {
var found = value.filter(function (value) {
if (result instanceof Array) {
var found = result.filter(function (value) {
if (value === element.value) {
element.checked = element.value === value;
return true;
Expand All @@ -245,7 +246,9 @@ var Bind = (function Bind(global) {
element.checked = false;
}
} else if (typeof result === 'boolean') {
element.checked = value;
element.checked = result;
} else {
// console.log('no op');
}
} else if (element.type === 'radio') {
element.checked = element.value === result;
Expand Down

0 comments on commit b233590

Please sign in to comment.