2019-07-18 11:11:36 +00:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
2021-02-20 14:54:12 +00:00
|
|
|
import styled from "styled-components";
|
2021-02-10 16:14:47 +00:00
|
|
|
|
2019-07-18 11:11:36 +00:00
|
|
|
import DropDown from "../drop-down";
|
|
|
|
import DropDownItem from "../drop-down-item";
|
2019-07-30 11:17:19 +00:00
|
|
|
import Checkbox from "../checkbox";
|
2021-02-10 16:14:47 +00:00
|
|
|
import {
|
|
|
|
StyledCheckbox,
|
|
|
|
Separator,
|
|
|
|
Caret,
|
|
|
|
StyledDropdownToggle,
|
|
|
|
StyledGroupButton,
|
|
|
|
} from "./styled-group-button";
|
2021-02-25 12:16:52 +00:00
|
|
|
import ExpanderDownIcon from "../../../public/images/expander-down.react.svg";
|
|
|
|
import commonIconsStyles from "../utils/common-icons-style";
|
2021-03-02 08:25:11 +00:00
|
|
|
import Base from "../themes/base";
|
2021-02-20 14:54:12 +00:00
|
|
|
|
2021-02-26 12:53:59 +00:00
|
|
|
const textColor = "#333333",
|
|
|
|
disabledTextColor = "#A3A9AE";
|
2021-03-02 08:25:11 +00:00
|
|
|
|
2021-02-20 14:54:12 +00:00
|
|
|
const StyledExpanderDownIcon = styled(ExpanderDownIcon)`
|
|
|
|
${commonIconsStyles}
|
|
|
|
path {
|
2021-02-26 13:29:50 +00:00
|
|
|
color: ${(props) =>
|
|
|
|
props.disabled
|
|
|
|
? props.theme.groupButton.disableColor
|
|
|
|
: props.theme.groupButton.color};
|
2021-02-20 14:54:12 +00:00
|
|
|
fill: ${(props) => props.color};
|
|
|
|
}
|
|
|
|
`;
|
2021-03-02 08:25:11 +00:00
|
|
|
StyledExpanderDownIcon.defaultProps = { theme: Base };
|
2020-10-14 14:38:04 +00:00
|
|
|
class GroupButton extends React.Component {
|
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,
|
2020-10-16 13:16:01 +00:00
|
|
|
selected: props.selected,
|
2019-07-18 11:11:36 +00:00
|
|
|
};
|
|
|
|
}
|
2019-06-06 07:12:28 +00:00
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
setIsOpen = (isOpen) => this.setState({ isOpen: isOpen });
|
2019-07-31 11:42:48 +00:00
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
setSelected = (selected) => this.setState({ selected: selected });
|
2019-07-31 11:42:48 +00:00
|
|
|
|
2020-01-31 13:42:05 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.opened !== prevProps.opened) {
|
|
|
|
this.setIsOpen(this.props.opened);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.selected !== prevProps.selected) {
|
|
|
|
this.setSelected(this.props.selected);
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 07:12:28 +00:00
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
clickOutsideAction = (e) => {
|
2020-12-10 17:06:04 +00:00
|
|
|
this.state.isOpen && !this.ref.current.contains(e.target);
|
2020-12-03 21:21:48 +00:00
|
|
|
this.setIsOpen(false);
|
2020-10-14 14:38:04 +00:00
|
|
|
};
|
2020-01-31 13:42:05 +00:00
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
checkboxChange = (e) => {
|
2020-01-31 13:42:05 +00:00
|
|
|
this.props.onChange && this.props.onChange(e.target && e.target.checked);
|
|
|
|
|
|
|
|
this.setSelected(this.props.selected);
|
2019-07-31 11:42:48 +00:00
|
|
|
};
|
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
dropDownItemClick = (e) => {
|
2020-08-11 08:03:09 +00:00
|
|
|
const index = e.currentTarget.dataset.index;
|
|
|
|
const child = this.props.children[index];
|
|
|
|
|
|
|
|
child && child.props.onClick && child.props.onClick(e);
|
2019-07-31 11:42:48 +00:00
|
|
|
this.props.onSelect && this.props.onSelect(child);
|
2020-08-11 08:03:09 +00:00
|
|
|
|
|
|
|
child && this.setSelected(child.props.label);
|
2020-01-31 13:42:05 +00:00
|
|
|
this.setIsOpen(!this.state.isOpen);
|
2019-07-31 11:42:48 +00:00
|
|
|
};
|
|
|
|
|
2020-01-31 13:42:05 +00:00
|
|
|
dropDownToggleClick = () => {
|
|
|
|
this.setIsOpen(!this.state.isOpen);
|
2019-07-31 11:42:48 +00:00
|
|
|
};
|
|
|
|
|
2019-07-18 11:11:36 +00:00
|
|
|
render() {
|
2019-08-13 11:21:28 +00:00
|
|
|
//console.log("GroupButton render");
|
2020-01-31 13:42:05 +00:00
|
|
|
const {
|
|
|
|
checked,
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
disabled,
|
|
|
|
dropDownMaxHeight,
|
|
|
|
id,
|
|
|
|
isDropdown,
|
|
|
|
isIndeterminate,
|
|
|
|
isSelect,
|
|
|
|
isSeparator,
|
|
|
|
label,
|
2020-10-16 13:16:01 +00:00
|
|
|
style,
|
2021-08-24 13:31:16 +00:00
|
|
|
alt,
|
2020-01-31 13:42:05 +00:00
|
|
|
} = this.props;
|
2019-08-22 08:03:27 +00:00
|
|
|
|
2019-07-31 11:42:48 +00:00
|
|
|
const itemLabel = !isSelect ? label : this.state.selected;
|
2020-10-14 14:38:04 +00:00
|
|
|
const dropDownMaxHeightProp = dropDownMaxHeight
|
|
|
|
? { maxHeight: dropDownMaxHeight }
|
|
|
|
: {};
|
|
|
|
const offsetSelectDropDown = isSelect
|
2021-10-26 11:59:04 +00:00
|
|
|
? { manualX: window.innerWidth <= 1024 ? "44px" : "50px" }
|
2020-10-14 14:38:04 +00:00
|
|
|
: {};
|
2019-06-06 07:12:28 +00:00
|
|
|
|
2021-10-26 12:48:31 +00:00
|
|
|
const manualY = window.innerWidth <= 1024 ? "60px" : "53px";
|
|
|
|
|
2019-06-06 07:12:28 +00:00
|
|
|
return (
|
2020-10-14 14:38:04 +00:00
|
|
|
<StyledGroupButton
|
|
|
|
ref={this.ref}
|
|
|
|
id={id}
|
|
|
|
className={className}
|
|
|
|
style={style}
|
|
|
|
>
|
|
|
|
{isDropdown ? (
|
|
|
|
<>
|
|
|
|
{isSelect && (
|
2019-07-30 11:17:19 +00:00
|
|
|
<StyledCheckbox>
|
|
|
|
<Checkbox
|
|
|
|
isChecked={checked}
|
|
|
|
isIndeterminate={isIndeterminate}
|
2020-10-14 14:38:04 +00:00
|
|
|
onChange={this.checkboxChange}
|
2021-04-15 07:54:12 +00:00
|
|
|
title={itemLabel}
|
2020-10-14 14:38:04 +00:00
|
|
|
/>
|
2019-07-30 11:17:19 +00:00
|
|
|
</StyledCheckbox>
|
2020-10-14 14:38:04 +00:00
|
|
|
)}
|
2019-08-22 08:03:27 +00:00
|
|
|
<StyledDropdownToggle
|
|
|
|
{...this.props}
|
|
|
|
onClick={this.dropDownToggleClick}
|
2021-04-15 07:54:12 +00:00
|
|
|
title={itemLabel}
|
2019-08-22 08:03:27 +00:00
|
|
|
>
|
2019-07-31 11:42:48 +00:00
|
|
|
{itemLabel}
|
2020-10-14 14:38:04 +00:00
|
|
|
<Caret isOpen={this.state.isOpen}>
|
2021-02-26 13:29:50 +00:00
|
|
|
<StyledExpanderDownIcon size="scale" disabled={disabled} />
|
2019-08-22 08:03:27 +00:00
|
|
|
</Caret>
|
2019-07-18 11:11:36 +00:00
|
|
|
</StyledDropdownToggle>
|
2019-08-22 08:03:27 +00:00
|
|
|
<DropDown
|
2021-09-13 09:56:02 +00:00
|
|
|
forwardedRef={this.ref}
|
2019-08-22 08:03:27 +00:00
|
|
|
{...dropDownMaxHeightProp}
|
2019-12-17 13:31:37 +00:00
|
|
|
{...offsetSelectDropDown}
|
2021-10-26 12:48:31 +00:00
|
|
|
manualY={manualY}
|
2019-12-17 13:31:37 +00:00
|
|
|
open={this.state.isOpen}
|
2020-01-31 13:42:05 +00:00
|
|
|
clickOutsideAction={this.clickOutsideAction}
|
2020-12-09 10:09:41 +00:00
|
|
|
showDisabledItems={true}
|
2021-04-15 07:54:12 +00:00
|
|
|
title={this.state.isOpen ? "" : itemLabel}
|
2019-08-22 08:03:27 +00:00
|
|
|
>
|
2020-10-16 13:16:01 +00:00
|
|
|
{React.Children.map(children, (child) => (
|
2019-08-22 08:03:27 +00:00
|
|
|
<DropDownItem
|
|
|
|
{...child.props}
|
2020-08-11 08:03:09 +00:00
|
|
|
onClick={this.dropDownItemClick}
|
2019-08-22 08:03:27 +00:00
|
|
|
/>
|
2020-10-14 14:38:04 +00:00
|
|
|
))}
|
2019-07-18 11:11:36 +00:00
|
|
|
</DropDown>
|
|
|
|
</>
|
2020-10-14 14:38:04 +00:00
|
|
|
) : (
|
2021-08-24 13:31:16 +00:00
|
|
|
<StyledDropdownToggle {...this.props} title={alt || itemLabel}>
|
2021-04-15 07:54:12 +00:00
|
|
|
{label}
|
|
|
|
</StyledDropdownToggle>
|
2020-10-14 14:38:04 +00:00
|
|
|
)}
|
2021-04-15 07:54:12 +00:00
|
|
|
{isSeparator && <Separator title="" />}
|
2019-07-18 11:11:36 +00:00
|
|
|
</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
|
|
|
activated: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Initial value of checkbox */
|
2020-01-31 13:42:05 +00:00
|
|
|
checked: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Children elements */
|
2020-01-31 13:42:05 +00:00
|
|
|
children: PropTypes.any,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Accepts class */
|
2020-01-31 13:42:05 +00:00
|
|
|
className: PropTypes.string,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Tells when the button should present a disabled state */
|
2020-01-31 13:42:05 +00:00
|
|
|
disabled: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Selected height value of DropDown */
|
2020-01-31 13:42:05 +00:00
|
|
|
dropDownMaxHeight: PropTypes.number,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Value of font weight */
|
2020-01-31 13:42:05 +00:00
|
|
|
fontWeight: PropTypes.string,
|
2019-07-18 11:11:36 +00:00
|
|
|
hovered: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Accepts id */
|
2020-01-31 13:42:05 +00:00
|
|
|
id: PropTypes.string,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Tells when the button should present a dropdown state */
|
2019-07-18 11:11:36 +00:00
|
|
|
isDropdown: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Initial value of Indeterminate checkbox */
|
2020-01-31 13:42:05 +00:00
|
|
|
isIndeterminate: PropTypes.bool,
|
|
|
|
isSelect: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Tells when the button should contain separator */
|
2019-07-18 11:11:36 +00:00
|
|
|
isSeparator: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Value of the group button */
|
2020-01-31 13:42:05 +00:00
|
|
|
label: PropTypes.string,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Called when checkbox is checked */
|
2020-01-31 13:42:05 +00:00
|
|
|
onChange: PropTypes.func,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Property for onClick action */
|
2019-07-18 11:11:36 +00:00
|
|
|
onClick: PropTypes.func,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Called when value is selected in selector */
|
2019-07-30 11:17:19 +00:00
|
|
|
onSelect: PropTypes.func,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Tells when the button should be opened by default */
|
2020-01-31 13:42:05 +00:00
|
|
|
opened: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Selected value label */
|
2019-09-09 14:32:20 +00:00
|
|
|
selected: PropTypes.string,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Accepts css style */
|
2020-01-31 13:42:05 +00:00
|
|
|
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Value of tab index */
|
2020-10-16 13:16:01 +00:00
|
|
|
tabIndex: PropTypes.number,
|
2021-08-24 13:31:16 +00:00
|
|
|
/** Alternative value of the group button */
|
|
|
|
alt: PropTypes.string,
|
2019-06-06 07:12:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GroupButton.defaultProps = {
|
2019-07-18 11:11:36 +00:00
|
|
|
activated: false,
|
2020-01-31 13:42:05 +00:00
|
|
|
disabled: false,
|
|
|
|
fontWeight: "600",
|
2019-07-18 11:11:36 +00:00
|
|
|
hovered: false,
|
|
|
|
isDropdown: false,
|
2020-01-31 13:42:05 +00:00
|
|
|
isSelect: false,
|
2019-07-18 11:11:36 +00:00
|
|
|
isSeparator: false,
|
2020-01-31 13:42:05 +00:00
|
|
|
label: "Group button",
|
|
|
|
opened: false,
|
2020-10-16 13:16:01 +00:00
|
|
|
tabIndex: -1,
|
2019-06-06 07:12:28 +00:00
|
|
|
};
|
|
|
|
|
2019-07-18 11:11:36 +00:00
|
|
|
export default GroupButton;
|