ant-design-vue/components/list/Item.jsx

149 lines
4.4 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import PropTypes from '../_util/vue-types';
import classNames from 'classnames';
import { getSlotOptions, getComponentFromProp, isEmptyElement } from '../_util/props-util';
import { Col } from '../grid';
2019-03-19 10:02:14 +08:00
import { ConfigConsumerProps } from '../config-provider';
2019-01-12 11:33:27 +08:00
import { ListGridType } from './index';
2018-06-16 21:30:41 +08:00
export const ListItemProps = {
prefixCls: PropTypes.string,
extra: PropTypes.any,
actions: PropTypes.arrayOf(PropTypes.any),
grid: ListGridType,
2019-01-12 11:33:27 +08:00
};
2018-06-16 21:30:41 +08:00
export const ListItemMetaProps = {
avatar: PropTypes.any,
description: PropTypes.any,
prefixCls: PropTypes.string,
title: PropTypes.any,
2019-01-12 11:33:27 +08:00
};
2018-06-16 21:30:41 +08:00
export const Meta = {
functional: true,
name: 'AListItemMeta',
__ANT_LIST_ITEM_META: true,
2019-03-19 10:02:14 +08:00
inject: {
2019-09-11 22:35:25 +08:00
configProvider: { default: () => ConfigConsumerProps },
2019-03-19 10:02:14 +08:00
},
2019-01-12 11:33:27 +08:00
render(h, context) {
2019-03-19 10:02:14 +08:00
const { props, slots, listeners, injections } = context;
2019-01-12 11:33:27 +08:00
const slotsMap = slots();
2019-09-11 22:35:25 +08:00
const getPrefixCls = injections.configProvider.getPrefixCls;
2019-03-19 10:02:14 +08:00
const { prefixCls: customizePrefixCls } = props;
const prefixCls = getPrefixCls('list', customizePrefixCls);
2019-01-12 11:33:27 +08:00
const avatar = props.avatar || slotsMap.avatar;
const title = props.title || slotsMap.title;
const description = props.description || slotsMap.description;
2018-06-16 21:30:41 +08:00
const content = (
<div class={`${prefixCls}-item-meta-content`}>
{title && <h4 class={`${prefixCls}-item-meta-title`}>{title}</h4>}
{description && <div class={`${prefixCls}-item-meta-description`}>{description}</div>}
</div>
2019-01-12 11:33:27 +08:00
);
2018-06-16 21:30:41 +08:00
return (
<div {...{ on: listeners }} class={`${prefixCls}-item-meta`}>
{avatar && <div class={`${prefixCls}-item-meta-avatar`}>{avatar}</div>}
{(title || description) && content}
</div>
2019-01-12 11:33:27 +08:00
);
2018-06-16 21:30:41 +08:00
},
2019-01-12 11:33:27 +08:00
};
2018-06-16 21:30:41 +08:00
2019-01-12 11:33:27 +08:00
function getGrid(grid, t) {
return grid[t] && Math.floor(24 / grid[t]);
2018-06-16 21:30:41 +08:00
}
export default {
name: 'AListItem',
Meta,
props: ListItemProps,
inject: {
2019-01-28 21:09:13 +08:00
listContext: { default: () => ({}) },
2019-09-11 22:35:25 +08:00
configProvider: { default: () => ConfigConsumerProps },
2018-06-16 21:30:41 +08:00
},
2019-01-12 11:33:27 +08:00
render() {
const { grid } = this.listContext;
2019-03-19 10:02:14 +08:00
const { prefixCls: customizePrefixCls, $slots, $listeners } = this;
2019-09-11 22:35:25 +08:00
const getPrefixCls = this.configProvider.getPrefixCls;
2019-03-19 10:02:14 +08:00
const prefixCls = getPrefixCls('list', customizePrefixCls);
2019-01-12 11:33:27 +08:00
const classString = `${prefixCls}-item`;
const extra = getComponentFromProp(this, 'extra');
const actions = getComponentFromProp(this, 'actions');
const metaContent = [];
const otherContent = [];
2018-06-16 21:30:41 +08:00
2019-01-12 11:33:27 +08:00
($slots.default || []).forEach(element => {
2018-06-16 21:30:41 +08:00
if (!isEmptyElement(element)) {
if (getSlotOptions(element).__ANT_LIST_ITEM_META) {
2019-01-12 11:33:27 +08:00
metaContent.push(element);
2018-06-16 21:30:41 +08:00
} else {
2019-01-12 11:33:27 +08:00
otherContent.push(element);
2018-06-16 21:30:41 +08:00
}
}
2019-01-12 11:33:27 +08:00
});
2018-06-16 21:30:41 +08:00
const contentClassString = classNames(`${prefixCls}-item-content`, {
2019-01-12 11:33:27 +08:00
[`${prefixCls}-item-content-single`]: metaContent.length < 1,
});
const content =
otherContent.length > 0 ? <div class={contentClassString}>{otherContent}</div> : null;
2018-06-16 21:30:41 +08:00
2019-01-12 11:33:27 +08:00
let actionsContent;
2018-06-16 21:30:41 +08:00
if (actions && actions.length > 0) {
const actionsContentItem = (action, i) => (
<li key={`${prefixCls}-item-action-${i}`}>
{action}
2019-01-12 11:33:27 +08:00
{i !== actions.length - 1 && <em class={`${prefixCls}-item-action-split`} />}
2018-06-16 21:30:41 +08:00
</li>
2019-01-12 11:33:27 +08:00
);
2018-06-16 21:30:41 +08:00
actionsContent = (
<ul class={`${prefixCls}-item-action`}>
{actions.map((action, i) => actionsContentItem(action, i))}
</ul>
2019-01-12 11:33:27 +08:00
);
2018-06-16 21:30:41 +08:00
}
const extraContent = (
<div class={`${prefixCls}-item-extra-wrap`}>
<div class={`${prefixCls}-item-main`}>
{metaContent}
{content}
{actionsContent}
</div>
<div class={`${prefixCls}-item-extra`}>{extra}</div>
</div>
2019-01-12 11:33:27 +08:00
);
2018-06-16 21:30:41 +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')}
xxl={getGrid(grid, 'xxl')}
>
<div {...{ on: $listeners }} class={classString}>
{extra && extraContent}
{!extra && metaContent}
{!extra && content}
{!extra && actionsContent}
</div>
</Col>
) : (
<div {...{ on: $listeners }} class={classString}>
{extra && extraContent}
{!extra && metaContent}
{!extra && content}
{!extra && actionsContent}
</div>
2019-01-12 11:33:27 +08:00
);
2018-06-16 21:30:41 +08:00
2019-01-12 11:33:27 +08:00
return mainContent;
2018-06-16 21:30:41 +08:00
},
2019-01-12 11:33:27 +08:00
};