UNPKG

@flatbiz/antd

Version:
140 lines (135 loc) 4.58 kB
/*! @flatjs/forge MIT @flatbiz/antd */ import _LoadingOutlined from '@ant-design/icons/es/icons/LoadingOutlined.js'; import { _ as _objectWithoutProperties, a as _slicedToArray, b as _objectSpread2 } from './_rollupPluginBabelHelpers-BYm17lo8.js'; import { isPromise } from '@dimjs/lang/is-promise'; import { hooks } from '@wove/react/hooks'; import { classNames } from '@dimjs/utils/class-names/class-names'; import { useState } from 'react'; import { Button } from 'antd'; import { isUndefinedOrNull } from '@flatbiz/utils'; import { C as ConfigProviderWrapper } from './config-provider-wrapper-DDa_64Ce.js'; import { fbaHooks } from './fba-hooks/index.js'; import { f as fbaUtils } from './fba-utils-Dfbczn1S.js'; import { jsx, jsxs } from 'react/jsx-runtime'; var _excluded = ["loadingPosition", "color", "debounceDuration", "permission", "hidden", "loading", "removeGap"]; var DefaultButton = function DefaultButton(props) { if (props.color && !props.disabled) { var buttonTheme = {}; if (props.type === 'link') { buttonTheme = { colorLink: props.color, colorLinkActive: props.color, colorLinkHover: props.color }; } else if (props.type === 'primary') { buttonTheme = { colorPrimary: props.color, colorPrimaryHover: props.color, colorPrimaryActive: props.color }; } else { buttonTheme = { colorPrimaryHover: props.color, colorPrimaryActive: props.color, colorText: props.color, colorBorder: props.color }; } return /*#__PURE__*/jsx(ConfigProviderWrapper, { theme: { components: { Button: buttonTheme } }, children: props.children }); } return props.children; }; /** * antd Button包装组件 * 1. 添加按钮 onClick 返回 Promise自动loading效果 * 2. 内置 防抖 效果(在第一触发函数后,在指定时间内再次触发无效,即两次触发的时间间隙大于指定时间) * @param props * @returns */ var ButtonWrapper = function ButtonWrapper(props) { var loadingPosition = props.loadingPosition, color = props.color, debounceDuration = props.debounceDuration, permission = props.permission, hidden = props.hidden, loading = props.loading, removeGap = props.removeGap, otherProps = _objectWithoutProperties(props, _excluded); var loadingNew = loading; var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), innerLoading = _useState2[0], setLoading = _useState2[1]; var loadingPositionNew = loadingPosition === undefined ? 'left' : loadingPosition; var isLoadingLeft = loadingPositionNew === 'left'; var debounceDurationMew = isUndefinedOrNull(debounceDuration) ? 500 : debounceDuration; var className = classNames(otherProps.className, { 'button-remove-gap': removeGap }); fbaHooks.useEffectCustom(function () { if (!isUndefinedOrNull(loading)) { setLoading(loadingNew || false); } }, [loading]); var onClick = hooks.useDebounceClick(function (e) { var onClick = props.onClick; if (!onClick) { return; } var result = onClick(e); if (result && isPromise(result)) { setLoading(true); result.catch(function (error) { console.error(error); }).finally(function () { setLoading(false); }); } }, debounceDurationMew); if (!fbaUtils.hasPermission(permission)) { return null; } if (hidden) { return null; } if (isLoadingLeft) { return /*#__PURE__*/jsx(DefaultButton, { color: color, disabled: props.disabled, type: props.type, children: /*#__PURE__*/jsx(Button, _objectSpread2(_objectSpread2({}, otherProps), {}, { onClick: onClick, loading: innerLoading, className: className, children: props.children })) }); } return /*#__PURE__*/jsx(DefaultButton, { color: color, disabled: props.disabled, type: props.type, children: /*#__PURE__*/jsxs(Button, _objectSpread2(_objectSpread2({}, otherProps), {}, { onClick: onClick, className: className, children: [innerLoading && /*#__PURE__*/jsx("div", { className: "bw-center-loading", children: /*#__PURE__*/jsx(_LoadingOutlined, {}) }), innerLoading ? /*#__PURE__*/jsx("div", { style: { opacity: 0.5 }, children: props.children }) : props.children] })) }); }; export { ButtonWrapper as B }; //# sourceMappingURL=button-wrapper-l9FUsrWl.js.map