2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-08-07 21:07:52 +08:00
|
|
|
import * as PropTypes from 'prop-types';
|
2017-08-09 15:45:08 +08:00
|
|
|
import classNames from 'classnames';
|
2017-08-14 11:08:19 +08:00
|
|
|
import { Col } from '../grid';
|
2017-11-21 19:02:04 +08:00
|
|
|
import { ListGridType, ColumnType } from './index';
|
2017-08-09 15:45:08 +08:00
|
|
|
|
|
|
|
export interface ListItemProps {
|
|
|
|
className?: string;
|
|
|
|
children?: React.ReactNode;
|
|
|
|
prefixCls?: string;
|
|
|
|
style?: React.CSSProperties;
|
2017-12-05 16:12:55 +08:00
|
|
|
extra?: React.ReactNode;
|
2018-11-01 14:49:51 +08:00
|
|
|
actions?: React.ReactNode[];
|
2017-08-21 21:14:20 +08:00
|
|
|
grid?: ListGridType;
|
2017-08-09 15:45:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ListItemMetaProps {
|
|
|
|
avatar?: React.ReactNode;
|
|
|
|
className?: string;
|
|
|
|
children?: React.ReactNode;
|
2017-12-05 16:28:12 +08:00
|
|
|
description?: React.ReactNode;
|
2017-08-09 15:45:08 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
style?: React.CSSProperties;
|
2017-12-05 16:28:12 +08:00
|
|
|
title?: React.ReactNode;
|
2017-08-09 15:45:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Meta = (props: ListItemMetaProps) => {
|
|
|
|
const {
|
|
|
|
prefixCls = 'ant-list',
|
|
|
|
className,
|
|
|
|
avatar,
|
|
|
|
title,
|
|
|
|
description,
|
2018-06-02 20:12:09 +08:00
|
|
|
...others
|
2017-08-21 21:14:20 +08:00
|
|
|
} = props;
|
2017-08-09 15:45:08 +08:00
|
|
|
|
|
|
|
const classString = classNames(`${prefixCls}-item-meta`, className);
|
|
|
|
|
|
|
|
const content = (
|
|
|
|
<div className={`${prefixCls}-item-meta-content`}>
|
|
|
|
{title && <h4 className={`${prefixCls}-item-meta-title`}>{title}</h4>}
|
2017-08-21 21:14:20 +08:00
|
|
|
{description && <div className={`${prefixCls}-item-meta-description`}>{description}</div>}
|
2017-08-09 15:45:08 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div {...others} className={classString}>
|
|
|
|
{avatar && <div className={`${prefixCls}-item-meta-avatar`}>{avatar}</div>}
|
|
|
|
{(title || description) && content}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-11-21 19:02:04 +08:00
|
|
|
function getGrid(grid: ListGridType, t: ColumnType) {
|
|
|
|
return grid[t] && Math.floor(24 / grid[t]!);
|
2017-08-21 21:14:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const GridColumns = ['', 1, 2, 3, 4, 6, 8, 12, 24];
|
|
|
|
|
2017-08-09 15:45:08 +08:00
|
|
|
export default class Item extends React.Component<ListItemProps, any> {
|
|
|
|
static Meta: typeof Meta = Meta;
|
2017-08-21 21:14:20 +08:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
column: PropTypes.oneOf(GridColumns),
|
|
|
|
xs: PropTypes.oneOf(GridColumns),
|
|
|
|
sm: PropTypes.oneOf(GridColumns),
|
|
|
|
md: PropTypes.oneOf(GridColumns),
|
|
|
|
lg: PropTypes.oneOf(GridColumns),
|
|
|
|
xl: PropTypes.oneOf(GridColumns),
|
2018-01-21 01:03:00 +08:00
|
|
|
xxl: PropTypes.oneOf(GridColumns),
|
2017-08-21 21:14:20 +08:00
|
|
|
};
|
|
|
|
|
2017-09-06 15:53:25 +08:00
|
|
|
static contextTypes = {
|
|
|
|
grid: PropTypes.any,
|
|
|
|
};
|
2017-08-14 11:08:19 +08:00
|
|
|
|
2017-08-09 15:45:08 +08:00
|
|
|
render() {
|
2017-09-06 15:53:25 +08:00
|
|
|
const { grid } = this.context;
|
|
|
|
const { prefixCls = 'ant-list', children, actions, extra, className, ...others } = this.props;
|
2017-08-09 15:45:08 +08:00
|
|
|
const classString = classNames(`${prefixCls}-item`, className);
|
|
|
|
|
2017-08-09 21:46:48 +08:00
|
|
|
const metaContent: React.ReactElement<any>[] = [];
|
|
|
|
const otherContent: React.ReactElement<any>[] = [];
|
|
|
|
|
|
|
|
React.Children.forEach(children, (element: React.ReactElement<any>) => {
|
|
|
|
if (element && element.type && element.type === Meta) {
|
|
|
|
metaContent.push(element);
|
|
|
|
} else {
|
|
|
|
otherContent.push(element);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-06 15:53:25 +08:00
|
|
|
const contentClassString = classNames(`${prefixCls}-item-content`, {
|
|
|
|
[`${prefixCls}-item-content-single`]: (metaContent.length < 1),
|
|
|
|
});
|
2017-10-12 18:09:37 +08:00
|
|
|
const content = otherContent.length > 0 ? (
|
2017-09-06 15:53:25 +08:00
|
|
|
<div className={contentClassString}>
|
2017-08-09 21:46:48 +08:00
|
|
|
{otherContent}
|
2017-10-12 18:09:37 +08:00
|
|
|
</div>) : null;
|
2017-08-09 21:46:48 +08:00
|
|
|
|
|
|
|
let actionsContent;
|
|
|
|
if (actions && actions.length > 0) {
|
2017-11-21 19:02:04 +08:00
|
|
|
const actionsContentItem = (action: React.ReactNode, i: number) => (
|
2017-09-06 15:53:25 +08:00
|
|
|
<li key={`${prefixCls}-item-action-${i}`}>
|
2017-08-09 21:46:48 +08:00
|
|
|
{action}
|
2017-08-14 11:08:19 +08:00
|
|
|
{i !== (actions.length - 1) && <em className={`${prefixCls}-item-action-split`}/>}
|
2017-08-09 21:46:48 +08:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
actionsContent = (
|
|
|
|
<ul className={`${prefixCls}-item-action`}>
|
|
|
|
{actions.map((action, i) => actionsContentItem(action, i))}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const extraContent = (
|
|
|
|
<div className={`${prefixCls}-item-extra-wrap`}>
|
|
|
|
<div className={`${prefixCls}-item-main`}>
|
|
|
|
{metaContent}
|
|
|
|
{content}
|
|
|
|
{actionsContent}
|
|
|
|
</div>
|
|
|
|
<div className={`${prefixCls}-item-extra`}>{extra}</div>
|
|
|
|
</div>
|
|
|
|
);
|
2017-08-09 15:45:08 +08:00
|
|
|
|
2017-08-21 21:14:20 +08:00
|
|
|
const mainContent = grid ? (
|
|
|
|
<Col
|
|
|
|
span={getGrid(grid, 'column')}
|
|
|
|
xs={getGrid(grid, 'xs')}
|
|
|
|
sm={getGrid(grid, 'sm')}
|
|
|
|
md={getGrid(grid, 'md')}
|
|
|
|
lg={getGrid(grid, 'lg')}
|
|
|
|
xl={getGrid(grid, 'xl')}
|
2018-01-21 01:03:00 +08:00
|
|
|
xxl={getGrid(grid, 'xxl')}
|
2017-08-21 21:14:20 +08:00
|
|
|
>
|
|
|
|
<div {...others} className={classString}>
|
|
|
|
{extra && extraContent}
|
|
|
|
{!extra && metaContent}
|
|
|
|
{!extra && content}
|
|
|
|
{!extra && actionsContent}
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
) : (
|
2017-08-09 15:45:08 +08:00
|
|
|
<div {...others} className={classString}>
|
2017-08-09 21:46:48 +08:00
|
|
|
{extra && extraContent}
|
|
|
|
{!extra && metaContent}
|
|
|
|
{!extra && content}
|
|
|
|
{!extra && actionsContent}
|
2017-08-09 15:45:08 +08:00
|
|
|
</div>
|
|
|
|
);
|
2017-08-14 11:08:19 +08:00
|
|
|
|
|
|
|
return mainContent;
|
2017-08-09 15:45:08 +08:00
|
|
|
}
|
|
|
|
}
|