forgeplus-react/src/components/AsyncButton/index.tsx

22 lines
697 B
TypeScript

import { Button } from 'antd'
import React, { useState } from 'react'
import { ButtonProps } from 'antd/es/button'
interface AsyncButtonProps extends ButtonProps {
onClick: (e?: React.MouseEvent<HTMLElement, MouseEvent>) => Promise<any>
}
export const AsyncButton = ({ children, ...props }: AsyncButtonProps) => {
const [btnLoading, setBtnLoading] = useState(false)
return <Button {...props} loading={btnLoading} onClick={async (e) => {
try {
setBtnLoading(true)
await props.onClick(e)
setBtnLoading(false)
} catch (error) {
console.error(error);
setBtnLoading(false)
}
}}>{children}</Button>
}