UNPKG

antd

Version:

An enterprise-class UI design language and React-based implementation

197 lines (185 loc) 7.61 kB
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 (s, e) { var t = {}; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]]; }return t; }; import * as React from 'react'; import { findDOMNode } from 'react-dom'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import omit from 'omit.js'; import Icon from '../icon'; var rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/; var isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar); function isString(str) { return typeof str === 'string'; } // Insert one space between two chinese characters automatically. function insertSpace(child, needInserted) { // Check the child if is undefined or null. if (child == null) { return; } var 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 React.createElement( 'span', null, child ); } return child; } var Button = function (_React$Component) { _inherits(Button, _React$Component); function Button(props) { _classCallCheck(this, Button); var _this = _possibleConstructorReturn(this, (Button.__proto__ || Object.getPrototypeOf(Button)).call(this, props)); _this.handleClick = function (e) { // Add click effect _this.setState({ clicked: true }); clearTimeout(_this.timeout); _this.timeout = window.setTimeout(function () { return _this.setState({ clicked: false }); }, 500); var onClick = _this.props.onClick; if (onClick) { onClick(e); } }; _this.state = { loading: props.loading, clicked: false, hasTwoCNChar: false }; return _this; } _createClass(Button, [{ key: 'componentDidMount', value: function componentDidMount() { // Fix for HOC usage like <FormatMessage /> var buttonText = findDOMNode(this).innerText; if (this.isNeedInserted() && isTwoCNChar(buttonText)) { this.setState({ hasTwoCNChar: true }); } } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { var _this2 = this; var currentLoading = this.props.loading; var loading = nextProps.loading; if (currentLoading) { clearTimeout(this.delayTimeout); } if (typeof loading !== 'boolean' && loading && loading.delay) { this.delayTimeout = window.setTimeout(function () { return _this2.setState({ loading: loading }); }, loading.delay); } else { this.setState({ loading: loading }); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.timeout) { clearTimeout(this.timeout); } if (this.delayTimeout) { clearTimeout(this.delayTimeout); } } }, { key: 'isNeedInserted', value: function isNeedInserted() { var _props = this.props, loading = _props.loading, icon = _props.icon, children = _props.children; var iconType = loading ? 'loading' : icon; return React.Children.count(children) === 1 && (!iconType || iconType === 'loading'); } }, { key: 'render', value: function render() { var _classNames, _this3 = this; var _a = this.props, type = _a.type, shape = _a.shape, size = _a.size, className = _a.className, htmlType = _a.htmlType, children = _a.children, icon = _a.icon, prefixCls = _a.prefixCls, ghost = _a.ghost, others = __rest(_a, ["type", "shape", "size", "className", "htmlType", "children", "icon", "prefixCls", "ghost"]);var _state = this.state, loading = _state.loading, clicked = _state.clicked, hasTwoCNChar = _state.hasTwoCNChar; // large => lg // small => sm var sizeCls = ''; switch (size) { case 'large': sizeCls = 'lg'; break; case 'small': sizeCls = 'sm'; default: break; } var ComponentProp = others.href ? 'a' : 'button'; var classes = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-' + type, type), _defineProperty(_classNames, prefixCls + '-' + shape, shape), _defineProperty(_classNames, prefixCls + '-' + sizeCls, sizeCls), _defineProperty(_classNames, prefixCls + '-icon-only', !children && icon), _defineProperty(_classNames, prefixCls + '-loading', loading), _defineProperty(_classNames, prefixCls + '-clicked', clicked), _defineProperty(_classNames, prefixCls + '-background-ghost', ghost), _defineProperty(_classNames, prefixCls + '-two-chinese-chars', hasTwoCNChar), _classNames)); var iconType = loading ? 'loading' : icon; var iconNode = iconType ? React.createElement(Icon, { type: iconType }) : null; var kids = children || children === 0 ? React.Children.map(children, function (child) { return insertSpace(child, _this3.isNeedInserted()); }) : null; return React.createElement( ComponentProp, _extends({}, omit(others, ['loading']), { type: others.href ? undefined : htmlType || 'button', className: classes, onClick: this.handleClick }), iconNode, kids ); } }]); return Button; }(React.Component); export default Button; Button.__ANT_BUTTON = true; Button.defaultProps = { prefixCls: 'ant-btn', loading: false, ghost: false }; Button.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 };