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组件
相关推荐
瓜牛呱呱 2020-11-12
柳木木的IT 2020-11-04
yifouhu 2020-11-02
lei0 2020-11-02
源码zanqunet 2020-10-26
码代码的陈同学 2020-10-14
lukezhong 2020-10-14
clh0 2020-09-18
changcongying 2020-09-17
星辰大海的路上 2020-09-13
abfdada 2020-08-26
mzy000 2020-08-24
shenlanse 2020-08-18
zhujiangtaotaise 2020-08-18
xiemanR 2020-08-17