2022-11-28 02:00:28 +00:00
|
|
|
import { PropsWithChildren, ReactNode } from 'react';
|
|
|
|
import { Loader2 } from 'lucide-react';
|
|
|
|
|
|
|
|
import { Icon } from '@@/Icon';
|
2021-12-20 17:21:19 +00:00
|
|
|
|
|
|
|
import { type Props as ButtonProps, Button } from './Button';
|
|
|
|
|
|
|
|
interface Props extends ButtonProps {
|
|
|
|
loadingText: string;
|
|
|
|
isLoading: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function LoadingButton({
|
|
|
|
loadingText,
|
|
|
|
isLoading,
|
|
|
|
disabled,
|
|
|
|
type = 'submit',
|
|
|
|
children,
|
2022-11-28 02:00:28 +00:00
|
|
|
icon,
|
2021-12-20 17:21:19 +00:00
|
|
|
...buttonProps
|
|
|
|
}: PropsWithChildren<Props>) {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
|
|
{...buttonProps}
|
|
|
|
type={type}
|
|
|
|
disabled={disabled || isLoading}
|
2022-11-28 02:00:28 +00:00
|
|
|
icon={loadingButtonIcon(isLoading, icon)}
|
2021-12-20 17:21:19 +00:00
|
|
|
>
|
2022-11-28 02:00:28 +00:00
|
|
|
{isLoading ? loadingText : children}
|
2021-12-20 17:21:19 +00:00
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
}
|
2022-11-28 02:00:28 +00:00
|
|
|
|
|
|
|
function loadingButtonIcon(isLoading: boolean, defaultIcon: ReactNode) {
|
|
|
|
if (!isLoading) {
|
|
|
|
return defaultIcon;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<Icon
|
|
|
|
icon={Loader2}
|
2023-02-12 21:04:24 +00:00
|
|
|
className="ml-1 animate-spin-slow"
|
2022-11-28 02:00:28 +00:00
|
|
|
aria-label="loading"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|