Merge pull request #309 from sveltejs/gh-10

use input events for two-way binding with textareas and non-checkbox/radio inputs
pull/314/head
Rich Harris 8 years ago committed by GitHub
commit 8d15078b97

@ -33,13 +33,18 @@ export default function createBinding ( generator, node, attribute, current, loc
let eventName = 'change'; let eventName = 'change';
if ( node.name === 'input' ) { if ( node.name === 'input' ) {
const type = node.attributes.find( attr => attr.type === 'Attribute' && attr.name === 'type' ); const typeAttribute = node.attributes.find( attr => attr.type === 'Attribute' && attr.name === 'type' );
if ( !type || type.value[0].data === 'text' ) { const type = typeAttribute ? typeAttribute.value[0].data : 'text'; // TODO in validation, should throw if type attribute is not static
// TODO in validation, should throw if type attribute is not static
if ( type !== 'checkbox' && type !== 'radio' ) {
eventName = 'input'; eventName = 'input';
} }
} }
else if ( node.name === 'textarea' ) {
eventName = 'input';
}
let value; let value;
if ( local.isComponent ) { if ( local.isComponent ) {

@ -0,0 +1,32 @@
export default {
data: {
value: 'some text'
},
html: `
<textarea></textarea>
<p>some text</p>
`,
test ( assert, component, target, window ) {
const textarea = target.querySelector( 'textarea' );
assert.equal( textarea.value, 'some text' );
const event = new window.Event( 'input' );
textarea.value = 'hello';
textarea.dispatchEvent( event );
assert.htmlEqual( target.innerHTML, `
<textarea></textarea>
<p>hello</p>
` );
component.set({ value: 'goodbye' });
assert.equal( textarea.value, 'goodbye' );
assert.htmlEqual( target.innerHTML, `
<textarea></textarea>
<p>goodbye</p>
` );
}
};

@ -0,0 +1,2 @@
<textarea bind:value></textarea>
<p>{{value}}</p>
Loading…
Cancel
Save