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-11-21 19:02:04 +08:00
|
|
|
import { ListGridType, ColumnType } from './index';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { Col } from '../grid';
|
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2017-08-09 15:45:08 +08:00
|
|
|
|
2019-01-10 12:13:27 +08:00
|
|
|
export interface ListItemProps extends React.HTMLAttributes<HTMLDivElement> {
|
2017-08-09 15:45:08 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
export const Meta = (props: ListItemMetaProps) => (
|
|
|
|
<ConfigConsumer>
|
|
|
|
{({ getPrefixCls }: ConfigConsumerProps) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
avatar,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
...others
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('list', customizePrefixCls);
|
|
|
|
const classString = classNames(`${prefixCls}-item-meta`, className);
|
|
|
|
|
|
|
|
const content = (
|
|
|
|
<div className={`${prefixCls}-item-meta-content`}>
|
|
|
|
{title && <h4 className={`${prefixCls}-item-meta-title`}>{title}</h4>}
|
|
|
|
{description && <div className={`${prefixCls}-item-meta-description`}>{description}</div>}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div {...others} className={classString}>
|
|
|
|
{avatar && <div className={`${prefixCls}-item-meta-avatar`}>{avatar}</div>}
|
|
|
|
{(title || description) && content}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ConfigConsumer>
|
|
|
|
);
|
2017-08-09 15:45:08 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-09-06 15:53:25 +08:00
|
|
|
static contextTypes = {
|
|
|
|
grid: PropTypes.any,
|
2019-03-11 23:34:05 +08:00
|
|
|
itemLayout: PropTypes.string,
|
2017-09-06 15:53:25 +08:00
|
|
|
};
|
2017-08-14 11:08:19 +08:00
|
|
|
|
2018-11-09 17:38:19 +08:00
|
|
|
context: any;
|
|
|
|
|
2019-03-06 16:31:07 +08:00
|
|
|
isItemContainsTextNode() {
|
|
|
|
const { children } = this.props;
|
|
|
|
let result;
|
|
|
|
React.Children.forEach(children, (element: React.ReactElement<any>) => {
|
|
|
|
if (typeof element === 'string') {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-11 23:34:05 +08:00
|
|
|
isFlexMode() {
|
|
|
|
const { extra } = this.props;
|
|
|
|
const { itemLayout } = this.context;
|
|
|
|
if (itemLayout === 'vertical') {
|
|
|
|
return !!extra;
|
|
|
|
}
|
|
|
|
return !this.isItemContainsTextNode();
|
|
|
|
}
|
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderItem = ({ getPrefixCls }: ConfigConsumerProps) => {
|
2019-03-11 23:34:05 +08:00
|
|
|
const { grid, itemLayout } = this.context;
|
2018-12-07 20:02:01 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
children,
|
|
|
|
actions,
|
|
|
|
extra,
|
|
|
|
className,
|
|
|
|
...others
|
|
|
|
} = this.props;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('list', customizePrefixCls);
|
2019-03-05 22:17:22 +08:00
|
|
|
const actionsContent = actions && actions.length > 0 && (
|
2019-03-06 16:31:07 +08:00
|
|
|
<ul className={`${prefixCls}-item-action`} key="actions">
|
2019-03-05 22:17:22 +08:00
|
|
|
{actions.map((action: React.ReactNode, i: number) => (
|
|
|
|
<li key={`${prefixCls}-item-action-${i}`}>
|
|
|
|
{action}
|
|
|
|
{i !== actions.length - 1 && <em className={`${prefixCls}-item-action-split`} />}
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
const itemChildren = (
|
2019-03-06 16:31:07 +08:00
|
|
|
<div
|
|
|
|
{...others}
|
|
|
|
className={classNames(`${prefixCls}-item`, className, {
|
2019-03-11 23:34:05 +08:00
|
|
|
[`${prefixCls}-item-no-flex`]: !this.isFlexMode(),
|
2019-03-06 16:31:07 +08:00
|
|
|
})}
|
|
|
|
>
|
2019-03-11 23:34:05 +08:00
|
|
|
{itemLayout === 'vertical' && extra
|
2019-03-06 16:31:07 +08:00
|
|
|
? [
|
|
|
|
<div className={`${prefixCls}-item-main`} key="content">
|
|
|
|
{children}
|
|
|
|
{actionsContent}
|
|
|
|
</div>,
|
|
|
|
<div className={`${prefixCls}-item-extra`} key="extra">
|
|
|
|
{extra}
|
|
|
|
</div>,
|
|
|
|
]
|
2019-03-11 23:34:05 +08:00
|
|
|
: [
|
|
|
|
children,
|
|
|
|
actionsContent,
|
|
|
|
extra ? React.cloneElement(extra as React.ReactElement<any>, { key: 'extra' }) : null,
|
|
|
|
]}
|
2019-03-05 22:17:22 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2019-03-06 15:23:51 +08:00
|
|
|
return grid ? (
|
2017-08-21 21:14:20 +08:00
|
|
|
<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
|
|
|
>
|
2019-03-05 22:17:22 +08:00
|
|
|
{itemChildren}
|
2017-08-21 21:14:20 +08:00
|
|
|
</Col>
|
|
|
|
) : (
|
2019-03-05 22:17:22 +08:00
|
|
|
itemChildren
|
2017-08-09 15:45:08 +08:00
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderItem}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2017-08-09 15:45:08 +08:00
|
|
|
}
|