2019-07-18 11:11:36 +00:00
|
|
|
import React from "react";
|
|
|
|
import styled, { css } from "styled-components";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import { Icons } from "../icons";
|
|
|
|
import DropDown from "../drop-down";
|
|
|
|
import DropDownItem from "../drop-down-item";
|
2019-07-30 11:17:19 +00:00
|
|
|
import Checkbox from "../checkbox";
|
2019-08-02 07:35:54 +00:00
|
|
|
import { handleAnyClick } from '../../utils/event';
|
2019-06-06 07:12:28 +00:00
|
|
|
|
2019-07-18 11:11:36 +00:00
|
|
|
const textColor = "#333333",
|
|
|
|
disabledTextColor = "#A3A9AE";
|
2019-06-06 07:12:28 +00:00
|
|
|
|
|
|
|
const activatedCss = css`
|
2019-07-18 11:11:36 +00:00
|
|
|
cursor: pointer;
|
2019-06-06 07:12:28 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
const hoveredCss = css`
|
2019-07-18 11:11:36 +00:00
|
|
|
cursor: pointer;
|
2019-06-06 07:12:28 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
const StyledGroupButton = styled.div`
|
2019-07-18 11:11:36 +00:00
|
|
|
position: relative;
|
|
|
|
display: inline-flex;
|
|
|
|
vertical-align: middle;
|
2019-06-06 07:12:28 +00:00
|
|
|
`;
|
|
|
|
|
2019-06-25 15:01:04 +00:00
|
|
|
const StyledDropdownToggle = styled.div`
|
2019-07-18 11:11:36 +00:00
|
|
|
font-family: Open Sans;
|
|
|
|
font-style: normal;
|
|
|
|
font-weight: ${props => props.fontWeight};
|
|
|
|
font-size: 14px;
|
|
|
|
line-height: 19px;
|
|
|
|
|
|
|
|
cursor: default;
|
|
|
|
|
|
|
|
color: ${props => (props.disabled ? disabledTextColor : textColor)};
|
|
|
|
|
|
|
|
float: left;
|
|
|
|
height: 19px;
|
2019-08-22 08:03:27 +00:00
|
|
|
margin: 18px 12px 19px ${props => props.isSelect ? '0px' : '12px'};
|
2019-07-18 11:11:36 +00:00
|
|
|
overflow: hidden;
|
|
|
|
padding: 0px;
|
|
|
|
|
|
|
|
text-align: center;
|
|
|
|
text-decoration: none;
|
|
|
|
white-space: nowrap;
|
|
|
|
|
|
|
|
user-select: none;
|
|
|
|
-o-user-select: none;
|
|
|
|
-moz-user-select: none;
|
|
|
|
-webkit-user-select: none;
|
|
|
|
|
|
|
|
${props =>
|
|
|
|
!props.disabled &&
|
|
|
|
(props.activated
|
|
|
|
? `${activatedCss}`
|
|
|
|
: css`
|
|
|
|
&:active {
|
2019-06-06 07:12:28 +00:00
|
|
|
${activatedCss}
|
2019-07-18 11:11:36 +00:00
|
|
|
}
|
|
|
|
`)}
|
|
|
|
|
|
|
|
${props =>
|
|
|
|
!props.disabled &&
|
|
|
|
(props.hovered
|
|
|
|
? `${hoveredCss}`
|
|
|
|
: css`
|
|
|
|
&:hover {
|
2019-06-06 07:12:28 +00:00
|
|
|
${hoveredCss}
|
2019-07-18 11:11:36 +00:00
|
|
|
}
|
|
|
|
`)}
|
2019-06-06 07:12:28 +00:00
|
|
|
`;
|
|
|
|
|
2019-08-22 08:03:27 +00:00
|
|
|
const Caret = styled.div`
|
|
|
|
display: inline-block;
|
|
|
|
width: 8px;
|
|
|
|
margin-left: 6px;
|
|
|
|
|
|
|
|
${props => props.isOpen && `
|
|
|
|
padding-bottom: 2px;
|
|
|
|
transform: scale(1, -1);
|
|
|
|
`}
|
2019-06-25 15:01:04 +00:00
|
|
|
`;
|
2019-06-06 07:12:28 +00:00
|
|
|
|
2019-06-25 15:01:04 +00:00
|
|
|
const Separator = styled.div`
|
2019-07-18 11:11:36 +00:00
|
|
|
vertical-align: middle;
|
|
|
|
border: 0.5px solid #eceef1;
|
|
|
|
width: 1px;
|
|
|
|
height: 24px;
|
2019-08-22 08:03:27 +00:00
|
|
|
margin: 16px 16px 0 16px;
|
2019-06-06 07:12:28 +00:00
|
|
|
`;
|
|
|
|
|
2019-07-30 11:17:19 +00:00
|
|
|
const StyledCheckbox = styled.div`
|
|
|
|
display: inline-block;
|
|
|
|
margin: auto 0 auto 16px;
|
|
|
|
|
|
|
|
& > * {
|
|
|
|
margin: 0px;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2019-07-24 17:43:18 +00:00
|
|
|
class GroupButton extends React.PureComponent {
|
2019-07-18 11:11:36 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-06-06 07:12:28 +00:00
|
|
|
|
2019-07-18 11:11:36 +00:00
|
|
|
this.ref = React.createRef();
|
2019-06-06 07:12:28 +00:00
|
|
|
|
2019-07-18 11:11:36 +00:00
|
|
|
this.state = {
|
2019-07-30 11:17:19 +00:00
|
|
|
isOpen: props.opened,
|
|
|
|
selected: props.selected
|
2019-07-18 11:11:36 +00:00
|
|
|
};
|
2019-07-24 17:43:18 +00:00
|
|
|
|
2019-08-22 08:03:27 +00:00
|
|
|
if (props.opened)
|
|
|
|
handleAnyClick(true, this.handleClick);
|
2019-07-18 11:11:36 +00:00
|
|
|
}
|
2019-06-06 07:12:28 +00:00
|
|
|
|
2019-08-22 08:03:27 +00:00
|
|
|
handleClick = (e) =>
|
2019-08-02 07:35:54 +00:00
|
|
|
this.state.isOpen && !this.ref.current.contains(e.target) && this.toggle(false);
|
2019-07-31 11:42:48 +00:00
|
|
|
|
2019-08-22 08:03:27 +00:00
|
|
|
stopAction = (e) => e.preventDefault();
|
2019-07-31 11:42:48 +00:00
|
|
|
|
2019-08-22 08:03:27 +00:00
|
|
|
toggle = (isOpen) => this.setState({ isOpen: isOpen });
|
2019-06-06 07:12:28 +00:00
|
|
|
|
2019-08-22 08:03:27 +00:00
|
|
|
checkboxChange = (e) => {
|
2019-07-31 11:42:48 +00:00
|
|
|
this.props.onChange && this.props.onChange(e.target.checked);
|
|
|
|
this.setState({ selected: this.props.selected });
|
|
|
|
};
|
|
|
|
|
2019-08-22 08:03:27 +00:00
|
|
|
dropDownItemClick = (child) => {
|
2019-07-31 11:42:48 +00:00
|
|
|
child.props.onClick && child.props.onClick();
|
|
|
|
this.props.onSelect && this.props.onSelect(child);
|
|
|
|
this.setState({ selected: child.props.label });
|
|
|
|
this.toggle(!this.state.isOpen);
|
|
|
|
};
|
|
|
|
|
2019-08-22 08:03:27 +00:00
|
|
|
dropDownToggleClick = (e) => {
|
2019-07-31 11:42:48 +00:00
|
|
|
this.props.disabled ? this.stopAction(e) : this.toggle(!this.state.isOpen);
|
|
|
|
};
|
|
|
|
|
2019-07-18 11:11:36 +00:00
|
|
|
componentWillUnmount() {
|
2019-08-02 07:35:54 +00:00
|
|
|
handleAnyClick(false, this.handleClick);
|
2019-07-18 11:11:36 +00:00
|
|
|
}
|
2019-06-06 07:12:28 +00:00
|
|
|
|
2019-08-02 07:35:54 +00:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
2019-07-18 11:11:36 +00:00
|
|
|
if (this.props.opened !== prevProps.opened) {
|
|
|
|
this.toggle(this.props.opened);
|
|
|
|
}
|
2019-08-02 07:35:54 +00:00
|
|
|
|
2019-08-22 08:03:27 +00:00
|
|
|
if (this.state.isOpen !== prevState.isOpen) {
|
2019-08-02 07:35:54 +00:00
|
|
|
handleAnyClick(this.state.isOpen, this.handleClick);
|
|
|
|
}
|
2019-07-18 11:11:36 +00:00
|
|
|
}
|
2019-06-06 07:12:28 +00:00
|
|
|
|
2019-07-18 11:11:36 +00:00
|
|
|
render() {
|
2019-08-13 11:21:28 +00:00
|
|
|
//console.log("GroupButton render");
|
2019-08-13 11:16:54 +00:00
|
|
|
const { label, isDropdown, disabled, isSeparator, isSelect, isIndeterminate, children, checked, dropDownMaxHeight } = this.props;
|
2019-08-22 08:03:27 +00:00
|
|
|
|
2019-07-30 11:17:19 +00:00
|
|
|
const color = disabled ? disabledTextColor : textColor;
|
2019-07-31 11:42:48 +00:00
|
|
|
const itemLabel = !isSelect ? label : this.state.selected;
|
2019-08-22 08:03:27 +00:00
|
|
|
const dropDownMaxHeightProp = dropDownMaxHeight ? { maxHeight: dropDownMaxHeight } : {};
|
2019-06-06 07:12:28 +00:00
|
|
|
|
|
|
|
return (
|
2019-07-18 11:11:36 +00:00
|
|
|
<StyledGroupButton ref={this.ref}>
|
2019-07-30 11:17:19 +00:00
|
|
|
{isDropdown
|
|
|
|
? <>
|
|
|
|
{isSelect &&
|
|
|
|
<StyledCheckbox>
|
|
|
|
<Checkbox
|
|
|
|
isChecked={checked}
|
|
|
|
isIndeterminate={isIndeterminate}
|
2019-07-31 11:42:48 +00:00
|
|
|
onChange={this.checkboxChange} />
|
2019-07-30 11:17:19 +00:00
|
|
|
</StyledCheckbox>
|
|
|
|
}
|
2019-08-22 08:03:27 +00:00
|
|
|
<StyledDropdownToggle
|
|
|
|
{...this.props}
|
|
|
|
onClick={this.dropDownToggleClick}
|
|
|
|
>
|
2019-07-31 11:42:48 +00:00
|
|
|
{itemLabel}
|
2019-08-22 08:03:27 +00:00
|
|
|
<Caret
|
|
|
|
isOpen={this.state.isOpen}
|
|
|
|
>
|
|
|
|
<Icons.ExpanderDownIcon
|
|
|
|
size="scale"
|
|
|
|
color={color}
|
|
|
|
/>
|
|
|
|
</Caret>
|
2019-07-18 11:11:36 +00:00
|
|
|
</StyledDropdownToggle>
|
2019-08-22 08:03:27 +00:00
|
|
|
<DropDown
|
|
|
|
{...dropDownMaxHeightProp}
|
|
|
|
isOpen={this.state.isOpen}
|
|
|
|
>
|
2019-07-31 11:42:48 +00:00
|
|
|
{React.Children.map(children, (child) =>
|
2019-08-22 08:03:27 +00:00
|
|
|
<DropDownItem
|
|
|
|
{...child.props}
|
|
|
|
onClick={this.dropDownItemClick.bind(this, child)}
|
|
|
|
/>
|
|
|
|
)}
|
2019-07-18 11:11:36 +00:00
|
|
|
</DropDown>
|
|
|
|
</>
|
2019-08-22 08:03:27 +00:00
|
|
|
: <StyledDropdownToggle
|
|
|
|
{...this.props}>
|
|
|
|
{label}
|
|
|
|
</StyledDropdownToggle>
|
2019-07-30 11:17:19 +00:00
|
|
|
}
|
2019-07-18 11:11:36 +00:00
|
|
|
{isSeparator && <Separator />}
|
|
|
|
</StyledGroupButton>
|
2019-06-06 07:12:28 +00:00
|
|
|
);
|
2019-07-18 11:11:36 +00:00
|
|
|
}
|
2019-06-06 07:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GroupButton.propTypes = {
|
2019-07-18 11:11:36 +00:00
|
|
|
label: PropTypes.string,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
activated: PropTypes.bool,
|
|
|
|
opened: PropTypes.bool,
|
|
|
|
hovered: PropTypes.bool,
|
|
|
|
isDropdown: PropTypes.bool,
|
|
|
|
isSeparator: PropTypes.bool,
|
|
|
|
tabIndex: PropTypes.number,
|
|
|
|
onClick: PropTypes.func,
|
2019-07-30 11:17:19 +00:00
|
|
|
fontWeight: PropTypes.string,
|
|
|
|
onSelect: PropTypes.func,
|
2019-09-09 14:32:20 +00:00
|
|
|
isSelect: PropTypes.bool,
|
|
|
|
selected: PropTypes.string,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
isIndeterminate: PropTypes.bool,
|
|
|
|
children: PropTypes.any,
|
|
|
|
checked: PropTypes.bool,
|
|
|
|
dropDownMaxHeight: PropTypes.number
|
2019-06-06 07:12:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GroupButton.defaultProps = {
|
2019-07-18 11:11:36 +00:00
|
|
|
label: "Group button",
|
|
|
|
disabled: false,
|
|
|
|
activated: false,
|
|
|
|
opened: false,
|
|
|
|
hovered: false,
|
|
|
|
isDropdown: false,
|
|
|
|
isSeparator: false,
|
|
|
|
tabIndex: -1,
|
2019-07-30 11:17:19 +00:00
|
|
|
fontWeight: "600",
|
|
|
|
isSelect: false
|
2019-06-06 07:12:28 +00:00
|
|
|
};
|
|
|
|
|
2019-07-18 11:11:36 +00:00
|
|
|
export default GroupButton;
|