dtd
Version:
根据数梦工场视觉规范打造的组件库,感谢react-components和ant design
1 lines • 3.96 kB
JavaScript
import _extends from"babel-runtime/helpers/extends";import _defineProperty from"babel-runtime/helpers/defineProperty";import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import _possibleConstructorReturn from"babel-runtime/helpers/possibleConstructorReturn";import _inherits from"babel-runtime/helpers/inherits";var __rest=this&&this.__rest||function(e,t){var r={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(o=Object.getOwnPropertySymbols(e);i<o.length;i++)t.indexOf(o[i])<0&&(r[o[i]]=e[o[i]])}return r};import*as React from"react";import{findDOMNode}from"react-dom";import*as PropTypes from"prop-types";import classNames from"classnames";import omit from"omit.js";import Icon from"../icon";var rxTwoCNChar=/^[\u4e00-\u9fa5]{2}$/,isTwoCNChar=rxTwoCNChar.test.bind(rxTwoCNChar);function isString(e){return"string"==typeof e}function insertSpace(e,t){if(null!=e){return"string"!=typeof e&&"number"!=typeof e&&isString(e.type)&&isTwoCNChar(e.props.children)?React.cloneElement(e,{},e.props.children.split("").join("")):"string"==typeof e?(isTwoCNChar(e)&&(e=e.split("").join("")),React.createElement("span",null,e)):e}}var Button=function(e){function t(e){_classCallCheck(this,t);var r=_possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.handleClick=function(e){r.setState({clicked:!0}),clearTimeout(r.timeout),r.timeout=window.setTimeout(function(){return r.setState({clicked:!1})},500);var t=r.props.onClick;t&&t(e)},r.state={loading:e.loading,clicked:!1,hasTwoCNChar:!1},r}return _inherits(t,e),_createClass(t,[{key:"componentDidMount",value:function(){var e=findDOMNode(this).innerText;this.isNeedInserted()&&isTwoCNChar(e)&&this.setState({hasTwoCNChar:!1})}},{key:"componentWillReceiveProps",value:function(e){var t=this,r=this.props.loading,o=e.loading;r&&clearTimeout(this.delayTimeout),"boolean"!=typeof o&&o&&o.delay?this.delayTimeout=window.setTimeout(function(){return t.setState({loading:o})},o.delay):this.setState({loading:o})}},{key:"componentWillUnmount",value:function(){this.timeout&&clearTimeout(this.timeout),this.delayTimeout&&clearTimeout(this.delayTimeout)}},{key:"isNeedInserted",value:function(){var e=this.props,t=e.loading,r=e.icon,o=e.children,i=t?"loading":r;return 1===React.Children.count(o)&&(!i||"loading"===i)}},{key:"render",value:function(){var e,t=this,r=this.props,o=r.type,i=r.shape,n=r.size,s=r.className,a=r.htmlType,l=r.children,p=r.icon,c=r.prefixCls,u=r.ghost,m=__rest(r,["type","shape","size","className","htmlType","children","icon","prefixCls","ghost"]),h=this.state,d=h.loading,f=h.clicked,y=h.hasTwoCNChar,C="";switch(n){case"large":C="lg";break;case"small":C="sm";break;case"mini":C="mini"}var T=m.href?"a":"button",g=classNames(c,s,(_defineProperty(e={},c+"-"+o,o),_defineProperty(e,c+"-"+i,i),_defineProperty(e,c+"-"+C,C),_defineProperty(e,c+"-icon-only",!l&&p),_defineProperty(e,c+"-loading",d),_defineProperty(e,c+"-clicked",f),_defineProperty(e,c+"-background-ghost",u),_defineProperty(e,c+"-two-chinese-chars",y),e)),b=d?"loading":p,_=b?React.createElement(Icon,{type:b}):null,P=l||0===l?React.Children.map(l,function(e){return insertSpace(e,t.isNeedInserted())}):null;return React.createElement(T,_extends({},omit(m,["loading"]),{type:m.href?void 0:a||"button",className:g,onClick:this.handleClick}),_,P)}}]),t}(React.Component);export default Button;Button.__ANT_BUTTON=!0,Button.defaultProps={prefixCls:"dt-btn",loading:!1,ghost:!1},Button.propTypes={type:PropTypes.string,shape:PropTypes.oneOf(["circle","circle-outline"]),size:PropTypes.oneOf(["large","default","small","mini"]),htmlType:PropTypes.oneOf(["submit","button","reset"]),onClick:PropTypes.func,loading:PropTypes.oneOfType([PropTypes.bool,PropTypes.object]),className:PropTypes.string,icon:PropTypes.string};