antd源码解读(2)- Icon

Icon

icon作为开发当中使用相对频繁的一个组件,其实现也很简单,但是其中比较麻烦的一部分是icon字体的制作,可以参看这篇文章

Antd的Icon组件使用了很简单的css来实现交互与动效

import React from 'react';
import classNames from 'classnames';  
import omit from 'omit.js';
//classNames是条件判断输出className的值
//omit是移出对象的指定属性,而实现浅拷贝

//定义IconProps接口
export interface IconProps {
  type: string;
  className?: string;
  title?: string;
  onClick?: React.MouseEventHandler<any>;
  spin?: boolean;
  style?: React.CSSProperties;
}

const Icon = (props: IconProps) => {
  const { type, className = '', spin } = props;
  const classString = classNames({
    anticon: true,
    'anticon-spin': !!spin || type === 'loading', // 是否需要旋转动画,loading这个icon是默认加上旋转动效的
    [`anticon-${type}`]: true,
  }, className);

  // 这里说一下为什么要用omit():html的<i>标签,其标准标签属性只有六种:id、class、title、style、dir、lang。
  // IconProps接口中的6种属性(方法),type、spin不属于上述六种。onClick为事件属性,可以;
  return <i {...omit(props, ['type', 'spin'])} className={classString} />;
};

export default Icon;

大家也可以根据上面所提供的制作icon的方法和这样的方式来实现自己的Icon组件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值