2023-11-13 16:39:00 +08:00
|
|
|
import * as React from 'react';
|
2022-05-26 20:50:16 +08:00
|
|
|
import classNames from 'classnames';
|
2023-05-16 22:24:41 +08:00
|
|
|
import type { Tab } from 'rc-tabs/lib/interface';
|
2022-05-26 20:50:16 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2023-11-13 16:39:00 +08:00
|
|
|
|
2024-01-30 15:00:25 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2022-05-26 20:50:16 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-05-12 14:53:47 +08:00
|
|
|
import useSize from '../config-provider/hooks/useSize';
|
2022-05-27 16:13:19 +08:00
|
|
|
import Skeleton from '../skeleton';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { TabsProps } from '../tabs';
|
|
|
|
import Tabs from '../tabs';
|
|
|
|
import Grid from './Grid';
|
2022-05-27 16:40:17 +08:00
|
|
|
import useStyle from './style';
|
2022-05-26 20:50:16 +08:00
|
|
|
|
|
|
|
export type CardType = 'inner';
|
|
|
|
export type CardSize = 'default' | 'small';
|
|
|
|
|
2023-05-16 22:24:41 +08:00
|
|
|
export interface CardTabListType extends Omit<Tab, 'label'> {
|
2022-05-26 20:50:16 +08:00
|
|
|
key: string;
|
2023-05-16 22:24:41 +08:00
|
|
|
/** @deprecated Please use `label` instead */
|
|
|
|
tab?: React.ReactNode;
|
|
|
|
label?: React.ReactNode;
|
2022-05-26 20:50:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface CardProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'title'> {
|
|
|
|
prefixCls?: string;
|
|
|
|
title?: React.ReactNode;
|
|
|
|
extra?: React.ReactNode;
|
|
|
|
bordered?: boolean;
|
2024-01-30 15:00:25 +08:00
|
|
|
/** @deprecated Please use `styles.header` instead */
|
2022-05-26 20:50:16 +08:00
|
|
|
headStyle?: React.CSSProperties;
|
2024-01-30 15:00:25 +08:00
|
|
|
/** @deprecated Please use `styles.body` instead */
|
2022-05-26 20:50:16 +08:00
|
|
|
bodyStyle?: React.CSSProperties;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
loading?: boolean;
|
|
|
|
hoverable?: boolean;
|
|
|
|
children?: React.ReactNode;
|
|
|
|
id?: string;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2022-05-26 20:50:16 +08:00
|
|
|
size?: CardSize;
|
|
|
|
type?: CardType;
|
|
|
|
cover?: React.ReactNode;
|
|
|
|
actions?: React.ReactNode[];
|
|
|
|
tabList?: CardTabListType[];
|
|
|
|
tabBarExtraContent?: React.ReactNode;
|
|
|
|
onTabChange?: (key: string) => void;
|
|
|
|
activeTabKey?: string;
|
|
|
|
defaultActiveTabKey?: string;
|
|
|
|
tabProps?: TabsProps;
|
2024-01-30 15:00:25 +08:00
|
|
|
classNames?: {
|
|
|
|
header?: string;
|
|
|
|
body?: string;
|
|
|
|
extra?: string;
|
|
|
|
title?: string;
|
|
|
|
actions?: string;
|
|
|
|
cover?: string;
|
|
|
|
};
|
|
|
|
styles?: {
|
|
|
|
header?: React.CSSProperties;
|
|
|
|
body?: React.CSSProperties;
|
|
|
|
extra?: React.CSSProperties;
|
|
|
|
title?: React.CSSProperties;
|
|
|
|
actions?: React.CSSProperties;
|
|
|
|
cover?: React.CSSProperties;
|
|
|
|
};
|
2022-05-26 20:50:16 +08:00
|
|
|
}
|
|
|
|
|
2024-01-30 15:00:25 +08:00
|
|
|
type CardClassNamesModule = keyof Exclude<CardProps['classNames'], undefined>;
|
|
|
|
type CardStylesModule = keyof Exclude<CardProps['styles'], undefined>;
|
|
|
|
|
|
|
|
const ActionNode: React.FC<{
|
|
|
|
actionClasses: string;
|
|
|
|
actions: React.ReactNode[];
|
|
|
|
actionStyle: React.CSSProperties;
|
|
|
|
}> = (props) => {
|
|
|
|
const { actionClasses, actions = [], actionStyle } = props;
|
2023-11-23 10:19:57 +08:00
|
|
|
return (
|
2024-01-30 15:00:25 +08:00
|
|
|
<ul className={actionClasses} style={actionStyle}>
|
2023-11-23 10:19:57 +08:00
|
|
|
{actions.map<React.ReactNode>((action, index) => {
|
|
|
|
// Move this out since eslint not allow index key
|
|
|
|
// And eslint-disable makes conflict with rollup
|
|
|
|
// ref https://github.com/ant-design/ant-design/issues/46022
|
|
|
|
const key = `action-${index}`;
|
|
|
|
return (
|
|
|
|
<li style={{ width: `${100 / actions.length}%` }} key={key}>
|
|
|
|
<span>{action}</span>
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
};
|
2022-05-26 20:50:16 +08:00
|
|
|
|
2023-05-12 14:53:47 +08:00
|
|
|
const Card = React.forwardRef<HTMLDivElement, CardProps>((props, ref) => {
|
2022-05-26 20:50:16 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2023-07-03 10:25:55 +08:00
|
|
|
style,
|
2022-05-26 20:50:16 +08:00
|
|
|
extra,
|
|
|
|
headStyle = {},
|
|
|
|
bodyStyle = {},
|
|
|
|
title,
|
|
|
|
loading,
|
|
|
|
bordered = true,
|
|
|
|
size: customizeSize,
|
|
|
|
type,
|
|
|
|
cover,
|
|
|
|
actions,
|
|
|
|
tabList,
|
|
|
|
children,
|
|
|
|
activeTabKey,
|
|
|
|
defaultActiveTabKey,
|
|
|
|
tabBarExtraContent,
|
|
|
|
hoverable,
|
|
|
|
tabProps = {},
|
2024-01-30 15:00:25 +08:00
|
|
|
classNames: customClassNames,
|
|
|
|
styles: customStyles,
|
2022-05-26 20:50:16 +08:00
|
|
|
...others
|
|
|
|
} = props;
|
|
|
|
|
2023-07-03 10:25:55 +08:00
|
|
|
const { getPrefixCls, direction, card } = React.useContext(ConfigContext);
|
2023-05-12 14:53:47 +08:00
|
|
|
|
2024-01-30 15:00:25 +08:00
|
|
|
// =================Warning===================
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
const warning = devUseWarning('Card');
|
|
|
|
[
|
|
|
|
['headStyle', 'styles.header'],
|
|
|
|
['bodyStyle', 'styles.body'],
|
|
|
|
].forEach(([deprecatedName, newName]) => {
|
|
|
|
warning.deprecated(!(deprecatedName in props), deprecatedName, newName);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-05-12 14:53:47 +08:00
|
|
|
const onTabChange = (key: string) => {
|
|
|
|
props.onTabChange?.(key);
|
|
|
|
};
|
|
|
|
|
2024-01-30 15:00:25 +08:00
|
|
|
const moduleClass = (moduleName: CardClassNamesModule) =>
|
|
|
|
classNames(card?.classNames?.[moduleName], customClassNames?.[moduleName]);
|
|
|
|
|
2024-06-03 11:24:39 +08:00
|
|
|
const moduleStyle = (moduleName: CardStylesModule): React.CSSProperties => ({
|
2024-01-30 15:00:25 +08:00
|
|
|
...card?.styles?.[moduleName],
|
|
|
|
...customStyles?.[moduleName],
|
|
|
|
});
|
|
|
|
|
2023-05-12 14:53:47 +08:00
|
|
|
const isContainGrid = React.useMemo<boolean>(() => {
|
|
|
|
let containGrid = false;
|
2024-04-01 15:49:45 +08:00
|
|
|
React.Children.forEach(children as React.ReactElement, (element: JSX.Element) => {
|
2024-06-22 21:59:12 +08:00
|
|
|
if (element?.type === Grid) {
|
2023-05-12 14:53:47 +08:00
|
|
|
containGrid = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return containGrid;
|
|
|
|
}, [children]);
|
|
|
|
|
2022-05-26 20:50:16 +08:00
|
|
|
const prefixCls = getPrefixCls('card', customizePrefixCls);
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2022-05-26 20:50:16 +08:00
|
|
|
|
|
|
|
const loadingBlock = (
|
2022-05-27 16:13:19 +08:00
|
|
|
<Skeleton loading active paragraph={{ rows: 4 }} title={false}>
|
|
|
|
{children}
|
|
|
|
</Skeleton>
|
2022-05-26 20:50:16 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const hasActiveTabKey = activeTabKey !== undefined;
|
|
|
|
const extraProps = {
|
|
|
|
...tabProps,
|
|
|
|
[hasActiveTabKey ? 'activeKey' : 'defaultActiveKey']: hasActiveTabKey
|
|
|
|
? activeTabKey
|
|
|
|
: defaultActiveTabKey,
|
|
|
|
tabBarExtraContent,
|
|
|
|
};
|
|
|
|
|
|
|
|
let head: React.ReactNode;
|
2023-05-12 14:53:47 +08:00
|
|
|
const mergedSize = useSize(customizeSize);
|
2023-05-07 17:44:36 +08:00
|
|
|
const tabSize = !mergedSize || mergedSize === 'default' ? 'large' : mergedSize;
|
2023-07-07 17:29:01 +08:00
|
|
|
const tabs = tabList ? (
|
|
|
|
<Tabs
|
|
|
|
size={tabSize}
|
|
|
|
{...extraProps}
|
|
|
|
className={`${prefixCls}-head-tabs`}
|
|
|
|
onChange={onTabChange}
|
2023-07-07 23:28:35 +08:00
|
|
|
items={tabList.map(({ tab, ...item }) => ({ label: tab, ...item }))}
|
2023-07-07 17:29:01 +08:00
|
|
|
/>
|
|
|
|
) : null;
|
2022-05-26 20:50:16 +08:00
|
|
|
if (title || extra || tabs) {
|
2024-01-30 15:00:25 +08:00
|
|
|
const headClasses = classNames(`${prefixCls}-head`, moduleClass('header'));
|
|
|
|
const titleClasses = classNames(`${prefixCls}-head-title`, moduleClass('title'));
|
|
|
|
const extraClasses = classNames(`${prefixCls}-extra`, moduleClass('extra'));
|
|
|
|
const mergedHeadStyle: React.CSSProperties = {
|
|
|
|
...headStyle,
|
|
|
|
...moduleStyle('header'),
|
|
|
|
};
|
2022-05-26 20:50:16 +08:00
|
|
|
head = (
|
2024-01-30 15:00:25 +08:00
|
|
|
<div className={headClasses} style={mergedHeadStyle}>
|
2022-05-26 20:50:16 +08:00
|
|
|
<div className={`${prefixCls}-head-wrapper`}>
|
2024-01-30 15:00:25 +08:00
|
|
|
{title && (
|
|
|
|
<div className={titleClasses} style={moduleStyle('title')}>
|
|
|
|
{title}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{extra && (
|
|
|
|
<div className={extraClasses} style={moduleStyle('extra')}>
|
|
|
|
{extra}
|
|
|
|
</div>
|
|
|
|
)}
|
2022-05-26 20:50:16 +08:00
|
|
|
</div>
|
|
|
|
{tabs}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2024-01-30 15:00:25 +08:00
|
|
|
const coverClasses = classNames(`${prefixCls}-cover`, moduleClass('cover'));
|
|
|
|
const coverDom = cover ? (
|
|
|
|
<div className={coverClasses} style={moduleStyle('cover')}>
|
|
|
|
{cover}
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
const bodyClasses = classNames(`${prefixCls}-body`, moduleClass('body'));
|
|
|
|
const mergedBodyStyle: React.CSSProperties = {
|
|
|
|
...bodyStyle,
|
|
|
|
...moduleStyle('body'),
|
|
|
|
};
|
2022-05-26 20:50:16 +08:00
|
|
|
const body = (
|
2024-01-30 15:00:25 +08:00
|
|
|
<div className={bodyClasses} style={mergedBodyStyle}>
|
2022-05-26 20:50:16 +08:00
|
|
|
{loading ? loadingBlock : children}
|
|
|
|
</div>
|
|
|
|
);
|
2023-11-23 10:19:57 +08:00
|
|
|
|
2024-01-30 15:00:25 +08:00
|
|
|
const actionClasses = classNames(`${prefixCls}-actions`, moduleClass('actions'));
|
2024-06-22 21:59:12 +08:00
|
|
|
const actionDom = actions?.length ? (
|
|
|
|
<ActionNode
|
|
|
|
actionClasses={actionClasses}
|
|
|
|
actionStyle={moduleStyle('actions')}
|
|
|
|
actions={actions}
|
|
|
|
/>
|
|
|
|
) : null;
|
2023-07-03 10:25:55 +08:00
|
|
|
|
2022-05-26 20:50:16 +08:00
|
|
|
const divProps = omit(others, ['onTabChange']);
|
2023-07-03 10:25:55 +08:00
|
|
|
|
2022-05-26 20:50:16 +08:00
|
|
|
const classString = classNames(
|
|
|
|
prefixCls,
|
2023-07-03 10:25:55 +08:00
|
|
|
card?.className,
|
2022-05-26 20:50:16 +08:00
|
|
|
{
|
|
|
|
[`${prefixCls}-loading`]: loading,
|
|
|
|
[`${prefixCls}-bordered`]: bordered,
|
|
|
|
[`${prefixCls}-hoverable`]: hoverable,
|
2023-05-12 14:53:47 +08:00
|
|
|
[`${prefixCls}-contain-grid`]: isContainGrid,
|
2024-06-22 21:59:12 +08:00
|
|
|
[`${prefixCls}-contain-tabs`]: tabList?.length,
|
2022-05-26 20:50:16 +08:00
|
|
|
[`${prefixCls}-${mergedSize}`]: mergedSize,
|
|
|
|
[`${prefixCls}-type-${type}`]: !!type,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-05-27 16:40:17 +08:00
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2022-05-26 20:50:16 +08:00
|
|
|
);
|
|
|
|
|
2023-07-03 10:25:55 +08:00
|
|
|
const mergedStyle: React.CSSProperties = { ...card?.style, ...style };
|
|
|
|
|
2023-11-13 16:39:00 +08:00
|
|
|
return wrapCSSVar(
|
2023-07-03 10:25:55 +08:00
|
|
|
<div ref={ref} {...divProps} className={classString} style={mergedStyle}>
|
2022-05-26 20:50:16 +08:00
|
|
|
{head}
|
|
|
|
{coverDom}
|
|
|
|
{body}
|
|
|
|
{actionDom}
|
2022-05-27 16:40:17 +08:00
|
|
|
</div>,
|
2022-05-26 20:50:16 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default Card;
|