import * as React from 'react';
import classNames from 'classnames';
import omit from 'omit.js';
import Grid from './Grid';
import Meta from './Meta';
import Tabs from '../tabs';
import Row from '../row';
import Col from '../col';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import warning from '../_util/warning';
import { Omit } from '../_util/type';
function getAction(actions: React.ReactNode[]) {
const actionList = actions.map((action, index) => (
// eslint-disable-next-line react/no-array-index-key
{action}
));
return actionList;
}
export { CardGridProps } from './Grid';
export { CardMetaProps } from './Meta';
export type CardType = 'inner';
export type CardSize = 'default' | 'small';
export interface CardTabListType {
key: string;
tab: React.ReactNode;
disabled?: boolean;
}
export interface CardProps extends Omit, 'title'> {
prefixCls?: string;
title?: React.ReactNode;
extra?: React.ReactNode;
bordered?: boolean;
headStyle?: React.CSSProperties;
bodyStyle?: React.CSSProperties;
style?: React.CSSProperties;
loading?: boolean;
noHovering?: boolean;
hoverable?: boolean;
children?: React.ReactNode;
id?: string;
className?: string;
size?: CardSize;
type?: CardType;
cover?: React.ReactNode;
actions?: React.ReactNode[];
tabList?: CardTabListType[];
onTabChange?: (key: string) => void;
activeTabKey?: string;
defaultActiveTabKey?: string;
}
export default class Card extends React.Component {
static Grid: typeof Grid = Grid;
static Meta: typeof Meta = Meta;
componentDidMount() {
if ('noHovering' in this.props) {
warning(
!this.props.noHovering,
'Card',
'`noHovering` is deprecated, you can remove it safely or use `hoverable` instead.',
);
warning(
!!this.props.noHovering,
'Card',
'`noHovering={false}` is deprecated, use `hoverable` instead.',
);
}
}
// For 2.x compatible
getCompatibleHoverable() {
const { noHovering, hoverable } = this.props;
if ('noHovering' in this.props) {
return !noHovering || hoverable;
}
return !!hoverable;
}
onTabChange = (key: string) => {
if (this.props.onTabChange) {
this.props.onTabChange(key);
}
};
isContainGrid() {
let containGrid;
React.Children.forEach(this.props.children, (element: JSX.Element) => {
if (element && element.type && element.type === Grid) {
containGrid = true;
}
});
return containGrid;
}
renderCard = ({ getPrefixCls }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
className,
extra,
headStyle = {},
bodyStyle = {},
title,
loading,
bordered = true,
size = 'default',
type,
cover,
actions,
tabList,
children,
activeTabKey,
defaultActiveTabKey,
...others
} = this.props;
const prefixCls = getPrefixCls('card', customizePrefixCls);
const classString = classNames(prefixCls, className, {
[`${prefixCls}-loading`]: loading,
[`${prefixCls}-bordered`]: bordered,
[`${prefixCls}-hoverable`]: this.getCompatibleHoverable(),
[`${prefixCls}-contain-grid`]: this.isContainGrid(),
[`${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 = (
);
const hasActiveTabKey = activeTabKey !== undefined;
const extraProps = {
[hasActiveTabKey ? 'activeKey' : 'defaultActiveKey']: hasActiveTabKey
? activeTabKey
: defaultActiveTabKey,
};
let head;
const tabs =
tabList && tabList.length ? (
{tabList.map(item => (
))}
) : null;
if (title || extra || tabs) {
head = (
{title &&
{title}
}
{extra &&
{extra}
}
{tabs}
);
}
const coverDom = cover ? {cover}
: null;
const body = (
{loading ? loadingBlock : children}
);
const actionDom =
actions && actions.length ? (
) : null;
const divProps = omit(others, ['onTabChange', 'noHovering', 'hoverable']);
return (
{head}
{coverDom}
{body}
{actionDom}
);
};
render() {
return {this.renderCard};
}
}