2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-02-19 19:35:49 +08:00
|
|
|
import classNames from 'classnames';
|
2017-08-09 14:34:10 +08:00
|
|
|
import omit from 'omit.js';
|
2017-07-07 13:36:54 +08:00
|
|
|
import Grid from './Grid';
|
2017-08-09 14:34:10 +08:00
|
|
|
import Meta from './Meta';
|
2020-03-14 19:59:15 +08:00
|
|
|
import Tabs, { TabsProps } from '../tabs';
|
2018-04-20 11:19:30 +08:00
|
|
|
import Row from '../row';
|
|
|
|
import Col from '../col';
|
2020-05-17 00:49:05 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2018-04-11 19:40:31 +08:00
|
|
|
import { Omit } from '../_util/type';
|
2020-01-03 13:38:16 +08:00
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
2016-07-14 13:29:50 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
function getAction(actions: React.ReactNode[]) {
|
|
|
|
const actionList = actions.map((action, index) => (
|
2019-08-06 15:02:05 +08:00
|
|
|
// eslint-disable-next-line react/no-array-index-key
|
2019-08-05 18:38:10 +08:00
|
|
|
<li style={{ width: `${100 / actions.length}%` }} key={`action-${index}`}>
|
|
|
|
<span>{action}</span>
|
|
|
|
</li>
|
|
|
|
));
|
|
|
|
return actionList;
|
|
|
|
}
|
|
|
|
|
2017-09-25 22:14:49 +08:00
|
|
|
export { CardGridProps } from './Grid';
|
|
|
|
export { CardMetaProps } from './Meta';
|
|
|
|
|
2017-08-09 14:34:10 +08:00
|
|
|
export type CardType = 'inner';
|
2018-11-18 05:35:56 +08:00
|
|
|
export type CardSize = 'default' | 'small';
|
2017-08-09 14:34:10 +08:00
|
|
|
|
|
|
|
export interface CardTabListType {
|
|
|
|
key: string;
|
|
|
|
tab: React.ReactNode;
|
2018-07-16 19:18:50 +08:00
|
|
|
disabled?: boolean;
|
2017-08-09 14:34:10 +08:00
|
|
|
}
|
|
|
|
|
2018-04-11 19:40:31 +08:00
|
|
|
export interface CardProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'title'> {
|
2016-12-19 15:19:15 +08:00
|
|
|
prefixCls?: string;
|
2016-07-14 13:29:50 +08:00
|
|
|
title?: React.ReactNode;
|
|
|
|
extra?: React.ReactNode;
|
|
|
|
bordered?: boolean;
|
2018-07-23 01:31:25 +08:00
|
|
|
headStyle?: React.CSSProperties;
|
2016-07-14 13:29:50 +08:00
|
|
|
bodyStyle?: React.CSSProperties;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
loading?: boolean;
|
2017-09-27 21:57:18 +08:00
|
|
|
hoverable?: boolean;
|
2017-07-31 20:29:56 +08:00
|
|
|
children?: React.ReactNode;
|
2016-09-21 09:27:58 +08:00
|
|
|
id?: string;
|
2016-10-21 16:27:26 +08:00
|
|
|
className?: string;
|
2018-11-18 05:35:56 +08:00
|
|
|
size?: CardSize;
|
2017-08-09 14:34:10 +08:00
|
|
|
type?: CardType;
|
|
|
|
cover?: React.ReactNode;
|
2018-11-01 14:49:51 +08:00
|
|
|
actions?: React.ReactNode[];
|
2017-08-09 14:34:10 +08:00
|
|
|
tabList?: CardTabListType[];
|
2019-08-23 14:14:41 +08:00
|
|
|
tabBarExtraContent?: React.ReactNode | null;
|
2017-08-09 14:34:10 +08:00
|
|
|
onTabChange?: (key: string) => void;
|
2018-03-10 14:44:12 +08:00
|
|
|
activeTabKey?: string;
|
|
|
|
defaultActiveTabKey?: string;
|
2020-03-14 19:59:15 +08:00
|
|
|
tabProps?: TabsProps;
|
2016-07-14 13:29:50 +08:00
|
|
|
}
|
|
|
|
|
2020-05-17 00:49:05 +08:00
|
|
|
interface CardInterface extends React.FC<CardProps> {
|
|
|
|
Grid: typeof Grid;
|
|
|
|
Meta: typeof Meta;
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-05-17 00:49:05 +08:00
|
|
|
const Card: CardInterface = props => {
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
const size = React.useContext(SizeContext);
|
2018-04-20 10:44:33 +08:00
|
|
|
|
2020-05-17 00:49:05 +08:00
|
|
|
const onTabChange = (key: string) => {
|
|
|
|
if (props.onTabChange) {
|
|
|
|
props.onTabChange(key);
|
2017-08-09 14:34:10 +08:00
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-11-22 23:35:40 +08:00
|
|
|
|
2020-05-17 00:49:05 +08:00
|
|
|
const isContainGrid = () => {
|
2017-07-14 16:10:04 +08:00
|
|
|
let containGrid;
|
2020-05-17 00:49:05 +08:00
|
|
|
React.Children.forEach(props.children, (element: JSX.Element) => {
|
2017-07-14 16:10:04 +08:00
|
|
|
if (element && element.type && element.type === Grid) {
|
|
|
|
containGrid = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return containGrid;
|
2020-05-17 00:49:05 +08:00
|
|
|
};
|
2018-11-22 23:35:40 +08:00
|
|
|
|
2020-05-17 00:49:05 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
extra,
|
|
|
|
headStyle = {},
|
|
|
|
bodyStyle = {},
|
|
|
|
title,
|
|
|
|
loading,
|
|
|
|
bordered = true,
|
|
|
|
size: customizeSize,
|
|
|
|
type,
|
|
|
|
cover,
|
|
|
|
actions,
|
|
|
|
tabList,
|
|
|
|
children,
|
|
|
|
activeTabKey,
|
|
|
|
defaultActiveTabKey,
|
|
|
|
tabBarExtraContent,
|
|
|
|
hoverable,
|
|
|
|
tabProps = {},
|
|
|
|
...others
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('card', customizePrefixCls);
|
|
|
|
|
|
|
|
const loadingBlockStyle =
|
|
|
|
bodyStyle.padding === 0 || bodyStyle.padding === '0px' ? { padding: 24 } : undefined;
|
|
|
|
|
2020-06-05 20:11:25 +08:00
|
|
|
const block = <div className={`${prefixCls}-loading-block`} />;
|
|
|
|
|
2020-05-17 00:49:05 +08:00
|
|
|
const loadingBlock = (
|
|
|
|
<div className={`${prefixCls}-loading-content`} style={loadingBlockStyle}>
|
|
|
|
<Row gutter={8}>
|
2020-06-05 20:11:25 +08:00
|
|
|
<Col span={22}>{block}</Col>
|
2020-05-17 00:49:05 +08:00
|
|
|
</Row>
|
|
|
|
<Row gutter={8}>
|
2020-06-05 20:11:25 +08:00
|
|
|
<Col span={8}>{block}</Col>
|
|
|
|
<Col span={15}>{block}</Col>
|
2020-05-17 00:49:05 +08:00
|
|
|
</Row>
|
|
|
|
<Row gutter={8}>
|
2020-06-05 20:11:25 +08:00
|
|
|
<Col span={6}>{block}</Col>
|
|
|
|
<Col span={18}>{block}</Col>
|
2020-05-17 00:49:05 +08:00
|
|
|
</Row>
|
|
|
|
<Row gutter={8}>
|
2020-06-05 20:11:25 +08:00
|
|
|
<Col span={13}>{block}</Col>
|
|
|
|
<Col span={9}>{block}</Col>
|
2020-05-17 00:49:05 +08:00
|
|
|
</Row>
|
|
|
|
<Row gutter={8}>
|
2020-06-05 20:11:25 +08:00
|
|
|
<Col span={4}>{block}</Col>
|
|
|
|
<Col span={3}>{block}</Col>
|
|
|
|
<Col span={16}>{block}</Col>
|
2020-05-17 00:49:05 +08:00
|
|
|
</Row>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
const hasActiveTabKey = activeTabKey !== undefined;
|
|
|
|
const extraProps = {
|
|
|
|
...tabProps,
|
|
|
|
[hasActiveTabKey ? 'activeKey' : 'defaultActiveKey']: hasActiveTabKey
|
|
|
|
? activeTabKey
|
|
|
|
: defaultActiveTabKey,
|
|
|
|
tabBarExtraContent,
|
|
|
|
};
|
2017-07-07 13:36:54 +08:00
|
|
|
|
2020-05-17 00:49:05 +08:00
|
|
|
let head: React.ReactNode;
|
|
|
|
const tabs =
|
|
|
|
tabList && tabList.length ? (
|
|
|
|
<Tabs
|
|
|
|
size="large"
|
|
|
|
{...extraProps}
|
|
|
|
className={`${prefixCls}-head-tabs`}
|
|
|
|
onChange={onTabChange}
|
|
|
|
>
|
|
|
|
{tabList.map(item => (
|
|
|
|
<Tabs.TabPane tab={item.tab} disabled={item.disabled} key={item.key} />
|
|
|
|
))}
|
|
|
|
</Tabs>
|
|
|
|
) : null;
|
|
|
|
if (title || extra || tabs) {
|
|
|
|
head = (
|
|
|
|
<div className={`${prefixCls}-head`} style={headStyle}>
|
|
|
|
<div className={`${prefixCls}-head-wrapper`}>
|
|
|
|
{title && <div className={`${prefixCls}-head-title`}>{title}</div>}
|
|
|
|
{extra && <div className={`${prefixCls}-extra`}>{extra}</div>}
|
2017-07-14 11:37:28 +08:00
|
|
|
</div>
|
2020-05-17 00:49:05 +08:00
|
|
|
{tabs}
|
2017-08-14 21:43:32 +08:00
|
|
|
</div>
|
|
|
|
);
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2020-05-17 00:49:05 +08:00
|
|
|
const coverDom = cover ? <div className={`${prefixCls}-cover`}>{cover}</div> : null;
|
|
|
|
const body = (
|
|
|
|
<div className={`${prefixCls}-body`} style={bodyStyle}>
|
|
|
|
{loading ? loadingBlock : children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
const actionDom =
|
|
|
|
actions && actions.length ? (
|
|
|
|
<ul className={`${prefixCls}-actions`}>{getAction(actions)}</ul>
|
|
|
|
) : null;
|
|
|
|
const divProps = omit(others, ['onTabChange']);
|
|
|
|
const mergedSize = customizeSize || size;
|
|
|
|
const classString = classNames(prefixCls, className, {
|
|
|
|
[`${prefixCls}-loading`]: loading,
|
|
|
|
[`${prefixCls}-bordered`]: bordered,
|
|
|
|
[`${prefixCls}-hoverable`]: hoverable,
|
|
|
|
[`${prefixCls}-contain-grid`]: isContainGrid(),
|
|
|
|
[`${prefixCls}-contain-tabs`]: tabList && tabList.length,
|
|
|
|
[`${prefixCls}-${mergedSize}`]: mergedSize,
|
|
|
|
[`${prefixCls}-type-${type}`]: !!type,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div {...divProps} className={classString}>
|
|
|
|
{head}
|
|
|
|
{coverDom}
|
|
|
|
{body}
|
|
|
|
{actionDom}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Card.Grid = Grid;
|
|
|
|
Card.Meta = Meta;
|
|
|
|
|
|
|
|
export default Card;
|