Ant Design源码分析(二):Button组件

news/2025/5/24 2:14:50

年底正式总结的好时机, Button组件的源码。

  • Button分析

    通过官方API文章,大家知道<Button /> 组件具备以下几个功能点
    1、多种样式风格可选: primary、ghost、 danger等,并且每一种风格都对应各自风格的交互
    2、接收click事件回调函数
    3、可以指定点击跳转指定的url
    4、可以控制图标旋转,模拟请求状态pending

源码如下

import * as React from 'react';
import { findDOMNode } from 'react-dom';
import * as PropTypes from 'prop-types';
import classNames from 'classnames';/* 引入了一个系的模块Wave,可能是功能函数,可能是组件,先不管它是什么,用到时再回来看 */
import Wave from '../_util/wave';
import Icon from '../icon';
import Group from './button-group';//* 组件逻辑的一些辅助常量 */
const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;/* 判断是否为两个中文字符*/
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
function isString(str: any) {return typeof str === 'string';
}// 组件逻辑函数: 在两个中文字符间插入一个空格
function insertSpace(child: React.ReactChild, needInserted: boolean) {// Check the child if is undefined or null.if (child == null) {return;}const SPACE = needInserted ? ' ' : '';// strictNullChecks oops.if (typeof child !== 'string' && typeof child !== 'number' &&isString(child.type) && isTwoCNChar(child.props.children)) {return React.cloneElement(child, {},child.props.children.split('').join(SPACE));}if (typeof child === 'string') {if (isTwoCNChar(child)) {child = child.split('').join(SPACE);}return <span>{child}</span>;}return child;
}/* 联合类型 Button.props中 type、shape、size、htmlType的取值范围  */
export type ButtonType = 'default' | 'primary' | 'ghost' | 'dashed' | 'danger';
export type ButtonShape = 'circle' | 'circle-outline';
export type ButtonSize = 'small' | 'default' | 'large';
export type ButtonHTMLType = 'submit' | 'button' | 'reset';/* 定义接口 相当于props-types */
export interface BaseButtonProps {type?: ButtonType;icon?: string;shape?: ButtonShape;size?: ButtonSize;loading?: boolean | { delay?: number };prefixCls?: string;className?: string;ghost?: boolean;block?: boolean;children?: React.ReactNode;
}export type AnchorButtonProps = {href: string;target?: string;onClick?: React.MouseEventHandler<HTMLAnchorElement>;
} & BaseButtonProps & React.AnchorHTMLAttributes<HTMLAnchorElement>;export type NativeButtonProps = {htmlType?: ButtonHTMLType;onClick?: React.MouseEventHandler<HTMLButtonElement>;
} & BaseButtonProps & React.ButtonHTMLAttributes<HTMLButtonElement>;export type ButtonProps = AnchorButtonProps | NativeButtonProps;export default class Button extends React.Component<ButtonProps, any> {static Group: typeof Group;static __ANT_BUTTON = true;static defaultProps = {prefixCls: 'ant-btn',loading: false,ghost: false,block: false,};static propTypes = {type: PropTypes.string,shape: PropTypes.oneOf(['circle', 'circle-outline']),size: PropTypes.oneOf(['large', 'default', 'small']),htmlType: PropTypes.oneOf(['submit', 'button', 'reset']),onClick: PropTypes.func,loading: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),className: PropTypes.string,icon: PropTypes.string,block: PropTypes.bool,};private delayTimeout: number;constructor(props: ButtonProps) {super(props);this.state = {/** 控制Button中Icon来旋转,通常应用在异步请返回之前的场景中,比如提交表单,请求结束前让Icon旋转,可以使得体验更好,用来实现文章开头时所描述的功能4 */loading: props.loading,/** 作为子元素中是否有两个中文字符的标识符, 以此作为是否插入空格的标识符*/hasTwoCNChar: false,};}componentDidMount() {this.fixTwoCNChar();}componentWillReceiveProps(nextProps: ButtonProps) {const currentLoading = this.props.loading;const loading = nextProps.loading;if (currentLoading) {clearTimeout(this.delayTimeout);}if (typeof loading !== 'boolean' && loading && loading.delay) {this.delayTimeout = window.setTimeout(() => this.setState({ loading }), loading.delay);} else {this.setState({ loading });}}componentDidUpdate() {this.fixTwoCNChar();}componentWillUnmount() {if (this.delayTimeout) {clearTimeout(this.delayTimeout);}}fixTwoCNChar() {// Fix for HOC usage like <FormatMessage />const node = (findDOMNode(this) as HTMLElement);const buttonText = node.textContent || node.innerText;if (this.isNeedInserted() && isTwoCNChar(buttonText)) {if (!this.state.hasTwoCNChar) {this.setState({hasTwoCNChar: true,});}} else if (this.state.hasTwoCNChar) {this.setState({hasTwoCNChar: false,});}}handleClick: React.MouseEventHandler<HTMLButtonElement | HTMLAnchorElement> = e => {const { onClick } = this.props;if (onClick) {(onClick as React.MouseEventHandler<HTMLButtonElement | HTMLAnchorElement>)(e);}}isNeedInserted() {const { icon, children } = this.props;return React.Children.count(children) === 1 && !icon;}render() {/** 通过Props来生成不同的className而达到不同的效果,有兴趣的可以研究下样式 */const {type, shape, size, className, children, icon, prefixCls, ghost, loading: _loadingProp, block, ...rest} = this.props;const { loading, hasTwoCNChar } = this.state;/* 通过size控制按钮的大小尺寸*/let sizeCls = '';switch (size) {case 'large':sizeCls = 'lg';break;case 'small':sizeCls = 'sm';default:break;}/* Antd 圣诞彩蛋事件代码,每年12.25搞一次, UI变换思路依旧是通过props.someKey 配合不同的className来实现 *//* 修复方式 https://github.com/ant-design/ant-design/issues/13848*/const now = new Date();const isChristmas = now.getMonth() === 11 && now.getDate() === 25;/** 通过Props来生成不同的className而达到不同的UI效果吗,有兴趣的可以研究下样式 */const classes = classNames(prefixCls, className, {[`${prefixCls}-${type}`]: type,[`${prefixCls}-${shape}`]: shape,[`${prefixCls}-${sizeCls}`]: sizeCls,[`${prefixCls}-icon-only`]: !children && icon,[`${prefixCls}-loading`]: loading,[`${prefixCls}-background-ghost`]: ghost,[`${prefixCls}-two-chinese-chars`]: hasTwoCNChar,[`${prefixCls}-block`]: block,christmas: isChristmas,});const iconType = loading ? 'loading' : icon;const iconNode = iconType ? <Icon type={iconType} /> : null;const kids = (children || children === 0)? React.Children.map(children, child => insertSpace(child, this.isNeedInserted())) : null;const title= isChristmas ? 'Ho Ho Ho!' : rest.title;/* 可以指定按钮跳转地址,实现功能3 */if ('href' in rest) {return (<a{...rest}className={classes}onClick={this.handleClick}title={title}>{iconNode}{kids}</a>);} else {// 这里的注释的意思是React不推荐在DOM element上使用 ‘htmlType' 这个属性,因此在前面的IProps接口中,没有定义htmlType,但仍可以使用// 在ES6与React,通常使用(剩余参数)这种方式可以扩展组件的IProps接口// React does not recognize the `htmlType` prop on a DOM element. Here we pick it out of `rest`.const { htmlType, ...otherProps } = rest;/***  这里出现了开头的的外部依赖Wave,看到这种写法,对React组件设计比较熟悉的应该能猜到这个Wave是做什么的了,没错Wave是容器组件*  React推崇的是组件化开发,经过这些年的发展与沉淀,有两个关键词越来越活跃:`compose`与`recompose`,看过设计模式的知道,良好的软件设计因该是组合优于继承的,这两个关键词也是这个思路*  为了提高组件的可复用性,社区同时提出了几种React组件的设计思路HOC、Render Callback、 容器组件与展示组件等*  组件拆分的目的是为了复用,复用什么呢? 通常是是UI逻辑*  这里我们先不去关注关注这个Wave是做什么的,我们只需要知道此时,返回一个<button><button>即可, 我们在下一篇文章中去看下这个Wave组件,这里我*  这里我们只需要知道返回了一个button DOM元素,可以接收className、click事件、可以指定点击时跳转到指定url,* */return (<Wave><button{...otherProps}type={htmlType || 'button'}className={classes}onClick={this.handleClick}title={title}>{iconNode}{kids}</button></Wave>);}}
}
分析过`<Button />`组件,再结合之前的`<Icon />`组件,我们其实可以发现一些Antd的一点设计模式(经过两年的React项目踩坑,回过头来看时,发现React社区中存在着大量的设计模式),将之成为`Control  CSS with Props`,后面简称为`CCP`。
在以前JQuery + CSS 横扫各大浏览器的时候,大家写CSS时已经注意到了复用的便利性,下面的代码,前端开发人员肯定写过,我们来看下面这段css代码
// 抽取出一个组件的样式
.btn {display: inline-block;font-weight: @btn-font-weight;text-align: center;touch-action: manipulation;cursor: pointer;background-image: none;border: @border-width-base @border-style-base transparent;white-space: nowrap;.button-size(@btn-height-base; @btn-padding-base; @font-size-base; @btn-border-radius-base);user-select: none;transition: all .3s @ease-in-out;position: relative;box-shadow: 0 2px 0 rgba(0, 0, 0, .015);
}// 在此基础上变形,与扩展
.btn-danger{color: red;
}.btn-primary{background: blue;
}
相信上面这段代码对前端开人员来说,如果放到`html + css`中,如喝水吃饭一样习以为常, 不过是之前的模式在React中经过了变化,此模式将在后面的代码中大量出现,所以与大家约定这种`CCP`的名字
css已经有了,怎么跟Html匹配上呢,看下JSX中的写法
class SimpleBtn extends React.component {render(){const {type} = this.this.props;const cls = 'btn';/**  根据props.type 来生成不同DOM节点 */const btnCls = classNames({[`${cls}-danger`]: type === 'danger',[`${cls}-primary`]: type === 'type',}, cls);return (<button className={btnCls}>{this.props.children}</button>)}
}

