2020-10-14 09:57:38 +00:00
|
|
|
import { App, defineComponent, inject } from 'vue';
|
|
|
|
import PropsTypes from '../_util/vue-types';
|
|
|
|
import { getComponent, getSlot } from '../_util/props-util';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-10-14 09:57:38 +00:00
|
|
|
import { VueNode } from '../_util/type';
|
|
|
|
export const CommentProps = {
|
|
|
|
actions: PropsTypes.array,
|
2020-09-18 04:15:01 +00:00
|
|
|
/** The element to display as the comment author. */
|
2020-10-14 09:57:38 +00:00
|
|
|
author: PropsTypes.VNodeChild,
|
2020-09-18 04:15:01 +00:00
|
|
|
/** The element to display as the comment avatar - generally an antd Avatar */
|
2020-10-14 09:57:38 +00:00
|
|
|
avatar: PropsTypes.VNodeChild,
|
2020-09-18 04:15:01 +00:00
|
|
|
/** The main content of the comment */
|
2020-10-14 09:57:38 +00:00
|
|
|
content: PropsTypes.VNodeChild,
|
2020-09-18 04:15:01 +00:00
|
|
|
/** Comment prefix defaults to '.ant-comment' */
|
2020-10-14 09:57:38 +00:00
|
|
|
prefixCls: PropsTypes.string,
|
2020-09-18 04:15:01 +00:00
|
|
|
/** A datetime element containing the time to be displayed */
|
2020-10-14 09:57:38 +00:00
|
|
|
datetime: PropsTypes.VNodeChild,
|
|
|
|
};
|
|
|
|
|
|
|
|
const Comment = defineComponent({
|
|
|
|
name: 'AComment',
|
|
|
|
props: CommentProps,
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getAction(actions: VueNode[]) {
|
|
|
|
if (!actions || !actions.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const actionList = actions.map((action, index) => <li key={`action-${index}`}>{action}</li>);
|
|
|
|
return actionList;
|
|
|
|
},
|
|
|
|
renderNested(prefixCls: string, children: VueNode) {
|
|
|
|
return <div class={`${prefixCls}-nested`}>{children}</div>;
|
|
|
|
},
|
|
|
|
},
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2020-10-14 09:57:38 +00:00
|
|
|
render() {
|
|
|
|
const { prefixCls: customizePrefixCls } = this.$props;
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2020-10-14 09:57:38 +00:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
|
|
|
const prefixCls = getPrefixCls('comment', customizePrefixCls);
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2020-10-14 09:57:38 +00:00
|
|
|
const actions = getComponent(this, 'actions');
|
|
|
|
const author = getComponent(this, 'author');
|
|
|
|
const avatar = getComponent(this, 'avatar');
|
|
|
|
const content = getComponent(this, 'content');
|
|
|
|
const datetime = getComponent(this, 'datetime');
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2020-10-14 09:57:38 +00:00
|
|
|
const avatarDom = (
|
|
|
|
<div class={`${prefixCls}-avatar`}>
|
|
|
|
{typeof avatar === 'string' ? <img src={avatar} alt="comment-avatar" /> : avatar}
|
|
|
|
</div>
|
|
|
|
);
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2020-10-14 09:57:38 +00:00
|
|
|
const actionDom = actions ? (
|
2020-09-18 04:15:01 +00:00
|
|
|
<ul class={`${prefixCls}-actions`}>
|
2020-10-14 09:57:38 +00:00
|
|
|
{this.getAction(Array.isArray(actions) ? actions : [actions])}
|
2020-09-18 04:15:01 +00:00
|
|
|
</ul>
|
|
|
|
) : null;
|
|
|
|
|
2020-10-14 09:57:38 +00:00
|
|
|
const authorContent = (
|
|
|
|
<div class={`${prefixCls}-content-author`}>
|
|
|
|
{author && <span class={`${prefixCls}-content-author-name`}>{author}</span>}
|
|
|
|
{datetime && <span class={`${prefixCls}-content-author-time`}>{datetime}</span>}
|
|
|
|
</div>
|
|
|
|
);
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2020-10-14 09:57:38 +00:00
|
|
|
const contentDom = (
|
|
|
|
<div class={`${prefixCls}-content`}>
|
|
|
|
{authorContent}
|
|
|
|
<div class={`${prefixCls}-content-detail`}>{content}</div>
|
|
|
|
{actionDom}
|
|
|
|
</div>
|
|
|
|
);
|
2020-09-18 04:15:01 +00:00
|
|
|
|
2020-10-14 09:57:38 +00:00
|
|
|
const comment = (
|
2020-09-18 04:15:01 +00:00
|
|
|
<div class={`${prefixCls}-inner`}>
|
|
|
|
{avatarDom}
|
|
|
|
{contentDom}
|
|
|
|
</div>
|
2020-10-14 09:57:38 +00:00
|
|
|
);
|
|
|
|
const children = getSlot(this);
|
|
|
|
return (
|
|
|
|
<div class={prefixCls}>
|
|
|
|
{comment}
|
|
|
|
{children && children.length ? this.renderNested(prefixCls, children) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2020-09-18 04:15:01 +00:00
|
|
|
|
|
|
|
/* istanbul ignore next */
|
|
|
|
Comment.install = function(app: App) {
|
|
|
|
app.component(Comment.name, Comment);
|
2020-10-13 11:14:56 +00:00
|
|
|
return app;
|
2020-09-18 04:15:01 +00:00
|
|
|
};
|
|
|
|
export default Comment;
|