antd-mobile-taro-ui
Version:
以antd-mobile为设计标准,基于taro框架的微信小程序组件库
71 lines (67 loc) • 2.44 kB
JavaScript
import { __awaiter } from "tslib";
import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import classNames from 'classnames';
import { View } from '@tarojs/components';
import { mergeProps } from 'antd-mobile/es/utils/with-default-props';
import { withNativeProps } from 'antd-mobile/es/utils/native-props';
import { isPromise } from 'antd-mobile/es/utils/validate';
import DotLoading from '../dot-loading';
const classPrefix = `adm-button`;
const defaultProps = {
color: 'default',
fill: 'solid',
block: false,
loading: false,
loadingIcon: React.createElement(DotLoading, {
color: 'currentColor'
}),
type: '',
shape: 'default',
size: 'middle',
onTouchStart: () => {}
};
export const Button = 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 = e => __awaiter(void 0, void 0, void 0, function* () {
if (disabled) return;
if (!props.onClick) return;
const promise = props.onClick(e);
if (isPromise(promise)) {
try {
setInnerLoading(true);
yield promise;
setInnerLoading(false);
} catch (e) {
setInnerLoading(false);
throw e;
}
}
});
return withNativeProps(props, React.createElement(View, {
ref: nativeButtonRef,
onClick: handleClick,
className: classNames(classPrefix, props.color ? `${classPrefix}-${props.color}` : null, {
[`${classPrefix}-block`]: props.block,
[`${classPrefix}-disabled`]: disabled,
[`${classPrefix}-fill-outline`]: props.fill === 'outline',
[`${classPrefix}-fill-none`]: props.fill === 'none',
[`${classPrefix}-mini`]: props.size === 'mini',
[`${classPrefix}-small`]: props.size === 'small',
[`${classPrefix}-large`]: props.size === 'large',
[`${classPrefix}-loading`]: loading
}, `${classPrefix}-shape-${props.shape}`),
onTouchStart: props.onTouchStart,
onTouchEnd: props.onTouchEnd
}, loading ? React.createElement(View, {
className: `${classPrefix}-loading-wrapper`
}, props.loadingIcon, props.loadingText) : props.children));
});