import * as React from 'react'; import classNames from 'classnames'; import addEventListener from 'rc-util/lib/Dom/addEventListener'; import omit from 'omit.js'; import Grid from './Grid'; import Meta from './Meta'; import Tabs from '../tabs'; import { throttleByAnimationFrameDecorator } from '../_util/throttleByAnimationFrame'; import warning from '../_util/warning'; export { CardGridProps } from './Grid'; export { CardMetaProps } from './Meta'; export type CardType = 'inner'; export interface CardTabListType { key: string; tab: React.ReactNode; } export interface CardProps { prefixCls?: string; title?: React.ReactNode; extra?: React.ReactNode; bordered?: boolean; bodyStyle?: React.CSSProperties; style?: React.CSSProperties; loading?: boolean; noHovering?: boolean; hoverable?: boolean; children?: React.ReactNode; id?: string; className?: string; type?: CardType; cover?: React.ReactNode; actions?: Array; tabList?: CardTabListType[]; onTabChange?: (key: string) => void; } export default class Card extends React.Component { static Grid: typeof Grid = Grid; static Meta: typeof Meta = Meta; resizeEvent: any; updateWiderPaddingCalled: boolean; state = { widerPadding: false, }; private container: HTMLDivElement; componentDidMount() { this.updateWiderPadding(); this.resizeEvent = addEventListener(window, 'resize', this.updateWiderPadding); if ('noHovering' in this.props) { warning( !this.props.noHovering, '`noHovering` of Card is deperated, you can remove it safely or use `hoverable` instead.', ); warning(!!this.props.noHovering, '`noHovering={false}` of Card is deperated, use `hoverable` instead.'); } } componentWillUnmount() { if (this.resizeEvent) { this.resizeEvent.remove(); } (this.updateWiderPadding as any).cancel(); } @throttleByAnimationFrameDecorator() updateWiderPadding() { if (!this.container) { return; } // 936 is a magic card width pixer number indicated by designer const WIDTH_BOUDARY_PX = 936; if (this.container.offsetWidth >= WIDTH_BOUDARY_PX && !this.state.widerPadding) { this.setState({ widerPadding: true }, () => { this.updateWiderPaddingCalled = true; // first render without css transition }); } if (this.container.offsetWidth < WIDTH_BOUDARY_PX && this.state.widerPadding) { this.setState({ widerPadding: false }, () => { this.updateWiderPaddingCalled = true; // first render without css transition }); } } onTabChange = (key: string) => { if (this.props.onTabChange) { this.props.onTabChange(key); } } saveRef = (node: HTMLDivElement) => { this.container = node; } isContainGrid() { let containGrid; React.Children.forEach(this.props.children, (element: JSX.Element) => { if (element && element.type && element.type === Grid) { containGrid = true; } }); return containGrid; } getAction(actions: React.ReactNode[]) { if (!actions || !actions.length) { return null; } const actionList = actions.map((action, index) => (
  • {action}
  • ), ); return actionList; } // For 2.x compatible getCompatibleHoverable() { const { noHovering, hoverable } = this.props; if ('noHovering' in this.props) { return !noHovering || hoverable; } return !!hoverable; } render() { const { prefixCls = 'ant-card', className, extra, bodyStyle, noHovering, hoverable, title, loading, bordered = true, type, cover, actions, tabList, children, ...others, } = this.props; const classString = classNames(prefixCls, className, { [`${prefixCls}-loading`]: loading, [`${prefixCls}-bordered`]: bordered, [`${prefixCls}-hoverable`]: this.getCompatibleHoverable(), [`${prefixCls}-wider-padding`]: this.state.widerPadding, [`${prefixCls}-padding-transition`]: this.updateWiderPaddingCalled, [`${prefixCls}-contain-grid`]: this.isContainGrid(), [`${prefixCls}-contain-tabs`]: tabList && tabList.length, [`${prefixCls}-type-${type}`]: !!type, }); const loadingBlock = (

    ); 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', ]); return (
    {head} {coverDom} {children ? body : null} {actionDom}
    ); } }