@antmjs/vantui
Version:
一套适用于Taro3及React的vantui组件库
148 lines • 6.48 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["type", "size", "block", "round", "plain", "square", "loading", "disabled", "hairline", "color", "loadingSize", "loadingType", "loadingText", "icon", "classPrefix", "onClick", "children", "style", "className", "loadingMode", "loadingMask"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import { useState, useEffect, useCallback } from 'react';
import { pxTransform } from '@tarojs/taro';
import { Button as TaroButton, View } from '@tarojs/components';
import * as utils from '../wxs/utils';
import { Icon } from '../icon/index';
import { Loading } from '../loading/index';
import { Toast } from '../toast/index';
import { get } from '../default-props';
import * as computed from './wxs';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var d = get().Button || {};
var defaultConfig = {
asyncLoading: false
};
var index = 0;
function Button(props) {
var _d$props = _objectSpread(_objectSpread({}, d), props),
_d$props$type = _d$props.type,
type = _d$props$type === void 0 ? 'default' : _d$props$type,
_d$props$size = _d$props.size,
size = _d$props$size === void 0 ? 'normal' : _d$props$size,
block = _d$props.block,
round = _d$props.round,
plain = _d$props.plain,
square = _d$props.square,
loading = _d$props.loading,
disabled = _d$props.disabled,
hairline = _d$props.hairline,
color = _d$props.color,
_d$props$loadingSize = _d$props.loadingSize,
loadingSize = _d$props$loadingSize === void 0 ? pxTransform(40) : _d$props$loadingSize,
_d$props$loadingType = _d$props.loadingType,
loadingType = _d$props$loadingType === void 0 ? 'circular' : _d$props$loadingType,
_d$props$loadingText = _d$props.loadingText,
loadingText = _d$props$loadingText === void 0 ? '加载中...' : _d$props$loadingText,
icon = _d$props.icon,
_d$props$classPrefix = _d$props.classPrefix,
classPrefix = _d$props$classPrefix === void 0 ? 'van-icon' : _d$props$classPrefix,
onClick = _d$props.onClick,
children = _d$props.children,
style = _d$props.style,
className = _d$props.className,
_d$props$loadingMode = _d$props.loadingMode,
loadingMode = _d$props$loadingMode === void 0 ? 'normal' : _d$props$loadingMode,
_d$props$loadingMask = _d$props.loadingMask,
loadingMask = _d$props$loadingMask === void 0 ? true : _d$props$loadingMask,
others = _objectWithoutProperties(_d$props, _excluded);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
innerLoading = _useState2[0],
setInnerLoading = _useState2[1];
var _useState3 = useState(++index),
_useState4 = _slicedToArray(_useState3, 1),
compIndex = _useState4[0];
var toastId = "van-button-toast_".concat(compIndex);
useEffect(function () {
setInnerLoading(loading);
}, [loading]);
useEffect(function () {
if (loadingMode !== 'toast') return;
if (innerLoading) {
Toast.loading({
selector: "#".concat(toastId),
duration: 60 * 60,
message: loadingText,
loadingType: loadingType,
mask: loadingMask
});
} else {
Toast.clear();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [innerLoading, loadingMode]);
var _click = useCallback(function (e) {
if (onClick && !loading) {
var res = onClick(e);
// @ts-ignore
if (res !== null && res !== void 0 && res.then && res !== null && res !== void 0 && res.catch && defaultConfig.asyncLoading) {
setInnerLoading(true);
res.finally(function () {
setInnerLoading(false);
});
}
}
}, [loading, onClick]);
return /*#__PURE__*/_jsxs(View, {
className: ' ' + utils.bem('button', [type, size, {
block: block,
round: round,
plain: plain,
square: square,
loading: innerLoading,
disabled: disabled,
hairline: hairline,
unclickable: disabled || innerLoading
}]) + ' ' + (hairline ? 'van-hairline--surround' : '') + " ".concat(className || ''),
style: utils.style([computed.rootStyle({
plain: plain,
color: color
}), style]),
children: [/*#__PURE__*/_jsx(Toast, {
id: toastId
}), /*#__PURE__*/_jsx(TaroButton, _objectSpread({
className: "van-native-button",
disabled: disabled,
onClick: disabled || innerLoading ? undefined : _click
}, others)), innerLoading && loadingMode === 'normal' ? /*#__PURE__*/_jsxs(View, {
style: "display: flex",
children: [/*#__PURE__*/_jsx(Loading, {
className: "loading-class",
size: loadingSize,
type: loadingType,
color: computed.loadingColor({
type: type,
color: color,
plain: plain
})
}), loadingText && /*#__PURE__*/_jsx(View, {
className: "van-button__loading-text",
children: loadingText
})]
}) : /*#__PURE__*/_jsxs(_Fragment, {
children: [icon && /*#__PURE__*/_jsx(Icon, {
size: "1.2em",
name: icon,
classPrefix: classPrefix,
className: "van-button__icon",
style: "line-height: inherit;"
}), /*#__PURE__*/_jsx(View, {
className: "van-button__text",
children: children
})]
})]
});
}
Button['config'] = function (config) {
defaultConfig = config;
};
export default Button;
export { Button };