Skip to content
Snippets Groups Projects
ToolGroupComponent.js 1.42 KiB
Newer Older
chris's avatar
chris committed
import React, { useState } from "react";
import { isFunction } from "lodash";
import styled from "styled-components";
import icons from "../icons/icons";

const ToolGroupStyled = styled.div`
  border-right: 1px solid #ecedf1;
  &:last-child {
    border-right: none;
  }
`;

const MoreButton = styled.button`
  background: none;
  border: none;
  cursor: pointer;
chris's avatar
chris committed
  &:active {
    outline: none;
  }
chris's avatar
chris committed

const InnerStyled = styled.div`
  display: flex;
  width: 0;
  top: 40px;
  position: relative;
  right: 100%;
`;

const ToolGroupComponent = ({ view, tools, name, title }) => {
  const [more, showHide] = useState(false),
    toolsShown = [],
    rest = [],
    DisplayTitle = isFunction(title) ? title : () => title;

  tools.forEach(tool => {
chris's avatar
chris committed
    tool.isIntoMoreSection() && tool.isDisplayed()
chris's avatar
chris committed
      ? rest.push(tool.renderTool(view))
      : toolsShown.push(tool.renderTool(view));
  });

  return (
    <ToolGroupStyled data-name={name}>
chris's avatar
chris committed
      <DisplayTitle />
      {toolsShown}
      {rest.length && !more ? (
        <MoreButton title="show more tools" onClick={() => showHide(!more)}>
          {icons.ellipses}
        </MoreButton>
chris's avatar
chris committed
      ) : null}
      {more && (
        <div>
          <MoreButton title="hide" onClick={() => showHide(!more)}>
            {icons.ellipses}
          </MoreButton>
chris's avatar
chris committed
          <InnerStyled>{rest}</InnerStyled>
        </div>
      )}
    </ToolGroupStyled>
chris's avatar
chris committed
  );
};

export default ToolGroupComponent;