Basic.js 3.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

19
import React, { useEffect, useState, useRef } from "react";
20 21 22
import styled from "styled-components";

const Basic = props => {
23 24 25 26 27 28 29 30 31 32 33 34 35 36
  let { options} = props;
  const [toggle, setToggle] = useState(false);
  const dropdownRef = useRef();
  useEffect(() => {
    const setToggleOnEvent = event => {
      if (!(event.target.classList.contains('dropbtn') || event.target.classList.contains('dropdown-item')) && dropdownRef.current.classList.contains('show')) {
        setToggle(false);
      }
    };
    window.addEventListener("mousedown", setToggleOnEvent);
    return () => {
      window.removeEventListener("mousedown", setToggleOnEvent);
    };
  }, []);
37 38 39 40

  const ComponentStyle = styled.div`
    > div {
      display: inline-block;
41
      width: 160px;
42 43 44 45 46 47
    }
    label {
      margin-right: 5px;
    }
  `;

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  const DropDownComp = styled.div`
    .dropbtn {
      background-color: #ffffff;
      color: rgb(45, 55, 71);
      padding: 5px;
      font-size: 14px;
      border: 1px solid rgb(204, 204, 204);
      cursor: pointer;
      width: 100%;
      text-align: left;
      border-radius: 4px;
      &:hover, &:focus {
        border: 1px solid rgb(55, 187, 155);
      }
    }

    .dropdown {
      position: relative;
      display: inline-block;
      width: 100%;
    }

    .dropdown-content {
      margin-top: 10px;
      display: none;
      position: absolute;
      background-color: #ffffff;
      width: 160px;
      overflow: auto;
      max-height: 300px;
      box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
      z-index: 1;
      .dropdown-item {
        padding: 5px 12px;
        &:hover {
          background-color: rgba(55, 187, 155, 0.1);;
        }
      }
    }

    .dropdown-content a {
      padding: 12px 16px;
      text-decoration: none;
      display: block;
    }

    .dropdown a:hover {background-color: #ddd;}

    .show {display: block;}
    i {
      color: rgb(45, 55, 71);
      border: solid black;
      border-width: 0 1px 1px 0;
      display: inline-block;
      padding: 3px;
      float: right;
    }
    .up {
      margin-top: 5px;
      transform: rotate(-135deg);
      -webkit-transform: rotate(-135deg);
    }

    .down {
      margin-top: 3px;
      transform: rotate(45deg);
      -webkit-transform: rotate(45deg);
    }
  `;

118 119
  return (
    <ComponentStyle>
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
      <DropDownComp>
        <div className="dropdown">
          <button className="dropbtn" onClick={() => setToggle(c => !c)}>Versions <i className={`${toggle ? 'up' : 'down'}`}></i></button>
          <div ref={dropdownRef} id="myDropdown" className={`dropdown-content ${toggle ? 'show' : ''}`}>
            {
              options.map(function(opt, i) {
                return <div className="dropdown-item" key={i} data-href={opt.href} onClick={
                  event => {
                    const dataHref = event.currentTarget.dataset.href;
                    const href = `${window.location.origin}${dataHref}`;
                    window.open(href, "_blank");
                  }}>
                    {opt.title}
                </div>
              })
135
            }
136 137 138
          </div>
        </div>
      </DropDownComp>
139 140 141 142
    </ComponentStyle>
  );
};
Basic.propTypes = {};
143
export default Basic;