refactor: comment

pull/4134/head
tangjinzhou 2021-05-26 22:13:04 +08:00
parent e9854dafd8
commit 5ee3d30858
4 changed files with 129 additions and 69 deletions

View File

@ -1,9 +1,9 @@
import { defineComponent, inject } from 'vue'; import { defineComponent, ExtractPropTypes } from 'vue';
import PropsTypes from '../_util/vue-types'; import PropsTypes from '../_util/vue-types';
import { getComponent, getSlot } from '../_util/props-util'; import { flattenChildren } from '../_util/props-util';
import { defaultConfigProvider } from '../config-provider';
import { VueNode, withInstall } from '../_util/type'; import { VueNode, withInstall } from '../_util/type';
export const CommentProps = { import useConfigInject from '../_util/hooks/useConfigInject';
export const commentProps = {
actions: PropsTypes.array, actions: PropsTypes.array,
/** The element to display as the comment author. */ /** The element to display as the comment author. */
author: PropsTypes.VNodeChild, author: PropsTypes.VNodeChild,
@ -17,79 +17,79 @@ export const CommentProps = {
datetime: PropsTypes.VNodeChild, datetime: PropsTypes.VNodeChild,
}; };
export type CommentProps = Partial<ExtractPropTypes<typeof commentProps>>;
const Comment = defineComponent({ const Comment = defineComponent({
name: 'AComment', name: 'AComment',
props: CommentProps, props: commentProps,
setup() { slots: ['actions', 'author', 'avatar', 'content', 'datetime'],
return { setup(props, { slots }) {
configProvider: inject('configProvider', defaultConfigProvider), const { prefixCls, direction } = useConfigInject('comment', props);
const renderNested = (prefixCls: string, children: VueNode) => {
return <div class={`${prefixCls}-nested`}>{children}</div>;
}; };
}, const getAction = (actions: VueNode[]) => {
methods: {
getAction(actions: VueNode[]) {
if (!actions || !actions.length) { if (!actions || !actions.length) {
return null; return null;
} }
const actionList = actions.map((action, index) => <li key={`action-${index}`}>{action}</li>); const actionList = actions.map((action, index) => <li key={`action-${index}`}>{action}</li>);
return actionList; return actionList;
}, };
renderNested(prefixCls: string, children: VueNode) { return () => {
return <div class={`${prefixCls}-nested`}>{children}</div>; const pre = prefixCls.value;
},
},
render() { const actions = props.actions ?? slots.actions?.();
const { prefixCls: customizePrefixCls } = this.$props; const author = props.author ?? slots.author?.();
const avatar = props.avatar ?? slots.avatar?.();
const getPrefixCls = this.configProvider.getPrefixCls; const content = props.content ?? slots.content?.();
const prefixCls = getPrefixCls('comment', customizePrefixCls); const datetime = props.datetime ?? slots.datetime?.();
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');
const avatarDom = ( const avatarDom = (
<div class={`${prefixCls}-avatar`}> <div class={`${pre}-avatar`}>
{typeof avatar === 'string' ? <img src={avatar} alt="comment-avatar" /> : avatar} {typeof avatar === 'string' ? <img src={avatar} alt="comment-avatar" /> : avatar}
</div> </div>
); );
const actionDom = actions ? ( const actionDom = actions ? (
<ul class={`${prefixCls}-actions`}> <ul class={`${pre}-actions`}>{getAction(Array.isArray(actions) ? actions : [actions])}</ul>
{this.getAction(Array.isArray(actions) ? actions : [actions])}
</ul>
) : null; ) : null;
const authorContent = ( const authorContent = (
<div class={`${prefixCls}-content-author`}> <div class={`${pre}-content-author`}>
{author && <span class={`${prefixCls}-content-author-name`}>{author}</span>} {author && <span class={`${pre}-content-author-name`}>{author}</span>}
{datetime && <span class={`${prefixCls}-content-author-time`}>{datetime}</span>} {datetime && <span class={`${pre}-content-author-time`}>{datetime}</span>}
</div> </div>
); );
const contentDom = ( const contentDom = (
<div class={`${prefixCls}-content`}> <div class={`${pre}-content`}>
{authorContent} {authorContent}
<div class={`${prefixCls}-content-detail`}>{content}</div> <div class={`${pre}-content-detail`}>{content}</div>
{actionDom} {actionDom}
</div> </div>
); );
const comment = ( const comment = (
<div class={`${prefixCls}-inner`}> <div class={`${pre}-inner`}>
{avatarDom} {avatarDom}
{contentDom} {contentDom}
</div> </div>
); );
const children = getSlot(this); const children = flattenChildren(slots.default?.());
return ( return (
<div class={prefixCls}> <div
class={[
pre,
{
[`${pre}-rtl`]: direction.value === 'rtl',
},
]}
>
{comment} {comment}
{children && children.length ? this.renderNested(prefixCls, children) : null} {children && children.length ? renderNested(pre, children) : null}
</div> </div>
); );
};
}, },
}); });

View File

@ -15,8 +15,9 @@
&-avatar { &-avatar {
position: relative; position: relative;
flex-shrink: 0; flex-shrink: 0;
margin-right: 12px; margin-right: @margin-sm;
cursor: pointer; cursor: pointer;
img { img {
width: 32px; width: 32px;
height: 32px; height: 32px;
@ -35,11 +36,11 @@
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
justify-content: flex-start; justify-content: flex-start;
margin-bottom: 4px; margin-bottom: @margin-xss;
font-size: @comment-font-size-base; font-size: @comment-font-size-base;
& > a, & > a,
& > span { & > span {
padding-right: 8px; padding-right: @padding-xs;
font-size: @comment-font-size-sm; font-size: @comment-font-size-sm;
line-height: 18px; line-height: 18px;
} }
@ -64,23 +65,27 @@
} }
&-detail p { &-detail p {
margin-bottom: @comment-content-detail-p-margin-bottom;
white-space: pre-wrap; white-space: pre-wrap;
} }
} }
&-actions { &-actions {
margin-top: 12px; margin-top: @comment-actions-margin-top;
margin-bottom: @comment-actions-margin-bottom;
padding-left: 0; padding-left: 0;
> li { > li {
display: inline-block; display: inline-block;
color: @comment-action-color; color: @comment-action-color;
> span { > span {
padding-right: 10px; margin-right: 10px;
color: @comment-action-color; color: @comment-action-color;
font-size: @comment-font-size-sm; font-size: @comment-font-size-sm;
cursor: pointer; cursor: pointer;
transition: color 0.3s; transition: color 0.3s;
user-select: none; user-select: none;
&:hover { &:hover {
color: @comment-action-hover-color; color: @comment-action-hover-color;
} }
@ -92,3 +97,5 @@
margin-left: @comment-nest-indent; margin-left: @comment-nest-indent;
} }
} }
@import './rtl';

View File

@ -0,0 +1,50 @@
@import '../../style/themes/index';
@import '../../style/mixins/index';
@comment-prefix-cls: ~'@{ant-prefix}-comment';
.@{comment-prefix-cls} {
&-rtl {
direction: rtl;
}
&-avatar {
.@{comment-prefix-cls}-rtl & {
margin-right: 0;
margin-left: 12px;
}
}
&-content {
&-author {
& > a,
& > span {
.@{comment-prefix-cls}-rtl & {
padding-right: 0;
padding-left: 8px;
}
}
}
}
&-actions {
.@{comment-prefix-cls}-rtl & {
padding-right: 0;
}
> li {
> span {
.@{comment-prefix-cls}-rtl & {
margin-right: 0;
margin-left: 10px;
}
}
}
}
&-nested {
.@{comment-prefix-cls}-rtl & {
margin-right: @comment-nest-indent;
margin-left: 0;
}
}
}

View File

@ -612,7 +612,7 @@
// Comment // Comment
// --- // ---
@comment-bg: inherit; @comment-bg: inherit;
@comment-padding-base: 16px 0; @comment-padding-base: @padding-md 0;
@comment-nest-indent: 44px; @comment-nest-indent: 44px;
@comment-font-size-base: @font-size-base; @comment-font-size-base: @font-size-base;
@comment-font-size-sm: @font-size-sm; @comment-font-size-sm: @font-size-sm;
@ -620,6 +620,9 @@
@comment-author-time-color: #ccc; @comment-author-time-color: #ccc;
@comment-action-color: @text-color-secondary; @comment-action-color: @text-color-secondary;
@comment-action-hover-color: #595959; @comment-action-hover-color: #595959;
@comment-actions-margin-bottom: inherit;
@comment-actions-margin-top: @margin-sm;
@comment-content-detail-p-margin-bottom: inherit;
// Tabs // Tabs
// --- // ---