-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcard.tsx
More file actions
100 lines (91 loc) · 2.62 KB
/
Copy pathcard.tsx
File metadata and controls
100 lines (91 loc) · 2.62 KB
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
import React, { ReactNode, useContext } from 'react';
import classNames from 'classnames';
import { CARD_CONTENT_MARK, hasMarker } from '../_utils/component-markers';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import { CardContentProps, CardProps, CardVariant } from './types';
const Card = React.forwardRef<HTMLDivElement, CardProps>((props, ref) => {
const {
variant,
bordered = true,
active = false,
hoverable = false,
prefixCls: customisedCls,
title,
extra,
header,
headerStyle,
actions,
footer,
footerStyle,
className,
style,
children,
...otherProps
} = props;
const configContext = useContext(ConfigContext);
const prefixCls = getPrefixCls('card', configContext.prefixCls, customisedCls);
// When variant is set, it takes precedence over the legacy bordered prop
const resolvedVariant: CardVariant | undefined = variant ?? (bordered ? 'outlined' : undefined);
const cls = classNames(prefixCls, className, {
[`${prefixCls}_${resolvedVariant}`]: resolvedVariant,
[`${prefixCls}_active`]: active,
[`${prefixCls}_hoverable`]: hoverable,
});
const renderHeader = (): ReactNode => {
if (header) {
return header;
} else if (title || extra) {
return (
<div className={`${prefixCls}__header`} style={headerStyle}>
{title}
{extra}
</div>
);
} else {
return null;
}
};
const renderFooter = (): ReactNode => {
if (footer) {
return footer;
} else if (actions) {
return (
<div className={`${prefixCls}__footer`} style={footerStyle}>
{actions}
</div>
);
} else {
return null;
}
};
const renderChildren = (): ReactNode => {
if (children) {
return React.Children.map(children, (child) => {
if (!React.isValidElement(child)) {
return child;
}
// Pass prefixCls attribute to child if it is a CardContent instance
const childElement = child as React.FunctionComponentElement<CardContentProps>;
if (hasMarker(childElement.type, CARD_CONTENT_MARK)) {
const childProps: Partial<CardContentProps> = {
prefixCls,
};
return React.cloneElement(childElement, childProps);
} else {
return child;
}
});
}
return null;
};
return (
<div ref={ref} {...otherProps} className={cls} style={style}>
{renderHeader()}
{renderChildren()}
{renderFooter()}
</div>
);
});
Card.displayName = 'Card';
export default Card;