in packages/element/src/array-cards/index.ts [44:232]
setup(props, { attrs }) {
const fieldRef = useField<ArrayField>()
const schemaRef = useFieldSchema()
const prefixCls = `${stylePrefix}-array-cards`
const { getKey, keyMap } = ArrayBase.useKey(schemaRef.value)
return () => {
const field = fieldRef.value
const schema = schemaRef.value
const dataSource = Array.isArray(field.value) ? field.value : []
if (!schema) throw new Error('can not found schema object')
const renderItems = () => {
return dataSource?.map((item, index) => {
const items = Array.isArray(schema.items)
? schema.items[index] || schema.items[0]
: schema.items
const title = h(
'span',
{},
{
default: () => [
h(
RecursionField,
{
props: {
schema: items,
name: index,
filterProperties: (schema) => {
if (!isIndexComponent(schema)) return false
return true
},
onlyRenderProperties: true,
},
},
{}
),
attrs.title || field.title,
],
}
)
const extra = h(
'span',
{},
{
default: () => [
h(
RecursionField,
{
props: {
schema: items,
name: index,
filterProperties: (schema) => {
if (!isOperationComponent(schema)) return false
return true
},
onlyRenderProperties: true,
},
},
{}
),
attrs.extra,
],
}
)
const content = h(
RecursionField,
{
props: {
schema: items,
name: index,
filterProperties: (schema) => {
if (isIndexComponent(schema)) return false
if (isOperationComponent(schema)) return false
return true
},
},
},
{}
)
return h(
ArrayBase.Item,
{
key: getKey(item, index),
props: {
index,
record: item,
},
},
{
default: () =>
h(
Card,
{
class: [`${prefixCls}-item`],
attrs: {
shadow: 'never',
...attrs,
},
},
{
default: () => [content],
header: () =>
h(
Row,
{
props: {
type: 'flex',
justify: 'space-between',
},
},
{
default: () => [title, extra],
}
),
}
),
}
)
})
}
const renderAddition = () => {
return schema.reduceProperties((addition, schema) => {
if (isAdditionComponent(schema)) {
return h(
RecursionField,
{
props: {
schema,
name: 'addition',
},
},
{}
)
}
return addition
}, null)
}
const renderEmpty = () => {
if (dataSource?.length) return
return h(
Card,
{
class: [`${prefixCls}-item`],
attrs: {
shadow: 'never',
...attrs,
header: attrs.title || field.title,
},
},
{
default: () =>
h(
Empty,
{ props: { description: 'No Data', imageSize: 100 } },
{}
),
}
)
}
return h(
'div',
{
class: [prefixCls],
},
{
default: () =>
h(
ArrayBase,
{
props: {
keyMap,
},
},
{
default: () => [
renderEmpty(),
renderItems(),
renderAddition(),
],
}
),
}
)
}
},