antd
Version:
An enterprise-class UI design language and React components implementation
310 lines (245 loc) • 12.8 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
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 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
/* eslint-disable react/button-has-type */
import * as React from 'react';
import classNames from 'classnames';
import LoadingOutlined from '@ant-design/icons/LoadingOutlined';
import omit from 'omit.js';
import { ConfigContext } from '../config-provider';
import Wave from '../_util/wave';
import { tuple } from '../_util/type';
import warning from '../_util/warning';
import SizeContext from '../config-provider/SizeContext';
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 /*#__PURE__*/React.createElement("span", null, child);
}
return child;
}
function spaceChildren(children, needInserted) {
var isPrevChildPure = false;
var childList = [];
React.Children.forEach(children, function (child) {
var type = _typeof(child);
var isCurrentChildPure = type === 'string' || type === 'number';
if (isPrevChildPure && isCurrentChildPure) {
var lastIndex = childList.length - 1;
var lastChild = childList[lastIndex];
childList[lastIndex] = "".concat(lastChild).concat(child);
} else {
childList.push(child);
}
isPrevChildPure = isCurrentChildPure;
}); // Pass to React.Children.map to auto fill key
return React.Children.map(childList, function (child) {
return insertSpace(child, needInserted);
});
}
var ButtonTypes = tuple('default', 'primary', 'ghost', 'dashed', 'danger', 'link');
var ButtonShapes = tuple('circle', 'circle-outline', 'round');
var ButtonHTMLTypes = tuple('submit', 'button', 'reset');
var Button = /*#__PURE__*/function (_React$Component) {
_inherits(Button, _React$Component);
var _super = _createSuper(Button);
function Button(props) {
var _this;
_classCallCheck(this, Button);
_this = _super.call(this, props);
_this.saveButtonRef = function (node) {
_this.buttonNode = node;
};
_this.handleClick = function (e) {
var loading = _this.state.loading;
var onClick = _this.props.onClick;
if (loading) {
return;
}
if (onClick) {
onClick(e);
}
};
_this.state = {
loading: props.loading,
hasTwoCNChar: false
};
return _this;
}
_createClass(Button, [{
key: "componentDidMount",
value: function componentDidMount() {
this.fixTwoCNChar();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this2 = this;
this.fixTwoCNChar();
if (prevProps.loading && typeof prevProps.loading !== 'boolean') {
clearTimeout(this.delayTimeout);
}
var loading = this.props.loading;
if (loading && typeof loading !== 'boolean' && loading.delay) {
this.delayTimeout = window.setTimeout(function () {
_this2.setState({
loading: loading
});
}, loading.delay);
} else if (prevProps.loading !== loading) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({
loading: loading
});
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.delayTimeout) {
clearTimeout(this.delayTimeout);
}
}
}, {
key: "fixTwoCNChar",
value: function fixTwoCNChar() {
var autoInsertSpaceInButton = this.context.autoInsertSpaceInButton; // Fix for HOC usage like <FormatMessage />
if (!this.buttonNode || autoInsertSpaceInButton === false) {
return;
}
var buttonText = this.buttonNode.textContent;
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 _this$props = this.props,
icon = _this$props.icon,
children = _this$props.children,
type = _this$props.type;
return React.Children.count(children) === 1 && !icon && type !== 'link';
}
}, {
key: "render",
value: function render() {
var _this3 = this;
var _this$context = this.context,
getPrefixCls = _this$context.getPrefixCls,
autoInsertSpaceInButton = _this$context.autoInsertSpaceInButton,
direction = _this$context.direction;
return /*#__PURE__*/React.createElement(SizeContext.Consumer, null, function (size) {
var _classNames;
var _a = _this3.props,
customizePrefixCls = _a.prefixCls,
type = _a.type,
danger = _a.danger,
shape = _a.shape,
customizeSize = _a.size,
className = _a.className,
children = _a.children,
icon = _a.icon,
ghost = _a.ghost,
block = _a.block,
rest = __rest(_a, ["prefixCls", "type", "danger", "shape", "size", "className", "children", "icon", "ghost", "block"]);
var _this3$state = _this3.state,
loading = _this3$state.loading,
hasTwoCNChar = _this3$state.hasTwoCNChar;
warning(!(typeof icon === 'string' && icon.length > 2), 'Button', "`icon` is using ReactNode instead of string naming in v4. Please check `".concat(icon, "` at https://ant.design/components/icon"));
var prefixCls = getPrefixCls('btn', customizePrefixCls);
var autoInsertSpace = autoInsertSpaceInButton !== false; // large => lg
// small => sm
var sizeCls = '';
switch (customizeSize || size) {
case 'large':
sizeCls = 'lg';
break;
case 'small':
sizeCls = 'sm';
break;
default:
break;
}
var iconType = loading ? 'loading' : icon;
var classes = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(type), type), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(shape), shape), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(sizeCls), sizeCls), _defineProperty(_classNames, "".concat(prefixCls, "-icon-only"), !children && children !== 0 && iconType), _defineProperty(_classNames, "".concat(prefixCls, "-loading"), !!loading), _defineProperty(_classNames, "".concat(prefixCls, "-background-ghost"), ghost), _defineProperty(_classNames, "".concat(prefixCls, "-two-chinese-chars"), hasTwoCNChar && autoInsertSpace), _defineProperty(_classNames, "".concat(prefixCls, "-block"), block), _defineProperty(_classNames, "".concat(prefixCls, "-dangerous"), !!danger), _defineProperty(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), _classNames));
var iconNode = loading ? /*#__PURE__*/React.createElement(LoadingOutlined, null) : icon || null;
var kids = children || children === 0 ? spaceChildren(children, _this3.isNeedInserted() && autoInsertSpace) : null;
var linkButtonRestProps = omit(rest, ['htmlType', 'loading']);
if (linkButtonRestProps.href !== undefined) {
return /*#__PURE__*/React.createElement("a", _extends({}, linkButtonRestProps, {
className: classes,
onClick: _this3.handleClick,
ref: _this3.saveButtonRef
}), iconNode, kids);
} // React does not recognize the `htmlType` prop on a DOM element. Here we pick it out of `rest`.
var _b = rest,
htmlType = _b.htmlType,
otherProps = __rest(_b, ["htmlType"]);
var buttonNode = /*#__PURE__*/React.createElement("button", _extends({}, omit(otherProps, ['loading']), {
type: htmlType,
className: classes,
onClick: _this3.handleClick,
ref: _this3.saveButtonRef
}), iconNode, kids);
if (type === 'link') {
return buttonNode;
}
return /*#__PURE__*/React.createElement(Wave, null, buttonNode);
});
}
}]);
return Button;
}(React.Component);
Button.__ANT_BUTTON = true;
Button.contextType = ConfigContext;
Button.defaultProps = {
loading: false,
ghost: false,
block: false,
htmlType: 'button'
};
export default Button;