代码先锋网 代码片段及技术文章聚合

antd源码-alert解析

技术标签: antd源码

antd源码-alert

alert的源码不难理解,主要分为几个部分:

计算alert组件的类名

 const alertCls = classNames(
      prefixCls,// ant-alert
      `${prefixCls}-${type}`, // 样式类型
      {
        [`${prefixCls}-closing`]: closing, // 关闭中的样式
        [`${prefixCls}-with-description`]: !!description,// 描述样式类
        [`${prefixCls}-no-icon`]: !showIcon,// 没有icon的样式
        [`${prefixCls}-banner`]: !!banner, // 作为顶部功能的样式
        [`${prefixCls}-closable`]: closable,// 能否关闭的样式
      },
      className,// 传入的类名
    );

关闭按钮的组件

   const closeIcon = closable ? (
      <button
        type="button"
        onClick={this.handleClose}
        className={`${prefixCls}-close-icon`}
        tabIndex={0}
      >
        {closeText ? (
          <span className={`${prefixCls}-close-text`}>{closeText}</span>
        ) : (
          <Icon type="close" />// 默认为Icon图标 关闭按钮
        )}
      </button>
    ) : null;

icon图标组件

    const iconNode = (icon &&
      (React.isValidElement<{ className?: string }>(icon) ? (// 判断传入的是不是一个正确的react组件
        React.cloneElement(icon, {// 复制元素
          className: classNames(`${prefixCls}-icon`, { // 合并传入的class和ant-icon
            [icon.props.className as string]: icon.props.className,
          }),
        })
      ) : (
        <span className={`${prefixCls}-icon`}>{icon}</span>// 不是react组件,只是一个普通的文字的话显示这个
      ))) || <Icon className={`${prefixCls}-icon`} type={iconType} theme={iconTheme} />;
		// 没有传入icon,则默认ant的icon

具体渲染alert的地方

 return closed ? null : (
      <Animate
        component=""
        showProp="data-show"
        transitionName={`${prefixCls}-slide-up`}
        onEnd={this.animationEnd}
      >
        <div data-show={!closing} className={alertCls} style={style} {...dataOrAriaProps}>
          {showIcon ? iconNode : null}
          <span className={`${prefixCls}-message`}>{message}</span>
          <span className={`${prefixCls}-description`}>{description}</span>
          {closeIcon}
        </div>
      </Animate>
    );
  };

Animate(引用自rc-animate)

我去查了rc-animate的api

name type default description
component React.Element/String ‘span’ wrap dom node or component for children. set to ‘’ if you do not wrap for only one child
componentProps Object {} extra props that will be passed to component
showProp String 控制显示隐藏的属性
exclusive Boolean
transitionName String|Object 平滑的类名 例:fade,整个动画所有transition相关的都会加入fade前缀
transitionAppear Boolean false 出现时的动画
transitionEnter Boolean true 开启进入时的动画
transitionLeave Boolean true 开启离开时的动画
onEnd function(key:String, exists:Boolean) true 动画结束时的回调
animation Object {} 可以配置上面的参数

整个案例

css
.fade-leave{
 animation: antAlertSlideUpOut 5s ease-out;
 animation-fill-mode: both;


}
.closeing{
  height: 0 !important;
  margin: 0;
  padding-top: 0;
  padding-bottom: 0;
  transform-origin: 50% 0;
  transition: all 0.3s ease-in-out;
}
    <Animate  
        component=""
        showProp='data-show'
        transitionName="fade">
        <div data-show={!enter} key="1" className={enter?'closeing':''} style={{
        display:'block',
        marginTop: '20px',
        width: '200px',
        height: '200px',
        backgroundColor: 'red',
        }}
      >
        asdasdasd
      </div>
        </Animate>

和button的wave动画不同的是,alert的动画是将元素的高度改变为0,最后移除dom,button,是加入水波样式,一定时间后自动移除动画。

