fix: add loading state to buttons in forms and update button styles
This commit is contained in:
parent
a45a6fb87e
commit
8cddc82031
@ -1,4 +1,5 @@
|
|||||||
import { Button as HeadlessButton } from '@headlessui/react'
|
import { Button as HeadlessButton } from '@headlessui/react'
|
||||||
|
import { ArrowPathIcon } from '@heroicons/react/20/solid'
|
||||||
import { cva, type VariantProps } from 'class-variance-authority'
|
import { cva, type VariantProps } from 'class-variance-authority'
|
||||||
import type { ReactNode, ElementType, ComponentPropsWithoutRef } from 'react'
|
import type { ReactNode, ElementType, ComponentPropsWithoutRef } from 'react'
|
||||||
import { twMerge } from 'tailwind-merge'
|
import { twMerge } from 'tailwind-merge'
|
||||||
@ -38,6 +39,7 @@ type ButtonBaseProperties = {
|
|||||||
variant?: VariantProps<typeof buttonVariants>['variant']
|
variant?: VariantProps<typeof buttonVariants>['variant']
|
||||||
size?: VariantProps<typeof buttonVariants>['size']
|
size?: VariantProps<typeof buttonVariants>['size']
|
||||||
className?: string
|
className?: string
|
||||||
|
isLoading?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
type PolymorphicReference<C extends ElementType> =
|
type PolymorphicReference<C extends ElementType> =
|
||||||
@ -48,22 +50,27 @@ type ButtonProperties<C extends ElementType> = ButtonBaseProperties & {
|
|||||||
ref?: PolymorphicReference<C>
|
ref?: PolymorphicReference<C>
|
||||||
} & Omit<ComponentPropsWithoutRef<C>, keyof ButtonBaseProperties>
|
} & Omit<ComponentPropsWithoutRef<C>, keyof ButtonBaseProperties>
|
||||||
|
|
||||||
export const Button = <C extends ElementType = 'button'>({
|
export const Button = <C extends ElementType = 'button'>(
|
||||||
as,
|
properties: ButtonProperties<C>,
|
||||||
children,
|
) => {
|
||||||
variant,
|
const {
|
||||||
size,
|
as,
|
||||||
className,
|
children,
|
||||||
...properties
|
variant,
|
||||||
}: ButtonProperties<C>) => {
|
size,
|
||||||
|
className,
|
||||||
|
isLoading = false,
|
||||||
|
...restProperties
|
||||||
|
} = properties
|
||||||
const Component = as || HeadlessButton
|
const Component = as || HeadlessButton
|
||||||
const classes = twMerge(buttonVariants({ variant, size, className }))
|
const classes = twMerge(buttonVariants({ variant, size, className }))
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Component
|
<Component
|
||||||
className={classes}
|
className={classes}
|
||||||
{...properties}
|
{...restProperties}
|
||||||
>
|
>
|
||||||
|
{isLoading && <ArrowPathIcon className="animate-spin" />}
|
||||||
{children}
|
{children}
|
||||||
</Component>
|
</Component>
|
||||||
)
|
)
|
||||||
|
|||||||
@ -24,7 +24,6 @@ export const FormLogin = () => {
|
|||||||
} = useNewsContext()
|
} = useNewsContext()
|
||||||
const fetcher = useFetcher()
|
const fetcher = useFetcher()
|
||||||
const [error, setError] = useState<string>()
|
const [error, setError] = useState<string>()
|
||||||
const [disabled, setDisabled] = useState(false)
|
|
||||||
|
|
||||||
const formMethods = useRemixForm<TLoginSchema>({
|
const formMethods = useRemixForm<TLoginSchema>({
|
||||||
mode: 'onSubmit',
|
mode: 'onSubmit',
|
||||||
@ -37,11 +36,9 @@ export const FormLogin = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!fetcher.data?.success) {
|
if (!fetcher.data?.success) {
|
||||||
setError(fetcher.data?.message)
|
setError(fetcher.data?.message)
|
||||||
setDisabled(false)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
setDisabled(true)
|
|
||||||
setError(undefined)
|
setError(undefined)
|
||||||
setIsLoginOpen(false)
|
setIsLoginOpen(false)
|
||||||
|
|
||||||
@ -95,7 +92,8 @@ export const FormLogin = () => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
disabled={disabled}
|
isLoading={fetcher.state !== 'idle'}
|
||||||
|
disabled={fetcher.state !== 'idle'}
|
||||||
type="submit"
|
type="submit"
|
||||||
className="w-full rounded-md py-2"
|
className="w-full rounded-md py-2"
|
||||||
>
|
>
|
||||||
|
|||||||
@ -40,7 +40,6 @@ export const FormRegister = () => {
|
|||||||
const { setIsLoginOpen, setIsRegisterOpen, setIsSuccessOpen } =
|
const { setIsLoginOpen, setIsRegisterOpen, setIsSuccessOpen } =
|
||||||
useNewsContext()
|
useNewsContext()
|
||||||
const [error, setError] = useState<string>()
|
const [error, setError] = useState<string>()
|
||||||
const [disabled, setDisabled] = useState(false)
|
|
||||||
const fetcher = useFetcher()
|
const fetcher = useFetcher()
|
||||||
const loaderData = useRouteLoaderData<typeof loader>('routes/_news')
|
const loaderData = useRouteLoaderData<typeof loader>('routes/_news')
|
||||||
const { subscriptionsData: subscriptions } = loaderData || {}
|
const { subscriptionsData: subscriptions } = loaderData || {}
|
||||||
@ -56,11 +55,9 @@ export const FormRegister = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!fetcher.data?.success) {
|
if (!fetcher.data?.success) {
|
||||||
setError(fetcher.data?.message)
|
setError(fetcher.data?.message)
|
||||||
setDisabled(false)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
setDisabled(true)
|
|
||||||
setError(undefined)
|
setError(undefined)
|
||||||
setIsRegisterOpen(false)
|
setIsRegisterOpen(false)
|
||||||
setIsSuccessOpen('register')
|
setIsSuccessOpen('register')
|
||||||
@ -120,7 +117,8 @@ export const FormRegister = () => {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
disabled={disabled}
|
isLoading={fetcher.state !== 'idle'}
|
||||||
|
disabled={fetcher.state !== 'idle'}
|
||||||
type="submit"
|
type="submit"
|
||||||
className="w-full rounded-md py-2"
|
className="w-full rounded-md py-2"
|
||||||
>
|
>
|
||||||
|
|||||||
@ -29,7 +29,6 @@ export default function FormSubscription() {
|
|||||||
const { setIsSubscribeOpen, setIsSuccessOpen } = useNewsContext()
|
const { setIsSubscribeOpen, setIsSuccessOpen } = useNewsContext()
|
||||||
const fetcher = useFetcher()
|
const fetcher = useFetcher()
|
||||||
const [error, setError] = useState<string>()
|
const [error, setError] = useState<string>()
|
||||||
const [disabled, setDisabled] = useState(false)
|
|
||||||
const loaderData = useRouteLoaderData<typeof loader>('routes/_news')
|
const loaderData = useRouteLoaderData<typeof loader>('routes/_news')
|
||||||
const { subscriptionsData: subscriptions } = loaderData || {}
|
const { subscriptionsData: subscriptions } = loaderData || {}
|
||||||
|
|
||||||
@ -44,11 +43,9 @@ export default function FormSubscription() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!fetcher.data?.success) {
|
if (!fetcher.data?.success) {
|
||||||
setError(fetcher.data?.message)
|
setError(fetcher.data?.message)
|
||||||
setDisabled(false)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
setDisabled(true)
|
|
||||||
setError(undefined)
|
setError(undefined)
|
||||||
setIsSubscribeOpen(false)
|
setIsSubscribeOpen(false)
|
||||||
setIsSuccessOpen('payment')
|
setIsSuccessOpen('payment')
|
||||||
@ -77,7 +74,8 @@ export default function FormSubscription() {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
disabled={disabled}
|
isLoading={fetcher.state !== 'idle'}
|
||||||
|
disabled={fetcher.state !== 'idle'}
|
||||||
type="submit"
|
type="submit"
|
||||||
className="mt-5 w-full rounded-md py-2"
|
className="mt-5 w-full rounded-md py-2"
|
||||||
>
|
>
|
||||||
|
|||||||
@ -34,8 +34,10 @@ export const HeaderTop = () => {
|
|||||||
>
|
>
|
||||||
<Button
|
<Button
|
||||||
variant="newsSecondary"
|
variant="newsSecondary"
|
||||||
className="hidden sm:block"
|
className="hidden sm:flex"
|
||||||
type="submit"
|
type="submit"
|
||||||
|
disabled={fetcher.state !== 'idle'}
|
||||||
|
isLoading={fetcher.state !== 'idle'}
|
||||||
>
|
>
|
||||||
Logout
|
Logout
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user