2021-06-26 01:35:40 +00:00
|
|
|
import type { ExtractPropTypes } from 'vue';
|
|
|
|
import { defineComponent } from 'vue';
|
2021-12-16 09:20:18 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2021-06-07 09:35:03 +00:00
|
|
|
import { flattenChildren } from '../_util/props-util';
|
2021-06-26 01:35:40 +00:00
|
|
|
import type { VueNode } from '../_util/type';
|
|
|
|
import { withInstall } from '../_util/type';
|
2021-06-07 09:35:03 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
2022-03-26 14:52:54 +00:00
|
|
|
export const commentProps = () => ({
|
|
|
|
actions: Array,
|
2020-09-18 04:15:01 +00:00
|
|
|
/** The element to display as the comment author. */
|
2021-12-16 09:20:18 +00:00
|
|
|
author: PropTypes.any,
|
2020-09-18 04:15:01 +00:00
|
|
|
/** The element to display as the comment avatar - generally an antd Avatar */
|
2021-12-16 09:20:18 +00:00
|
|
|
avatar: PropTypes.any,
|
2020-09-18 04:15:01 +00:00
|
|
|
/** The main content of the comment */
|
2021-12-16 09:20:18 +00:00
|
|
|
content: PropTypes.any,
|
2020-09-18 04:15:01 +00:00
|
|
|
/** Comment prefix defaults to '.ant-comment' */
|
2022-03-26 14:52:54 +00:00
|
|
|
prefixCls: String,
|
2020-09-18 04:15:01 +00:00
|
|
|
/** A datetime element containing the time to be displayed */
|
2021-12-16 09:20:18 +00:00
|
|
|
datetime: PropTypes.any,
|
2022-03-26 14:52:54 +00:00
|
|
|
});
|
2020-10-14 09:57:38 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export type CommentProps = Partial<ExtractPropTypes<ReturnType<typeof commentProps>>>;
|
2021-06-07 09:35:03 +00:00
|
|
|
|
2020-10-14 09:57:38 +00:00
|
|
|
const Comment = defineComponent({
|
|
|
|
name: 'AComment',
|
2022-03-26 14:52:54 +00:00
|
|
|
props: commentProps(),
|
2021-06-07 09:35:03 +00:00
|
|
|
slots: ['actions', 'author', 'avatar', 'content', 'datetime'],
|
|
|
|
setup(props, { slots }) {
|
|
|
|
const { prefixCls, direction } = useConfigInject('comment', props);
|
|
|
|
const renderNested = (prefixCls: string, children: VueNode) => {
|
|
|
|
return <div class={`${prefixCls}-nested`}>{children}</div>;
|
2020-10-14 09:57:38 +00:00
|
|
|
};
|
2021-06-07 09:35:03 +00:00
|
|
|
const getAction = (actions: VueNode[]) => {
|
2020-10-14 09:57:38 +00:00
|
|
|
if (!actions || !actions.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const actionList = actions.map((action, index) => <li key={`action-${index}`}>{action}</li>);
|
|
|
|
return actionList;
|
2021-06-07 09:35:03 +00:00
|
|
|
};
|
|
|
|
return () => {
|
|
|
|
const pre = prefixCls.value;
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const actions = props.actions ?? slots.actions?.();
|
|
|
|
const author = props.author ?? slots.author?.();
|
|
|
|
const avatar = props.avatar ?? slots.avatar?.();
|
|
|
|
const content = props.content ?? slots.content?.();
|
|
|
|
const datetime = props.datetime ?? slots.datetime?.();
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const avatarDom = (
|
|
|
|
<div class={`${pre}-avatar`}>
|
|
|
|
{typeof avatar === 'string' ? <img src={avatar} alt="comment-avatar" /> : avatar}
|
|
|
|
</div>
|
|
|
|
);
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const actionDom = actions ? (
|
|
|
|
<ul class={`${pre}-actions`}>{getAction(Array.isArray(actions) ? actions : [actions])}</ul>
|
|
|
|
) : null;
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const authorContent = (
|
|
|
|
<div class={`${pre}-content-author`}>
|
|
|
|
{author && <span class={`${pre}-content-author-name`}>{author}</span>}
|
|
|
|
{datetime && <span class={`${pre}-content-author-time`}>{datetime}</span>}
|
|
|
|
</div>
|
|
|
|
);
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const contentDom = (
|
|
|
|
<div class={`${pre}-content`}>
|
|
|
|
{authorContent}
|
|
|
|
<div class={`${pre}-content-detail`}>{content}</div>
|
|
|
|
{actionDom}
|
|
|
|
</div>
|
|
|
|
);
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2021-06-07 09:35:03 +00:00
|
|
|
const comment = (
|
|
|
|
<div class={`${pre}-inner`}>
|
|
|
|
{avatarDom}
|
|
|
|
{contentDom}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
const children = flattenChildren(slots.default?.());
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
class={[
|
|
|
|
pre,
|
|
|
|
{
|
|
|
|
[`${pre}-rtl`]: direction.value === 'rtl',
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
{comment}
|
|
|
|
{children && children.length ? renderNested(pre, children) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2020-10-14 09:57:38 +00:00
|
|
|
},
|
|
|
|
});
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2020-11-01 07:03:33 +00:00
|
|
|
export default withInstall(Comment);
|