in packages/element/src/preview-text/index.ts [137:206]
setup(_props, { attrs }) {
const fieldRef = useField<Field>()
const field = fieldRef.value
const props = attrs as unknown as CascaderProps
const dataSource: any[] = field?.dataSource?.length
? field.dataSource
: props?.options?.length
? props.options
: []
const placeholder = usePlaceholder()
const valueKey = props.props?.value || 'value'
const labelKey = props.props?.label || 'label'
const getSelected = () => {
return isArr(props.value) ? props.value : []
}
const findLabel = (value: any, dataSource: any[]) => {
for (let i = 0; i < dataSource?.length; i++) {
const item = dataSource[i]
if (item?.[valueKey] === value) {
return item?.[labelKey]
} else {
const childLabel = findLabel(value, item?.children)
if (childLabel) return childLabel
}
}
}
const getLabels = () => {
const selected = getSelected()
if (!selected?.length) {
return h(
Tag,
{},
{
default: () => placeholder.value,
}
)
}
return selected.map((value, key) => {
const text = findLabel(value, dataSource)
return h(
Tag,
{
key,
props: {
type: 'info',
effect: 'light',
},
},
{
default: () => text || placeholder.value,
}
)
})
}
return () => {
return h(
Space,
{
class: [prefixCls],
style: attrs.style,
},
{
default: () => getLabels(),
}
)
}
},