258 lines
10 KiB
JavaScript
258 lines
10 KiB
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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
|
|
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
|
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
|
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
|
|
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
|
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
|
|
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 omit from 'omit.js';
|
|
import Group from './button-group';
|
|
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';
|
|
import LoadingIcon from './LoadingIcon';
|
|
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 = function Button(_a) {
|
|
var props = __rest(_a, []);
|
|
|
|
var _React$useState = React.useState(props.loading),
|
|
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
loading = _React$useState2[0],
|
|
setLoading = _React$useState2[1];
|
|
|
|
var _React$useState3 = React.useState(false),
|
|
_React$useState4 = _slicedToArray(_React$useState3, 2),
|
|
hasTwoCNChar = _React$useState4[0],
|
|
setHasTwoCNChar = _React$useState4[1];
|
|
|
|
var _React$useContext = React.useContext(ConfigContext),
|
|
getPrefixCls = _React$useContext.getPrefixCls,
|
|
autoInsertSpaceInButton = _React$useContext.autoInsertSpaceInButton,
|
|
direction = _React$useContext.direction;
|
|
|
|
var buttonRef = React.createRef();
|
|
var delayTimeout;
|
|
|
|
var isNeedInserted = function isNeedInserted() {
|
|
var icon = props.icon,
|
|
children = props.children,
|
|
type = props.type;
|
|
return React.Children.count(children) === 1 && !icon && type !== 'link';
|
|
};
|
|
|
|
var fixTwoCNChar = function fixTwoCNChar() {
|
|
// Fix for HOC usage like <FormatMessage />
|
|
if (!buttonRef || !buttonRef.current || autoInsertSpaceInButton === false) {
|
|
return;
|
|
}
|
|
|
|
var buttonText = buttonRef.current.textContent;
|
|
|
|
if (isNeedInserted() && isTwoCNChar(buttonText)) {
|
|
if (!hasTwoCNChar) {
|
|
setHasTwoCNChar(true);
|
|
}
|
|
} else if (hasTwoCNChar) {
|
|
setHasTwoCNChar(false);
|
|
}
|
|
};
|
|
|
|
React.useEffect(function () {
|
|
if (props.loading && typeof props.loading !== 'boolean') {
|
|
clearTimeout(delayTimeout);
|
|
}
|
|
|
|
if (props.loading && typeof props.loading !== 'boolean' && props.loading.delay) {
|
|
delayTimeout = window.setTimeout(function () {
|
|
setLoading(props.loading);
|
|
}, props.loading.delay);
|
|
} else if (props.loading !== loading) {
|
|
setLoading(props.loading);
|
|
}
|
|
}, [props.loading]);
|
|
React.useEffect(function () {
|
|
fixTwoCNChar();
|
|
}, [buttonRef]);
|
|
|
|
var handleClick = function handleClick(e) {
|
|
var onClick = props.onClick;
|
|
|
|
if (loading) {
|
|
return;
|
|
}
|
|
|
|
if (onClick) {
|
|
onClick(e);
|
|
}
|
|
};
|
|
|
|
return /*#__PURE__*/React.createElement(SizeContext.Consumer, null, function (size) {
|
|
var _classNames;
|
|
|
|
var customizePrefixCls = props.prefixCls,
|
|
type = props.type,
|
|
danger = props.danger,
|
|
shape = props.shape,
|
|
customizeSize = props.size,
|
|
className = props.className,
|
|
children = props.children,
|
|
icon = props.icon,
|
|
ghost = props.ghost,
|
|
block = props.block,
|
|
rest = __rest(props, ["prefixCls", "type", "danger", "shape", "size", "className", "children", "icon", "ghost", "block"]);
|
|
|
|
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, "-background-ghost"), ghost), _defineProperty(_classNames, "".concat(prefixCls, "-loading"), loading), _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 = icon && !loading ? icon : /*#__PURE__*/React.createElement(LoadingIcon, {
|
|
existIcon: !!icon,
|
|
prefixCls: prefixCls,
|
|
loading: loading
|
|
});
|
|
var kids = children || children === 0 ? spaceChildren(children, isNeedInserted() && autoInsertSpace) : null;
|
|
var linkButtonRestProps = omit(rest, ['htmlType', 'loading']);
|
|
|
|
if (linkButtonRestProps.href !== undefined) {
|
|
return /*#__PURE__*/React.createElement("a", _extends({}, linkButtonRestProps, {
|
|
className: classes,
|
|
onClick: handleClick,
|
|
ref: buttonRef
|
|
}), iconNode, kids);
|
|
} // React does not recognize the `htmlType` prop on a DOM element. Here we pick it out of `rest`.
|
|
|
|
|
|
var _a = rest,
|
|
htmlType = _a.htmlType,
|
|
otherProps = __rest(_a, ["htmlType"]);
|
|
|
|
var buttonNode = /*#__PURE__*/React.createElement("button", _extends({}, omit(otherProps, ['loading']), {
|
|
type: htmlType,
|
|
className: classes,
|
|
onClick: handleClick,
|
|
ref: buttonRef
|
|
}), iconNode, kids);
|
|
|
|
if (type === 'link') {
|
|
return buttonNode;
|
|
}
|
|
|
|
return /*#__PURE__*/React.createElement(Wave, null, buttonNode);
|
|
});
|
|
};
|
|
|
|
Button.defaultProps = {
|
|
loading: false,
|
|
ghost: false,
|
|
block: false,
|
|
htmlType: 'button'
|
|
};
|
|
Button.Group = Group;
|
|
Button.__ANT_BUTTON = true;
|
|
export default Button; |