[MVPTN-66] feat(Componente): Cria componente de botão loading que é acionado ao enviar formulário
This commit is contained in:
parent
b1e39a1518
commit
43d236acf7
3 changed files with 60 additions and 0 deletions
|
|
@ -161,6 +161,7 @@ export default function TTBAndamentoServico() {
|
|||
onClose={handleCloseForm}
|
||||
onSave={handleSave}
|
||||
/>
|
||||
|
||||
</div>
|
||||
); 4
|
||||
}
|
||||
37
src/app/_components/buttonLoading/LoadingButton.tsx
Normal file
37
src/app/_components/buttonLoading/LoadingButton.tsx
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
// components/LoadingButton.tsx
|
||||
"use client";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { ButtonProps } from "@/components/ui/button";
|
||||
|
||||
interface LoadingButtonProps extends ButtonProps {
|
||||
isLoading?: boolean;
|
||||
loadingText?: string;
|
||||
}
|
||||
|
||||
export function LoadingButton({
|
||||
children,
|
||||
isLoading = false,
|
||||
loadingText = "Salvando...",
|
||||
disabled,
|
||||
type = "submit", // padrão para funcionar com forms
|
||||
...props
|
||||
}: LoadingButtonProps) {
|
||||
return (
|
||||
<Button
|
||||
type={type}
|
||||
disabled={isLoading || disabled}
|
||||
{...props}
|
||||
>
|
||||
{isLoading ? (
|
||||
<>
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
{loadingText}
|
||||
</>
|
||||
) : (
|
||||
children
|
||||
)}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
22
src/app/_components/buttonLoading/useAsyncAction.ts
Normal file
22
src/app/_components/buttonLoading/useAsyncAction.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// hooks/useAsyncAction.ts
|
||||
import { useState, useCallback } from "react";
|
||||
|
||||
export function useAsyncAction<T extends (...args: any[]) => Promise<any>>(asyncFunction: T) {
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const execute = useCallback(
|
||||
async (...args: Parameters<T>): Promise<ReturnType<T> | void> => {
|
||||
try {
|
||||
setLoading(true);
|
||||
return await asyncFunction(...args);
|
||||
} catch (error) {
|
||||
console.error("Erro na ação assíncrona:", error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
},
|
||||
[asyncFunction]
|
||||
);
|
||||
|
||||
return { execute, loading };
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue