From 6401da20827fba7d5cda5eae2f160c1e06372d78 Mon Sep 17 00:00:00 2001 From: Tom Xu Date: Thu, 23 Apr 2020 20:52:15 +0800 Subject: [PATCH] refactor(comment): rewrite with hook (#23498) * refactor(comment): rewrite with hook * improve comment code --- components/comment/index.tsx | 118 +++++++++++++++++------------------ 1 file changed, 57 insertions(+), 61 deletions(-) diff --git a/components/comment/index.tsx b/components/comment/index.tsx index 3287d24ecc..c457ff89ae 100644 --- a/components/comment/index.tsx +++ b/components/comment/index.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; import classNames from 'classnames'; -import { ConfigConsumer, ConfigConsumerProps } from '../config-provider'; +import { ConfigContext } from '../config-provider'; function getAction(actions: React.ReactNode[]) { if (!actions || !actions.length) { @@ -32,72 +32,68 @@ export interface CommentProps { datetime?: React.ReactNode; } -export default class Comment extends React.Component { - renderNested = (prefixCls: string, children: any) => { - return
{children}
; +const Comment: React.FC = ({ + actions, + author, + avatar, + children, + className, + content, + prefixCls: customizePrefixCls, + style, + datetime, + ...otherProps +}) => { + const { getPrefixCls, direction } = React.useContext(ConfigContext); + + const renderNested = (prefixCls: string, nestedChildren: any) => { + return
{nestedChildren}
; }; - renderComment = ({ getPrefixCls, direction }: ConfigConsumerProps) => { - const { - actions, - author, - avatar, - children, - className, - content, - prefixCls: customizePrefixCls, - style, - datetime, - ...otherProps - } = this.props; + const prefixCls = getPrefixCls('comment', customizePrefixCls); - const prefixCls = getPrefixCls('comment', customizePrefixCls); + const avatarDom = ( +
+ {typeof avatar === 'string' ? comment-avatar : avatar} +
+ ); - const avatarDom = ( -
- {typeof avatar === 'string' ? comment-avatar : avatar} -
- ); + const actionDom = + actions && actions.length ? ( +
    {getAction(actions)}
+ ) : null; - const actionDom = - actions && actions.length ? ( -
    {getAction(actions)}
- ) : null; + const authorContent = ( +
+ {author && {author}} + {datetime && {datetime}} +
+ ); - const authorContent = ( -
- {author && {author}} - {datetime && {datetime}} -
- ); + const contentDom = ( +
+ {authorContent} +
{content}
+ {actionDom} +
+ ); - const contentDom = ( -
- {authorContent} -
{content}
- {actionDom} -
- ); + const comment = ( +
+ {avatarDom} + {contentDom} +
+ ); - const comment = ( -
- {avatarDom} - {contentDom} -
- ); + const cls = classNames(prefixCls, className, { + [`${prefixCls}-rtl`]: direction === 'rtl', + }); + return ( +
+ {comment} + {children ? renderNested(prefixCls, children) : null} +
+ ); +}; - const cls = classNames(prefixCls, className, { - [`${prefixCls}-rtl`]: direction === 'rtl', - }); - return ( -
- {comment} - {children ? this.renderNested(prefixCls, children) : null} -
- ); - }; - - render() { - return {this.renderComment}; - } -} +export default Comment;