Merge pull request #3242 from umanghome/v3-class-deps-fix

[v3] Fix issue where class directives wouldn't work with spread props and class prop
pull/3219/head
Rich Harris 5 years ago committed by GitHub
commit 15c57e1248
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -548,6 +548,13 @@ export default class ElementWrapper extends Wrapper {
}
add_attributes(block: Block) {
// Get all the class dependencies first
this.attributes.forEach((attribute) => {
if (attribute.node.name === 'class' && attribute.node.is_dynamic) {
this.class_dependencies.push(...attribute.node.dependencies);
}
});
// @ts-ignore todo:
if (this.node.attributes.find(attr => attr.type === 'Spread')) {
this.add_spread_attributes(block);
@ -555,9 +562,6 @@ export default class ElementWrapper extends Wrapper {
}
this.attributes.forEach((attribute) => {
if (attribute.node.name === 'class' && attribute.node.is_dynamic) {
this.class_dependencies.push(...attribute.node.dependencies);
}
attribute.render(block);
});
}

@ -109,6 +109,9 @@ export default function(node: Element, renderer: Renderer, options: RenderOption
) {
// a boolean attribute with one non-Text chunk
args.push(`{ ${quote_name_if_necessary(attribute.name)}: ${snip(attribute.chunks[0])} }`);
} else if (attribute.name === 'class' && class_expression) {
// Add class expression
args.push(`{ ${quote_name_if_necessary(attribute.name)}: [\`${stringify_attribute(attribute, true)}\`, \`\${${class_expression}}\`].join(' ').trim() }`);
} else {
args.push(`{ ${quote_name_if_necessary(attribute.name)}: \`${stringify_attribute(attribute, true)}\` }`);
}

@ -0,0 +1,21 @@
export default {
props: {
myClass: 'one two',
attributes: {
role: 'button'
}
},
html: `<div class="one two three" role="button"></div>`,
test({ assert, component, target, window }) {
component.myClass = 'one';
component.attributes = {
'aria-label': 'Test'
};
assert.htmlEqual(target.innerHTML, `
<div class="one three" aria-label="Test"></div>
`);
}
};

@ -0,0 +1,6 @@
<script>
export let myClass;
export let attributes = {};
</script>
<div class={myClass} class:three={true} {...attributes}></div>

@ -0,0 +1,21 @@
export default {
props: {
myClass: 'one two',
attributes: {
role: 'button'
}
},
html: `<div class="one two" role="button"></div>`,
test({ assert, component, target, window }) {
component.myClass = 'one';
component.attributes = {
'aria-label': 'Test'
};
assert.htmlEqual(target.innerHTML, `
<div class="one" aria-label="Test"></div>
`);
}
};

@ -0,0 +1,6 @@
<script>
export let myClass;
export let attributes = {};
</script>
<div class={myClass} {...attributes}></div>
Loading…
Cancel
Save