Button
Button包括了两个组件,Button与ButtonGroup。
ButtonProps看一个组件首先看的是他的传参也就是props,所以我们这里先看Button组件的ButtonProps
export type ButtonType = "primary" | "ghost" | "dashed" | "danger"; export type ButtonShape = "circle" | "circle-outline"; export type ButtonSize = "small" | "large"; // typescript语法,这里表示的是一些参数,参数后面跟上 ? 是可选参数的意思,不跟就是必须参数 // 参数后面所跟的就是参数的类型,类型可以是自定义的类型,就如‘ButtonType’,‘ButtonShape’,‘ButtonSize’ // 也可以是函数或者类,如React.FormEventHandlerRender()// 详情请看这里 https://www.tslang.cn/docs/handbook/interfaces.html export interface ButtonProps { type?: ButtonType; htmlType?: string; icon?: string; shape?: ButtonShape; size?: ButtonSize; onClick?: React.FormEventHandler ; onMouseUp?: React.FormEventHandler ; onMouseDown?: React.FormEventHandler ; loading?: boolean | { delay?: number }; disabled?: boolean; style?: React.CSSProperties; prefixCls?: string; className?: string; ghost?: boolean; }
看完其参数有哪些之后我们就直接跳过组件内部的其他的东西,直接看他的渲染函数,毕竟这里是执行的入口
这里顺带提一下这句代码
// 这里的意思是将传入两个参数,React.Component的参数第一个是Props,第二个是state, // 然后利用typescript的类型检查,Props类型需要时上面定义的ButtonProps中的可选参数中的变量名 // state这里传入任意都行 export default class Button extends React.Component
// 接下来是render()
render() {
// 将参数从props解构出来
const {
type, shape, size = "", className, htmlType, children, icon, prefixCls, ghost, ...others,
} = this.props;
// 将loading和clicked两个状态从state解构
const { loading, clicked } = this.state;
// large => lg
// small => sm
let sizeCls = "";
switch (size) {
case "large":
sizeCls = "lg";
break;
case "small":
sizeCls = "sm";
default:
break;
}
// 组建样式
const classes = classNames(prefixCls, className, {
[`${prefixCls}-${type}`]: type,
[`${prefixCls}-${shape}`]: shape,
[`${prefixCls}-${sizeCls}`]: sizeCls,
[`${prefixCls}-icon-only`]: !children && icon,
[`${prefixCls}-loading`]: loading,
[`${prefixCls}-clicked`]: clicked,
[`${prefixCls}-background-ghost`]: ghost,
});
// 是否需要加载
const iconType = loading ? "loading" : icon;
// 是否需要添加Icon,不过官方给的是如果需要用到icon的话最好自己写在里面
const iconNode = iconType ? : null;
const needInserted = React.Children.count(children) === 1 && (!iconType || iconType === "loading");
// 重点在这里,敲黑板了
// 这里引用了React.Children.map这个函数来对这个包裹在这个Button组件中的内容渲染出来
// 其中insertSpace()这个函数也有意思,这个函数主要是为了在当组建中间写的是中文汉字的时
// 候给其汉字之间添加一个空格作为分隔,这里有的同学会问为什么不用css里面的letter-space
// 属性,这个我也不是很清楚。。。不过他不用的话可能是是不想在英文字母中间添加空格吧
const kids = React.Children.map(children, child => insertSpace(child, needInserted));
return (
);
}
InsertSpace()
上面讲到了这个函数,这里就来仔细看看是干嘛的吧
const rxTwoCNChar = /^[u4e00-u9fa5]{2}$/;
// 这里的bind有必要好好的理解一下
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
function isString(str: any) {
return typeof str === "string";
}
// Insert one space between two chinese characters automatically.
function insertSpace(child: React.ReactChild, needInserted: boolean) {
// Check the child if is undefined or null.
if (child == null) {
return;
}
const SPACE = needInserted ? " " : "";
// strictNullChecks oops.
// 这个判断的意思是当这个child不是字符串也不是数字并且child.type为字符串并且child的children是汉字的情况下
// 给其加上空格,上面说的是代码直译,那么代码意译下来就是这样的一个情况
// 这种情况(所以这里他才会有一个英文注释,说的是不是严格意义的检查,啊哈哈,尴尬的实现方法)
//
// 这里说明一下,child.type以及child.props.children是react在渲染的时候会给虚拟dom添加的一些属性,如图
if (typeof child !== "string" && typeof child !== "number" &&
isString(child.type) && isTwoCNChar(child.props.children)) {
return React.cloneElement(child, {},
child.props.children.split("").join(SPACE));
}
// 这种情况就很明了了 就是Button组件中写的汉字
if (typeof child === "string") {
if (isTwoCNChar(child)) {
child = child.split("").join(SPACE);
}
return {child};
}
return child;
}
完整源代码
剩下的都是一些简单的东西,也没有什么可以讲的了
import React from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
import omit from "omit.js";
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";
}
// Insert one space between two chinese characters automatically.
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 {child};
}
return child;
}
export type ButtonType = "primary" | "ghost" | "dashed" | "danger";
export type ButtonShape = "circle" | "circle-outline";
export type ButtonSize = "small" | "large";
export interface ButtonProps {
type?: ButtonType;
htmlType?: string;
icon?: string;
shape?: ButtonShape;
size?: ButtonSize;
onClick?: React.FormEventHandler;
onMouseUp?: React.FormEventHandler;
onMouseDown?: React.FormEventHandler;
loading?: boolean | { delay?: number };
disabled?: boolean;
style?: React.CSSProperties;
prefixCls?: string;
className?: string;
ghost?: boolean;
}
export default class Button extends React.Component {
// 这里这样子写只是为了方便这样子写Button.Group来引用ButtonGroup这个组件,下一节将会讲解这个组件
static Group: typeof Group;
static __ANT_BUTTON = true;
static defaultProps = {
prefixCls: "ant-btn",
loading: false,
clicked: false,
ghost: 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,
};
timeout: number;
delayTimeout: number;
constructor(props: ButtonProps) {
super(props);
this.state = {
loading: props.loading,
};
}
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 = setTimeout(() => this.setState({ loading }), loading.delay);
} else {
this.setState({ loading });
}
}
// 在组件销毁的时候一定要记得将定时器也一同销毁
componentWillUnmount() {
if (this.timeout) {
clearTimeout(this.timeout);
}
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
}
handleClick = (e: React.MouseEvent) => {
// Add click effect
this.setState({ clicked: true });
clearTimeout(this.timeout);
this.timeout = setTimeout(() => this.setState({ clicked: false }), 500);
const onClick = this.props.onClick;
if (onClick) {
onClick(e);
}
}
render() {
const {
type, shape, size = "", className, htmlType, children, icon, prefixCls, ghost, ...others,
} = this.props;
const { loading, clicked } = this.state;
// large => lg
// small => sm
let sizeCls = "";
switch (size) {
case "large":
sizeCls = "lg";
break;
case "small":
sizeCls = "sm";
default:
break;
}
const classes = classNames(prefixCls, className, {
[`${prefixCls}-${type}`]: type,
[`${prefixCls}-${shape}`]: shape,
[`${prefixCls}-${sizeCls}`]: sizeCls,
[`${prefixCls}-icon-only`]: !children && icon,
[`${prefixCls}-loading`]: loading,
[`${prefixCls}-clicked`]: clicked,
[`${prefixCls}-background-ghost`]: ghost,
});
const iconType = loading ? "loading" : icon;
const iconNode = iconType ? : null;
const needInserted = React.Children.count(children) === 1 && (!iconType || iconType === "loading");
const kids = React.Children.map(children, child => insertSpace(child, needInserted));
return (
);
}
}
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/89015.html
摘要:作为开发当中使用相对频繁的一个组件,其实现也很简单,但是其中比较麻烦的一部分是字体的制作,可以参看这篇文章。接口中的种属性方法,不属于上述六种。为事件属性,可以大家也可以根据上面所提供的制作的方法和这样的方式来实现自己的组件 Icon icon作为开发当中使用相对频繁的一个组件,其实现也很简单,但是其中比较麻烦的一部分是icon字体的制作,可以参看这篇文章。 Antd的Icon组件使用...
github: 地址gitbook: 地址 Index.js 看一个代码的时候首先当然是从他的入口文件开始看起,所以第一份代码我们看的是/index.js文件 开始 打开index.js文件,代码只有28行,其中包含了一个camelCase函数(看函数名就知道这是个给名称进行驼峰命名法的函数),一个req变量,以及这个的变量操作和export操作 在这个文件里面我首先查了require.conte...
摘要:结构项目结构如下,负责外层封装,负责事件绑定与渲染控制。节点通过决定事件绑定情况,即通过属性绑定事件情况,事件控制组件的属性,这里就不详细说了。为隐藏状态下的添加的,并包裹懒加载组件。 引言 antd的Tooltip组件在react-componment/trigger的基础上进行封装,而组件Popover和Popconfirm是使用Tooltip组件的进行pop,在react-com...
摘要:在装载组件之前调用会组件的构造函数。当实现子类的构造函数时,应该在任何其他语句之前调用设置初始状态绑定键盘回车事件,添加新任务修改状态值,每次修改以后,自动调用方法,再次渲染组件。可以通过直接安装到项目中,使用或进行引用。 首先我们看一下我们完成后的最终形态:TodoMvc: showImg(https://segmentfault.com/img/remote/14600000085...
摘要:这个组件是一个图钉组件,使用的布局,让组件固定在窗口的某一个位置上,并且可以在到达指定位置的时候才去固定。 Affix 这个组件是一个图钉组件,使用的fixed布局,让组件固定在窗口的某一个位置上,并且可以在到达指定位置的时候才去固定。 AffixProps 还是老样子,看一个组件首先我们先来看看他可以传入什么参数 // Affix export interface Affix...
阅读 1561·2025-02-07 13:40
阅读 1883·2025-02-07 13:37
阅读 2496·2024-11-06 13:38
阅读 1773·2024-09-10 13:19
阅读 2010·2024-08-22 19:45
阅读 1820·2021-11-19 09:40
阅读 3450·2021-11-18 13:14
阅读 4754·2021-10-09 10:02