UNPKG

yzsd

Version:
127 lines 5.39 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _typeof from "@babel/runtime/helpers/esm/typeof"; 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; }; import React, { createRef } from 'react'; import classNames from 'classnames'; import { composeRef } from "rc-util/es/ref"; import Group from './button-group'; 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.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.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 Button = /*#__PURE__*/React.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 = 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, "-block"), block), _classNames)); var isNeedInserted = function isNeedInserted() { var icon = props.icon, children = props.children, type = props.type; return React.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__*/createRef(); var buttonRef = composeRef(ref, internalRef); var kids = children || children === 0 ? spaceChildren(children, isNeedInserted() && autoInsertSpace) : null; var buttonNode = /*#__PURE__*/React.createElement("button", _extends({}, 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.createElement(React.Fragment, null, buttonNode); }); Button.Group = Group; export default Button;