ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-11-26 12:01:30  阅读:251  来源: 互联网

标签:Comment content author prefixCls comment Ant 源码 avatar ReactNode


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>
  );
};

标签:Comment,content,author,prefixCls,comment,Ant,源码,avatar,ReactNode
来源: https://blog.csdn.net/qq_37588125/article/details/121323222

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有