ohif-viewer/platform/ui/src/components/Button/Button.jsx

151 lines
4.0 KiB
React
Raw Normal View History

2020-02-28 19:31:42 +01:00
import React, { useRef } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
2020-02-28 19:31:42 +01:00
const baseClasses =
'text-center items-center outline-none transition duration-300 ease-in-out font-bold focus:outline-none';
const defaults = {
color: 'default',
disabled: false,
2020-02-28 19:31:42 +01:00
fullWidth: false,
rounded: 'medium',
size: 'medium',
type: 'button',
2020-02-28 19:31:42 +01:00
variant: 'contained',
};
2020-02-28 19:31:42 +01:00
const roundedClasses = {
none: '',
2020-02-28 19:31:42 +01:00
small: 'rounded',
medium: 'rounded-md',
large: 'rounded-lg',
full: 'rounded-full',
};
const disabledClasses = {
true: 'cursor-not-allowed',
false: '',
};
const variantClasses = {
text: {
2020-02-28 19:31:42 +01:00
default:
'text-primary-light hover:bg-primary-light hover:text-white active:opacity-80 focus:bg-primary-light focus:text-white',
primary:
'text-primary-main hover:bg-primary-main hover:text-white active:opacity-80 focus:bg-primary-main focus:text-white',
secondary:
'text-darkBlue-100 hover:bg-darkBlue-100 hover:text-white active:opacity-80 focus:bg-darkBlue-100 focus:text-white',
white:
'text-white hover:bg-white hover:text-black active:opacity-80 focus:bg-white focus:text-black',
},
outlined: {
2020-02-28 19:31:42 +01:00
default:
'border bg-trasparent border-primary-light text-primary-light hover:opacity-80 active:opacity-100 focus:opacity-80',
primary:
'border bg-transparent border-primary-main text-primary-main hover:opacity-80 active:opacity-100 focus:opacity-80',
secondary:
'border bg-transparent border-darkBlue-100 text-darkBlue-100 hover:opacity-80 active:opacity-100 focus:opacity-80',
white:
'border bg-transparent border-white text-white hover:opacity-80 active:opacity-100 focus:opacity-80',
},
contained: {
2020-02-28 19:31:42 +01:00
default:
'bg-primary-light text-white hover:opacity-80 active:opacity-100 focus:opacity-80',
primary:
'bg-primary-main text-white hover:opacity-80 active:opacity-100 focus:opacity-80',
secondary:
'bg-darkBlue-100 text-white hover:opacity-80 active:opacity-100 focus:opacity-80',
white:
'bg-white text-black hover:opacity-80 active:opacity-100 focus:opacity-80',
},
};
const sizeClasses = {
2020-02-28 19:31:42 +01:00
small: 'py-1 px-3 text-base',
medium: 'py-1 px-4 text-lg',
large: 'py-1 px-6 text-xl',
};
const fullWidthClasses = {
true: 'flex w-full',
false: 'inline-flex',
};
const Button = ({
children,
variant = defaults.variant,
color = defaults.color,
size = defaults.size,
2020-02-28 19:31:42 +01:00
rounded = defaults.rounded,
disabled = defaults.disabled,
type = defaults.type,
2020-02-28 19:31:42 +01:00
fullWidth = defaults.fullWidth,
startIcon: startIconProp,
endIcon: endIconProp,
className,
...rest
}) => {
const startIcon = startIconProp && (
2020-02-28 19:31:42 +01:00
<div className="mr-2">
{React.cloneElement(startIconProp, {
className: classnames('w-4 h-4 fill-current'),
})}
</div>
);
const endIcon = endIconProp && (
<div className="ml-2">
{React.cloneElement(endIconProp, {
className: classnames('w-4 h-4 fill-current'),
})}
</div>
);
2020-02-28 19:31:42 +01:00
const buttonElement = useRef(null);
2020-02-28 19:31:42 +01:00
const handleOnClick = e => {
buttonElement.current.blur();
if (rest.onClick) {
rest.onClick(e);
}
};
return (
<button
className={classnames(
baseClasses,
variantClasses[variant][color],
2020-02-28 19:31:42 +01:00
roundedClasses[rounded],
sizeClasses[size],
2020-02-28 19:31:42 +01:00
fullWidthClasses[fullWidth],
disabledClasses[disabled],
className
)}
2020-02-28 19:31:42 +01:00
ref={buttonElement}
onClick={handleOnClick}
type={type}
{...rest}
>
{startIcon}
{children}
{endIcon}
</button>
);
};
Button.propTypes = {
2020-02-14 23:46:27 +01:00
children: PropTypes.node,
size: PropTypes.oneOf(['small', 'medium', 'large']),
2020-02-28 19:31:42 +01:00
rounded: PropTypes.oneOf(['none', 'small', 'medium', 'large', 'full']),
variant: PropTypes.oneOf(['text', 'outlined', 'contained']),
2020-02-28 19:31:42 +01:00
color: PropTypes.oneOf(['default', 'primary', 'secondary', 'white']),
fullWidth: PropTypes.bool,
2020-02-14 23:46:27 +01:00
disabled: PropTypes.bool,
type: PropTypes.string,
startIcon: PropTypes.node,
endIcon: PropTypes.node,
className: PropTypes.node,
};
export default Button;