import React, { Component } from 'react' import Downshift from 'downshift' import matchSorter from 'match-sorter' import ArrowDown from './svg/Arrowdown' import CheckMark from './svg/Checkmark' import { COLORS } from '../lib/constants' class Dropdown extends Component { state = { inputValue: this.props.selected.name, itemsToShow: this.props.list } userInputtedValue = '' onUserAction = changes => { this.setState(({ inputValue, itemsToShow }) => { const clearUserInput = changes.hasOwnProperty('isOpen') if (changes.hasOwnProperty('inputValue')) { if (changes.type === Downshift.stateChangeTypes.keyDownEscape) { inputValue = this.userInputtedValue } else { inputValue = changes.inputValue this.userInputtedValue = changes.inputValue } } itemsToShow = this.userInputtedValue ? matchSorter(this.props.list, this.userInputtedValue, { keys: ['name'] }) : this.props.list if ( changes.hasOwnProperty('highlightedIndex') && (changes.type === Downshift.stateChangeTypes.keyDownArrowUp || changes.type === Downshift.stateChangeTypes.keyDownArrowDown) ) { inputValue = itemsToShow[changes.highlightedIndex].name } if (clearUserInput) { this.userInputtedValue = '' } return { inputValue, itemsToShow } }) } render() { const { button, color, list, selected, onChange } = this.props return ( it === selected)} itemToString={item => item.name} onChange={onChange} onUserAction={this.onUserAction} /> ) } } const renderDropdown = ({ button, color, list, selected }) => ({ isOpen, highlightedIndex, setHighlightedIndex, selectHighlightedItem, selectedItem, getRootProps, getButtonProps, getInputProps, getItemProps }) => { return ( {selectedItem.name} {isOpen ? ( {list.map((item, index) => ( {item.name} ))} ) : null} ) } const DropdownContainer = ({ children, innerRef, minWidth, ...rest }) => { return (
{children}
) } const SelectedItem = ({ getButtonProps, getInputProps, children, isOpen, color, button }) => { const itemColor = color || COLORS.SECONDARY return ( {button ? ( {children} ) : ( )}
) } const ListItems = ({ children, color }) => { return ( ) } const ListItem = ({ children, color, isHighlighted, isSelected, ...rest }) => { const itemColor = color || COLORS.SECONDARY return (
  • {children} {isSelected ? : null}
  • ) } function minWidth(isButton, selected, list) { const items = isButton ? [...list, selected] : list return items.reduce((max, { name }) => { const wordSize = name.length * 10 + 32 return wordSize > max ? wordSize : max }, 0) } export default Dropdown