344 lines
14 KiB
JavaScript
344 lines
14 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports["default"] = void 0;
|
|
|
|
var React = _interopRequireWildcard(require("react"));
|
|
|
|
var PropTypes = _interopRequireWildcard(require("prop-types"));
|
|
|
|
var _classnames = _interopRequireDefault(require("classnames"));
|
|
|
|
var _reactLifecyclesCompat = require("react-lifecycles-compat");
|
|
|
|
var _omit = _interopRequireDefault(require("omit.js"));
|
|
|
|
var _icon = _interopRequireDefault(require("../icon"));
|
|
|
|
var _configProvider = require("../config-provider");
|
|
|
|
var _wave = _interopRequireDefault(require("../_util/wave"));
|
|
|
|
var _type = require("../_util/type");
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
|
|
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
|
|
|
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
|
|
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 _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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { 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 _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 = void 0 && (void 0).__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 */
|
|
|
|
|
|
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 /*#__PURE__*/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 = (0, _type.tuple)('default', 'primary', 'ghost', 'dashed', 'danger', 'link');
|
|
var ButtonShapes = (0, _type.tuple)('circle', 'circle-outline', 'round');
|
|
var ButtonSizes = (0, _type.tuple)('large', 'default', 'small');
|
|
var ButtonHTMLTypes = (0, _type.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.renderButton = function (_ref) {
|
|
var _classNames;
|
|
|
|
var getPrefixCls = _ref.getPrefixCls,
|
|
autoInsertSpaceInButton = _ref.autoInsertSpaceInButton;
|
|
|
|
var _a = _this.props,
|
|
customizePrefixCls = _a.prefixCls,
|
|
type = _a.type,
|
|
shape = _a.shape,
|
|
size = _a.size,
|
|
className = _a.className,
|
|
children = _a.children,
|
|
icon = _a.icon,
|
|
ghost = _a.ghost,
|
|
block = _a.block,
|
|
rest = __rest(_a, ["prefixCls", "type", "shape", "size", "className", "children", "icon", "ghost", "block"]);
|
|
|
|
var _this$state = _this.state,
|
|
loading = _this$state.loading,
|
|
hasTwoCNChar = _this$state.hasTwoCNChar;
|
|
var prefixCls = getPrefixCls('btn', customizePrefixCls);
|
|
var autoInsertSpace = autoInsertSpaceInButton !== false; // large => lg
|
|
// small => sm
|
|
|
|
var sizeCls = '';
|
|
|
|
switch (size) {
|
|
case 'large':
|
|
sizeCls = 'lg';
|
|
break;
|
|
|
|
case 'small':
|
|
sizeCls = 'sm';
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
var iconType = loading ? 'loading' : icon;
|
|
var classes = (0, _classnames["default"])(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), _classNames));
|
|
var iconNode = iconType ? /*#__PURE__*/React.createElement(_icon["default"], {
|
|
type: iconType
|
|
}) : null;
|
|
var kids = children || children === 0 ? spaceChildren(children, _this.isNeedInserted() && autoInsertSpace) : null;
|
|
var linkButtonRestProps = (0, _omit["default"])(rest, ['htmlType', 'loading']);
|
|
|
|
if (linkButtonRestProps.href !== undefined) {
|
|
return /*#__PURE__*/React.createElement("a", _extends({}, linkButtonRestProps, {
|
|
className: classes,
|
|
onClick: _this.handleClick,
|
|
ref: _this.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({}, (0, _omit["default"])(otherProps, ['loading']), {
|
|
type: htmlType,
|
|
className: classes,
|
|
onClick: _this.handleClick,
|
|
ref: _this.saveButtonRef
|
|
}), iconNode, kids);
|
|
|
|
if (type === 'link') {
|
|
return buttonNode;
|
|
}
|
|
|
|
return /*#__PURE__*/React.createElement(_wave["default"], null, buttonNode);
|
|
};
|
|
|
|
_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() {
|
|
// Fix for HOC usage like <FormatMessage />
|
|
if (!this.buttonNode) {
|
|
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() {
|
|
return /*#__PURE__*/React.createElement(_configProvider.ConfigConsumer, null, this.renderButton);
|
|
}
|
|
}]);
|
|
|
|
return Button;
|
|
}(React.Component);
|
|
|
|
Button.__ANT_BUTTON = true;
|
|
Button.defaultProps = {
|
|
loading: false,
|
|
ghost: false,
|
|
block: false,
|
|
htmlType: 'button'
|
|
};
|
|
Button.propTypes = {
|
|
type: PropTypes.string,
|
|
shape: PropTypes.oneOf(ButtonShapes),
|
|
size: PropTypes.oneOf(ButtonSizes),
|
|
htmlType: PropTypes.oneOf(ButtonHTMLTypes),
|
|
onClick: PropTypes.func,
|
|
loading: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
|
|
className: PropTypes.string,
|
|
icon: PropTypes.string,
|
|
block: PropTypes.bool,
|
|
title: PropTypes.string
|
|
};
|
|
(0, _reactLifecyclesCompat.polyfill)(Button);
|
|
var _default = Button;
|
|
exports["default"] = _default; |