pay-sdk-react
Version:
A cross-platform payment SDK for React, supporting Alipay, WeChat Pay, PayPal, Stripe, Payssion, and Airwallex, compatible with H5, PC, and App environments.
99 lines • 4.15 kB
JavaScript
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
import _asyncToGenerator from "@babel/runtime-corejs3/helpers/esm/asyncToGenerator";
import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
import _regeneratorRuntime from "@babel/runtime-corejs3/regenerator";
import React, { useImperativeHandle, useRef, useState } from 'react';
import cs from '../utils/classNames';
import { withNativeProps } from '../utils/native-props';
import { mergeProps } from '../utils/with-default-props';
import { isPromise } from '../utils/is';
import DotLoading from '../dot-loading';
import { getPrefixCls } from '../utils/getPrefixCls';
var classPrefix = getPrefixCls('button');
var defaultProps = {
color: 'default',
fill: 'solid',
block: false,
loading: false,
loadingIcon: /*#__PURE__*/React.createElement(DotLoading, {
color: "currentColor"
}),
type: 'button',
shape: 'default',
size: 'middle'
};
export var Button = /*#__PURE__*/React.forwardRef(function (p, ref) {
var props = mergeProps(defaultProps, p);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
innerLoading = _useState2[0],
setInnerLoading = _useState2[1];
var nativeButtonRef = useRef(null);
var loading = props.loading === 'auto' ? innerLoading : props.loading;
var disabled = props.disabled || loading;
useImperativeHandle(ref, function () {
return {
get nativeElement() {
return nativeButtonRef.current;
}
};
});
var handleClick = /*#__PURE__*/function () {
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(e) {
var promise, _t;
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
if (props.onClick) {
_context.next = 1;
break;
}
return _context.abrupt("return");
case 1:
promise = props.onClick(e);
if (!isPromise(promise)) {
_context.next = 5;
break;
}
_context.prev = 2;
setInnerLoading(true);
_context.next = 3;
return promise;
case 3:
setInnerLoading(false);
_context.next = 5;
break;
case 4:
_context.prev = 4;
_t = _context["catch"](2);
setInnerLoading(false);
throw _t;
case 5:
case "end":
return _context.stop();
}
}, _callee, null, [[2, 4]]);
}));
return function handleClick(_x) {
return _ref.apply(this, arguments);
};
}();
return withNativeProps(props, /*#__PURE__*/React.createElement("button", {
ref: nativeButtonRef,
type: props.type,
form: props.form,
onClick: handleClick,
className: cs(classPrefix, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(classPrefix, "-").concat(props.color), props.color), "".concat(classPrefix, "-block"), props.block), "".concat(classPrefix, "-disabled"), disabled), "".concat(classPrefix, "-fill-outline"), props.fill === 'outline'), "".concat(classPrefix, "-fill-none"), props.fill === 'none'), "".concat(classPrefix, "-mini"), props.size === 'mini'), "".concat(classPrefix, "-small"), props.size === 'small'), "".concat(classPrefix, "-large"), props.size === 'large'), "".concat(classPrefix, "-loading"), loading), "".concat(classPrefix, "-shape-").concat(props.shape)),
disabled: disabled,
onMouseDown: props.onMouseDown,
onMouseUp: props.onMouseUp,
onTouchStart: props.onTouchStart,
onTouchEnd: props.onTouchEnd
}, loading ? /*#__PURE__*/React.createElement("div", {
className: "".concat(classPrefix, "-loading-wrapper")
}, props.loadingIcon, props.loadingText) : /*#__PURE__*/React.createElement("span", {
className: "".concat(classPrefix, "-content")
}, props.children)));
});
Button.displayName = 'Button';
export default Button;