Created
April 26, 2020 17:46
-
-
Save jokamjohn/be5ee853b36650f232e26fad115aec13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from "react"; | |
import styled from "styled-components"; | |
const Main = styled("div")` | |
font-family: sans-serif; | |
background: #f0f0f0; | |
height: 100vh; | |
`; | |
const DropDownContainer = styled("div")` | |
width: 10.5em; | |
margin: 0 auto; | |
`; | |
const DropDownHeader = styled("div")` | |
margin-bottom: 0.8em; | |
padding: 0.4em 2em 0.4em 1em; | |
box-shadow: 0 2px 3px rgba(0, 0, 0, 0.15); | |
font-weight: 500; | |
font-size: 1.3rem; | |
color: #3faffa; | |
background: #ffffff; | |
`; | |
const DropDownListContainer = styled("div")``; | |
const DropDownList = styled("ul")` | |
padding: 0; | |
margin: 0; | |
padding-left: 1em; | |
background: #ffffff; | |
border: 2px solid #e5e5e5; | |
box-sizing: border-box; | |
color: #3faffa; | |
font-size: 1.3rem; | |
font-weight: 500; | |
&:first-child { | |
padding-top: 0.8em; | |
} | |
`; | |
const ListItem = styled("li")` | |
list-style: none; | |
margin-bottom: 0.8em; | |
`; | |
const options = ["Mangoes", "Apples", "Oranges"]; | |
export default function App() { | |
const [isOpen, setIsOpen] = useState(false); | |
const [selectedOption, setSelectedOption] = useState(null); | |
const toggling = () => setIsOpen(!isOpen); | |
const onOptionClicked = value => () => { | |
setSelectedOption(value); | |
setIsOpen(false); | |
console.log(selectedOption); | |
}; | |
return ( | |
<Main> | |
<h1>Custom Select/dropdown</h1> | |
<DropDownContainer> | |
<DropDownHeader onClick={toggling}> | |
{selectedOption || "Mangoes"} | |
</DropDownHeader> | |
{isOpen && ( | |
<DropDownListContainer> | |
<DropDownList> | |
{options.map(option => ( | |
<ListItem onClick={onOptionClicked(option)} key={Math.random()}> | |
{option} | |
</ListItem> | |
))} | |
</DropDownList> | |
</DropDownListContainer> | |
)} | |
</DropDownContainer> | |
</Main> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment