-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathCard.tsx
194 lines (182 loc) · 6.41 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import type { VNodeTypes, PropType, VNode, ExtractPropTypes, CSSProperties } from 'vue';
import { isVNode, defineComponent } from 'vue';
import Tabs from '../tabs';
import PropTypes from '../_util/vue-types';
import { flattenChildren, isEmptyElement, filterEmptyWithUndefined } from '../_util/props-util';
import type { SizeType } from '../config-provider';
import isPlainObject from 'lodash-es/isPlainObject';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import devWarning from '../vc-util/devWarning';
import useStyle from './style';
import Skeleton from '../skeleton';
import type { CustomSlotsType } from '../_util/type';
import { customRenderSlot } from '../_util/vnode';
export interface CardTabListType {
key: string;
tab: any;
/** @deprecated Please use `customTab` instead. */
slots?: { tab: string };
disabled?: boolean;
}
export type CardType = 'inner';
export type CardSize = 'default' | 'small';
const { TabPane } = Tabs;
export const cardProps = () => ({
prefixCls: String,
title: PropTypes.any,
extra: PropTypes.any,
bordered: { type: Boolean, default: true },
bodyStyle: { type: Object as PropType<CSSProperties>, default: undefined as CSSProperties },
headStyle: { type: Object as PropType<CSSProperties>, default: undefined as CSSProperties },
loading: { type: Boolean, default: false },
hoverable: { type: Boolean, default: false },
type: { type: String as PropType<CardType> },
size: { type: String as PropType<CardSize> },
actions: PropTypes.any,
tabList: {
type: Array as PropType<CardTabListType[]>,
},
tabBarExtraContent: PropTypes.any,
activeTabKey: String,
defaultActiveTabKey: String,
cover: PropTypes.any,
onTabChange: {
type: Function as PropType<(key: string) => void>,
},
});
export type CardProps = Partial<ExtractPropTypes<ReturnType<typeof cardProps>>>;
const Card = defineComponent({
name: 'ACard',
inheritAttrs: false,
props: cardProps(),
slots: Object as CustomSlotsType<{
title: any;
extra: any;
tabBarExtraContent: any;
actions: any;
cover: any;
customTab: CardTabListType;
default: any;
}>,
setup(props, { slots, attrs }) {
const { prefixCls, direction, size } = useConfigInject('card', props);
const [wrapSSR, hashId] = useStyle(prefixCls);
const getAction = (actions: VNodeTypes[]) => {
const actionList = actions.map((action, index) =>
(isVNode(action) && !isEmptyElement(action)) || !isVNode(action) ? (
<li style={{ width: `${100 / actions.length}%` }} key={`action-${index}`}>
<span>{action}</span>
</li>
) : null,
);
return actionList;
};
const triggerTabChange = (key: string) => {
props.onTabChange?.(key);
};
const isContainGrid = (obj: VNode[] = []) => {
let containGrid: boolean;
obj.forEach(element => {
if (element && isPlainObject(element.type) && (element.type as any).__ANT_CARD_GRID) {
containGrid = true;
}
});
return containGrid;
};
return () => {
const {
headStyle = {},
bodyStyle = {},
loading,
bordered = true,
type,
tabList,
hoverable,
activeTabKey,
defaultActiveTabKey,
tabBarExtraContent = filterEmptyWithUndefined(slots.tabBarExtraContent?.()),
title = filterEmptyWithUndefined(slots.title?.()),
extra = filterEmptyWithUndefined(slots.extra?.()),
actions = filterEmptyWithUndefined(slots.actions?.()),
cover = filterEmptyWithUndefined(slots.cover?.()),
} = props;
const children = flattenChildren(slots.default?.());
const pre = prefixCls.value;
const classString = {
[`${pre}`]: true,
[hashId.value]: true,
[`${pre}-loading`]: loading,
[`${pre}-bordered`]: bordered,
[`${pre}-hoverable`]: !!hoverable,
[`${pre}-contain-grid`]: isContainGrid(children),
[`${pre}-contain-tabs`]: tabList && tabList.length,
[`${pre}-${size.value}`]: size.value,
[`${pre}-type-${type}`]: !!type,
[`${pre}-rtl`]: direction.value === 'rtl',
};
const loadingBlock = (
<Skeleton loading active paragraph={{ rows: 4 }} title={false}>
{children}
</Skeleton>
);
const hasActiveTabKey = activeTabKey !== undefined;
const tabsProps = {
size: 'large' as SizeType,
[hasActiveTabKey ? 'activeKey' : 'defaultActiveKey']: hasActiveTabKey
? activeTabKey
: defaultActiveTabKey,
onChange: triggerTabChange,
class: `${pre}-head-tabs`,
};
let head;
const tabs =
tabList && tabList.length ? (
<Tabs
{...tabsProps}
v-slots={{ rightExtra: tabBarExtraContent ? () => tabBarExtraContent : null }}
>
{tabList.map(item => {
const { tab: temp, slots: itemSlots } = item as CardTabListType;
const name = itemSlots?.tab;
devWarning(
!itemSlots,
'Card',
`tabList slots is deprecated, Please use \`customTab\` instead.`,
);
let tab = temp !== undefined ? temp : slots[name] ? slots[name](item) : null;
tab = customRenderSlot(slots, 'customTab', item as any, () => [tab]);
return <TabPane tab={tab} key={item.key} disabled={item.disabled} />;
})}
</Tabs>
) : null;
if (title || extra || tabs) {
head = (
<div class={`${pre}-head`} style={headStyle}>
<div class={`${pre}-head-wrapper`}>
{title && <div class={`${pre}-head-title`}>{title}</div>}
{extra && <div class={`${pre}-extra`}>{extra}</div>}
</div>
{tabs}
</div>
);
}
const coverDom = cover ? <div class={`${pre}-cover`}>{cover}</div> : null;
const body = (
<div class={`${pre}-body`} style={bodyStyle}>
{loading ? loadingBlock : children}
</div>
);
const actionDom =
actions && actions.length ? <ul class={`${pre}-actions`}>{getAction(actions)}</ul> : null;
return wrapSSR(
<div ref="cardContainerRef" {...attrs} class={[classString, attrs.class]}>
{head}
{coverDom}
{children && children.length ? body : null}
{actionDom}
</div>,
);
};
},
});
export default Card;