-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: make tag name a computed property * feat: render default slot content * feat: render default slot content * feat: render named slot content * fix: Node -> Child * fix: avoid referencing slots in computed
- Loading branch information
Showing
8 changed files
with
225 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<script lang="ts"> | ||
import Vue, { VNode } from 'vue' | ||
import Child from './Child.vue' | ||
import { Element } from '@/parser/template/types' | ||
import { DefaultValue, ChildComponent } from '@/parser/script/types' | ||
export default Vue.extend({ | ||
name: 'NodeSlot', | ||
functional: true, | ||
props: { | ||
uri: { | ||
type: String, | ||
required: true | ||
}, | ||
data: { | ||
type: Object as { (): Element }, | ||
required: true | ||
}, | ||
scope: { | ||
type: Object as { (): Record<string, DefaultValue> }, | ||
required: true | ||
}, | ||
childComponents: { | ||
type: Array as { (): ChildComponent[] }, | ||
required: true | ||
}, | ||
slots: { | ||
type: Object as { (): Record<string, VNode[]> }, | ||
required: true | ||
} | ||
}, | ||
render(h, { props }): any /* VNode[] */ { | ||
const { data, slots } = props | ||
const slotAttr = data.startTag.attributes.find(attr => attr.name === 'name') | ||
const slot = slots[slotAttr ? slotAttr.value : 'default'] | ||
if (!slot) { | ||
// placeholder content | ||
return props.data.children.map(child => { | ||
return h(Child, { | ||
props: { | ||
uri: props.uri, | ||
data: child, | ||
scope: props.scope, | ||
childComponents: props.childComponents, | ||
slots | ||
} | ||
}) | ||
}) | ||
} | ||
return slot | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`VueComponent slot renders named slot content 1`] = `"<div><style></style><div class=\\"\\"><style></style><div data-scope-scope-id=\\"\\" class=\\"\\"><p data-scope-scope-id=\\"\\" class=\\"\\">foo content</p><p slot=\\"test\\" class=\\"\\">named</p></div></div></div>"`; | ||
exports[`VueComponent slot renders placeholder slot content 1`] = `"<div><style></style><div class=\\"\\"><p class=\\"\\">placeholder content</p></div></div>"`; | ||
exports[`VueComponent slot renders slot content 1`] = `"<div><style></style><div class=\\"\\"><style></style><div data-scope-scope-id=\\"\\" class=\\"\\"><p data-scope-scope-id=\\"\\" class=\\"\\">foo content</p><p class=\\"\\">injected</p></div></div></div>"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { createTemplate, h, render, a } from '../../helpers/template' | ||
|
||
describe('VueComponent slot', () => { | ||
it('renders placeholder slot content', () => { | ||
// prettier-ignore | ||
const template = createTemplate([ | ||
h('div', [], [ | ||
h('slot', [], [ | ||
h('p', [], ['placeholder content']) | ||
]) | ||
]) | ||
]) | ||
|
||
const wrapper = render(template) | ||
expect(wrapper.html()).toMatchSnapshot() | ||
}) | ||
|
||
it('renders slot content', () => { | ||
// prettier-ignore | ||
const template = createTemplate([ | ||
h('Foo', [], [ | ||
h('p', [], ['injected']) | ||
]) | ||
]) | ||
|
||
const components = { | ||
'file://Foo.vue': { | ||
// prettier-ignore | ||
template: createTemplate([ | ||
h('div', [], [ | ||
h('p', [], ['foo content']), | ||
h('slot', [], []) | ||
]) | ||
]) | ||
} | ||
} | ||
|
||
const wrapper = render( | ||
template, | ||
[], | ||
[], | ||
[ | ||
{ | ||
name: 'Foo', | ||
uri: 'file://Foo.vue' | ||
} | ||
], | ||
components | ||
) | ||
|
||
expect(wrapper.html()).toMatchSnapshot() | ||
}) | ||
|
||
it('renders named slot content', () => { | ||
// prettier-ignore | ||
const template = createTemplate([ | ||
h('Foo', [], [ | ||
h('p', [], ['default']), | ||
h('p', [a('slot', 'test')], ['named']) | ||
]) | ||
]) | ||
|
||
const components = { | ||
'file://Foo.vue': { | ||
// prettier-ignore | ||
template: createTemplate([ | ||
h('div', [], [ | ||
h('p', [], ['foo content']), | ||
h('slot', [a('name', 'test')], []) | ||
]) | ||
]) | ||
} | ||
} | ||
|
||
const wrapper = render( | ||
template, | ||
[], | ||
[], | ||
[ | ||
{ | ||
name: 'Foo', | ||
uri: 'file://Foo.vue' | ||
} | ||
], | ||
components | ||
) | ||
|
||
expect(wrapper.html()).toMatchSnapshot() | ||
}) | ||
}) |