import React, { Component } from 'react'; import classnames from 'classnames'; import DropdownShell from './DropdownShell'; import removeIcon from 'assets/images/icon-remove.svg'; import './ColorDropdown.scss'; interface Option { name: any; value: T; color?: string; hidden: boolean | undefined; onRemove?(): void; } interface Props { value: T; options: Option[]; label?: string; ariaLabel: string; extra?: any; size?: string; color?: string; menuAlign?: string; disabled?: boolean; onChange(value: T): void; } export default class ColorDropdown extends Component, {}> { private dropdownShell: DropdownShell | null; public render() { const { ariaLabel, disabled, color, size } = this.props; return ( (this.dropdownShell = el)} disabled={disabled} /> ); } private renderLabel = () => { const label = this.props.label ? `${this.props.label}:` : ''; const activeOption = this.getActiveOption(); return ( {label} {activeOption ? activeOption.name : '-'} ); }; private renderOptions = () => { const { options, value, menuAlign, extra } = this.props; const listItems = options.filter(opt => !opt.hidden).reduce((prev: any[], opt) => { const prevOpt = prev.length ? prev[prev.length - 1] : null; if (prevOpt && !prevOpt.divider && prevOpt.color !== opt.color) { prev.push({ divider: true }); } prev.push(opt); return prev; }, []); const menuClass = classnames({ ColorDropdown: true, 'dropdown-menu': true, [`dropdown-menu-${menuAlign || ''}`]: !!menuAlign }); return ( ); }; private onChange = (value: any) => { this.props.onChange(value); if (this.dropdownShell) { this.dropdownShell.close(); } }; private onRemove(onRemove: () => void, ev?: React.FormEvent) { if (ev) { ev.preventDefault(); ev.stopPropagation(); } onRemove(); } private getActiveOption() { return this.props.options.find(opt => opt.value === this.props.value); } }