Skip to content

Commit 24f5dc2

Browse files
committed
use input events for two-way binding with textareas and non-checkbox/radio inputs (#10)
1 parent db486ab commit 24f5dc2

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/generators/dom/visitors/attributes/binding/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ export default function createBinding ( generator, node, attribute, current, loc
3333

3434
let eventName = 'change';
3535
if ( node.name === 'input' ) {
36-
const type = node.attributes.find( attr => attr.type === 'Attribute' && attr.name === 'type' );
37-
if ( !type || type.value[0].data === 'text' ) {
38-
// TODO in validation, should throw if type attribute is not static
36+
const typeAttribute = node.attributes.find( attr => attr.type === 'Attribute' && attr.name === 'type' );
37+
const type = typeAttribute ? typeAttribute.value[0].data : 'text'; // TODO in validation, should throw if type attribute is not static
38+
39+
if ( type !== 'checkbox' && type !== 'radio' ) {
3940
eventName = 'input';
4041
}
4142
}
4243

44+
else if ( node.name === 'textarea' ) {
45+
eventName = 'input';
46+
}
47+
4348
let value;
4449

4550
if ( local.isComponent ) {
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export default {
2+
data: {
3+
value: 'some text'
4+
},
5+
6+
html: `
7+
<textarea></textarea>
8+
<p>some text</p>
9+
`,
10+
11+
test ( assert, component, target, window ) {
12+
const textarea = target.querySelector( 'textarea' );
13+
assert.equal( textarea.value, 'some text' );
14+
15+
const event = new window.Event( 'input' );
16+
17+
textarea.value = 'hello';
18+
textarea.dispatchEvent( event );
19+
20+
assert.htmlEqual( target.innerHTML, `
21+
<textarea></textarea>
22+
<p>hello</p>
23+
` );
24+
25+
component.set({ value: 'goodbye' });
26+
assert.equal( textarea.value, 'goodbye' );
27+
assert.htmlEqual( target.innerHTML, `
28+
<textarea></textarea>
29+
<p>goodbye</p>
30+
` );
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<textarea bind:value></textarea>
2+
<p>{{value}}</p>

0 commit comments

Comments
 (0)