2020-03-06 16:47:40 -03:00
|
|
|
import React, { useRef } from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
2020-07-02 22:41:55 -03:00
|
|
|
const baseButtonClass = 'border outline-none';
|
2020-03-06 16:47:40 -03:00
|
|
|
const roundedClasses = {
|
|
|
|
|
vertical: {
|
|
|
|
|
none: '',
|
|
|
|
|
small: 'first:rounded-t last:rounded-b',
|
|
|
|
|
medium: 'first:rounded-t-md last:rounded-b-md',
|
|
|
|
|
large: 'first:rounded-t-lg last:rounded-b-lg',
|
|
|
|
|
full: 'first:rounded-t-full last:rounded-b-full',
|
|
|
|
|
},
|
|
|
|
|
horizontal: {
|
|
|
|
|
none: '',
|
|
|
|
|
small: 'first:rounded-l last:rounded-r',
|
|
|
|
|
medium: 'first:rounded-l-md last:rounded-r-md',
|
|
|
|
|
large: 'first:rounded-l-lg last:rounded-r-lg',
|
|
|
|
|
full: 'first:rounded-l-full last:rounded-r-full',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const borderClasses = {
|
|
|
|
|
text: {
|
|
|
|
|
vertical: 'border-t-0 border-l-0 border-r-0 last:border-b-0',
|
|
|
|
|
horizontal: 'border-l-0 border-t-0 border-b-0 last:border-r-0',
|
|
|
|
|
},
|
|
|
|
|
outlined: {
|
|
|
|
|
vertical: 'border border-b-0 last:border-b',
|
|
|
|
|
horizontal: 'border border-r-0 last:border-r',
|
|
|
|
|
},
|
|
|
|
|
contained: {
|
|
|
|
|
vertical: 'border-t-0 border-l-0 border-r-0 last:border-b-0',
|
|
|
|
|
horizontal: 'border-l-0 border-t-0 border-b-0 last:border-r-0',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const variantClasses = {
|
|
|
|
|
text: {
|
2020-04-02 15:52:41 -03:00
|
|
|
default: 'border-primary-light',
|
|
|
|
|
primary: 'border-primary-main',
|
|
|
|
|
secondary: 'border-secondary-light',
|
2020-03-06 16:47:40 -03:00
|
|
|
white: 'border-white',
|
2020-07-02 22:41:55 -03:00
|
|
|
black: 'border-primary-main',
|
2020-03-06 16:47:40 -03:00
|
|
|
},
|
|
|
|
|
outlined: {
|
|
|
|
|
default: '',
|
|
|
|
|
primary: '',
|
2022-11-16 14:19:52 -05:00
|
|
|
secondary: 'border border-secondary-light',
|
2020-03-06 16:47:40 -03:00
|
|
|
white: '',
|
2020-07-02 22:41:55 -03:00
|
|
|
black:
|
|
|
|
|
'border-primary-main group-hover:bg-primary-main group-hover:border-black',
|
2020-03-06 16:47:40 -03:00
|
|
|
},
|
|
|
|
|
contained: {
|
|
|
|
|
default: 'border-white',
|
|
|
|
|
primary: 'border-white',
|
|
|
|
|
secondary: 'border-white',
|
|
|
|
|
white: 'border-black',
|
2020-07-02 22:41:55 -03:00
|
|
|
black: 'border-primary-main',
|
2020-03-06 16:47:40 -03:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const orientationClasses = {
|
|
|
|
|
vertical: 'flex-col',
|
|
|
|
|
horizontal: 'flex-row',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const baseDisplayClass = 'inline-flex';
|
|
|
|
|
const fullWidthDisplayClass = 'flex';
|
|
|
|
|
|
2022-11-16 14:19:52 -05:00
|
|
|
// css class that are applied for buttons that not the first or last
|
|
|
|
|
const nonFirstLastClasses = {
|
|
|
|
|
vertical: 'border-t-0 border-b-0',
|
|
|
|
|
horizontal: 'border-l-0 last:border-r-0',
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-06 16:47:40 -03:00
|
|
|
const ButtonGroup = ({
|
|
|
|
|
children,
|
|
|
|
|
className,
|
|
|
|
|
disabled = false,
|
|
|
|
|
fullWidth = false,
|
|
|
|
|
color = 'default',
|
|
|
|
|
orientation = 'horizontal',
|
|
|
|
|
rounded = 'medium',
|
|
|
|
|
size = 'medium',
|
|
|
|
|
variant = 'outlined',
|
2022-11-16 14:19:52 -05:00
|
|
|
splitBorder = true,
|
2020-03-06 16:47:40 -03:00
|
|
|
...other
|
|
|
|
|
}) => {
|
|
|
|
|
const ref = useRef(null);
|
|
|
|
|
|
|
|
|
|
const buttonClasses = classnames(
|
|
|
|
|
baseButtonClass,
|
|
|
|
|
borderClasses[variant] && borderClasses[variant][orientation],
|
|
|
|
|
variantClasses[variant] && variantClasses[variant][color],
|
|
|
|
|
roundedClasses[orientation] && roundedClasses[orientation][rounded]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
role="group"
|
|
|
|
|
className={classnames(
|
2020-07-02 22:41:55 -03:00
|
|
|
'group',
|
2020-03-06 16:47:40 -03:00
|
|
|
className,
|
|
|
|
|
orientationClasses[orientation],
|
|
|
|
|
fullWidth ? fullWidthDisplayClass : baseDisplayClass
|
|
|
|
|
)}
|
|
|
|
|
ref={ref}
|
|
|
|
|
{...other}
|
|
|
|
|
>
|
2022-11-16 14:19:52 -05:00
|
|
|
{React.Children.map(children, (child, index) => {
|
|
|
|
|
const isFirst = index === 0;
|
|
|
|
|
const isLast = index === React.Children.count(children) - 1;
|
|
|
|
|
const isNotFirstOrLast = !isFirst && !isLast;
|
|
|
|
|
|
2020-03-06 16:47:40 -03:00
|
|
|
if (!React.isValidElement(child)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return React.cloneElement(child, {
|
2022-11-16 14:19:52 -05:00
|
|
|
className: classnames(
|
|
|
|
|
buttonClasses,
|
|
|
|
|
child.props.className,
|
|
|
|
|
!splitBorder &&
|
|
|
|
|
isNotFirstOrLast &&
|
|
|
|
|
nonFirstLastClasses[orientation],
|
|
|
|
|
!splitBorder && isLast && 'last:border-l-0'
|
|
|
|
|
),
|
2020-03-06 16:47:40 -03:00
|
|
|
disabled: child.props.disabled || disabled,
|
|
|
|
|
color: child.props.color || color,
|
|
|
|
|
fullWidth,
|
|
|
|
|
rounded: 'none',
|
|
|
|
|
size: child.props.size || size,
|
|
|
|
|
variant: child.props.variant || variant,
|
|
|
|
|
});
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ButtonGroup.propTypes = {
|
|
|
|
|
children: PropTypes.node.isRequired,
|
|
|
|
|
className: PropTypes.string,
|
2020-07-02 22:41:55 -03:00
|
|
|
color: PropTypes.oneOf([
|
|
|
|
|
'default',
|
|
|
|
|
'inherit',
|
|
|
|
|
'primary',
|
|
|
|
|
'secondary',
|
|
|
|
|
'white',
|
|
|
|
|
'black',
|
|
|
|
|
]),
|
2020-03-06 16:47:40 -03:00
|
|
|
disabled: PropTypes.bool,
|
|
|
|
|
fullWidth: PropTypes.bool,
|
|
|
|
|
orientation: PropTypes.oneOf(['vertical', 'horizontal']),
|
|
|
|
|
rounded: PropTypes.oneOf(['none', 'small', 'medium', 'large', 'full']),
|
2020-07-24 12:51:57 +02:00
|
|
|
size: PropTypes.oneOf(['small', 'medium', 'large', 'inherit']),
|
2020-03-06 16:47:40 -03:00
|
|
|
variant: PropTypes.oneOf(['text', 'outlined', 'contained']),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ButtonGroup;
|