UNPKG

oui-antd

Version:

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

265 lines (225 loc) 9.73 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends2 = require('babel-runtime/helpers/extends'); var _extends3 = _interopRequireDefault(_extends2); var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); var _defineProperty3 = _interopRequireDefault(_defineProperty2); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _react = require('react'); var React = _interopRequireWildcard(_react); var _reactDom = require('react-dom'); var _propTypes = require('prop-types'); var PropTypes = _interopRequireWildcard(_propTypes); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _wave = require('../_util/wave'); var _wave2 = _interopRequireDefault(_wave); var _icon = require('../icon'); var _icon2 = _interopRequireDefault(_icon); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } var __rest = undefined && undefined.__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; }; 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) { (0, _inherits3['default'])(Button, _React$Component); function Button(props) { (0, _classCallCheck3['default'])(this, Button); var _this = (0, _possibleConstructorReturn3['default'])(this, (Button.__proto__ || Object.getPrototypeOf(Button)).call(this, props)); _this.handleClick = function (e) { var onClick = _this.props.onClick; if (onClick) { onClick(e); } }; _this.state = { loading: props.loading, hasTwoCNChar: false }; return _this; } (0, _createClass3['default'])(Button, [{ key: 'componentDidMount', value: function componentDidMount() { this.fixTwoCNChar(); } }, { 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: 'componentDidUpdate', value: function componentDidUpdate() { this.fixTwoCNChar(); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.delayTimeout) { clearTimeout(this.delayTimeout); } } }, { key: 'fixTwoCNChar', value: function fixTwoCNChar() { // Fix for HOC usage like <FormatMessage /> var node = (0, _reactDom.findDOMNode)(this); var 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 }); } } }, { key: 'isNeedInserted', value: function isNeedInserted() { var _props = this.props, icon = _props.icon, children = _props.children; return React.Children.count(children) === 1 && !icon; } }, { 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, children = _a.children, icon = _a.icon, prefixCls = _a.prefixCls, ghost = _a.ghost, _loadingProp = _a.loading, block = _a.block, rest = __rest(_a, ["type", "shape", "size", "className", "children", "icon", "prefixCls", "ghost", "loading", "block"]);var _state = this.state, loading = _state.loading, hasTwoCNChar = _state.hasTwoCNChar; // large => lg // small => sm var sizeCls = ''; switch (size) { case 'large': sizeCls = 'lg'; break; case 'small': sizeCls = 'sm'; default: break; } var classes = (0, _classnames2['default'])(prefixCls, className, (_classNames = {}, (0, _defineProperty3['default'])(_classNames, prefixCls + '-' + type, type), (0, _defineProperty3['default'])(_classNames, prefixCls + '-' + shape, shape), (0, _defineProperty3['default'])(_classNames, prefixCls + '-' + sizeCls, sizeCls), (0, _defineProperty3['default'])(_classNames, prefixCls + '-icon-only', !children && icon), (0, _defineProperty3['default'])(_classNames, prefixCls + '-loading', loading), (0, _defineProperty3['default'])(_classNames, prefixCls + '-background-ghost', ghost), (0, _defineProperty3['default'])(_classNames, prefixCls + '-two-chinese-chars', hasTwoCNChar), (0, _defineProperty3['default'])(_classNames, prefixCls + '-block', block), _classNames)); var iconType = loading ? 'loading' : icon; var iconNode = iconType ? React.createElement(_icon2['default'], { type: iconType }) : null; var kids = children || children === 0 ? React.Children.map(children, function (child) { return insertSpace(child, _this3.isNeedInserted()); }) : null; if ('href' in rest) { return React.createElement( 'a', (0, _extends3['default'])({}, rest, { className: classes, onClick: this.handleClick }), iconNode, kids ); } else { // React does not recognize the `htmlType` prop on a DOM element. Here we pick it out of `rest`. var htmlType = rest.htmlType, otherProps = __rest(rest, ["htmlType"]); return React.createElement( _wave2['default'], null, React.createElement( 'button', (0, _extends3['default'])({}, otherProps, { type: htmlType || 'button', className: classes, onClick: this.handleClick }), iconNode, kids ) ); } } }]); return Button; }(React.Component); exports['default'] = Button; Button.__ANT_BUTTON = true; Button.defaultProps = { prefixCls: 'ant-btn', loading: false, ghost: false, block: 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, block: PropTypes.bool }; module.exports = exports['default'];