如何使用样式组件对下拉菜单进行样式设置

时间:2020-07-09 13:03:20

标签: drop-down-menu styled-components react-typescript

我正在为我的应用程序使用React JS + Typescript。对于样式,我使用styled-components。我真的是样式组件的新手。我创建了一个下拉菜单。逻辑工作正常,但UI看起来很糟糕。我将代码上传到Code sand box中。我想像Tailwind那样设计我的下拉列表。但是由于我是styled-components的新人,所以我只是不知道该怎么做。

这是我的下拉菜单

import React, { useState } from "react";
import styled from "styled-components";
import Arrow from './Arrow.svg'
const Wrapper = styled.div<
  {
    active: boolean;
  }
  >`
text-align: left;
width: 100%;
color: #bfc5cd;
font-size: 16px;
font-weight: 300;
position: relative;
margin: 2em 0;
@media (min-width: 400px) {
  max-width: 300px;
}
svg {
  fill: #798697;
  transition: all 0.2s ease;
}
${props =>
    props.active
      ? `
  svg {
    transform: rotate(180deg);
  }
`
      : ``}
`;

const MenuLabel = styled.span`
display:inline-block;
color: grey;
border: 1px solid green;
background: white;
box-shadow: 0 0 5px -1px rgba(0,0,0,0.2);
cursor:pointer;
vertical-align:middle;
max-width: 100px;
padding: 40px 40px;
font-size: 12px;
text-align: center;
border: 1px solid ${({ theme }) => theme.inputBorderColor};
&:focus {
  outline: none;
  box-shadow: 0px 0px 0px 1px ${({ theme }) => theme.inputBorderColorActive};
  border: 1px solid ${({ theme }) => theme.inputBorderColorActive};
}
`;

const ItemList = styled.div`
color: #798697;
background: white;
line-height: 30px;
padding: .25em 2em .25em 2em;
cursor: defaul;
user-select: none;
transition: all .25s ease;
&:hover,
&.selected {
  background: #F7F7F7;
  color: #4A4A4A;
}
`;

export interface IOptions {
  label: string;
  value: number;
}

export interface IDropdown {
  labelDefault: string;
  options: IOptions[];
}

const Dropdown = ({ labelDefault, options }: IDropdown) => {
  const [isOpened, setIsOpened] = useState(false);
  const [selectedOption, setSelectedOption] = useState("");
  const [label, setLabel] = useState("");

  const handleSelectedItem = (obj: any) => {
    setSelectedOption(obj.value);
    setLabel(obj.label);
    setIsOpened(!isOpened);
  };

  return (
    <Wrapper active={isOpened}>
      <MenuLabel onClick={() => setIsOpened(!isOpened)}>
        {selectedOption ? label : labelDefault}
      </MenuLabel>
      <ul
        style={
          isOpened
            ? {
              display: "block",
              listStyleType: "none"
            }
            : { display: "none" }
        }
      >
        {options.map(el => (
          <ItemList
            key={el.value.toString()}
            onClick={() => handleSelectedItem(el)}
          >
            {el.label}
          </ItemList>
        ))}

      </ul>
    </Wrapper>
  );
}

export default Dropdown;

这是父组件

import * as React from "react";
import Dropdown from "./dropdown";

const MockData = [
  { label: "one", value: 1 },
  { label: "two", value: 2 },
  { label: "three", value: 3 }
];

export default function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <Dropdown labelDefault="Select a label" options={MockData} />
    </div>
  );
}

0 个答案:

没有答案