2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2021-06-23 06:44:21 +00:00
|
|
|
import { isStringElement, isEmptyElement, flattenChildren } from '../_util/props-util';
|
2019-01-12 03:33:27 +00:00
|
|
|
import { Col } from '../grid';
|
2020-03-07 11:45:13 +00:00
|
|
|
import { cloneElement } from '../_util/vnode';
|
2022-03-26 14:52:54 +00:00
|
|
|
import type { CSSProperties, ExtractPropTypes, PropType } from 'vue';
|
2021-06-23 06:44:21 +00:00
|
|
|
import { defineComponent, inject, ref } from 'vue';
|
|
|
|
import ItemMeta from './ItemMeta';
|
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
2021-06-28 09:45:04 +00:00
|
|
|
import { ListContextKey } from './contextKey';
|
2022-03-26 14:52:54 +00:00
|
|
|
import type { ListGridType } from '.';
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export const listItemProps = () => ({
|
|
|
|
prefixCls: String,
|
2018-06-16 13:30:41 +00:00
|
|
|
extra: PropTypes.any,
|
2020-10-12 05:27:16 +00:00
|
|
|
actions: PropTypes.array,
|
2022-03-26 14:52:54 +00:00
|
|
|
grid: Object as PropType<ListGridType>,
|
|
|
|
colStyle: { type: Object as PropType<CSSProperties>, default: undefined as CSSProperties },
|
|
|
|
});
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export type ListItemProps = Partial<ExtractPropTypes<ReturnType<typeof listItemProps>>>;
|
2020-10-24 13:40:39 +00:00
|
|
|
export default defineComponent({
|
2018-06-16 13:30:41 +00:00
|
|
|
name: 'AListItem',
|
2020-07-15 07:05:33 +00:00
|
|
|
inheritAttrs: false,
|
2021-06-23 06:44:21 +00:00
|
|
|
Meta: ItemMeta,
|
2022-03-26 14:52:54 +00:00
|
|
|
props: listItemProps(),
|
2021-06-23 06:44:21 +00:00
|
|
|
slots: ['actions', 'extra'],
|
|
|
|
setup(props, { slots, attrs }) {
|
|
|
|
const { itemLayout, grid } = inject(ListContextKey, {
|
|
|
|
grid: ref(),
|
|
|
|
itemLayout: ref(),
|
|
|
|
});
|
|
|
|
const { prefixCls } = useConfigInject('list', props);
|
|
|
|
|
|
|
|
const isItemContainsTextNodeAndNotSingular = () => {
|
|
|
|
const children = slots.default?.() || [];
|
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;
|
2021-06-23 06:44:21 +00:00
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-06-23 06:44:21 +00:00
|
|
|
const isFlexMode = () => {
|
|
|
|
const extra = props.extra ?? slots.extra?.();
|
|
|
|
if (itemLayout.value === 'vertical') {
|
2020-03-07 11:45:13 +00:00
|
|
|
return !!extra;
|
|
|
|
}
|
2021-06-23 06:44:21 +00:00
|
|
|
return !isItemContainsTextNodeAndNotSingular();
|
|
|
|
};
|
2018-06-16 13:30:41 +00:00
|
|
|
|
2021-06-23 06:44:21 +00:00
|
|
|
return () => {
|
|
|
|
const { class: className, ...restAttrs } = attrs;
|
|
|
|
const pre = prefixCls.value;
|
|
|
|
const extra = props.extra ?? slots.extra?.();
|
|
|
|
const children = slots.default?.();
|
|
|
|
let actions = props.actions ?? flattenChildren(slots.actions?.());
|
|
|
|
actions = actions && !Array.isArray(actions) ? [actions] : actions;
|
|
|
|
const actionsContent = actions && actions.length > 0 && (
|
|
|
|
<ul class={`${pre}-item-action`} key="actions">
|
|
|
|
{actions.map((action, i) => (
|
|
|
|
<li key={`${pre}-item-action-${i}`}>
|
|
|
|
{action}
|
|
|
|
{i !== actions.length - 1 && <em class={`${pre}-item-action-split`} />}
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
const Element = grid.value ? 'div' : 'li';
|
|
|
|
const itemChildren = (
|
|
|
|
<Element
|
|
|
|
{...(restAttrs as any)} // `li` element `onCopy` prop args is not same as `div`
|
|
|
|
class={classNames(
|
|
|
|
`${pre}-item`,
|
|
|
|
{
|
|
|
|
[`${pre}-item-no-flex`]: !isFlexMode(),
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{itemLayout.value === 'vertical' && extra
|
|
|
|
? [
|
|
|
|
<div class={`${pre}-item-main`} key="content">
|
|
|
|
{children}
|
|
|
|
{actionsContent}
|
|
|
|
</div>,
|
|
|
|
<div class={`${pre}-item-extra`} key="extra">
|
|
|
|
{extra}
|
|
|
|
</div>,
|
|
|
|
]
|
|
|
|
: [children, actionsContent, cloneElement(extra, { key: 'extra' })]}
|
|
|
|
</Element>
|
|
|
|
);
|
|
|
|
return grid.value ? (
|
|
|
|
<Col flex={1} style={props.colStyle}>
|
|
|
|
{itemChildren}
|
|
|
|
</Col>
|
|
|
|
) : (
|
|
|
|
itemChildren
|
|
|
|
);
|
|
|
|
};
|
2018-06-16 13:30:41 +00:00
|
|
|
},
|
2020-10-24 13:40:39 +00:00
|
|
|
});
|