UNPKG

yzsd

Version:
138 lines (137 loc) 7.29 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof3 = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _ref = require("rc-util/lib/ref"); var _buttonGroup = _interopRequireDefault(require("./button-group")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof3(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; } 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; }; var rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/; var isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar); function isString(str) { return typeof str === 'string'; } 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["default"].cloneElement(child, {}, child.props.children.split('').join(SPACE)); } if (typeof child === 'string') { if (isTwoCNChar(child)) { // eslint-disable-next-line no-param-reassign child = child.split('').join(SPACE); } return /*#__PURE__*/_react["default"].createElement("span", null, child); } return child; } function spaceChildren(children, needInserted) { var isPrevChildPure = false; var childList = []; _react["default"].Children.forEach(children, function (child) { var type = (0, _typeof2["default"])(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["default"].Children.map(childList, function (child) { return insertSpace(child, needInserted); }); } var Button = /*#__PURE__*/_react["default"].forwardRef(function (props, ref) { var _classNames; var _props$loading = props.loading, loading = _props$loading === void 0 ? false : _props$loading, _props$prefixCls = props.prefixCls, customizePrefixCls = _props$prefixCls === void 0 ? 'yzs-btn' : _props$prefixCls, _props$type = props.type, type = _props$type === void 0 ? 'default' : _props$type, danger = props.danger, shape = props.shape, customizeSize = props.size, styles = props.styles, className = props.className, rootClassName = props.rootClassName, children = props.children, icon = props.icon, _props$ghost = props.ghost, ghost = _props$ghost === void 0 ? false : _props$ghost, _props$block = props.block, block = _props$block === void 0 ? false : _props$block, customClassNames = props.classNames, reset = __rest(props, ["loading", "prefixCls", "type", "danger", "shape", "size", "styles", "className", "rootClassName", "children", "icon", "ghost", "block", "classNames"]); var prefixCls = "".concat(customizePrefixCls); var sizeCls = ''; switch (customizeSize) { 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 = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-").concat(type), type), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-").concat(shape), shape), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-").concat(sizeCls), sizeCls), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-icon-only"), !children && children !== 0 && iconType), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-loading"), !!loading), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-background-ghost"), ghost), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-block"), block), _classNames)); var isNeedInserted = function isNeedInserted() { var icon = props.icon, children = props.children, type = props.type; return _react["default"].Children.count(children) === 1 && !icon && type !== 'link'; }; var autoInsertSpace = false; var iconNode = iconType ? iconType : null; var htmlType = reset.htmlType, otherProps = __rest(reset, ["htmlType"]); var internalRef = /*#__PURE__*/(0, _react.createRef)(); var buttonRef = (0, _ref.composeRef)(ref, internalRef); var kids = children || children === 0 ? spaceChildren(children, isNeedInserted() && autoInsertSpace) : null; var buttonNode = /*#__PURE__*/_react["default"].createElement("button", (0, _extends2["default"])({}, otherProps, { type: htmlType, className: classes, onClick: function onClick(e) { var loading = props.loading, onClick = props.onClick; if (loading) { return; } if (onClick) { onClick(e); } }, ref: buttonRef }), iconNode, kids); return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, buttonNode); }); Button.Group = _buttonGroup["default"]; var _default = Button; exports["default"] = _default;