ant-design-vue/components/comment/index.jsx

100 lines
2.9 KiB
Vue
Raw Normal View History

2019-01-12 11:33:27 +08:00
import PropsTypes from '../_util/vue-types';
import { initDefaultProps, getComponentFromProp } from '../_util/props-util';
2019-03-15 15:12:28 +08:00
import { ConfigConsumerProps } from '../config-provider';
import Base from '../base';
2019-01-01 15:33:43 +08:00
export const CommentProps = {
actions: PropsTypes.array,
/** The element to display as the comment author. */
author: PropsTypes.any,
/** The element to display as the comment avatar - generally an antd Avatar */
avatar: PropsTypes.any,
/** The main content of the comment */
content: PropsTypes.any,
/** Comment prefix defaults to '.ant-comment' */
prefixCls: PropsTypes.string,
/** A datetime element containing the time to be displayed */
datetime: PropsTypes.any,
2019-01-12 11:33:27 +08:00
};
2019-01-01 15:33:43 +08:00
const Comment = {
name: 'AComment',
2019-03-15 15:12:28 +08:00
props: CommentProps,
inject: {
2019-09-11 22:35:25 +08:00
configProvider: { default: () => ConfigConsumerProps },
2019-03-15 15:12:28 +08:00
},
2019-01-01 15:33:43 +08:00
methods: {
2019-01-12 11:33:27 +08:00
getAction(actions) {
2019-01-01 15:33:43 +08:00
if (!actions || !actions.length) {
2019-01-12 11:33:27 +08:00
return null;
2019-01-01 15:33:43 +08:00
}
2019-01-12 11:33:27 +08:00
const actionList = actions.map((action, index) => <li key={`action-${index}`}>{action}</li>);
return actionList;
2019-01-01 15:33:43 +08:00
},
2019-01-12 11:33:27 +08:00
renderNested(children) {
const { prefixCls } = this.$props;
2019-01-01 15:33:43 +08:00
2019-01-12 11:33:27 +08:00
return <div class={`${prefixCls}-nested`}>{children}</div>;
2019-01-01 15:33:43 +08:00
},
},
2019-01-12 11:33:27 +08:00
render() {
2019-03-15 15:12:28 +08:00
const { prefixCls: customizePrefixCls } = this.$props;
2019-09-11 22:35:25 +08:00
const getPrefixCls = this.configProvider.getPrefixCls;
2019-03-15 15:12:28 +08:00
const prefixCls = getPrefixCls('comment', customizePrefixCls);
2019-01-01 15:33:43 +08:00
2019-01-12 11:33:27 +08:00
const actions = getComponentFromProp(this, 'actions');
const author = getComponentFromProp(this, 'author');
const avatar = getComponentFromProp(this, 'avatar');
const content = getComponentFromProp(this, 'content');
const datetime = getComponentFromProp(this, 'datetime');
2019-01-01 15:33:43 +08:00
const avatarDom = (
<div class={`${prefixCls}-avatar`}>
{typeof avatar === 'string' ? <img src={avatar} /> : avatar}
</div>
2019-01-12 11:33:27 +08:00
);
2019-01-01 15:33:43 +08:00
const actionDom =
actions && actions.length ? (
<ul class={`${prefixCls}-actions`}>{this.getAction(actions)}</ul>
2019-01-12 11:33:27 +08:00
) : null;
2019-01-01 15:33:43 +08: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>
2019-01-12 11:33:27 +08:00
);
2019-01-01 15:33:43 +08:00
const contentDom = (
<div class={`${prefixCls}-content`}>
{authorContent}
<div class={`${prefixCls}-content-detail`}>{content}</div>
{actionDom}
</div>
2019-01-12 11:33:27 +08:00
);
2019-01-01 15:33:43 +08:00
const comment = (
<div class={`${prefixCls}-inner`}>
{avatarDom}
{contentDom}
</div>
2019-01-12 11:33:27 +08:00
);
const children = this.$slots.default;
2019-01-01 15:33:43 +08:00
return (
2019-01-02 11:03:46 +08:00
<div class={prefixCls} {...{ on: this.$listeners }}>
2019-01-01 15:33:43 +08:00
{comment}
{children ? this.renderNested(children) : null}
</div>
2019-01-12 11:33:27 +08:00
);
2019-01-01 15:33:43 +08:00
},
2019-01-12 11:33:27 +08:00
};
2019-01-01 15:33:43 +08:00
/* istanbul ignore next */
2019-01-12 11:33:27 +08:00
Comment.install = function(Vue) {
Vue.use(Base);
2019-01-12 11:33:27 +08:00
Vue.component(Comment.name, Comment);
};
export default Comment;