forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCard.jsx
206 lines (198 loc) · 6.17 KB
/
Card.jsx
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
195
196
197
198
199
200
201
202
203
204
205
206
import { inject, isVNode } from 'vue';
import Tabs from '../tabs';
import Row from '../row';
import Col from '../col';
import PropTypes from '../_util/vue-types';
import { getComponent, getSlot, isEmptyElement } from '../_util/props-util';
import BaseMixin from '../_util/BaseMixin';
import { ConfigConsumerProps } from '../config-provider';
import isPlainObject from 'lodash-es/isPlainObject';
const { TabPane } = Tabs;
export default {
name: 'ACard',
mixins: [BaseMixin],
props: {
prefixCls: PropTypes.string,
title: PropTypes.any,
extra: PropTypes.any,
bordered: PropTypes.bool.def(true),
bodyStyle: PropTypes.object,
headStyle: PropTypes.object,
loading: PropTypes.bool.def(false),
hoverable: PropTypes.bool.def(false),
type: PropTypes.string,
size: PropTypes.oneOf(['default', 'small']),
actions: PropTypes.any,
tabList: PropTypes.array,
tabBarExtraContent: PropTypes.any,
activeTabKey: PropTypes.string,
defaultActiveTabKey: PropTypes.string,
cover: PropTypes.any,
onTabChange: PropTypes.func,
},
setup() {
return {
configProvider: inject('configProvider', ConfigConsumerProps),
};
},
data() {
return {
widerPadding: false,
};
},
methods: {
getAction(actions) {
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;
},
triggerTabChange(key) {
this.$emit('tabChange', key);
},
isContainGrid(obj = []) {
let containGrid;
obj.forEach(element => {
if (element && isPlainObject(element.type) && element.type.__ANT_CARD_GRID) {
containGrid = true;
}
});
return containGrid;
},
},
render() {
const {
prefixCls: customizePrefixCls,
headStyle = {},
bodyStyle = {},
loading,
bordered = true,
size = 'default',
type,
tabList,
hoverable,
activeTabKey,
defaultActiveTabKey,
} = this.$props;
const { $slots } = this;
const children = getSlot(this);
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('card', customizePrefixCls);
const tabBarExtraContent = getComponent(this, 'tabBarExtraContent');
const classString = {
[`${prefixCls}`]: true,
[`${prefixCls}-loading`]: loading,
[`${prefixCls}-bordered`]: bordered,
[`${prefixCls}-hoverable`]: !!hoverable,
[`${prefixCls}-contain-grid`]: this.isContainGrid(children),
[`${prefixCls}-contain-tabs`]: tabList && tabList.length,
[`${prefixCls}-${size}`]: size !== 'default',
[`${prefixCls}-type-${type}`]: !!type,
};
const loadingBlockStyle =
bodyStyle.padding === 0 || bodyStyle.padding === '0px' ? { padding: 24 } : undefined;
const loadingBlock = (
<div class={`${prefixCls}-loading-content`} style={loadingBlockStyle}>
<Row gutter={8}>
<Col span={22}>
<div class={`${prefixCls}-loading-block`} />
</Col>
</Row>
<Row gutter={8}>
<Col span={8}>
<div class={`${prefixCls}-loading-block`} />
</Col>
<Col span={15}>
<div class={`${prefixCls}-loading-block`} />
</Col>
</Row>
<Row gutter={8}>
<Col span={6}>
<div class={`${prefixCls}-loading-block`} />
</Col>
<Col span={18}>
<div class={`${prefixCls}-loading-block`} />
</Col>
</Row>
<Row gutter={8}>
<Col span={13}>
<div class={`${prefixCls}-loading-block`} />
</Col>
<Col span={9}>
<div class={`${prefixCls}-loading-block`} />
</Col>
</Row>
<Row gutter={8}>
<Col span={4}>
<div class={`${prefixCls}-loading-block`} />
</Col>
<Col span={3}>
<div class={`${prefixCls}-loading-block`} />
</Col>
<Col span={16}>
<div class={`${prefixCls}-loading-block`} />
</Col>
</Row>
</div>
);
const hasActiveTabKey = activeTabKey !== undefined;
const tabsProps = {
size: 'large',
[hasActiveTabKey ? 'activeKey' : 'defaultActiveKey']: hasActiveTabKey
? activeTabKey
: defaultActiveTabKey,
tabBarExtraContent,
onChange: this.triggerTabChange,
class: `${prefixCls}-head-tabs`,
};
let head;
const tabs =
tabList && tabList.length ? (
<Tabs {...tabsProps}>
{tabList.map(item => {
const { tab: temp, scopedSlots = {}, slots = {} } = item;
const name = slots.tab || scopedSlots.tab;
const tab = temp !== undefined ? temp : $slots[name] ? $slots[name](item) : null;
return <TabPane tab={tab} key={item.key} disabled={item.disabled} />;
})}
</Tabs>
) : null;
const titleDom = getComponent(this, 'title');
const extraDom = getComponent(this, 'extra');
if (titleDom || extraDom || tabs) {
head = (
<div class={`${prefixCls}-head`} style={headStyle}>
<div class={`${prefixCls}-head-wrapper`}>
{titleDom && <div class={`${prefixCls}-head-title`}>{titleDom}</div>}
{extraDom && <div class={`${prefixCls}-extra`}>{extraDom}</div>}
</div>
{tabs}
</div>
);
}
const cover = getComponent(this, 'cover');
const coverDom = cover ? <div class={`${prefixCls}-cover`}>{cover}</div> : null;
const body = (
<div class={`${prefixCls}-body`} style={bodyStyle}>
{loading ? loadingBlock : children}
</div>
);
const actions = getComponent(this, 'actions');
const actionDom =
actions && actions.length ? (
<ul class={`${prefixCls}-actions`}>{this.getAction(actions)}</ul>
) : null;
return (
<div class={classString} ref="cardContainerRef">
{head}
{coverDom}
{children ? body : null}
{actionDom}
</div>
);
},
};