-
Notifications
You must be signed in to change notification settings - Fork 668
feat: support object class binding in stubbed functional components #1476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,10 +60,25 @@ function getCoreProperties(componentOptions: Component): Object { | |
} | ||
|
||
function createClassString(staticClass, dynamicClass) { | ||
if (staticClass && dynamicClass) { | ||
return staticClass + ' ' + dynamicClass | ||
// :class="someComputedObject" can return a string, object or undefined | ||
// if it is a string, we don't need to do anything special. | ||
let evaluatedDynamicClass = dynamicClass | ||
|
||
// if it is an object, eg { 'foo': true }, we need to evaluate it. | ||
// see https://github.com/vuejs/vue-test-utils/issues/1474 for more context. | ||
if (typeof dynamicClass === 'object') { | ||
evaluatedDynamicClass = Object.keys(dynamicClass).reduce((acc, key) => { | ||
if (dynamicClass[key] === true) { | ||
return acc + ' ' + key | ||
} | ||
return acc | ||
}, '') | ||
} | ||
|
||
if (staticClass && evaluatedDynamicClass) { | ||
return staticClass + ' ' + evaluatedDynamicClass | ||
} | ||
return staticClass || dynamicClass | ||
return staticClass || evaluatedDynamicClass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldnt have pushing all to an array and using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think both are fine - was having problems iterating over I like |
||
} | ||
|
||
function resolveOptions(component, _Vue) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<template> | ||
<functional-component | ||
:class="{ bar: a + b === 2, foo: a === 1, qux: a === 2 }" | ||
/> | ||
</template> | ||
|
||
<script> | ||
import FunctionalComponent from './functional-component.vue' | ||
|
||
export default { | ||
components: { | ||
FunctionalComponent | ||
}, | ||
|
||
data() { | ||
return { | ||
a: 1, | ||
b: 1 | ||
} | ||
} | ||
} | ||
</script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check if this is a truthy value or like this, straight up true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, I just went with this to be explicit. I haven't seen people use something like
:class={ 'my-class': 'some-truthy-val' }
. I guess we should just copy whatever Vue does - will try it out.