C/C++教程

Ant-design 源码分析之数据展示(七)Comment

本文主要是介绍Ant-design 源码分析之数据展示(七)Comment,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Ant-design 源码分析之数据展示(七)Comment

2021SC@SDUSC

一、组件结构

1、ant代码结构
在这里插入图片描述
2、组件结构

ant中Comment的index.tsx中引入了config-provider。

二、antd组件调用关系

1、index.tsx
导入相应模块以及相应的ICON图标

import * as React from 'react';
import classNames from 'classnames';
import { ConfigContext } from '../config-provider';

声明CommentProps接口

export interface CommentProps {
  /** List of action items rendered below the comment content */
  actions?: Array<React.ReactNode>;
  /** The element to display as the comment author. */
  author?: React.ReactNode;
  /** The element to display as the comment avatar - generally an antd Avatar */
  avatar?: React.ReactNode;
  /** ClassName of comment */
  className?: string;
  /** The main content of the comment */
  content: React.ReactNode;
  /** Nested comments should be provided as children of the Comment */
  children?: React.ReactNode;
  /** Comment prefix defaults to '.ant-comment' */
  prefixCls?: string;
  /** Additional style for the comment */
  style?: React.CSSProperties;
  /** A datetime element containing the time to be displayed */
  datetime?: React.ReactNode;
}

actions:在评论内容下面呈现的操作项列表,类型为Array
author:要显示为注释作者的元素,类型为ReactNode
avatar:要显示为评论头像的元素 - 通常是 antd Avatar 或者 src,类型为ReactNode
children:嵌套注释应作为注释的子项提供,类型为ReactNode
content:评论的主要内容,类型为ReactNode
datetime:展示时间描述,类型为ReactNode

const Comment: React.FC<CommentProps> = ({
  actions,
  author,
  avatar,
  children,
  className,
  content,
  prefixCls: customizePrefixCls,
  datetime,
  ...otherProps
}) => {
  const { getPrefixCls, direction } = React.useContext(ConfigContext);

  const renderNested = (prefixCls: string, nestedChildren: any) => (
    <div className={classNames(`${prefixCls}-nested`)}>{nestedChildren}</div>
  );

  const prefixCls = getPrefixCls('comment', customizePrefixCls);

  const avatarDom = avatar ? (
    <div className={`${prefixCls}-avatar`}>
      {typeof avatar === 'string' ? <img src={avatar} alt="comment-avatar" /> : avatar}
    </div>
  ) : null;

  const actionDom =
    actions && actions.length ? (
      <ul className={`${prefixCls}-actions`}>
        {actions.map((action, index) => (
          <li key={`action-${index}`}>{action}</li> // eslint-disable-line react/no-array-index-key
        ))}
      </ul>
    ) : null;

  const authorContent = (author || datetime) && (
    <div className={`${prefixCls}-content-author`}>
      {author && <span className={`${prefixCls}-content-author-name`}>{author}</span>}
      {datetime && <span className={`${prefixCls}-content-author-time`}>{datetime}</span>}
    </div>
  );

  const contentDom = (
    <div className={`${prefixCls}-content`}>
      {authorContent}
      <div className={`${prefixCls}-content-detail`}>{content}</div>
      {actionDom}
    </div>
  );

  const cls = classNames(
    prefixCls,
    {
      [`${prefixCls}-rtl`]: direction === 'rtl',
    },
    className,
  );

  return (
    <div {...otherProps} className={cls}>
      <div className={`${prefixCls}-inner`}>
        {avatarDom}
        {contentDom}
      </div>
      {children ? renderNested(prefixCls, children) : null}
    </div>
  );
};

                    
这篇关于Ant-design 源码分析之数据展示(七)Comment的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!