2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import classNames from 'classnames';
|
2020-01-19 08:58:38 +00:00
|
|
|
import {
|
|
|
|
getComponentFromProp,
|
2020-03-07 11:45:13 +00:00
|
|
|
isStringElement,
|
2020-01-19 08:58:38 +00:00
|
|
|
getListeners,
|
2020-03-07 11:45:13 +00:00
|
|
|
isEmptyElement,
|
2020-01-19 08:58:38 +00:00
|
|
|
} 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';
|
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
|
|
|
|
|
|
|
export const Meta = {
|
|
|
|
functional: true,
|
|
|
|
name: 'AListItemMeta',
|
|
|
|
__ANT_LIST_ITEM_META: true,
|
2019-03-19 02:02:14 +00:00
|
|
|
inject: {
|
2019-09-11 14:35:25 +00:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2019-03-19 02:02:14 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
render(h, context) {
|
2019-03-19 02:02:14 +00:00
|
|
|
const { props, slots, listeners, injections } = context;
|
2019-01-12 03:33:27 +00:00
|
|
|
const slotsMap = slots();
|
2019-09-11 14:35:25 +00:00
|
|
|
const getPrefixCls = injections.configProvider.getPrefixCls;
|
2019-03-19 02:02:14 +00:00
|
|
|
const { prefixCls: customizePrefixCls } = props;
|
|
|
|
const prefixCls = getPrefixCls('list', customizePrefixCls);
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
const avatar = props.avatar || slotsMap.avatar;
|
|
|
|
const title = props.title || slotsMap.title;
|
|
|
|
const description = props.description || slotsMap.description;
|
2018-06-16 13:30:41 +00: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 03:33:27 +00:00
|
|
|
);
|
2018-06-16 13:30:41 +00: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 03:33:27 +00:00
|
|
|
);
|
2018-06-16 13:30:41 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-06-16 13:30:41 +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',
|
|
|
|
Meta,
|
|
|
|
props: ListItemProps,
|
|
|
|
inject: {
|
2019-01-28 13:09:13 +00:00
|
|
|
listContext: { default: () => ({}) },
|
2019-09-11 14:35:25 +00:00
|
|
|
configProvider: { default: () => ConfigConsumerProps },
|
2018-06-16 13:30:41 +00:00
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
methods: {
|
|
|
|
isItemContainsTextNodeAndNotSingular() {
|
|
|
|
const { $slots } = this;
|
|
|
|
let result;
|
|
|
|
const children = $slots.default || [];
|
|
|
|
children.forEach(element => {
|
|
|
|
if (isStringElement(element) && !isEmptyElement(element)) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result && children.length > 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
isFlexMode() {
|
|
|
|
const extra = getComponentFromProp(this, 'extra');
|
|
|
|
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-01-19 08:58:38 +00:00
|
|
|
const { prefixCls: customizePrefixCls, $slots } = this;
|
|
|
|
const listeners = getListeners(this);
|
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);
|
2019-01-12 03:33:27 +00:00
|
|
|
const extra = getComponentFromProp(this, 'extra');
|
|
|
|
const actions = getComponentFromProp(this, 'actions');
|
2018-06-16 13:30:41 +00:00
|
|
|
|
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>
|
|
|
|
);
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
const Tag = grid ? 'div' : 'li';
|
|
|
|
const itemChildren = (
|
|
|
|
<Tag
|
|
|
|
{...{ on: listeners }}
|
|
|
|
class={classNames(`${prefixCls}-item`, {
|
|
|
|
[`${prefixCls}-item-no-flex`]: !this.isFlexMode(),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{itemLayout === 'vertical' && extra
|
|
|
|
? [
|
|
|
|
<div class={`${prefixCls}-item-main`} key="content">
|
|
|
|
{$slots.default}
|
|
|
|
{actionsContent}
|
|
|
|
</div>,
|
|
|
|
<div class={`${prefixCls}-item-extra`} key="extra">
|
|
|
|
{extra}
|
|
|
|
</div>,
|
|
|
|
]
|
|
|
|
: [$slots.default, actionsContent, cloneElement(extra, { key: 'extra' })]}
|
|
|
|
</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
|
|
|
};
|