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
|
|
|
| 'danger'
|
|
|
|
| 'link'
|
|
|
|
| 'light'
|
2022-07-27 21:53:19 +00:00
|
|
|
| 'dangerlight'
|
2022-11-21 20:40:44 +00:00
|
|
|
| 'warninglight'
|
2023-02-14 08:19:41 +00:00
|
|
|
| 'warning'
|
2022-07-27 21:53:19 +00:00
|
|
|
| 'none';
|
2021-11-30 02:31:16 +00:00
|
|
|
type Size = 'xsmall' | 'small' | 'medium' | 'large';
|
2021-12-20 17:21:19 +00:00
|
|
|
|
2022-12-20 21:07:34 +00:00
|
|
|
export interface Props<TasProps = unknown>
|
|
|
|
extends AriaAttributes,
|
|
|
|
AutomationTestingProps {
|
2022-07-06 14:05:00 +00:00
|
|
|
icon?: ReactNode | ComponentType<unknown>;
|
|
|
|
|
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-12-20 21:07:34 +00:00
|
|
|
as?: ComponentType<TasProps> | string;
|
2022-03-08 12:14:23 +00:00
|
|
|
onClick?: MouseEventHandler<HTMLButtonElement>;
|
2022-12-20 21:07:34 +00:00
|
|
|
props?: TasProps;
|
2021-11-16 12:33:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 21:07:34 +00:00
|
|
|
export function Button<TasProps = unknown>({
|
2021-11-16 12:33:01 +00:00
|
|
|
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,
|
2021-11-16 12:33:01 +00:00
|
|
|
children,
|
2022-12-20 21:07:34 +00:00
|
|
|
as = 'button',
|
|
|
|
props,
|
2022-06-23 07:25:56 +00:00
|
|
|
...ariaProps
|
2022-12-20 21:07:34 +00:00
|
|
|
}: PropsWithChildren<Props<TasProps>>) {
|
|
|
|
const Component = as as 'button';
|
2021-11-16 12:33:01 +00:00
|
|
|
return (
|
2022-12-20 21:07:34 +00:00
|
|
|
<Component
|
2021-11-16 12:33:01 +00:00
|
|
|
/* eslint-disable-next-line react/button-has-type */
|
|
|
|
type={type}
|
|
|
|
disabled={disabled}
|
2022-08-10 04:12:20 +00:00
|
|
|
className={clsx(`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}
|
2022-12-20 21:07:34 +00:00
|
|
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
|
|
{...props}
|
2021-11-16 12:33:01 +00:00
|
|
|
>
|
2022-12-01 06:40:52 +00:00
|
|
|
{icon && <Icon icon={icon} size={getIconSize(size)} />}
|
2021-11-16 12:33:01 +00:00
|
|
|
{children}
|
2022-12-20 21:07:34 +00:00
|
|
|
</Component>
|
2021-11-16 12:33:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|