import React, { useCallback, useState } from "react"; import { CommonComponentProps, hexToRgba } from "./common"; import { ReactComponent as DownArrow } from "../../assets/icons/ads/down_arrow.svg"; import Text, { TextType } from "./Text"; import styled from "styled-components"; type DropdownOption = { label: string; value: string; }; type DropdownProps = CommonComponentProps & { options: DropdownOption[]; onSelect: (selectedValue: string) => void; selectedOption: DropdownOption; }; const DropdownWrapper = styled.div` width: 100%; position: relative; `; const SelectedItem = styled.div` display: flex; align-items: center; cursor: pointer; user-select: none; span { margin-right: ${props => props.theme.spaces[1] + 1}px; } `; const OptionsWrapper = styled.div` position: absolute; margin-top: ${props => props.theme.spaces[8]}px; left: -60px; width: 200px; display: flex; flex-direction: column; background-color: ${props => props.theme.colors.blackShades[3]}; box-shadow: ${props => props.theme.spaces[0]}px ${props => props.theme.spaces[5]}px ${props => props.theme.spaces[13] - 2}px ${props => hexToRgba(props.theme.colors.blackShades[0], 0.75)}; `; const DropdownOption = styled.div<{ selected: DropdownOption; option: DropdownOption; }>` display: flex; flex-direction: column; padding: 10px 12px; cursor: pointer; background-color: ${props => props.option.label === props.selected.label ? props.theme.colors.blackShades[4] : "transparent"}; span:last-child { margin-top: ${props => props.theme.spaces[1] + 1}px; } &:hover { span { color: ${props => props.theme.colors.blackShades[9]}; } } `; const TableDropdown = (props: DropdownProps) => { const [selected, setSelected] = useState(props.selectedOption); const [isDropdownOpen, setIsDropdownOpen] = useState(false); const dropdownHandler = () => { setIsDropdownOpen(!isDropdownOpen); }; const optionSelector = (option: DropdownOption) => { setSelected(option); setIsDropdownOpen(false); }; return ( dropdownHandler()}> {selected.label} {isDropdownOpen ? ( {props.options.map((el: DropdownOption, index: number) => ( optionSelector(el)} > {el.label} {el.value} ))} ) : null} ); }; export default TableDropdown;