2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import classNames from 'classnames';
|
2020-07-15 07:05:33 +00:00
|
|
|
import { getComponent, isStringElement, isEmptyElement, getSlot } from '../_util/props-util';
|
2019-01-12 03:33:27 +00:00
|
|
|
import { Col } from '../grid';
|
2019-03-19 02:02:14 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2019-01-12 03:33:27 +00:00
|
|
|
import { ListGridType } from './index';
|
2020-03-07 11:45:13 +00:00
|
|
|
import { cloneElement } from '../_util/vnode';
|
2020-07-15 07:05:33 +00:00
|
|
|
import { inject } from 'vue';
|
2018-06-16 13:30:41 +00:00
|
|
|
|
|
|
|
export const ListItemProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
extra: PropTypes.any,
|
|
|
|
actions: PropTypes.arrayOf(PropTypes.any),
|
|
|
|
grid: ListGridType,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-16 13:30:41 +00:00
|
|
|
|
|
|
|
export const ListItemMetaProps = {
|
|
|
|
avatar: PropTypes.any,
|
|
|
|
description: PropTypes.any,
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
title: PropTypes.any,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2020-08-20 10:05:12 +00:00
|
|
|
export const ListItemMeta = (props, { slots }) => {
|
2020-07-15 07:05:33 +00:00
|
|
|
const configProvider = inject('configProvider', ConfigConsumerProps);
|
|
|
|
const getPrefixCls = configProvider.getPrefixCls;
|
|
|
|
const { prefixCls: customizePrefixCls } = props;
|
|
|
|
const prefixCls = getPrefixCls('list', customizePrefixCls);
|
|
|
|
const avatar = props.avatar || slots.avatar?.();
|
|
|
|
const title = props.title || slots.title?.();
|
|
|
|
const description = props.description || slots.description?.();
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
return (
|
2020-07-15 14:39:16 +00:00
|
|
|
<div class={`${prefixCls}-item-meta`}>
|
2020-07-15 07:05:33 +00:00
|
|
|
{avatar && <div class={`${prefixCls}-item-meta-avatar`}>{avatar}</div>}
|
|
|
|
{(title || description) && content}
|
|
|
|
</div>
|
|
|
|
);
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2020-08-20 10:05:12 +00:00
|
|
|
Object.assign(ListItemMeta, {
|
2020-07-15 07:05:33 +00:00
|
|
|
props: ListItemMetaProps,
|
|
|
|
__ANT_LIST_ITEM_META: true,
|
2020-08-20 10:05:12 +00:00
|
|
|
componentName: 'AListItemMeta',
|
2020-07-15 07:05:33 +00:00
|
|
|
});
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
function getGrid(grid, t) {
|
|
|
|
return grid[t] && Math.floor(24 / grid[t]);
|
2018-06-16 13:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'AListItem',
|
2020-07-15 07:05:33 +00:00
|
|
|
inheritAttrs: false,
|
2020-08-20 10:05:12 +00:00
|
|
|
Meta: ListItemMeta,
|
2018-06-16 13:30:41 +00:00
|
|
|
props: ListItemProps,
|
2020-07-15 07:05:33 +00:00
|
|
|
setup() {
|
|
|
|
const listContext = inject('listContext', {});
|
|
|
|
const configProvider = inject('configProvider', ConfigConsumerProps);
|
|
|
|
return {
|
|
|
|
listContext,
|
|
|
|
configProvider,
|
|
|
|
};
|
2018-06-16 13:30:41 +00:00
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
methods: {
|
|
|
|
isItemContainsTextNodeAndNotSingular() {
|
2020-07-15 07:05:33 +00:00
|
|
|
const children = getSlot(this) || [];
|
2020-03-07 11:45:13 +00:00
|
|
|
let result;
|
|
|
|
children.forEach(element => {
|
|
|
|
if (isStringElement(element) && !isEmptyElement(element)) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result && children.length > 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
isFlexMode() {
|
2020-07-15 07:05:33 +00:00
|
|
|
const extra = getComponent(this, 'extra');
|
2020-03-07 11:45:13 +00:00
|
|
|
const { itemLayout } = this.listContext;
|
|
|
|
if (itemLayout === 'vertical') {
|
|
|
|
return !!extra;
|
|
|
|
}
|
|
|
|
return !this.isItemContainsTextNodeAndNotSingular();
|
|
|
|
},
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { grid, itemLayout } = this.listContext;
|
2020-07-31 15:40:06 +00:00
|
|
|
const { prefixCls: customizePrefixCls, $attrs } = this;
|
2020-07-15 14:39:16 +00:00
|
|
|
const { class: _className, ...restAttrs } = $attrs;
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-19 02:02:14 +00:00
|
|
|
const prefixCls = getPrefixCls('list', customizePrefixCls);
|
2020-07-15 07:05:33 +00:00
|
|
|
const extra = getComponent(this, 'extra');
|
2020-07-29 12:56:55 +00:00
|
|
|
let actions = getComponent(this, 'actions');
|
|
|
|
actions = actions && !Array.isArray(actions) ? [actions] : actions;
|
2020-03-07 11:45:13 +00:00
|
|
|
const actionsContent = actions && actions.length > 0 && (
|
|
|
|
<ul class={`${prefixCls}-item-action`} key="actions">
|
|
|
|
{actions.map((action, i) => (
|
|
|
|
<li key={`${prefixCls}-item-action-${i}`}>
|
|
|
|
{action}
|
|
|
|
{i !== actions.length - 1 && <em class={`${prefixCls}-item-action-split`} />}
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
);
|
2020-07-15 14:39:16 +00:00
|
|
|
const children = getSlot(this);
|
2020-03-07 11:45:13 +00:00
|
|
|
const Tag = grid ? 'div' : 'li';
|
|
|
|
const itemChildren = (
|
|
|
|
<Tag
|
2020-07-15 14:39:16 +00:00
|
|
|
{...restAttrs}
|
2020-07-15 07:05:33 +00:00
|
|
|
class={classNames(`${prefixCls}-item `, _className, {
|
2020-03-07 11:45:13 +00:00
|
|
|
[`${prefixCls}-item-no-flex`]: !this.isFlexMode(),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{itemLayout === 'vertical' && extra
|
|
|
|
? [
|
|
|
|
<div class={`${prefixCls}-item-main`} key="content">
|
2020-07-31 15:40:06 +00:00
|
|
|
{children}
|
2020-03-07 11:45:13 +00:00
|
|
|
{actionsContent}
|
|
|
|
</div>,
|
|
|
|
<div class={`${prefixCls}-item-extra`} key="extra">
|
|
|
|
{extra}
|
|
|
|
</div>,
|
|
|
|
]
|
2020-07-15 07:05:33 +00:00
|
|
|
: [children, actionsContent, cloneElement(extra, { key: 'extra' })]}
|
2020-03-07 11:45:13 +00:00
|
|
|
</Tag>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-06-16 13:30:41 +00: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')}
|
|
|
|
>
|
2020-03-07 11:45:13 +00:00
|
|
|
{itemChildren}
|
2018-06-16 13:30:41 +00:00
|
|
|
</Col>
|
|
|
|
) : (
|
2020-03-07 11:45:13 +00:00
|
|
|
itemChildren
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
return mainContent;
|
2018-06-16 13:30:41 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|