Skip to content

Commit

Permalink
Merge pull request #606 from sveltejs/gh-584
Browse files Browse the repository at this point in the history
coerce empty string in number/range inputs to undefined
  • Loading branch information
Rich-Harris authored Jun 1, 2017
2 parents 66a1fd7 + 6ae01b6 commit b5b484b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/generators/dom/visitors/Element/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function getBindingValue ( generator: DomGenerator, block: Block, state: State,

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

// everything else
Expand Down
4 changes: 4 additions & 0 deletions src/shared/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ export function getBindingGroupValue ( group ) {
if ( group[i].checked ) value.push( group[i].__value );
}
return value;
}

export function toNumber ( value ) {
return value === '' ? undefined : +value;
}
10 changes: 10 additions & 0 deletions test/runtime/samples/binding-input-number/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,15 @@ export default {
<input type='number'>
<p>number 44</p>
` );

// empty string should be treated as undefined
input.value = '';
input.dispatchEvent( event );

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

0 comments on commit b5b484b

Please sign in to comment.