-
Notifications
You must be signed in to change notification settings - Fork 672
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
feat: add scoped slots option #507
Changes from 1 commit
cecf8eb
e7da1f7
37c5950
3c841a1
ea2d16f
8f3239f
d4e7bdf
e6ad08d
8da1a4e
7665971
0953171
f737344
d925dcf
58399c5
4678bfb
032513c
a1f008c
cb000ea
4690a6a
73a7e5f
125c39c
303d1a6
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 |
---|---|---|
|
@@ -9,6 +9,9 @@ export function addScopedSlots (vm: Component, scopedSlots: Object): void { | |
if (template.substr(0, 9) === '<template') { | ||
throwError('the scopedSlots option does not support a template tag as the root element.') | ||
} | ||
const domParser = new window.DOMParser() | ||
const document = domParser.parseFromString(template, 'text/html') | ||
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. Can you rename this variable to avoid collisions with 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 changed it. |
||
vm.$_vueTestUtils_scopedSlots[key] = compileToFunctions(template).render | ||
vm.$_vueTestUtils_slotScopes[key] = document.body.firstChild.getAttribute('slot-scope') | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,20 +60,33 @@ export default function createInstance ( | |
addListeners(vm, options.listeners) | ||
|
||
if (options.scopedSlots) { | ||
if (window.navigator.userAgent.match(/PhantomJS/i)) { | ||
throwError('the scopedSlots option does not support strings in PhantomJS. Please use Puppeteer, or pass a component.') | ||
} | ||
const vueVersion = Number(`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`) | ||
if (vueVersion >= 2.5) { | ||
vm.$_vueTestUtils_scopedSlots = {} | ||
vm.$_vueTestUtils_slotScopes = {} | ||
const renderSlot = vm._renderProxy._t | ||
|
||
vm._renderProxy._t = function (name, feedback, props, bindObject) { | ||
const scopedSlotFn = vm.$_vueTestUtils_scopedSlots[name] | ||
const slotScope = vm.$_vueTestUtils_slotScopes[name] | ||
if (scopedSlotFn) { | ||
props = { ...bindObject, ...props } | ||
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. Why do we need to set vm._renderProxy.props? 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.
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. There is a bug. |
||
vm._renderProxy.props = props | ||
return scopedSlotFn.call(vm._renderProxy) | ||
const proxy = {} | ||
Object.keys(vm._renderProxy).concat(Object.keys(Vue.prototype)).forEach((key) => { | ||
if (key[0] === '_') { | ||
proxy[key] = vm._renderProxy[key] | ||
} | ||
}) | ||
proxy[slotScope] = props | ||
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. Since when
scopedSlotFn
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. Does Vue throw an error if there are two conflicting slot scopes? If not, we should throw an error here 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. It is impossible to set a word that starts with "_" in Vue.
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 meant if you mounted with this option:
The second scopedSlot would overwrite the first scopedSlots in 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. It is not possible to register the same key.
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. 🤦♂️ Of course ;p |
||
return scopedSlotFn.call(proxy) | ||
} else { | ||
return renderSlot.call(vm._renderProxy, name, feedback, props, bindObject) | ||
} | ||
} | ||
|
||
// $FlowIgnore | ||
addScopedSlots(vm, options.scopedSlots) | ||
} else { | ||
|
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.
Can you change to: