fix: add loading state to buttons in forms and update button styles

This commit is contained in:
Ardeman
2025-03-11 06:00:49 +08:00
parent a45a6fb87e
commit 8cddc82031
5 changed files with 25 additions and 22 deletions
+16 -9
View File
@@ -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<typeof buttonVariants>['variant']
size?: VariantProps<typeof buttonVariants>['size']
className?: string
isLoading?: boolean
}
type PolymorphicReference<C extends ElementType> =
@@ -48,22 +50,27 @@ type ButtonProperties<C extends ElementType> = ButtonBaseProperties & {
ref?: PolymorphicReference<C>
} & Omit<ComponentPropsWithoutRef<C>, keyof ButtonBaseProperties>
export const Button = <C extends ElementType = 'button'>({
as,
children,
variant,
size,
className,
...properties
}: ButtonProperties<C>) => {
export const Button = <C extends ElementType = 'button'>(
properties: ButtonProperties<C>,
) => {
const {
as,
children,
variant,
size,
className,
isLoading = false,
...restProperties
} = properties
const Component = as || HeadlessButton
const classes = twMerge(buttonVariants({ variant, size, className }))
return (
<Component
className={classes}
{...properties}
{...restProperties}
>
{isLoading && <ArrowPathIcon className="animate-spin" />}
{children}
</Component>
)