2024-11-05 11:54:57 +03:00
|
|
|
'use client';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-11-05 11:54:57 +03:00
|
|
|
import { useFormStatus } from 'react-dom';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-11-05 11:54:57 +03:00
|
|
|
import { LoaderIcon } from '@/components/custom/icons';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-11-05 11:54:57 +03:00
|
|
|
import { Button } from '../ui/button';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-11-08 17:59:06 +03:00
|
|
|
export function SubmitButton({
|
|
|
|
|
children,
|
|
|
|
|
isSuccessful,
|
|
|
|
|
}: {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
isSuccessful: boolean;
|
|
|
|
|
}) {
|
2024-10-11 18:00:22 +05:30
|
|
|
const { pending } = useFormStatus();
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Button
|
2024-11-05 11:54:57 +03:00
|
|
|
type={pending ? 'button' : 'submit'}
|
2024-11-08 17:59:06 +03:00
|
|
|
aria-disabled={pending || isSuccessful}
|
|
|
|
|
disabled={pending || isSuccessful}
|
2024-10-11 18:00:22 +05:30
|
|
|
className="relative"
|
|
|
|
|
>
|
|
|
|
|
{children}
|
2024-11-08 17:59:06 +03:00
|
|
|
|
|
|
|
|
{(pending || isSuccessful) && (
|
2024-10-11 18:00:22 +05:30
|
|
|
<span className="animate-spin absolute right-4">
|
|
|
|
|
<LoaderIcon />
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
2024-11-08 17:59:06 +03:00
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
<span aria-live="polite" className="sr-only" role="status">
|
2024-11-08 17:59:06 +03:00
|
|
|
{pending || isSuccessful ? 'Loading' : 'Submit form'}
|
2024-10-11 18:00:22 +05:30
|
|
|
</span>
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
}
|