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.
74 lines • 2.75 kB
JavaScript
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';
const classPrefix = getPrefixCls('button');
const defaultProps = {
color: 'default',
fill: 'solid',
block: false,
loading: false,
loadingIcon: /*#__PURE__*/React.createElement(DotLoading, {
color: "currentColor"
}),
type: 'button',
shape: 'default',
size: 'middle'
};
export const Button = /*#__PURE__*/React.forwardRef((p, ref) => {
const props = mergeProps(defaultProps, p);
const [innerLoading, setInnerLoading] = useState(false);
const nativeButtonRef = useRef(null);
const loading = props.loading === 'auto' ? innerLoading : props.loading;
const disabled = props.disabled || loading;
useImperativeHandle(ref, () => ({
get nativeElement() {
return nativeButtonRef.current;
}
}));
const handleClick = async e => {
if (!props.onClick) return;
const promise = props.onClick(e);
if (isPromise(promise)) {
try {
setInnerLoading(true);
await promise;
setInnerLoading(false);
} catch (e) {
setInnerLoading(false);
throw e;
}
}
};
return withNativeProps(props, /*#__PURE__*/React.createElement("button", {
ref: nativeButtonRef,
type: props.type,
form: props.form,
onClick: handleClick,
className: cs(classPrefix, {
["".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;