2022-07-06 14:05:00 +00:00
|
|
|
import {
|
|
|
|
AriaAttributes,
|
|
|
|
ComponentType,
|
|
|
|
MouseEventHandler,
|
|
|
|
PropsWithChildren,
|
|
|
|
ReactNode,
|
|
|
|
} from 'react';
|
2021-11-16 12:33:01 +00:00
|
|
|
import clsx from 'clsx';
|
|
|
|
|
2022-06-23 07:25:56 +00:00
|
|
|
import { AutomationTestingProps } from '@/types';
|
|
|
|
|
2022-07-06 14:05:00 +00:00
|
|
|
import { Icon } from '@@/Icon';
|
2022-07-27 21:53:19 +00:00
|
|
|
import './Button.css';
|
2022-07-06 14:05:00 +00:00
|
|
|
|
2021-12-20 17:21:19 +00:00
|
|
|
type Type = 'submit' | 'button' | 'reset';
|
2022-06-23 06:32:18 +00:00
|
|
|
type Color =
|
|
|
|
| 'default'
|
|
|
|
| 'primary'
|
2022-07-01 01:14:22 +00:00
|
|
|
| 'secondary'
|
2022-06-23 06:32:18 +00:00
|
|
|
| 'success'
|
|
|
|
| 'warning'
|
|
|
|
| 'danger'
|
|
|
|
| 'link'
|
|
|
|
| 'light'
|
2022-07-27 21:53:19 +00:00
|
|
|
| 'dangerlight'
|
|
|
|
| 'none';
|
2021-11-30 02:31:16 +00:00
|
|
|
type Size = 'xsmall' | 'small' | 'medium' | 'large';
|
2021-12-20 17:21:19 +00:00
|
|
|
|
2022-06-23 07:25:56 +00:00
|
|
|
export interface Props extends AriaAttributes, AutomationTestingProps {
|
2022-07-06 14:05:00 +00:00
|
|
|
icon?: ReactNode | ComponentType<unknown>;
|
|
|
|
featherIcon?: boolean;
|
|
|
|
|
2021-11-16 12:33:01 +00:00
|
|
|
color?: Color;
|
|
|
|
size?: Size;
|
|
|
|
disabled?: boolean;
|
2021-11-30 02:31:16 +00:00
|
|
|
title?: string;
|
|
|
|
className?: string;
|
2021-12-20 17:21:19 +00:00
|
|
|
type?: Type;
|
2022-03-08 12:14:23 +00:00
|
|
|
onClick?: MouseEventHandler<HTMLButtonElement>;
|
2021-11-16 12:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function Button({
|
|
|
|
type = 'button',
|
|
|
|
color = 'primary',
|
|
|
|
size = 'small',
|
|
|
|
disabled = false,
|
2021-11-30 02:31:16 +00:00
|
|
|
className,
|
2021-11-16 12:33:01 +00:00
|
|
|
onClick,
|
2021-11-30 02:31:16 +00:00
|
|
|
title,
|
2022-07-06 14:05:00 +00:00
|
|
|
icon,
|
|
|
|
featherIcon,
|
2021-11-16 12:33:01 +00:00
|
|
|
children,
|
2022-07-06 14:05:00 +00:00
|
|
|
|
2022-06-23 07:25:56 +00:00
|
|
|
...ariaProps
|
2021-11-16 12:33:01 +00:00
|
|
|
}: PropsWithChildren<Props>) {
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
/* eslint-disable-next-line react/button-has-type */
|
|
|
|
type={type}
|
|
|
|
disabled={disabled}
|
2022-07-06 14:05:00 +00:00
|
|
|
className={clsx(
|
|
|
|
{
|
|
|
|
'opacity-60': disabled,
|
|
|
|
},
|
|
|
|
`btn btn-${color}`,
|
|
|
|
sizeClass(size),
|
|
|
|
className
|
|
|
|
)}
|
2021-11-16 12:33:01 +00:00
|
|
|
onClick={onClick}
|
2021-11-30 02:31:16 +00:00
|
|
|
title={title}
|
2022-06-23 07:25:56 +00:00
|
|
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
|
|
{...ariaProps}
|
2021-11-16 12:33:01 +00:00
|
|
|
>
|
2022-07-06 14:05:00 +00:00
|
|
|
{icon && (
|
|
|
|
<Icon
|
|
|
|
icon={icon}
|
|
|
|
size={getIconSize(size)}
|
|
|
|
className="inline-flex"
|
|
|
|
feather={featherIcon}
|
|
|
|
/>
|
|
|
|
)}
|
2021-11-16 12:33:01 +00:00
|
|
|
{children}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:05:00 +00:00
|
|
|
function getIconSize(size: Size) {
|
|
|
|
switch (size) {
|
|
|
|
case 'xsmall':
|
|
|
|
return 'xs';
|
|
|
|
case 'medium':
|
|
|
|
return 'md';
|
|
|
|
case 'large':
|
|
|
|
return 'lg';
|
|
|
|
case 'small':
|
|
|
|
default:
|
|
|
|
return 'sm';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:33:01 +00:00
|
|
|
function sizeClass(size?: Size) {
|
|
|
|
switch (size) {
|
|
|
|
case 'large':
|
|
|
|
return 'btn-lg';
|
2021-11-30 02:31:16 +00:00
|
|
|
case 'medium':
|
|
|
|
return 'btn-md';
|
2021-11-16 12:33:01 +00:00
|
|
|
case 'xsmall':
|
|
|
|
return 'btn-xs';
|
|
|
|
default:
|
|
|
|
return 'btn-sm';
|
|
|
|
}
|
|
|
|
}
|