You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
4.7 KiB
157 lines
4.7 KiB
6 years ago
|
import PropTypes from '../_util/vue-types';
|
||
4 years ago
|
import classNames from '../_util/classNames';
|
||
4 years ago
|
import { getComponent, isStringElement, isEmptyElement, getSlot } from '../_util/props-util';
|
||
6 years ago
|
import { Col } from '../grid';
|
||
4 years ago
|
import { defaultConfigProvider } from '../config-provider';
|
||
5 years ago
|
import { cloneElement } from '../_util/vnode';
|
||
4 years ago
|
import { defineComponent, ExtractPropTypes, FunctionalComponent, inject } from 'vue';
|
||
7 years ago
|
|
||
|
export const ListItemProps = {
|
||
|
prefixCls: PropTypes.string,
|
||
|
extra: PropTypes.any,
|
||
4 years ago
|
actions: PropTypes.array,
|
||
4 years ago
|
grid: PropTypes.any,
|
||
6 years ago
|
};
|
||
7 years ago
|
|
||
|
export const ListItemMetaProps = {
|
||
|
avatar: PropTypes.any,
|
||
|
description: PropTypes.any,
|
||
|
prefixCls: PropTypes.string,
|
||
|
title: PropTypes.any,
|
||
6 years ago
|
};
|
||
7 years ago
|
|
||
4 years ago
|
export const ListItemMeta: FunctionalComponent<Partial<
|
||
|
ExtractPropTypes<typeof ListItemMetaProps>
|
||
|
>> = (props, { slots }) => {
|
||
4 years ago
|
const configProvider = inject('configProvider', defaultConfigProvider);
|
||
4 years ago
|
const { getPrefixCls } = configProvider;
|
||
4 years ago
|
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 (
|
||
4 years ago
|
<div class={`${prefixCls}-item-meta`}>
|
||
4 years ago
|
{avatar && <div class={`${prefixCls}-item-meta-avatar`}>{avatar}</div>}
|
||
|
{(title || description) && content}
|
||
|
</div>
|
||
|
);
|
||
6 years ago
|
};
|
||
7 years ago
|
|
||
4 years ago
|
Object.assign(ListItemMeta, {
|
||
4 years ago
|
props: ListItemMetaProps,
|
||
|
__ANT_LIST_ITEM_META: true,
|
||
4 years ago
|
displayName: 'AListItemMeta',
|
||
4 years ago
|
});
|
||
|
|
||
6 years ago
|
function getGrid(grid, t) {
|
||
|
return grid[t] && Math.floor(24 / grid[t]);
|
||
7 years ago
|
}
|
||
|
|
||
4 years ago
|
export interface ListContext {
|
||
|
grid?: any;
|
||
|
itemLayout?: string;
|
||
|
}
|
||
|
|
||
|
export default defineComponent({
|
||
7 years ago
|
name: 'AListItem',
|
||
4 years ago
|
inheritAttrs: false,
|
||
4 years ago
|
Meta: ListItemMeta,
|
||
7 years ago
|
props: ListItemProps,
|
||
4 years ago
|
setup() {
|
||
4 years ago
|
const listContext = inject<ListContext>('listContext', {});
|
||
4 years ago
|
const configProvider = inject('configProvider', defaultConfigProvider);
|
||
4 years ago
|
return {
|
||
|
listContext,
|
||
|
configProvider,
|
||
|
};
|
||
7 years ago
|
},
|
||
5 years ago
|
methods: {
|
||
|
isItemContainsTextNodeAndNotSingular() {
|
||
4 years ago
|
const children = getSlot(this) || [];
|
||
5 years ago
|
let result;
|
||
|
children.forEach(element => {
|
||
|
if (isStringElement(element) && !isEmptyElement(element)) {
|
||
|
result = true;
|
||
|
}
|
||
|
});
|
||
|
return result && children.length > 1;
|
||
|
},
|
||
|
|
||
|
isFlexMode() {
|
||
4 years ago
|
const extra = getComponent(this, 'extra');
|
||
5 years ago
|
const { itemLayout } = this.listContext;
|
||
|
if (itemLayout === 'vertical') {
|
||
|
return !!extra;
|
||
|
}
|
||
|
return !this.isItemContainsTextNodeAndNotSingular();
|
||
|
},
|
||
|
},
|
||
6 years ago
|
render() {
|
||
5 years ago
|
const { grid, itemLayout } = this.listContext;
|
||
4 years ago
|
const { prefixCls: customizePrefixCls, $attrs } = this;
|
||
4 years ago
|
const { class: _className, ...restAttrs } = $attrs;
|
||
5 years ago
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
||
6 years ago
|
const prefixCls = getPrefixCls('list', customizePrefixCls);
|
||
4 years ago
|
const extra = getComponent(this, 'extra');
|
||
4 years ago
|
let actions = getComponent(this, 'actions');
|
||
|
actions = actions && !Array.isArray(actions) ? [actions] : actions;
|
||
5 years ago
|
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>
|
||
|
);
|
||
4 years ago
|
const children = getSlot(this);
|
||
5 years ago
|
const Tag = grid ? 'div' : 'li';
|
||
|
const itemChildren = (
|
||
|
<Tag
|
||
4 years ago
|
{...restAttrs}
|
||
4 years ago
|
class={classNames(`${prefixCls}-item`, _className, {
|
||
5 years ago
|
[`${prefixCls}-item-no-flex`]: !this.isFlexMode(),
|
||
|
})}
|
||
|
>
|
||
|
{itemLayout === 'vertical' && extra
|
||
|
? [
|
||
|
<div class={`${prefixCls}-item-main`} key="content">
|
||
4 years ago
|
{children}
|
||
5 years ago
|
{actionsContent}
|
||
|
</div>,
|
||
|
<div class={`${prefixCls}-item-extra`} key="extra">
|
||
|
{extra}
|
||
|
</div>,
|
||
|
]
|
||
4 years ago
|
: [children, actionsContent, cloneElement(extra, { key: 'extra' })]}
|
||
5 years ago
|
</Tag>
|
||
6 years ago
|
);
|
||
7 years ago
|
|
||
|
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')}
|
||
|
>
|
||
5 years ago
|
{itemChildren}
|
||
7 years ago
|
</Col>
|
||
|
) : (
|
||
5 years ago
|
itemChildren
|
||
6 years ago
|
);
|
||
7 years ago
|
|
||
6 years ago
|
return mainContent;
|
||
7 years ago
|
},
|
||
4 years ago
|
});
|