调用方式如下

improt {SimpleBtn} from 'smpePath';class Clent extends React.Component{render(){return (<div>// 显示一个红色的按钮<SimpleBtn type="danger"></SimpleBtn>// 显示一个蓝色按钮<SimpleBtn type="primary"></SimpleBtn></div>)}
}

相信看到这里,大家对这种设计模式已经了然于心了。在后面的组件中会大量出现这种组件设计模式。
本篇完


https://dhexx.cn/news/show-109515.html

相关文章

C++内存分配方式详解——堆、栈、自由存储区、全局/静态存储区和常量存储区

栈&#xff0c;就是那些由编译器在需要的时候分配&#xff0c;在不需要的时候自动清除的变量的存储区。里面的变量通常是局部变量、函数参数等。在一个进程中&#xff0c;位于用户虚拟地址空间顶部的是用户栈&#xff0c;编译器用它来实现函数的调用。和堆一样&#xff0c;用户…

kafka命令行工具大全

2019独角兽企业重金招聘Python工程师标准>>> 创建主题sh kafka-topics.sh --zookeeper <zookeeper connect> --create --topic <string> --replication-factor <integer> --partitions <integer> --if-not-exists示例&#xff1a; sh kafka…

volatile

volatile的作用   volatile的作用: 作为指令关键字,确保本条指令不会因 编译器的优化而省略,且要求每次直接读值. 简单地说就是防止编译器对代码进行优化.比如如下程序: XBYTE[2]0x55; XBYTE[2]0x56; XBYTE[2]0x57; XBYTE[2]0x58; 如果对外部硬件上述四条语句分别表示不同的操…

录制开讲啦杂感

题外话 遇到这种场景的时候&#xff0c;我总是会有一种不真实感&#xff0c;甚至常常会忘了自己在哪儿&#xff0c;自己在做什么。 比如第一次在格莱美&#xff0c;觉得自己仿佛也只是在听听歌而已。 又比如第一次去录制朗读者&#xff0c;满心期待看见卿姐&#xff0c;也真的看…

2023年7大最佳跨浏览器测试工具

根据Capgemini 与 Sogeti 和 Micro Focus 合作发布的《2023 年世界质量报告》 &#xff0c;72% 的组织认为质量工程和测试可以在可持续 IT 的环境方面发挥作用。为了实现无缝的最终用户体验&#xff0c;人们需要以不同于过去的方式测试新技术&#xff0c;采用全新的测试和软件质…

J2EE初识

一。 概念J2EE&#xff08;Java 2 Platform, Enterprise Edition&#xff09;是一个为大企业主机级的计算类型而设计的Java平台。Sun微系统&#xff08;与其工业伙伴一起&#xff0c;例如IBM&#xff09;设计了J2EE&#xff0c;以此来简化在受客户级环境下的应用开发。由于创造…

消息响应COMMAND 和 UPDATE_COMMAND_UI的区别,使用范围的区别,使用对象的区别,等等方面的。

简单的说ON_COMMAND是点了按钮或菜单项后的响应消息,ON_UPDATE_COMMAND_UI是用来表示对应的按钮和菜单项的状态的响应消息. 比如WINDOWS自带的记事本程序,它的"格式"菜单下有个"自动换行",那么在编写记事本这个程序时,对文本进行自动换行或不自动换行的操…

Qt之界面(自定义标题栏、无边框、可移动、缩放)

效果 自定义标题栏 titleBar.h #ifndef TITLEBAR_H #define TITLEBAR_H#include <QLabel> #include <QPushButton>class titleBar : public QWidget {Q_OBJECTpublic:explicit titleBar(QWidget *parent nullptr);~titleBar();protected://双击标题栏进行界面的最…

Swift和R3联手了,跨境支付市场竞争升级

本周三&#xff0c;全球银行支付网络SWIFT CEO Gottfried Leibbrandt在巴黎金融科技论坛上宣布&#xff1a;将会与区块链企业R3一起合作&#xff0c;将R3的平台与其新的支付标准框架GPI整合在一起。合作背景 GPI全称为Global Payments Innovation&#xff0c;由SWIFT组织发起、…

MFC应用程序框架入门

1 MFC概述   顾名思意&#xff0c;MFC应用程序框架是以MFC作为框架基础的&#xff0c;以此程序框架模式搭建起来的应用程序在程序结构组织上是完全不同于以前的Win32 SDK编程方式的。自20世纪90年代初问世以来&#xff0c;MFC一直试图把Windows API函数封装到类库中个各个…