Skip to content

Commit ff7e790

Browse files
authored
docs(vue): add more scopedSlot content tests and readme (#2226)
1 parent 2489182 commit ff7e790

File tree

3 files changed

+147
-9
lines changed

3 files changed

+147
-9
lines changed

packages/vue/docs/demos/questions/scoped-slot.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const TextPreviewer = {
1616
render(h, context) {
1717
return h('div', {}, [
1818
context.scopedSlots.default({
19-
scopedProp: 'default 作用域插槽',
19+
slotProp: 'default 作用域插槽组件的插槽属性值',
2020
onScopedFunc: ($event) => {
2121
alert($event)
2222
},
@@ -53,7 +53,7 @@ const ScopedSlotComponent = {
5353
},
5454
},
5555
},
56-
[props.scopedProp]
56+
[props.slotProp]
5757
)
5858
},
5959
}

packages/vue/docs/questions/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ sidebar: auto
3232

3333
## 如何使用作用域插槽?
3434

35-
`x-content` 使用函数式组件时, 渲染函数增加第二个参数,通过其 `props` 成员访问作用域插槽传入属性。
35+
`x-content` 使用函数式组件时, 渲染函数增加第二个参数,通过其 `props` 成员访问作用域插槽传入属性,支持 observer() 和 connect() 接入组件
3636

3737
<dumi-previewer demoPath="questions/scoped-slot" />

packages/vue/src/__tests__/schema.json.spec.ts

Lines changed: 144 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { render, waitFor } from '@testing-library/vue'
44
import { mount } from '@vue/test-utils'
55
import Vue, { FunctionalComponentOptions } from 'vue'
66
import { FormProvider, createSchemaField } from '../vue2-components'
7+
import { connect, mapProps, mapReadPretty } from '../'
8+
import { defineComponent } from 'vue-demi'
79

810
Vue.component('FormProvider', FormProvider)
911

@@ -81,7 +83,7 @@ const Previewer3: FunctionalComponentOptions = {
8183
},
8284
[
8385
context.scopedSlots.default({
84-
scopedProp: '123',
86+
slotProp: '123',
8587
}),
8688
]
8789
)
@@ -100,7 +102,7 @@ const Previewer4: FunctionalComponentOptions = {
100102
},
101103
[
102104
context.scopedSlots.content({
103-
scopedProp: '123',
105+
slotProp: '123',
104106
}),
105107
]
106108
)
@@ -444,7 +446,7 @@ describe('x-content', () => {
444446
const Content = {
445447
functional: true,
446448
render(h, context) {
447-
return h('span', context.props.scopedProp)
449+
return h('span', context.props.slotProp)
448450
},
449451
}
450452

@@ -481,7 +483,7 @@ describe('x-content', () => {
481483
const Content = {
482484
functional: true,
483485
render(h, context) {
484-
return h('span', context.props.scopedProp)
486+
return h('span', context.props.slotProp)
485487
},
486488
}
487489
const { SchemaField } = createSchemaField({
@@ -520,7 +522,7 @@ describe('x-content', () => {
520522
const Content = {
521523
functional: true,
522524
render(h, context) {
523-
return h('span', context.props.scopedProp)
525+
return h('span', context.props.slotProp)
524526
},
525527
}
526528
const { SchemaField } = createSchemaField({
@@ -558,7 +560,7 @@ describe('x-content', () => {
558560
const Content = {
559561
functional: true,
560562
render(h, context) {
561-
return h('span', context.props.scopedProp)
563+
return h('span', context.props.slotProp)
562564
},
563565
}
564566
const { SchemaField } = createSchemaField({
@@ -591,6 +593,142 @@ describe('x-content', () => {
591593
expect(queryByTestId('previewer4').textContent).toEqual('123')
592594
})
593595

596+
test('scoped slot with connect', () => {
597+
const form = createForm()
598+
const ConnectedComponent = connect(
599+
defineComponent({
600+
render(h) {
601+
return h(
602+
'div',
603+
{
604+
attrs: {
605+
'data-testid': 'ConnectedComponent',
606+
},
607+
},
608+
[
609+
this.$scopedSlots.default({
610+
slotProp: '123',
611+
}),
612+
]
613+
)
614+
},
615+
}),
616+
mapProps((props, field) => {
617+
return {
618+
...props,
619+
}
620+
})
621+
)
622+
623+
const scopeSlotComponent = {
624+
functional: true,
625+
render(h, context) {
626+
return h('span', context.props.slotProp)
627+
},
628+
}
629+
const { SchemaField } = createSchemaField({
630+
components: {
631+
ConnectedComponent,
632+
},
633+
})
634+
const { queryByTestId } = render({
635+
components: { SchemaField },
636+
data() {
637+
return {
638+
form,
639+
schema: new Schema({
640+
type: 'string',
641+
name: 'ConnectedComponent',
642+
'x-component': 'ConnectedComponent',
643+
'x-content': {
644+
default: scopeSlotComponent,
645+
},
646+
}),
647+
}
648+
},
649+
template: `<FormProvider :form="form">
650+
<SchemaField
651+
name="string"
652+
:schema="schema"
653+
/>
654+
</FormProvider>`,
655+
})
656+
expect(queryByTestId('ConnectedComponent')).toBeVisible()
657+
expect(queryByTestId('ConnectedComponent').textContent).toEqual('123')
658+
})
659+
660+
test('scoped slot with connect and readPretty', () => {
661+
const form = createForm()
662+
663+
const ConnectedWithMapReadPretty = connect(
664+
defineComponent({
665+
render(h) {
666+
return h(
667+
'div',
668+
{
669+
attrs: {
670+
'data-testid': 'ConnectedWithMapReadPretty',
671+
},
672+
},
673+
[
674+
this.$scopedSlots.withMapReadPretty({
675+
slotProp: '123',
676+
}),
677+
]
678+
)
679+
},
680+
}),
681+
mapProps((props, field) => {
682+
return {
683+
...props,
684+
}
685+
}),
686+
mapReadPretty({
687+
render(h) {
688+
return h('div', 'read pretty')
689+
},
690+
})
691+
)
692+
693+
const scopeSlotComponent = {
694+
functional: true,
695+
render(h, context) {
696+
return h('span', context.props.slotProp)
697+
},
698+
}
699+
const { SchemaField } = createSchemaField({
700+
components: {
701+
ConnectedWithMapReadPretty,
702+
},
703+
})
704+
const { queryByTestId } = render({
705+
components: { SchemaField },
706+
data() {
707+
return {
708+
form,
709+
schema: new Schema({
710+
type: 'string',
711+
name: 'ConnectedWithMapReadPretty',
712+
'x-component': 'ConnectedWithMapReadPretty',
713+
'x-content': {
714+
withMapReadPretty: scopeSlotComponent,
715+
},
716+
}),
717+
}
718+
},
719+
template: `<FormProvider :form="form">
720+
<SchemaField
721+
name="string"
722+
:schema="schema"
723+
/>
724+
</FormProvider>`,
725+
})
726+
expect(queryByTestId('ConnectedWithMapReadPretty')).toBeVisible()
727+
expect(queryByTestId('ConnectedWithMapReadPretty').textContent).toEqual(
728+
'123'
729+
)
730+
})
731+
594732
test('slot compitible', () => {
595733
const form = createForm()
596734
const { SchemaField } = createSchemaField({

0 commit comments

Comments
 (0)