-
Notifications
You must be signed in to change notification settings - Fork 138
/
Card.tsx
157 lines (136 loc) · 5.09 KB
/
Card.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* @license
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE
*/
import type { CardProps } from './types'
import { Slots, VNode, VNodeTypes, computed, defineComponent, isVNode, normalizeClass, provide } from 'vue'
import { isString } from 'lodash-es'
import { callEmit, useControlledProp } from '@idux/cdk/utils'
import { ɵHeader } from '@idux/components/_private/header'
import { IxButton } from '@idux/components/button'
import { useGlobalConfig } from '@idux/components/config'
import { IxCol, IxRow } from '@idux/components/grid'
import { IxIcon } from '@idux/components/icon'
import { cardToken } from './token'
import { cardProps } from './types'
export default defineComponent({
name: 'IxCard',
props: cardProps,
setup(props, { slots }) {
const common = useGlobalConfig('common')
const mergedPrefixCls = computed(() => `${common.prefixCls}-card`)
const config = useGlobalConfig('card')
const hoverable = computed(() => props.hoverable ?? config.hoverable)
provide(cardToken, { hoverable })
const [selected, setSelected] = useControlledProp(props, 'selected', false)
const isDisabled = computed(() => props.disabled)
const isSelectable = computed(() => props.selectable)
const handleClick = () => {
if (isDisabled.value || !isSelectable.value) {
return
}
const newSelected = !selected.value
setSelected(newSelected)
callEmit(props.onSelectedChange, newSelected)
}
return () => {
const prefixCls = mergedPrefixCls.value
const { borderless = config.borderless, loading, size = config.size, shadow } = props
const children = slots.default?.() ?? []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const hasGrid = children.some(node => node.type && (node.type as any).name === 'IxCardGrid')
const classes = normalizeClass({
[prefixCls]: true,
[`${prefixCls}-borderless`]: borderless,
[`${prefixCls}-hoverable`]: !hasGrid && hoverable.value,
[`${prefixCls}-loading`]: loading,
[`${prefixCls}-has-shadow`]: shadow,
[`${prefixCls}-has-grid`]: hasGrid,
[`${prefixCls}-${size}`]: true,
[`${prefixCls}-selectable`]: isSelectable.value,
[`${prefixCls}-selected`]: isSelectable.value && selected.value,
[`${prefixCls}-disabled`]: isDisabled.value,
})
return (
<div class={classes} onClick={handleClick}>
{renderCover(props, slots, prefixCls)}
<ɵHeader v-slots={slots} size={size} header={props.header} />
{renderBody(props, children, hasGrid, prefixCls)}
{renderFooter(props, slots, prefixCls)}
{renderCornerMark(props, prefixCls)}
</div>
)
}
},
})
const renderCornerMark = (props: CardProps, prefixCls: string) => {
const isSelectable = computed(() => props.selectable ?? false)
if (!isSelectable.value) {
return undefined
}
return (
<div class={`${prefixCls}-mark-wrap`}>
<div class={`${prefixCls}-mark`}></div>
<IxIcon name="success" class={`${prefixCls}-mark-icon`} color="white" />
</div>
)
}
const renderCover = (props: CardProps, slots: Slots, prefixCls: string) => {
let coverNode: VNodeTypes | undefined
if (slots.cover) {
coverNode = slots.cover()
} else if (props.cover) {
const { cover } = props
const imgProps = isString(cover) ? { src: cover } : cover
coverNode = <img {...imgProps} />
}
return coverNode ? <div class={`${prefixCls}-cover`}>{coverNode}</div> : undefined
}
const renderBody = (props: CardProps, children: VNode[], hasGrid: boolean, prefixCls: string) => {
let bodyNode: VNodeTypes | undefined
if (props.loading) {
bodyNode = renderLoading(prefixCls)
} else if (children.length) {
bodyNode = hasGrid ? <IxRow>{children}</IxRow> : children
}
return bodyNode ? <div class={`${prefixCls}-body`}>{bodyNode}</div> : undefined
}
const loadingSpans = [[22], [8, 15], [6, 18], [13, 9], [4, 3, 16], [8, 6, 8]]
const renderLoading = (prefixCls: string) => {
const loadingChild = loadingSpans.map(spans => {
const cols = spans.map(span => (
<IxCol span={span} class={`${prefixCls}-loading-col`}>
<div class={`${prefixCls}-loading-block`}></div>
</IxCol>
))
return <IxRow>{cols}</IxRow>
})
return <div class={`${prefixCls}-loading`}>{loadingChild}</div>
}
const renderFooter = (props: CardProps, slots: Slots, prefixCls: string) => {
if (!slots.footer && !props.footer) {
return undefined
}
let footerNode: VNodeTypes
if (slots.footer) {
footerNode = slots.footer()
} else {
footerNode = props.footer!.map(item => {
let itemChild: VNodeTypes | null
if (isVNode(item)) {
itemChild = item
} else {
const { text, ...rest } = item
itemChild = (
<IxButton waveless={true} {...rest}>
{text}
</IxButton>
)
}
return itemChild ? <li>{itemChild}</li> : null
})
}
return <ul class={`${prefixCls}-footer`}>{footerNode}</ul>
}