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-02 15:08:55 +08:00
|
|
|
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
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';
|
|
|
|
import Tabs from '../tabs';
|
2018-04-20 11:19:30 +08:00
|
|
|
import Row from '../row';
|
|
|
|
import Col from '../col';
|
2017-07-14 11:37:28 +08:00
|
|
|
import { throttleByAnimationFrameDecorator } from '../_util/throttleByAnimationFrame';
|
2017-09-27 21:57:18 +08:00
|
|
|
import warning from '../_util/warning';
|
2018-04-11 19:40:31 +08:00
|
|
|
import { Omit } from '../_util/type';
|
2016-07-14 13:29:50 +08:00
|
|
|
|
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';
|
|
|
|
|
|
|
|
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-07-07 13:36:54 +08:00
|
|
|
noHovering?: 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;
|
2017-08-09 14:34:10 +08:00
|
|
|
type?: CardType;
|
|
|
|
cover?: React.ReactNode;
|
|
|
|
actions?: Array<React.ReactNode>;
|
|
|
|
tabList?: CardTabListType[];
|
|
|
|
onTabChange?: (key: string) => void;
|
2018-03-10 14:44:12 +08:00
|
|
|
activeTabKey?: string;
|
|
|
|
defaultActiveTabKey?: string;
|
2016-07-14 13:29:50 +08:00
|
|
|
}
|
|
|
|
|
2018-04-20 10:44:33 +08:00
|
|
|
export interface CardState {
|
|
|
|
widerPadding: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Card extends React.Component<CardProps, CardState> {
|
2017-07-14 11:55:00 +08:00
|
|
|
static Grid: typeof Grid = Grid;
|
2017-08-09 14:34:10 +08:00
|
|
|
static Meta: typeof Meta = Meta;
|
2018-04-20 10:44:33 +08:00
|
|
|
|
2017-07-14 11:37:28 +08:00
|
|
|
state = {
|
|
|
|
widerPadding: false,
|
|
|
|
};
|
2018-04-20 10:44:33 +08:00
|
|
|
|
|
|
|
private resizeEvent: any;
|
|
|
|
private updateWiderPaddingCalled: boolean = false;
|
2017-09-17 15:48:44 +08:00
|
|
|
private container: HTMLDivElement;
|
2017-07-14 11:37:28 +08:00
|
|
|
componentDidMount() {
|
|
|
|
this.updateWiderPadding();
|
|
|
|
this.resizeEvent = addEventListener(window, 'resize', this.updateWiderPadding);
|
2017-09-27 21:57:18 +08:00
|
|
|
|
|
|
|
if ('noHovering' in this.props) {
|
|
|
|
warning(
|
|
|
|
!this.props.noHovering,
|
2018-04-15 16:52:04 +08:00
|
|
|
'`noHovering` of Card is deprecated, you can remove it safely or use `hoverable` instead.',
|
2017-09-27 21:57:18 +08:00
|
|
|
);
|
2018-04-15 16:52:04 +08:00
|
|
|
warning(!!this.props.noHovering, '`noHovering={false}` of Card is deprecated, use `hoverable` instead.');
|
2017-09-27 21:57:18 +08:00
|
|
|
}
|
2016-02-22 16:25:13 +08:00
|
|
|
}
|
2017-07-14 11:37:28 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.resizeEvent) {
|
|
|
|
this.resizeEvent.remove();
|
|
|
|
}
|
2017-07-29 14:16:27 +08:00
|
|
|
(this.updateWiderPadding as any).cancel();
|
2016-10-14 14:41:21 +08:00
|
|
|
}
|
2017-07-14 11:37:28 +08:00
|
|
|
@throttleByAnimationFrameDecorator()
|
|
|
|
updateWiderPadding() {
|
2017-07-29 14:16:27 +08:00
|
|
|
if (!this.container) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-15 16:52:04 +08:00
|
|
|
// 936 is a magic card width pixel number indicated by designer
|
|
|
|
const WIDTH_BOUNDARY_PX = 936;
|
|
|
|
if (this.container.offsetWidth >= WIDTH_BOUNDARY_PX && !this.state.widerPadding) {
|
2017-07-29 14:16:27 +08:00
|
|
|
this.setState({ widerPadding: true }, () => {
|
|
|
|
this.updateWiderPaddingCalled = true; // first render without css transition
|
|
|
|
});
|
|
|
|
}
|
2018-04-15 16:52:04 +08:00
|
|
|
if (this.container.offsetWidth < WIDTH_BOUNDARY_PX && this.state.widerPadding) {
|
2017-07-29 14:16:27 +08:00
|
|
|
this.setState({ widerPadding: false }, () => {
|
|
|
|
this.updateWiderPaddingCalled = true; // first render without css transition
|
|
|
|
});
|
2017-07-14 11:37:28 +08:00
|
|
|
}
|
|
|
|
}
|
2017-11-22 11:07:19 +08:00
|
|
|
onTabChange = (key: string) => {
|
2017-08-09 14:34:10 +08:00
|
|
|
if (this.props.onTabChange) {
|
|
|
|
this.props.onTabChange(key);
|
|
|
|
}
|
|
|
|
}
|
2017-07-22 21:12:55 +08:00
|
|
|
saveRef = (node: HTMLDivElement) => {
|
2017-07-14 11:37:28 +08:00
|
|
|
this.container = node;
|
|
|
|
}
|
2017-07-14 16:10:04 +08:00
|
|
|
isContainGrid() {
|
|
|
|
let containGrid;
|
2017-11-17 14:38:54 +08:00
|
|
|
React.Children.forEach(this.props.children, (element: JSX.Element) => {
|
2017-07-14 16:10:04 +08:00
|
|
|
if (element && element.type && element.type === Grid) {
|
|
|
|
containGrid = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return containGrid;
|
|
|
|
}
|
2017-11-22 11:07:19 +08:00
|
|
|
getAction(actions: React.ReactNode[]) {
|
2017-08-09 14:34:10 +08:00
|
|
|
if (!actions || !actions.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const actionList = actions.map((action, index) => (
|
|
|
|
<li style={{ width: `${100 / actions.length}%` }} key={`action-${index}`}>
|
|
|
|
<span>{action}</span>
|
|
|
|
</li>
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return actionList;
|
|
|
|
}
|
2017-09-27 21:57:18 +08:00
|
|
|
// For 2.x compatible
|
|
|
|
getCompatibleHoverable() {
|
|
|
|
const { noHovering, hoverable } = this.props;
|
|
|
|
if ('noHovering' in this.props) {
|
|
|
|
return !noHovering || hoverable;
|
|
|
|
}
|
|
|
|
return !!hoverable;
|
|
|
|
}
|
2017-07-14 11:37:28 +08:00
|
|
|
render() {
|
|
|
|
const {
|
2018-07-23 01:31:25 +08:00
|
|
|
prefixCls = 'ant-card', className, extra, headStyle = {}, bodyStyle = {}, noHovering, hoverable, title, loading,
|
2018-06-02 20:12:09 +08:00
|
|
|
bordered = true, type, cover, actions, tabList, children, activeTabKey, defaultActiveTabKey, ...others
|
2017-07-14 11:37:28 +08:00
|
|
|
} = this.props;
|
2017-07-14 16:10:04 +08:00
|
|
|
|
2017-07-14 11:37:28 +08:00
|
|
|
const classString = classNames(prefixCls, className, {
|
|
|
|
[`${prefixCls}-loading`]: loading,
|
|
|
|
[`${prefixCls}-bordered`]: bordered,
|
2017-09-27 21:57:18 +08:00
|
|
|
[`${prefixCls}-hoverable`]: this.getCompatibleHoverable(),
|
2017-07-14 11:37:28 +08:00
|
|
|
[`${prefixCls}-wider-padding`]: this.state.widerPadding,
|
2017-07-14 15:57:26 +08:00
|
|
|
[`${prefixCls}-padding-transition`]: this.updateWiderPaddingCalled,
|
2017-07-14 16:10:04 +08:00
|
|
|
[`${prefixCls}-contain-grid`]: this.isContainGrid(),
|
2017-08-23 19:02:27 +08:00
|
|
|
[`${prefixCls}-contain-tabs`]: tabList && tabList.length,
|
2017-08-09 14:34:10 +08:00
|
|
|
[`${prefixCls}-type-${type}`]: !!type,
|
2017-07-14 11:37:28 +08:00
|
|
|
});
|
2016-02-22 16:25:13 +08:00
|
|
|
|
2018-05-03 15:17:04 +08:00
|
|
|
const loadingBlockStyle = (bodyStyle.padding === 0 || bodyStyle.padding === '0px')
|
|
|
|
? { padding: 24 } : undefined;
|
|
|
|
|
2017-08-09 14:34:10 +08:00
|
|
|
const loadingBlock = (
|
2018-05-03 15:17:04 +08:00
|
|
|
<div
|
|
|
|
className={`${prefixCls}-loading-content`}
|
|
|
|
style={loadingBlockStyle}
|
|
|
|
>
|
2018-04-20 11:19:30 +08:00
|
|
|
<Row gutter={8}>
|
|
|
|
<Col span={22}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row gutter={8}>
|
|
|
|
<Col span={8}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
<Col span={15}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row gutter={8}>
|
|
|
|
<Col span={6}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
<Col span={18}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row gutter={8}>
|
|
|
|
<Col span={13}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
<Col span={9}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row gutter={8}>
|
|
|
|
<Col span={4}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
<Col span={3}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
<Col span={16}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row gutter={8}>
|
|
|
|
<Col span={8}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
<Col span={6}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
<Col span={8}>
|
|
|
|
<div className={`${prefixCls}-loading-block`} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2017-08-09 14:34:10 +08:00
|
|
|
</div>
|
|
|
|
);
|
2017-07-07 13:36:54 +08:00
|
|
|
|
2018-03-10 14:44:12 +08:00
|
|
|
const hasActiveTabKey = activeTabKey !== undefined;
|
|
|
|
const extraProps = {
|
|
|
|
[hasActiveTabKey ? 'activeKey' : 'defaultActiveKey']: hasActiveTabKey
|
|
|
|
? activeTabKey
|
|
|
|
: defaultActiveTabKey,
|
|
|
|
};
|
|
|
|
|
2017-07-14 11:37:28 +08:00
|
|
|
let head;
|
2017-08-09 14:34:10 +08:00
|
|
|
const tabs = tabList && tabList.length ? (
|
2018-03-10 14:44:12 +08:00
|
|
|
<Tabs
|
|
|
|
{...extraProps}
|
|
|
|
className={`${prefixCls}-head-tabs`}
|
|
|
|
size="large"
|
|
|
|
onChange={this.onTabChange}
|
|
|
|
>
|
2018-07-16 19:18:50 +08:00
|
|
|
{tabList.map(item => <Tabs.TabPane tab={item.tab} disabled={item.disabled} key={item.key} />)}
|
2017-08-15 22:50:00 +08:00
|
|
|
</Tabs>
|
|
|
|
) : null;
|
|
|
|
if (title || extra || tabs) {
|
2017-08-15 22:34:48 +08:00
|
|
|
head = (
|
2018-07-23 01:31:25 +08:00
|
|
|
<div className={`${prefixCls}-head`} style={headStyle}>
|
2017-09-20 21:56:56 +08:00
|
|
|
<div className={`${prefixCls}-head-wrapper`}>
|
|
|
|
{title && <div className={`${prefixCls}-head-title`}>{title}</div>}
|
|
|
|
{extra && <div className={`${prefixCls}-extra`}>{extra}</div>}
|
|
|
|
</div>
|
2017-08-09 14:34:10 +08:00
|
|
|
{tabs}
|
2017-07-14 11:37:28 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-08-09 14:34:10 +08:00
|
|
|
const coverDom = cover ? <div className={`${prefixCls}-cover`}>{cover}</div> : null;
|
2017-08-14 21:43:32 +08:00
|
|
|
const body = (
|
|
|
|
<div className={`${prefixCls}-body`} style={bodyStyle}>
|
2018-02-02 11:53:41 +08:00
|
|
|
{loading ? loadingBlock : children}
|
2017-08-14 21:43:32 +08:00
|
|
|
</div>
|
|
|
|
);
|
2017-08-09 14:34:10 +08:00
|
|
|
const actionDom = actions && actions.length ?
|
|
|
|
<ul className={`${prefixCls}-actions`}>{this.getAction(actions)}</ul> : null;
|
|
|
|
const divProps = omit(others, [
|
|
|
|
'onTabChange',
|
|
|
|
]);
|
|
|
|
return (
|
|
|
|
<div {...divProps} className={classString} ref={this.saveRef}>
|
2017-12-05 13:35:32 +08:00
|
|
|
{head}
|
|
|
|
{coverDom}
|
2018-02-06 11:45:22 +08:00
|
|
|
{body}
|
2017-08-09 14:34:10 +08:00
|
|
|
{actionDom}
|
2017-07-14 11:37:28 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|