总结

  1. rc-animate的使用,在日常的开发工作中,我们可以运用rc-animate创造一些简单的动效,如果需要我们自己创造组件的话,

  2. React api 提高对react react-dom的api理解和认知,创造组件起大作用。

    React.cloneElement( // 复制react元素,并赋予props及children
      element,
      [props],
      [...children]
    )
    React.isValidElement(object) // 检查是否react元素
    
  3. 运用classnames 对组件的样式进行抽离和分离,按功能、用途等赋予不同的样式类,分离组件样式之间的耦合性。

版权声明:本文为luo_qianyu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/luo_qianyu/article/details/105253845

智能推荐

antd-tools/lib/gulpfile.js 源码解析

上一篇我们说到了gulp任务流,这一篇继续说 查看package.json,可以看到对于antd-tools用到了以下几个命令: "lint:ts": "npm run tsc && antd-tools run ts-lint" "lint-fix:ts": "npm run tsc && an...

antd-tools/lib/cli/index.js 源码解析

富文本编辑器前前后后迭代了一个多月,准备往开源的路上走,发现离开源还有好长的路要走。目前看下来有以下几步: 1. js=>ts化,使代码更规范对IDE也更友好 2. 抽象耦合自己的项目的业务逻辑 3. .d.ts声明文件的生成与DefinetlyTypes的发布 4. npm发布 5. readme.md的文档编写 首当其冲的就是typescript化,于是周末两天浏览完了官网的文档,心里知...

antd-tools/lib/cli/run.js 源码解析

《antd-tools/lib/cli/index.js 源码解析》中我们讲到了子命令模式,今天根据子命令模式继续探究 antd-tools 今天我们来看下 antd-tools/lib/cli/run.js 这是源码: 这是解析: const gulp = require('gulp');:引入gulp任务流处理工具 program.on('--help', () => { console...

selenium中使用显示等待判断并切换window、iframe、alert源码解析

一、简介 本文只针对用显示等待去判断window、iframe、alert是否存在,其中 window用的是new_window_is_opene()这个类,真判断是否存在,不切换 iframe用的是frame_to_be_available_and_switch_to_it()这个类,判断是否存在,存在返回True并切换 alert用的是alert_is_present()这个类,判断是否存在,...

antd文档系统解析

antd的文档系统是基于bisheng开源框架来做的,本文主要简单介绍下文档系统中几个关键点,重点理解其思路 1、google Analytics   作用:跟踪用户数据,形成分析报告   使用方法:在文档中添加如下代码   工作原理:   JavaScript 跟踪代码段定义了一个称为“命令队列&rdqu...

猜你喜欢

vue antd 解析 Excel

vue antd 解析 Excel 先运行 npm install xlsx 解析方法 export function readExcel (file, that, properties) { return new Promise(function (resolve, reject) { // 返回Promise对象 const reader = new FileReader() reader.o...

antd 源码试读

dialog dialog/src/DialogWrap.tsx 看到了代码可以这么用,以为create-react-class是第三方库,查阅后发现是react自带的模块,不用ES6的写法,而使用这种方式应该是为了使用mixin 不使用 ES6 React 模态框秘密和“轮子”渐进设计提到了一般我们将modal的dom节点渲染到body下面,其中的原因是怕调用modal的...

antd源码修改

下载源码,npm i 安装包以后,启动,查看怎么使用的! 如果要修改message的默认配置,你可以设置提供商NZ_MESSAGE_CONFIG的值来修改。 (如:在你的模块的providers中加入 { provide: NZ_MESSAGE_CONFIG, useValue: { nzDuration: 3000 } },NZ_MESSAGE_CONFIG可以从ng-zorro-antd导入)...

antd源码-form解析(初始化到表单收集校验过程)

antd源码-form解析(初始化到表单收集校验过程) form是我们日常使用最多的控件,form是怎么收集表单信息的?是怎么回填数据的,怎么对表单校验的呢?它背后的逻辑和思路是怎样的。研究源码一是可以帮助我们将我们日常工作所使用的控件有一个完整的闭环的理解,二是帮助我们学习到优秀团队对问题的解决思路和方法。 Form.create做了什么? createDOMForm 深入源码form.crea...

alert

...