From 8cddc8203173038db968e9887c922f145d5dd828 Mon Sep 17 00:00:00 2001 From: Ardeman Date: Tue, 11 Mar 2025 06:00:49 +0800 Subject: [PATCH] fix: add loading state to buttons in forms and update button styles --- app/components/ui/button.tsx | 25 ++++++++++++++++--------- app/layouts/news/form-login.tsx | 6 ++---- app/layouts/news/form-register.tsx | 6 ++---- app/layouts/news/form-subscription.tsx | 6 ++---- app/layouts/news/header-top.tsx | 4 +++- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/app/components/ui/button.tsx b/app/components/ui/button.tsx index 5356358..798fa4f 100644 --- a/app/components/ui/button.tsx +++ b/app/components/ui/button.tsx @@ -1,4 +1,5 @@ import { Button as HeadlessButton } from '@headlessui/react' +import { ArrowPathIcon } from '@heroicons/react/20/solid' import { cva, type VariantProps } from 'class-variance-authority' import type { ReactNode, ElementType, ComponentPropsWithoutRef } from 'react' import { twMerge } from 'tailwind-merge' @@ -38,6 +39,7 @@ type ButtonBaseProperties = { variant?: VariantProps['variant'] size?: VariantProps['size'] className?: string + isLoading?: boolean } type PolymorphicReference = @@ -48,22 +50,27 @@ type ButtonProperties = ButtonBaseProperties & { ref?: PolymorphicReference } & Omit, keyof ButtonBaseProperties> -export const Button = ({ - as, - children, - variant, - size, - className, - ...properties -}: ButtonProperties) => { +export const Button = ( + properties: ButtonProperties, +) => { + const { + as, + children, + variant, + size, + className, + isLoading = false, + ...restProperties + } = properties const Component = as || HeadlessButton const classes = twMerge(buttonVariants({ variant, size, className })) return ( + {isLoading && } {children} ) diff --git a/app/layouts/news/form-login.tsx b/app/layouts/news/form-login.tsx index bd31a6b..131d249 100644 --- a/app/layouts/news/form-login.tsx +++ b/app/layouts/news/form-login.tsx @@ -24,7 +24,6 @@ export const FormLogin = () => { } = useNewsContext() const fetcher = useFetcher() const [error, setError] = useState() - const [disabled, setDisabled] = useState(false) const formMethods = useRemixForm({ mode: 'onSubmit', @@ -37,11 +36,9 @@ export const FormLogin = () => { useEffect(() => { if (!fetcher.data?.success) { setError(fetcher.data?.message) - setDisabled(false) return } - setDisabled(true) setError(undefined) setIsLoginOpen(false) @@ -95,7 +92,8 @@ export const FormLogin = () => {