Skip to content

Commit

Permalink
Merge pull request #591 from sveltejs/gh-590
Browse files Browse the repository at this point in the history
On `<select>`, don't try generating prop code until visiting attributes
  • Loading branch information
Rich-Harris authored May 21, 2017
2 parents 7c2fd8e + a3860f2 commit 2194de9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 34 deletions.
68 changes: 34 additions & 34 deletions src/generators/dom/visitors/Element/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function visitElement ( generator, block, state, node ) {
block.builders.create.addLine( `${generator.helper( 'setAttribute' )}( ${name}, '${generator.cssId}', '' );` );
}

function visitAttributes () {
function visitAttributesAndAddProps () {
let intro;
let outro;

Expand All @@ -63,6 +63,37 @@ export default function visitElement ( generator, block, state, node ) {
});

if ( intro || outro ) addTransitions( generator, block, childState, node, intro, outro );

if ( childState.allUsedContexts.length || childState.usesComponent ) {
const initialProps = [];
const updates = [];

if ( childState.usesComponent ) {
initialProps.push( `component: ${block.component}` );
}

childState.allUsedContexts.forEach( contextName => {
if ( contextName === 'state' ) return;

const listName = block.listNames.get( contextName );
const indexName = block.indexNames.get( contextName );

initialProps.push( `${listName}: ${listName},\n${indexName}: ${indexName}` );
updates.push( `${name}._svelte.${listName} = ${listName};\n${name}._svelte.${indexName} = ${indexName};` );
});

if ( initialProps.length ) {
block.builders.create.addBlock( deindent`
${name}._svelte = {
${initialProps.join( ',\n' )}
};
` );
}

if ( updates.length ) {
block.builders.update.addBlock( updates.join( '\n' ) );
}
}
}

if ( !state.parentNode ) {
Expand All @@ -74,7 +105,7 @@ export default function visitElement ( generator, block, state, node ) {
if ( node.name !== 'select' ) {
// <select> value attributes are an annoying special case — it must be handled
// *after* its children have been updated
visitAttributes();
visitAttributesAndAddProps();
}

// special case – bound <option> without a value attribute
Expand All @@ -83,37 +114,6 @@ export default function visitElement ( generator, block, state, node ) {
node.initialUpdate = node.lateUpdate = statement;
}

if ( childState.allUsedContexts.length || childState.usesComponent ) {
const initialProps = [];
const updates = [];

if ( childState.usesComponent ) {
initialProps.push( `component: ${block.component}` );
}

childState.allUsedContexts.forEach( contextName => {
if ( contextName === 'state' ) return;

const listName = block.listNames.get( contextName );
const indexName = block.indexNames.get( contextName );

initialProps.push( `${listName}: ${listName},\n${indexName}: ${indexName}` );
updates.push( `${name}._svelte.${listName} = ${listName};\n${name}._svelte.${indexName} = ${indexName};` );
});

if ( initialProps.length ) {
block.builders.create.addBlock( deindent`
${name}._svelte = {
${initialProps.join( ',\n' )}
};
` );
}

if ( updates.length ) {
block.builders.update.addBlock( updates.join( '\n' ) );
}
}

node.children.forEach( child => {
visit( generator, block, childState, child );
});
Expand All @@ -123,7 +123,7 @@ export default function visitElement ( generator, block, state, node ) {
}

if ( node.name === 'select' ) {
visitAttributes();
visitAttributesAndAddProps();
}

if ( node.initialUpdate ) {
Expand Down
15 changes: 15 additions & 0 deletions test/runtime/samples/select-props/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default {
test ( assert, component, target, window ) {
const selects = document.querySelectorAll( 'select' );

const event1 = new window.Event( 'change' );
selects[0].value = 'b';
selects[0].dispatchEvent(event1);

const event2 = new window.Event( 'change' );
selects[1].value = 'b';
selects[1].dispatchEvent(event2);

assert.deepEqual( component.get( 'log' ), [ 1, 2 ] );
}
};
19 changes: 19 additions & 0 deletions test/runtime/samples/select-props/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{#each foo as bar}}
<select on:change='handler(bar)'>
<option>a</option>
<option>b</option>
</select>
{{/each}}

<script>
export default {
data: () => ({ foo: [ 1, 2 ], log: [] }),
methods: {
handler ( bar ) {
let { log } = this.get();
log.push( bar );
this.set( { log } );
}
}
}
</script>

0 comments on commit 2194de9

Please sign in to comment.