Button
包括了兩個組件,Button
與ButtonGroup
。css
看一個組件首先看的是他的傳參也就是props
,因此咱們這裏先看Button
組件的ButtonProps
html
export type ButtonType = 'primary' | 'ghost' | 'dashed' | 'danger'; export type ButtonShape = 'circle' | 'circle-outline'; export type ButtonSize = 'small' | 'large'; // typescript語法,這裏表示的是一些參數,參數後面跟上 ? 是可選參數的意思,不跟就是必須參數 // 參數後面所跟的就是參數的類型,類型能夠是自定義的類型,就如‘ButtonType’,‘ButtonShape’,‘ButtonSize’ // 也能夠是函數或者類,如React.FormEventHandler<any> // 詳情請看這裏 https://www.tslang.cn/docs/handbook/interfaces.html export interface ButtonProps { type?: ButtonType; htmlType?: string; icon?: string; shape?: ButtonShape; size?: ButtonSize; onClick?: React.FormEventHandler<any>; onMouseUp?: React.FormEventHandler<any>; onMouseDown?: React.FormEventHandler<any>; loading?: boolean | { delay?: number }; disabled?: boolean; style?: React.CSSProperties; prefixCls?: string; className?: string; ghost?: boolean; }
看完其參數有哪些以後咱們就直接跳過組件內部的其餘的東西,直接看他的渲染函數,畢竟這裏是執行的入口
這裏順帶提一下這句代碼react
// 這裏的意思是將傳入兩個參數,React.Component的參數第一個是Props,第二個是state, // 而後利用typescript的類型檢查,Props類型須要時上面定義的ButtonProps中的可選參數中的變量名 // state這裏傳入任意都行 export default class Button extends React.Component<ButtonProps, any>
// 接下來是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 ? <Icon type={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 ( <button // 仍是和Icon組件同樣,將用不到的props去除掉 {...omit(others, ['loading', 'clicked'])} type={htmlType || 'button'} className={classes} onClick={this.handleClick} > {iconNode}{kids} </button> ); }
上面講到了這個函數,這裏就來仔細看看是幹嗎的吧typescript
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是漢字的狀況下 // 給其加上空格,上面說的是代碼直譯,那麼代碼意譯下來就是這樣的一個狀況 // 這種狀況(因此這裏他纔會有一個英文註釋,說的是否是嚴格意義的檢查,啊哈哈,尷尬的實現方法) // <Button> // <div>你好啊</div> // </Button> // 這裏說明一下,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 <span>{child}</span>; } return child; }
剩下的都是一些簡單的東西,也沒有什麼能夠講的了dom
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 <span>{child}</span>; } 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<any>; onMouseUp?: React.FormEventHandler<any>; onMouseDown?: React.FormEventHandler<any>; loading?: boolean | { delay?: number }; disabled?: boolean; style?: React.CSSProperties; prefixCls?: string; className?: string; ghost?: boolean; } export default class Button extends React.Component<ButtonProps, any> { // 這裏這樣子寫只是爲了方便這樣子寫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<HTMLButtonElement>) => { // 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 ? <Icon type={iconType} /> : null; const needInserted = React.Children.count(children) === 1 && (!iconType || iconType === 'loading'); const kids = React.Children.map(children, child => insertSpace(child, needInserted)); return ( <button {...omit(others, ['loading', 'clicked'])} type={htmlType || 'button'} className={classes} onClick={this.handleClick} > {iconNode}{kids} </button> ); } }