Skip to content

Commit

Permalink
read values from range/number inputs as numbers - fixes #436
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Apr 2, 2017
1 parent a3ecb67 commit 605040d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/generators/dom/visitors/attributes/addElementBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ function getBindingValue ( generator, local, node, attribute, isMultipleSelect,
return `${local.name}.__value`;
}

// <input type='range|number' bind:value>
if ( type === 'range' || type === 'number' ) {
return `+${local.name}.${attribute.name}`;
}

// everything else
return `${local.name}.${attribute.name}`;
}
Expand Down
33 changes: 33 additions & 0 deletions test/generator/samples/binding-input-number/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default {
data: {
count: 42
},

html: `
<input type='number'>
<p>number 42</p>
`,

test ( assert, component, target, window ) {
const input = target.querySelector( 'input' );
assert.equal( input.value, '42' );

const event = new window.Event( 'input' );

input.value = '43';
input.dispatchEvent( event );

assert.equal( component.get( 'count' ), 43 );
assert.htmlEqual( target.innerHTML, `
<input type='number'>
<p>number 43</p>
` );

component.set({ count: 44 });
assert.equal( input.value, '44' );
assert.htmlEqual( target.innerHTML, `
<input type='number'>
<p>number 44</p>
` );
}
};
2 changes: 2 additions & 0 deletions test/generator/samples/binding-input-number/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<input type='number' bind:value='count'>
<p>{{typeof count}} {{count}}</p>
33 changes: 33 additions & 0 deletions test/generator/samples/binding-input-range/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default {
data: {
count: 42
},

html: `
<input type='range'>
<p>number 42</p>
`,

test ( assert, component, target, window ) {
const input = target.querySelector( 'input' );
assert.equal( input.value, '42' );

const event = new window.Event( 'input' );

input.value = '43';
input.dispatchEvent( event );

assert.equal( component.get( 'count' ), 43 );
assert.htmlEqual( target.innerHTML, `
<input type='range'>
<p>number 43</p>
` );

component.set({ count: 44 });
assert.equal( input.value, '44' );
assert.htmlEqual( target.innerHTML, `
<input type='range'>
<p>number 44</p>
` );
}
};
2 changes: 2 additions & 0 deletions test/generator/samples/binding-input-range/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<input type='range' bind:value='count'>
<p>{{typeof count}} {{count}}</p>

0 comments on commit 605040d

Please sign in to comment.