Skip to content
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

fix: slot should extra params #919

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/vue-generator/src/generator/vue/sfc/genSetupSFC.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
handleTinyGrid,
handleTinyIcon,
handleExpressionChildren,
validEmptyTemplateHook
validEmptyTemplateHook,
handleSlotParams
} from './generateTemplate'
import { generateStyleTag } from './generateStyle'
import {
Expand Down Expand Up @@ -213,6 +214,7 @@ export const genSFCWithDefaultPlugin = (schema, componentsMap, config = {}) => {
const defaultComponentHooks = [handleComponentNameHook, handleTinyIcon]

const defaultAttributeHook = [
handleSlotParams,
handleTinyGrid,
handleJsxModelValueUpdate,
handleConditionAttrHook,
Expand Down
28 changes: 28 additions & 0 deletions packages/vue-generator/src/generator/vue/sfc/generateTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,34 @@ export const handleTinyGrid = (schemaData, globalHooks, config) => {
})
}

/**
* 解决往插槽传参的场景。
* 比如区块设置了插槽,需要往里面传数据,schema 协议会生成如下参数
* {
* params: [{ name: "test", value: { type: "JSExpression", value: "this.state.arr" } }]
* }
* 需要将数组进行解析然后转换,才能进行后续正确出码,
* @param {*} schemaData
* @returns
*/
export const handleSlotParams = (schemaData) => {
const { componentName, props } = schemaData.schema

if (componentName !== 'Slot' || !Array.isArray(props?.params)) {
return
}

props.params.forEach((paramItem) => {
const { name, value } = paramItem || {}

if (name && value) {
props[name] = value
}
})

delete props.params
}

export const handleExpressionChildren = (schemaData = {}, globalHooks, config) => {
const { children, schema } = schemaData
const type = schema?.children?.type
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"state": {
"personData": {
"name": "李华",
"age": "20",
"address": "china"
}
},
"methods": {},
"componentName": "Block",
"css": "",
"props": {},
"lifeCycles": {},
"children": [
{
"componentName": "Text",
"id": "52535213",
"props": {
"text": "请开启插槽"
}
},
{
"id": "42562b66",
"componentName": "div",
"props": {},
"children": [
{
"componentName": "Slot",
"id": "6153675d",
"props": {
"name": "test",
"params": [
{
"name": "testData",
"value": {
"type": "JSExpression",
"value": "this.state.personData"
}
}
]
}
}
]
}
],
"fileName": "BlockSlotParams"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<div>
<span>请开启插槽</span>
<div>
<slot name="test" :testData="state.personData"></slot>
</div>
</div>
</template>

<script setup>
import * as vue from 'vue'
import { defineProps, defineEmits } from 'vue'
import { I18nInjectionKey } from 'vue-i18n'

const props = defineProps({})

const emit = defineEmits([])
const { t, lowcodeWrap, stores } = vue.inject(I18nInjectionKey).lowcode()
const wrap = lowcodeWrap(props, { emit })
wrap({ stores })

const state = vue.reactive({ personData: { name: '李华', age: '20', address: 'china' } })
wrap({ state })
</script>
<style scoped></style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<template>
<div>
<block-slot-params>
<template #test="{ testData }">
<span>{{ testData.name }}</span></template
></block-slot-params
>
</div>
</template>

<script setup>
import * as vue from 'vue'
import { defineProps, defineEmits } from 'vue'
import { I18nInjectionKey } from 'vue-i18n'
import BlockSlotParams from '../components/BlockSlotParams.vue'

const props = defineProps({})

const emit = defineEmits([])
const { t, lowcodeWrap, stores } = vue.inject(I18nInjectionKey).lowcode()
const wrap = lowcodeWrap(props, { emit })
wrap({ stores })

const state = vue.reactive({})
wrap({ state })
</script>
<style scoped></style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"state": {},
"methods": {},
"componentName": "Page",
"css": "",
"props": {},
"lifeCycles": {},
"children": [
{
"componentName": "BlockSlotParams",
"props": {},
"id": "63623253",
"componentType": "Block",
"children": [
{
"id": "54396632",
"componentName": "Template",
"props": {
"slot": {
"name": "test",
"params": ["testData"]
}
},
"children": [
{
"id": "5242615f",
"componentName": "Text",
"props": {
"text": {
"type": "JSExpression",
"value": "testData.name"
}
}
}
]
}
]
}
],
"fileName": "slotModelValueTest"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect, test } from 'vitest'
import { genSFCWithDefaultPlugin } from '@/generator/vue/sfc'
import blockSchema from './block.schema.json'
import pageSchema from './page.schema.json'
import { formatCode } from '@/utils/formatCode'

test('should generate slot and pass testData params', async () => {
const res = genSFCWithDefaultPlugin(blockSchema, [])
const formattedCode = formatCode(res, 'vue')

await expect(formattedCode).toMatchFileSnapshot('./expected/slotParamsBlock.vue')
})

test('should generate slot params', async () => {
const res = genSFCWithDefaultPlugin(pageSchema, [])
const formattedCode = formatCode(res, 'vue')

await expect(formattedCode).toMatchFileSnapshot('./expected/slotParamsPage.vue')
})
Loading