2024-08-22 08:14:22 -04:00
|
|
|
import { forwardRef } from 'react'
|
2024-04-16 11:06:42 -04:00
|
|
|
import { Button as BS5Button, Spinner } from 'react-bootstrap-5'
|
2024-02-14 10:04:08 -05:00
|
|
|
import type { ButtonProps } from '@/features/ui/components/types/button-props'
|
2024-03-06 06:31:25 -05:00
|
|
|
import classNames from 'classnames'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2024-04-16 11:06:42 -04:00
|
|
|
import MaterialIcon from '@/shared/components/material-icon'
|
2024-02-14 10:04:08 -05:00
|
|
|
|
2024-08-22 08:14:22 -04:00
|
|
|
const Button = forwardRef<HTMLButtonElement, ButtonProps>(
|
|
|
|
(
|
|
|
|
{
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
leadingIcon,
|
|
|
|
isLoading = false,
|
|
|
|
loadingLabel,
|
|
|
|
trailingIcon,
|
|
|
|
variant = 'primary',
|
|
|
|
...props
|
|
|
|
},
|
|
|
|
ref
|
|
|
|
) => {
|
|
|
|
const { t } = useTranslation()
|
2024-03-06 06:31:25 -05:00
|
|
|
|
2024-10-08 10:22:13 -04:00
|
|
|
const buttonClassName = classNames('d-inline-grid', className, {
|
2024-08-22 08:14:22 -04:00
|
|
|
'button-loading': isLoading,
|
|
|
|
})
|
2024-10-08 10:22:13 -04:00
|
|
|
|
2024-08-22 08:14:22 -04:00
|
|
|
const loadingSpinnerClassName =
|
2024-10-08 10:22:13 -04:00
|
|
|
props.size === 'lg' ? 'loading-spinner-large' : 'loading-spinner-small'
|
|
|
|
const materialIconClassName =
|
|
|
|
props.size === 'lg' ? 'icon-large' : 'icon-small'
|
|
|
|
|
|
|
|
const leadingIconComponent =
|
|
|
|
leadingIcon && typeof leadingIcon === 'string' ? (
|
|
|
|
<MaterialIcon type={leadingIcon} className={materialIconClassName} />
|
|
|
|
) : (
|
|
|
|
leadingIcon
|
|
|
|
)
|
|
|
|
|
|
|
|
const trailingIconComponent =
|
|
|
|
trailingIcon && typeof trailingIcon === 'string' ? (
|
|
|
|
<MaterialIcon type={trailingIcon} className={materialIconClassName} />
|
|
|
|
) : (
|
|
|
|
trailingIcon
|
|
|
|
)
|
2024-02-14 10:04:08 -05:00
|
|
|
|
2024-08-22 08:14:22 -04:00
|
|
|
return (
|
|
|
|
<BS5Button
|
|
|
|
className={buttonClassName}
|
|
|
|
variant={variant}
|
|
|
|
{...props}
|
|
|
|
ref={ref}
|
|
|
|
>
|
|
|
|
{isLoading && (
|
|
|
|
<span className="spinner-container">
|
|
|
|
<Spinner
|
|
|
|
animation="border"
|
|
|
|
aria-hidden="true"
|
|
|
|
as="span"
|
|
|
|
className={loadingSpinnerClassName}
|
|
|
|
role="status"
|
|
|
|
/>
|
|
|
|
<span className="visually-hidden">
|
|
|
|
{loadingLabel ?? t('loading')}
|
|
|
|
</span>
|
2024-07-10 09:34:19 -04:00
|
|
|
</span>
|
2024-04-16 11:06:42 -04:00
|
|
|
)}
|
2024-08-22 08:14:22 -04:00
|
|
|
<span className="button-content" aria-hidden={isLoading}>
|
2024-10-08 10:22:13 -04:00
|
|
|
{leadingIconComponent}
|
2024-08-22 08:14:22 -04:00
|
|
|
{children}
|
2024-10-08 10:22:13 -04:00
|
|
|
{trailingIconComponent}
|
2024-08-22 08:14:22 -04:00
|
|
|
</span>
|
|
|
|
</BS5Button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
Button.displayName = 'Button'
|
|
|
|
|
|
|
|
export default Button
|