2019-07-30 11:17:19 +00:00
|
|
|
import React from 'react';
|
|
|
|
import styled from 'styled-components';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import GroupButton from '../group-button';
|
|
|
|
import DropDownItem from '../drop-down-item';
|
2019-06-17 08:58:02 +00:00
|
|
|
|
|
|
|
const StyledGroupButtonsMenu = styled.div`
|
|
|
|
position: sticky;
|
|
|
|
top: 0;
|
2019-06-25 15:01:04 +00:00
|
|
|
background: #FFFFFF;
|
|
|
|
box-shadow: 0px 2px 18px rgba(0, 0, 0, 0.100306);
|
|
|
|
height: 56px;
|
2019-06-17 08:58:02 +00:00
|
|
|
list-style: none;
|
2019-06-25 15:01:04 +00:00
|
|
|
padding: 0 18px 19px 0;
|
2019-07-08 07:12:43 +00:00
|
|
|
width: 100%;
|
|
|
|
white-space: nowrap;
|
|
|
|
display: ${state => state.visible ? 'block' : 'none'};
|
2019-07-25 08:28:19 +00:00
|
|
|
z-index: 350;
|
2019-06-17 08:58:02 +00:00
|
|
|
`;
|
|
|
|
|
2019-06-25 15:01:04 +00:00
|
|
|
const CloseButton = styled.div`
|
|
|
|
position: absolute;
|
|
|
|
right: 20px;
|
|
|
|
top: 20px;
|
|
|
|
width: 20px;
|
|
|
|
height: 20px;
|
|
|
|
|
|
|
|
&:hover{
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
&:before, &:after {
|
|
|
|
position: absolute;
|
|
|
|
left: 15px;
|
|
|
|
content: ' ';
|
|
|
|
height: 20px;
|
|
|
|
width: 1px;
|
|
|
|
background-color: #D8D8D8;
|
|
|
|
}
|
|
|
|
|
|
|
|
&:before {
|
|
|
|
transform: rotate(45deg);
|
|
|
|
}
|
|
|
|
|
|
|
|
&:after {
|
|
|
|
transform: rotate(-45deg);
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2019-07-31 11:43:05 +00:00
|
|
|
const GroupMenuWrapper = styled.div`
|
|
|
|
display: inline-block;
|
|
|
|
`;
|
|
|
|
|
2019-07-30 11:17:19 +00:00
|
|
|
class GroupButtonsMenu extends React.PureComponent {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2019-06-17 08:58:02 +00:00
|
|
|
|
2019-07-30 11:17:19 +00:00
|
|
|
this.state = {
|
|
|
|
priorityItems: props.menuItems,
|
|
|
|
moreItems: [],
|
|
|
|
visible: true
|
2019-06-17 08:58:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 11:17:19 +00:00
|
|
|
this.fullMenuArray = this.props.menuItems;
|
|
|
|
this.checkBox = this.props.checkBox;
|
2019-06-17 08:58:02 +00:00
|
|
|
|
2019-07-30 11:17:19 +00:00
|
|
|
this.updateMenu = this.updateMenu.bind(this);
|
|
|
|
this.howManyItemsInMenuArray = this.howManyItemsInMenuArray.bind(this);
|
2019-07-31 11:43:05 +00:00
|
|
|
this.closeMenu = this.closeMenu.bind(this);
|
|
|
|
this.groupButtonClick = this.groupButtonClick.bind(this);
|
2019-07-30 11:17:19 +00:00
|
|
|
}
|
2019-07-25 11:00:03 +00:00
|
|
|
|
2019-07-31 11:43:05 +00:00
|
|
|
closeMenu = (e) => {
|
|
|
|
this.setState({ visible: false });
|
|
|
|
this.props.onClose && this.props.onClose(e);
|
|
|
|
};
|
|
|
|
|
|
|
|
groupButtonClick = (item) => {
|
2019-08-21 11:44:30 +00:00
|
|
|
if (item.disabled) return;
|
2019-07-31 11:43:05 +00:00
|
|
|
item.onClick();
|
|
|
|
this.closeMenu();
|
|
|
|
};
|
|
|
|
|
2019-07-30 11:17:19 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.widthsArray = Array.from(document.getElementById("groupMenu").children).map(item => item.getBoundingClientRect().width);
|
|
|
|
window.addEventListener('resize', _.throttle(this.updateMenu), 300);
|
|
|
|
this.updateMenu();
|
|
|
|
}
|
2019-06-17 08:58:02 +00:00
|
|
|
|
2019-07-30 11:17:19 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.visible !== prevProps.visible) {
|
|
|
|
this.setState({ visible: this.props.visible });
|
2019-07-08 07:12:43 +00:00
|
|
|
}
|
2019-07-30 11:17:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
howManyItemsInMenuArray = (array, outerWidth, initialWidth) => {
|
|
|
|
let total = (initialWidth + 80);
|
|
|
|
for (let i = 0, len = array.length; i < len; i++) {
|
|
|
|
if (total + array[i] > outerWidth) {
|
|
|
|
return i < 1 ? 1 : i;
|
|
|
|
} else {
|
|
|
|
total += array[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
updateMenu = () => {
|
|
|
|
const moreMenuElement = document.getElementById("moreMenu");
|
2019-07-31 11:43:05 +00:00
|
|
|
const groupMenuOuterElement = document.getElementById("groupMenuOuter");
|
|
|
|
const outerWidth = groupMenuOuterElement ? groupMenuOuterElement.getBoundingClientRect().width : 0;
|
2019-07-30 11:17:19 +00:00
|
|
|
const moreMenuWidth = moreMenuElement ? moreMenuElement.getBoundingClientRect().width : 0;
|
|
|
|
const arrayAmount = this.howManyItemsInMenuArray(this.widthsArray, outerWidth, moreMenuWidth);
|
|
|
|
const navItemsCopy = this.fullMenuArray;
|
|
|
|
const priorityItems = navItemsCopy.slice(0, arrayAmount);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
priorityItems: priorityItems,
|
|
|
|
moreItems: priorityItems.length !== navItemsCopy.length ? navItemsCopy.slice(arrayAmount, navItemsCopy.length) : []
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.updateMenu());
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
//console.log("GroupButtonsMenu render");
|
|
|
|
return (
|
|
|
|
<StyledGroupButtonsMenu id="groupMenuOuter" visible={this.state.visible} {...this.state}>
|
2019-07-31 11:43:05 +00:00
|
|
|
<GroupMenuWrapper id="groupMenu">
|
2019-07-30 11:17:19 +00:00
|
|
|
{this.state.priorityItems.map((item, i) =>
|
|
|
|
<GroupButton key={`navItem-${i}`}
|
|
|
|
label={item.label}
|
|
|
|
isDropdown={item.isDropdown}
|
|
|
|
isSeparator={item.isSeparator}
|
|
|
|
isSelect={item.isSelect}
|
|
|
|
onSelect={item.onSelect}
|
|
|
|
fontWeight={item.fontWeight}
|
2019-08-21 11:44:30 +00:00
|
|
|
disabled={item.disabled}
|
2019-07-31 19:28:55 +00:00
|
|
|
onClick={this.groupButtonClick.bind(this, item)}
|
2019-07-31 11:43:05 +00:00
|
|
|
{...this.props}
|
|
|
|
>
|
2019-07-30 11:17:19 +00:00
|
|
|
{item.children}
|
|
|
|
</GroupButton>
|
|
|
|
)}
|
2019-07-31 11:43:05 +00:00
|
|
|
</GroupMenuWrapper>
|
2019-07-30 11:17:19 +00:00
|
|
|
{this.state.moreItems.length > 0 &&
|
|
|
|
<GroupButton id="moreMenu" isDropdown label={this.props.moreLabel}>
|
|
|
|
{this.state.moreItems.map((item, i) =>
|
|
|
|
<DropDownItem
|
|
|
|
key={`moreNavItem-${i}`}
|
|
|
|
label={item.label}
|
2019-08-21 11:44:30 +00:00
|
|
|
disabled={item.disabled}
|
2019-07-31 19:28:55 +00:00
|
|
|
onClick={this.groupButtonClick.bind(this, item)}
|
2019-07-30 11:17:19 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</GroupButton>
|
2019-07-25 11:00:03 +00:00
|
|
|
}
|
2019-07-31 11:43:05 +00:00
|
|
|
<CloseButton title={this.props.closeTitle} onClick={this.closeMenu} />
|
2019-07-30 11:17:19 +00:00
|
|
|
</StyledGroupButtonsMenu>
|
|
|
|
);
|
|
|
|
}
|
2019-06-17 08:58:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 11:00:03 +00:00
|
|
|
GroupButtonsMenu.propTypes = {
|
2019-07-30 11:17:19 +00:00
|
|
|
onClick: PropTypes.func,
|
|
|
|
onClose: PropTypes.func,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onSelect: PropTypes.func,
|
|
|
|
menuItems: PropTypes.array,
|
|
|
|
checked: PropTypes.bool,
|
|
|
|
selected: PropTypes.string,
|
|
|
|
visible: PropTypes.bool,
|
|
|
|
moreLabel: PropTypes.string,
|
|
|
|
closeTitle: PropTypes.string
|
2019-07-22 07:59:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 08:58:02 +00:00
|
|
|
export default GroupButtonsMenu;
|