antd源码笔记 - Alert

2,707 阅读3分钟

最近做项目的时候碰到antd的组件有啥事用着不顺,现在项目也上线了正好有空来看下源码,有不足之处,请大家指正。

import * as React from 'react'; // react
import * as ReactDOM from 'react-dom';  // react-dom
import Animate from 'rc-animate'; // react动画库
import Icon, { ThemeType } from '../icon'; // icon组件引用
import classNames from 'classnames'; // 便于管理class的组件库
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
// config-provider内使用create-react-context组件库来创建context对象,便于传值使用
import getDataOrAriaProps from '../_util/getDataOrAriaProps'; // 筛选遍历props中的参数,具体看getDataOrAriaProps.ts文件

function noop() {} // 暂时不知道有啥用处

export interface AlertProps { // 设置props参数的类型
  /**
   * Type of Alert styles, options:`success`, `info`, `warning`, `error`
   */
  type?: 'success' | 'info' | 'warning' | 'error';
  /** Whether Alert can be closed */
  closable?: boolean;
  /** Close text to show */
  closeText?: React.ReactNode;
  /** Content of Alert */
  message: React.ReactNode;
  /** Additional content of Alert */
  description?: React.ReactNode;
  /** Callback when close Alert */
  onClose?: React.MouseEventHandler<HTMLAnchorElement>;
  /** Trigger when animation ending of Alert */
  afterClose?: () => void;
  /** Whether to show icon */
  showIcon?: boolean;
  iconType?: string;
  style?: React.CSSProperties;
  prefixCls?: string;
  className?: string;
  banner?: boolean;
  icon?: React.ReactNode;
}

export interface AlertState { // 设置组件state参数的类型
  closing: boolean;
  closed: boolean;
}

export default class Alert extends React.Component<AlertProps, AlertState> {
  state: AlertState = {
    closing: true,
    closed: false,
  };

 // 组件内部关闭触发事件
  handleClose = (e: React.MouseEvent<HTMLAnchorElement>) => {
    e.preventDefault(); // 阻止默认的点击事
    const dom = ReactDOM.findDOMNode(this) as HTMLElement; // 在Html中找到当前的这个ReactDom
    // 设置dom的高度
    dom.style.height = `${dom.offsetHeight}px`;
    // Magic code
    // 重复一次后才能正确设置 height
    dom.style.height = `${dom.offsetHeight}px`;

    this.setState({
      closing: false,
    }); // 重置closing值
    (this.props.onClose || noop)(e); // 暂时不知道有什么用,noop也是
  };

// 动画结束后触发的事件
  animationEnd = () => {
    this.setState({
      closed: true,
      closing: true,
    });
    (this.props.afterClose || noop)();
  };

  // alert渲染函数
  renderAlert = ({ getPrefixCls }: ConfigConsumerProps) => {
    const {
      description,
      prefixCls: customizePrefixCls,
      message,
      closeText,
      banner,
      className = '',
      style,
      icon,
    } = this.props;
    let { closable, type, showIcon, iconType } = this.props;

    const prefixCls = getPrefixCls('alert', customizePrefixCls); // 返回样式,如果有自定义则返回自定义样式

    // banner模式默认有 Icon
    showIcon = banner && showIcon === undefined ? true : showIcon;
    // banner模式默认为警告
    type = banner && type === undefined ? 'warning' : type || 'info';

    let iconTheme: ThemeType = 'filled'; // icon的样式
    // should we give a warning?
    // warning(!iconType, `The property 'iconType' is deprecated. Use the property 'icon' instead.`);
    
    // 根据type来确定形式icon
    if (!iconType) {
      switch (type) {
        case 'success':
          iconType = 'check-circle';
          break;
        case 'info':
          iconType = 'info-circle';
          break;
        case 'error':
          iconType = 'close-circle';
          break;
        case 'warning':
          iconType = 'exclamation-circle';
          break;
        default:
          iconType = 'default';
      }

      // use outline icon in alert with description
      // 判断description是否存在,存在则iconTheme 使用outlined
      if (!!description) {
        iconTheme = 'outlined';
      }
    }

    // closeable when closeText is assigned
    // 如果分配了关闭文字,则可关闭开关为ture
    if (closeText) {
      closable = true;
    }
    
    // 设置alert 的class样式
    const alertCls = classNames(
      prefixCls,
      `${prefixCls}-${type}`,
      {
        [`${prefixCls}-close`]: !this.state.closing,
        [`${prefixCls}-with-description`]: !!description,
        [`${prefixCls}-no-icon`]: !showIcon,
        [`${prefixCls}-banner`]: !!banner,
        [`${prefixCls}-closable`]: closable,
      },
      className,
    );

    // 如果可关闭按钮为true 则显示关闭按钮,并且根据是否配置了文字来显示文字或是icon,如为false则返回null
    const closeIcon = closable ? (
      <a onClick={this.handleClose} className={`${prefixCls}-close-icon`}>
        {closeText || <Icon type="close" />}
      </a>
    ) : null;

   // 筛选data- 属性,暂不知道有啥作用
    const dataOrAriaProps = getDataOrAriaProps(this.props);

    // 如果icon存在 并且icon为一个react元素,则克隆icon(不知道有啥好处),否则就显示span标签内容为icon 或者直接显示Icon标签.
    
    // 这里克隆一个icon 有可能是想跟原始的icon区分开,免得有什么耦合关系在导致对现有的组件有所影响,
    //按照官网说的cloneElement方法是克隆并返回一个新的React元素(React Element),
    // 使用 element 作为起点。生成的元素将会拥有原始元素props与新props的浅合并。
    // 新的子级会替换现有的子级。来自原始元素的 key 和 ref 将会保留。
    
    const iconNode = (icon &&
      (React.isValidElement<{ className?: string }>(icon) ? (
        React.cloneElement(icon, {
          className: classNames({
            [icon.props.className as string]: icon.props.className,
            [`${prefixCls}-icon`]: true,
          }),
        })
      ) : (
        <span className={`${prefixCls}-icon`}>{icon}</span>
      ))) || <Icon className={`${prefixCls}-icon`} type={iconType} theme={iconTheme} />;

    // 判断组件是否是关闭状态,如果是则返回null,不是则返回由Animate包裹的组件内容本体
    return this.state.closed ? null : (
      <Animate
        component=""
        showProp="data-show"
        transitionName={`${prefixCls}-slide-up`}
        onEnd={this.animationEnd}
      >
        <div data-show={this.state.closing} className={alertCls} style={style} {...dataOrAriaProps}>
          {showIcon ? iconNode : null}
          <span className={`${prefixCls}-message`}>{message}</span>
          <span className={`${prefixCls}-description`}>{description}</span>
          {closeIcon}
        </div>
      </Animate>
    );
  };

  // 这里的ConfigConsumer 是为了跟其他地方的context关联起来,具体查看config-provider/index.tsx文件
  
  render() {
    return <ConfigConsumer>{this.renderAlert}</ConfigConsumer>;
  }
}