mirror of https://github.com/sveltejs/svelte
commit
3aa1814b22
@ -0,0 +1,11 @@
|
|||||||
|
export default function getStaticAttributeValue ( node, name ) {
|
||||||
|
const attribute = node.attributes.find( attr => attr.name.toLowerCase() === name );
|
||||||
|
if ( !attribute ) return null;
|
||||||
|
|
||||||
|
if ( attribute.value.length !== 1 || attribute.value[0].type !== 'Text' ) {
|
||||||
|
// TODO catch this in validation phase, give a more useful error (with location etc)
|
||||||
|
throw new Error( `'${name} must be a static attribute` );
|
||||||
|
}
|
||||||
|
|
||||||
|
return attribute.value[0].data;
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
const values = [
|
||||||
|
{ name: 'Alpha' },
|
||||||
|
{ name: 'Beta' },
|
||||||
|
{ name: 'Gamma' }
|
||||||
|
];
|
||||||
|
|
||||||
|
export default {
|
||||||
|
data: {
|
||||||
|
values,
|
||||||
|
selected: [ values[1] ]
|
||||||
|
},
|
||||||
|
|
||||||
|
'skip-ssr': true, // values are rendered as [object Object]
|
||||||
|
|
||||||
|
html: `
|
||||||
|
<label>
|
||||||
|
<input type="checkbox"> Alpha
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
<input type="checkbox"> Beta
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
<input type="checkbox"> Gamma
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<p>Beta</p>`,
|
||||||
|
|
||||||
|
test ( assert, component, target, window ) {
|
||||||
|
const inputs = target.querySelectorAll( 'input' );
|
||||||
|
assert.equal( inputs[0].checked, false );
|
||||||
|
assert.equal( inputs[1].checked, true );
|
||||||
|
assert.equal( inputs[2].checked, false );
|
||||||
|
|
||||||
|
const event = new window.Event( 'change' );
|
||||||
|
|
||||||
|
inputs[0].checked = true;
|
||||||
|
inputs[0].dispatchEvent( event );
|
||||||
|
|
||||||
|
assert.htmlEqual( target.innerHTML, `
|
||||||
|
<label>
|
||||||
|
<input type="checkbox"> Alpha
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
<input type="checkbox"> Beta
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
<input type="checkbox"> Gamma
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<p>Alpha, Beta</p>
|
||||||
|
` );
|
||||||
|
|
||||||
|
component.set({ selected: [ values[1], values[2] ] });
|
||||||
|
assert.equal( inputs[0].checked, false );
|
||||||
|
assert.equal( inputs[1].checked, true );
|
||||||
|
assert.equal( inputs[2].checked, true );
|
||||||
|
|
||||||
|
assert.htmlEqual( target.innerHTML, `
|
||||||
|
<label>
|
||||||
|
<input type="checkbox"> Alpha
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
<input type="checkbox"> Beta
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label>
|
||||||
|
<input type="checkbox"> Gamma
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<p>Beta, Gamma</p>
|
||||||
|
` );
|
||||||
|
}
|
||||||
|
};
|
@ -0,0 +1,7 @@
|
|||||||
|
{{#each values as value}}
|
||||||
|
<label>
|
||||||
|
<input type="checkbox" value="{{value}}" bind:group='selected' /> {{value.name}}
|
||||||
|
</label>
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
|
<p>{{selected.map( function ( value ) { return value.name; }).join( ', ' ) }}</p>
|
Loading…
Reference in new issue