mirror of https://github.com/sveltejs/svelte
allow components to have computed member expressions for bindings (fixes #624)
parent
5c53f5b6a2
commit
e0917fd874
@ -0,0 +1,7 @@
|
||||
import { Node } from '../interfaces';
|
||||
|
||||
export default function getObject(node: Node) {
|
||||
// TODO validation should ensure this is an Identifier or a MemberExpression
|
||||
while (node.type === 'MemberExpression') node = node.object;
|
||||
return node;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
import { Node } from '../interfaces';
|
||||
|
||||
export default function getTailSnippet(node: Node) {
|
||||
const end = node.end;
|
||||
while (node.type === 'MemberExpression') node = node.object;
|
||||
const start = node.end;
|
||||
|
||||
return `[✂${start}-${end}✂]`;
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
<label>
|
||||
{{field}} <input bind:value>
|
||||
</label>
|
@ -0,0 +1,34 @@
|
||||
export default {
|
||||
html: `
|
||||
<label>firstname <input></label>
|
||||
<label>lastname <input></label>
|
||||
`,
|
||||
|
||||
test ( assert, component, target, window ) {
|
||||
const input = new window.Event( 'input' );
|
||||
const inputs = target.querySelectorAll( 'input' );
|
||||
|
||||
inputs[0].value = 'Ada';
|
||||
inputs[0].dispatchEvent(input);
|
||||
assert.deepEqual(component.get('values'), {
|
||||
firstname: 'Ada',
|
||||
lastname: ''
|
||||
});
|
||||
|
||||
inputs[1].value = 'Lovelace';
|
||||
inputs[1].dispatchEvent(input);
|
||||
assert.deepEqual(component.get('values'), {
|
||||
firstname: 'Ada',
|
||||
lastname: 'Lovelace'
|
||||
});
|
||||
|
||||
component.set({
|
||||
values: {
|
||||
firstname: 'Grace',
|
||||
lastname: 'Hopper'
|
||||
}
|
||||
});
|
||||
assert.equal(inputs[0].value, 'Grace');
|
||||
assert.equal(inputs[1].value, 'Hopper');
|
||||
}
|
||||
};
|
@ -0,0 +1,22 @@
|
||||
{{#each fields as field}}
|
||||
<Nested :field bind:value='values[field]'/>
|
||||
{{/each}}
|
||||
|
||||
<script>
|
||||
import Nested from './Nested.html';
|
||||
|
||||
export default {
|
||||
data: function () {
|
||||
return {
|
||||
fields: ['firstname', 'lastname'],
|
||||
values: {
|
||||
firstname: '',
|
||||
lastname: ''
|
||||
}
|
||||
};
|
||||
},
|
||||
components: {
|
||||
Nested
|
||||
}
|
||||
};
|
||||
</script>
|
Loading…
Reference in new issue