Skip to content

Commit

Permalink
fix(vue): fix x-content is not work with array type (#1754)
Browse files Browse the repository at this point in the history
* fix(vue): fix x-content is not work with array type (#1719)

* feat(element): add upload x-content demo with arry type
  • Loading branch information
muuyao authored Jul 8, 2021
1 parent 43d1ef0 commit 66fd1bc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</div>
</div>

<div v-show="!collapsed" class="dumi-previewer-source">
<div v-show="!innerCollapsed" class="dumi-previewer-source">
<div v-html="highlightCode" class="language-vue extra-class" />
</div>
</section>
Expand Down
18 changes: 18 additions & 0 deletions packages/element/docs/demos/guide/upload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import {
} from '@formily/element'
import { Button } from 'element-ui'
const UploadButton = {
functional: true,
render(h) {
return h(Button, {}, '上传图片')
},
}
const schema = {
type: 'object',
properties: {
Expand Down Expand Up @@ -57,6 +64,17 @@ const schema = {
},
required: true,
},
custom: {
type: 'array',
title: '自定义按钮',
'x-decorator': 'FormItem',
'x-component': 'Upload',
'x-component-props': {
action: 'https://formily-vue.free.beeceptor.com/file',
},
'x-content': UploadButton,
required: true,
},
},
}
Expand Down
92 changes: 41 additions & 51 deletions packages/vue/src/components/RecursionField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,43 @@ const RecursionField = observer(
return () => {
const basePath = getBasePath()
const fieldProps = fieldPropsRef.value
const xContent = fieldSchemaRef.value['x-content']
const xContentMap: Record<string, any[]> = {}

if (typeof xContent === 'string') {
xContentMap['default'] = [xContent]
} else if (isVueOptions(xContent) || typeof xContent === 'function') {
// is vue component or functional component
xContentMap['default'] = [h(xContent, {}, {})]
} else if (xContent && typeof xContent === 'object') {
// for named slots
Object.keys(xContent).forEach((key) => {
const child = xContent[key]
if (typeof child === 'string') {
xContentMap[key] = [child]
} else if (isVueOptions(child) || typeof child === 'function') {
xContentMap[key] = [h(child, {}, {})]
}
})
}

const getSlots = (children = []) => {
const slots: Record<string, () => any> = {}

if (children.length > 0) {
slots.default = () => [...children]
}

Object.keys(xContentMap).forEach((key) => {
if (key === 'default') {
slots[key] = () => [...children, ...xContentMap[key]]
} else {
slots[key] = () => [...xContentMap[key]]
}
})

return slots
}

const renderProperties = (field?: Formily.Core.Types.GeneralField) => {
if (props.onlyRenderSelf) return
Expand Down Expand Up @@ -120,35 +157,7 @@ const RecursionField = observer(
}
)

const slots: Record<string, () => any> = {}

if (children.length > 0) {
slots.default = () => [...children]
}

const xContent = fieldSchemaRef.value['x-content']

if (typeof xContent === 'string') {
slots['default'] = () => [...children, xContent]
} else if (isVueOptions(xContent) || typeof xContent === 'function') {
slots['default'] = () => [...children, h(xContent, {}, {})]
} else if (xContent && typeof xContent === 'object') {
Object.keys(xContent).forEach((key) => {
const child = xContent[key]
if (key === 'default') {
if (typeof child === 'string') {
slots[key] = () => [...children, child]
} else if (isVueOptions(child) || typeof child === 'function') {
slots[key] = () => [...children, h(child, {}, {})]
}
}
if (typeof child === 'string') {
slots[key] = () => [child]
} else if (isVueOptions(child) || typeof child === 'function') {
slots[key] = () => [h(child, {}, {})]
}
})
}
const slots = getSlots(children)

return h(Fragment, {}, slots)
}
Expand All @@ -171,6 +180,7 @@ const RecursionField = observer(
}
)
} else if (fieldSchemaRef.value.type === 'array') {
const slots = getSlots()
return h(
ArrayField,
{
Expand All @@ -180,7 +190,7 @@ const RecursionField = observer(
basePath: basePath,
},
},
{}
slots
)
} else if (fieldSchemaRef.value.type === 'void') {
if (props.onlyRenderProperties) return renderProperties()
Expand All @@ -199,27 +209,7 @@ const RecursionField = observer(
)
}

const slots: Record<string, () => any> = {}

const xContent = fieldSchemaRef.value['x-content']

if (typeof xContent === 'string') {
slots['default'] = () => [xContent]
} else if (isVueOptions(xContent) || typeof xContent === 'function') {
// is vue component or functional component
slots['default'] = () => [h(xContent, {}, {})]
} else if (xContent && typeof xContent === 'object') {
// for named slots
Object.keys(xContent).forEach((key) => {
const child = xContent[key]
if (typeof child === 'string') {
slots[key] = () => [child]
} else if (isVueOptions(child) || typeof child === 'function') {
slots[key] = () => [h(child, {}, {})]
}
})
}

const slots = getSlots()
return h(
Field,
{
Expand Down

0 comments on commit 66fd1bc

Please sign in to comment.