UNPKG

linkmore-design

Version:

🌈 πŸš€lmη»„δ»ΆεΊ“γ€‚πŸš€

234 lines (229 loc) β€’ 9.1 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertLegacyProps = convertLegacyProps; exports.default = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _classnames = _interopRequireDefault(require("classnames")); var _omit = _interopRequireDefault(require("rc-util/lib/omit")); var React = _interopRequireWildcard(require("react")); var _configProvider = require("../config-provider"); var _DisabledContext = _interopRequireDefault(require("../config-provider/DisabledContext")); var _SizeContext = _interopRequireDefault(require("../config-provider/SizeContext")); var _Compact = require("../space/Compact"); var _reactNode = require("../_util/reactNode"); var _type = require("../_util/type"); var _warning = _interopRequireDefault(require("../_util/warning")); var _wave = _interopRequireDefault(require("../_util/wave")); var _buttonGroup = _interopRequireWildcard(require("./button-group")); var _LoadingIcon = _interopRequireDefault(require("./LoadingIcon")); /* eslint-disable react/button-has-type */ const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/; const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar); function isString(str) { return typeof str === 'string'; } function isUnBorderedButtonType(type) { return type === 'text' || type === 'link' || type === 'menu'; } // Insert one space between two chinese characters automatically. function insertSpace(child, needInserted) { // Check the child if is undefined or null. if (child === null || child === undefined) { return; } const SPACE = needInserted ? ' ' : ''; // strictNullChecks oops. if (typeof child !== 'string' && typeof child !== 'number' && isString(child.type) && isTwoCNChar(child.props.children)) { return (0, _reactNode.cloneElement)(child, { children: child.props.children.split('').join(SPACE) }); } if (typeof child === 'string') { return isTwoCNChar(child) ? /*#__PURE__*/React.createElement("span", null, child.split('').join(SPACE)) : /*#__PURE__*/React.createElement("span", null, child); } if ((0, _reactNode.isFragment)(child)) { return /*#__PURE__*/React.createElement("span", null, child); } return child; } function spaceChildren(children, needInserted) { let isPrevChildPure = false; const childList = []; React.Children.forEach(children, child => { const type = typeof child; const isCurrentChildPure = type === 'string' || type === 'number'; if (isPrevChildPure && isCurrentChildPure) { const lastIndex = childList.length - 1; const lastChild = childList[lastIndex]; childList[lastIndex] = `${lastChild}${child}`; } else { childList.push(child); } isPrevChildPure = isCurrentChildPure; }); // Pass to React.Children.map to auto fill key return React.Children.map(childList, child => insertSpace(child, needInserted)); } const ButtonTypes = (0, _type.tuple)('default', 'primary', 'ghost', 'dashed', 'link', 'text', 'menu'); const ButtonShapes = (0, _type.tuple)('default', 'circle', 'round'); const ButtonHTMLTypes = (0, _type.tuple)('submit', 'button', 'reset'); function convertLegacyProps(type) { if (type === 'danger') { return { danger: true }; } return { type }; } const InternalButton = (props, ref) => { const { loading = false, prefixCls: customizePrefixCls, type = 'default', danger, shape = 'default', size: customizeSize, disabled: customDisabled, className, children, icon, ghost = false, block = false, /** If we extract items here, we don't need use omit.js */ // React does not recognize the `htmlType` prop on a DOM element. Here we pick it out of `rest`. htmlType = 'button', ...rest } = props; const size = React.useContext(_SizeContext.default); // ===================== Disabled ===================== const disabled = React.useContext(_DisabledContext.default); const mergedDisabled = customDisabled ?? disabled; const groupSize = React.useContext(_buttonGroup.GroupSizeContext); const [innerLoading, setLoading] = React.useState(!!loading); const [hasTwoCNChar, setHasTwoCNChar] = React.useState(false); const { getPrefixCls, autoInsertSpaceInButton, direction } = React.useContext(_configProvider.ConfigContext); const buttonRef = ref || /*#__PURE__*/React.createRef(); const isNeedInserted = () => React.Children.count(children) === 1 && !icon && !isUnBorderedButtonType(type); const fixTwoCNChar = () => { // Fix for HOC usage like <FormatMessage /> if (!buttonRef || !buttonRef.current || autoInsertSpaceInButton === false) { return; } const buttonText = buttonRef.current.textContent; if (isNeedInserted() && isTwoCNChar(buttonText)) { if (!hasTwoCNChar) { setHasTwoCNChar(true); } } else if (hasTwoCNChar) { setHasTwoCNChar(false); } }; // =============== Update Loading =============== const loadingOrDelay = typeof loading === 'boolean' ? loading : loading?.delay || true; React.useEffect(() => { let delayTimer = null; if (typeof loadingOrDelay === 'number') { delayTimer = window.setTimeout(() => { delayTimer = null; setLoading(loadingOrDelay); }, loadingOrDelay); } else { setLoading(loadingOrDelay); } return () => { if (delayTimer) { // in order to not perform a React state update on an unmounted component // and clear timer after 'loadingOrDelay' updated. window.clearTimeout(delayTimer); delayTimer = null; } }; }, [loadingOrDelay]); React.useEffect(fixTwoCNChar, [buttonRef]); const handleClick = e => { const { onClick } = props; // https://github.com/ant-design/ant-design/issues/30207 if (innerLoading || mergedDisabled) { e.preventDefault(); return; } onClick?.(e); }; (0, _warning.default)(!(typeof icon === 'string' && icon.length > 2), 'Button', `\`icon\` is using ReactNode instead of string naming in v4. Please check \`${icon}\` at https://ant.design/components/icon`); (0, _warning.default)(!(ghost && isUnBorderedButtonType(type)), 'Button', "`link` or `text` button can't be a `ghost` button."); const prefixCls = getPrefixCls('btn', customizePrefixCls); const autoInsertSpace = autoInsertSpaceInButton !== false; const { compactSize, compactItemClassnames } = (0, _Compact.useCompactItemContext)(prefixCls, direction); const sizeClassNameMap = { large: 'lg', small: 'sm', middle: undefined }; const sizeFullname = compactSize || groupSize || customizeSize || size; const sizeCls = sizeFullname ? sizeClassNameMap[sizeFullname] || '' : ''; const iconType = innerLoading ? 'loading' : icon; const linkButtonRestProps = (0, _omit.default)(rest, ['navigate']); const classes = (0, _classnames.default)(prefixCls, { [`${prefixCls}-${shape}`]: shape !== 'default' && shape, // Note: Shape also has `default` [`${prefixCls}-${type}`]: type, [`${prefixCls}-${sizeCls}`]: sizeCls, [`${prefixCls}-icon-only`]: !children && children !== 0 && !!iconType, [`${prefixCls}-background-ghost`]: ghost && !isUnBorderedButtonType(type), [`${prefixCls}-loading`]: innerLoading, [`${prefixCls}-two-chinese-chars`]: hasTwoCNChar && autoInsertSpace && !innerLoading, [`${prefixCls}-block`]: block, [`${prefixCls}-dangerous`]: !!danger, [`${prefixCls}-rtl`]: direction === 'rtl', [`${prefixCls}-disabled`]: linkButtonRestProps.href !== undefined && mergedDisabled }, compactItemClassnames, className, 'lm-button'); const iconNode = icon && !innerLoading ? icon : /*#__PURE__*/React.createElement(_LoadingIcon.default, { existIcon: !!icon, prefixCls: prefixCls, loading: !!innerLoading }); const kids = children || children === 0 ? spaceChildren(children, isNeedInserted() && autoInsertSpace) : null; if (linkButtonRestProps.href !== undefined) { return /*#__PURE__*/React.createElement("a", (0, _extends2.default)({}, linkButtonRestProps, { className: classes, onClick: handleClick, ref: buttonRef }), iconNode, kids); } const buttonNode = /*#__PURE__*/React.createElement("button", (0, _extends2.default)({}, rest, { type: htmlType, className: classes, onClick: handleClick, disabled: mergedDisabled, ref: buttonRef }), iconNode, kids); if (isUnBorderedButtonType(type)) { return buttonNode; } return /*#__PURE__*/React.createElement(_wave.default, { disabled: !!innerLoading }, buttonNode); }; const Button = /*#__PURE__*/React.forwardRef(InternalButton); if (process.env.NODE_ENV !== 'production') { Button.displayName = 'Button'; } Button.Group = _buttonGroup.default; Button.__ANT_BUTTON = true; var _default = Button; exports.default = _default;