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
|
|
|
|
|
|
|
const sizeClasses = new Map<ButtonProps['size'], string>([
|
|
|
|
['small', 'btn-sm'],
|
|
|
|
['default', ''],
|
|
|
|
['large', 'btn-lg'],
|
|
|
|
])
|
|
|
|
|
2024-03-06 06:31:25 -05:00
|
|
|
export default function Button({
|
2024-02-14 10:04:08 -05:00
|
|
|
children,
|
2024-02-23 06:23:32 -05:00
|
|
|
className,
|
2024-04-16 11:06:42 -04:00
|
|
|
leadingIcon,
|
2024-03-06 06:31:25 -05:00
|
|
|
isLoading = false,
|
2024-07-10 09:34:19 -04:00
|
|
|
loadingLabel,
|
2024-03-06 06:31:25 -05:00
|
|
|
size = 'default',
|
2024-04-16 11:06:42 -04:00
|
|
|
trailingIcon,
|
|
|
|
variant = 'primary',
|
2024-03-06 06:31:25 -05:00
|
|
|
...props
|
2024-02-14 10:04:08 -05:00
|
|
|
}: ButtonProps) {
|
2024-03-06 06:31:25 -05:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2024-02-14 10:04:08 -05:00
|
|
|
const sizeClass = sizeClasses.get(size)
|
2024-03-06 06:31:25 -05:00
|
|
|
const buttonClassName = classNames('d-inline-grid', sizeClass, className, {
|
|
|
|
'button-loading': isLoading,
|
|
|
|
})
|
|
|
|
const loadingSpinnerClassName =
|
|
|
|
size === 'large' ? 'loading-spinner-large' : 'loading-spinner-small'
|
2024-04-16 11:06:42 -04:00
|
|
|
const materialIconClassName = size === 'large' ? 'icon-large' : 'icon-small'
|
2024-02-14 10:04:08 -05:00
|
|
|
|
|
|
|
return (
|
2024-04-16 11:06:42 -04:00
|
|
|
<BS5Button className={buttonClassName} variant={variant} {...props}>
|
2024-03-06 06:31:25 -05:00
|
|
|
{isLoading && (
|
|
|
|
<span className="spinner-container">
|
|
|
|
<Spinner
|
|
|
|
animation="border"
|
|
|
|
aria-hidden="true"
|
|
|
|
as="span"
|
|
|
|
className={loadingSpinnerClassName}
|
|
|
|
role="status"
|
|
|
|
/>
|
2024-07-10 09:34:19 -04:00
|
|
|
<span className="visually-hidden">
|
|
|
|
{loadingLabel ?? t('loading')}
|
|
|
|
</span>
|
2024-03-06 06:31:25 -05:00
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
<span className="button-content" aria-hidden={isLoading}>
|
2024-04-16 11:06:42 -04:00
|
|
|
{leadingIcon && (
|
|
|
|
<MaterialIcon type={leadingIcon} className={materialIconClassName} />
|
|
|
|
)}
|
2024-03-06 06:31:25 -05:00
|
|
|
{children}
|
2024-04-16 11:06:42 -04:00
|
|
|
{trailingIcon && (
|
|
|
|
<MaterialIcon type={trailingIcon} className={materialIconClassName} />
|
|
|
|
)}
|
2024-03-06 06:31:25 -05:00
|
|
|
</span>
|
2024-04-16 11:06:42 -04:00
|
|
|
</BS5Button>
|
2024-02-14 10:04:08 -05:00
|
|
|
)
|
|
|
|
}
|